npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-react-native-sass-classname

v0.0.9

Published

Babel plugin to make react web app more reusable as react-native apps taking the scss file with pre-extesions

Downloads

6

Readme

react-native-sass-classname

Babel plugin to make react web app more reusable as react-native apps and vice-versa.

Prerequirements

  • Babel being used to transpile your project.

What is the purpose of this plugin?

Indeed, react-native is a great alternative to frontend engineers feel comfortable working on mobile apps. Also, the effort invested by Nicolas Gallagher creating components to replace the native ones, it is very valuable. Despite the previous sentences show us the great advantages in this battle for productivity (having more people capable of doing the tasks), migration is not straight forward. Especially, due to the aspect of the same apps look different in mobile and web view. Besides, the react-native has a particular syntax to apply visual effects, that is the absence of className property widely used in web apps. So, rather than we adapt ourselves to this new reality, it is totally feasible adapting the new reality to the existing web environment, thus, making a migration seamless and painless.

The only gotcha here is that any engineer can use a module resolver to create their own replacement for native components. It has not been said undervaluing the React Native Web package developed by Nicolas. So, then, why? The reason for that is that creating your own replacement as a proper web component, you can take advantage of className property and using this babel plugin it takes care of the conversion from className to style property.

How to use?

Execute in your package/project folder:

npm install babel-plugin-react-native-sass-classname
// or
yarn add babel-plugin-react-native-sass-classname

Then, have your .babelrc ready with the plugin set:

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["react-native-sass-classname", {
      "extension": ["css", "scss", "sass"],
      "prefixExtension": "native"
    }]
  ],
  "ignore": "node_modules"
}

By default we already have the options below set, so, it is not required to set as it is displayed above.

  • extension: ["css", "scss", "sass"]
  • prefixExtension: "native"

How would look like my app?

As it has been said, creating aliases engineers will be able to have a DIV (web) represented by a VIEW (native):

const View = ({ children, ...otherProps }) => <div {...otherProps}>{children}</div>;

And a SPAN (web) by TEXT (native):

const Text = ({ children, ...otherProps }) => <span {...otherProps}>{children}</span>;

So, building a seamless app, they would have:

Application.js

import React from 'react';
import {
  Text,
  View
} from 'react-native'; // of course, you will need to map your module resolver to grab your own polyfills (custom Text and View)

import styles from './Application.scss';

const Application = (() => (
  <View
    className={[styles.container, styles.transparentContainer].join(' ')}
    style={{opacity: .8}}
  >
    <View className={styles.content}>
      <Text className={styles.helloworld}>Hello world!</Text>
    </View>
  </View>
));

Application.scss

.container {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  position: absolute;
}

.transparentContainer {
  background-color: rgba(0,0,0,0);
}

.content {
  flex: 1;
  justify-content: center;
  align-items: center;
}

.helloworld {
  flex: 1;
}

Application.native.scss

.container {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  position: absolute;
}

.transparentContainer {
  background-color: rgba(100, 190, 150, 80);
}

.content {
  flex: 1;
  justify-content: center;
  align-items: center;
}

.helloworld {
  flex: 1;
}

For the native apps, the scss consumed by the app it is going to be the Application.native.scss. This can be changed if the engineers set their custom prefix extension "prefixExtension": "my-native", thus, the end file consumed would be Application.my-native.scss. So, the file is read and the css properties are converted into style properties and applied to the components.

Limitations

Basically, components accept the following syntaxes:

  • className={styles.myCustomClass}
  • className={[styles.myCustomClass, styles.myMinifiedClass].join(' ')} very import to keep with the ' ' otherwise, the result is messed up.
  • className={styles.myCustomClass} style={{ color:'#ff558d' }} merging the properties will take the last ones as the highest priority
  • className={[styles.myCustomClass, styles.myMinifiedClass].join(' ')} style={{ color:'#ff558d' }} merging the properties will take the last ones as the highest priority

License

MIT

Contributions are welcome

Send your PR and help making the web/native development more exciting.