Giter VIP home page Giter VIP logo

react-native-less-transformer's Introduction

react-native-less-transformer NPM version Downloads per month contributions welcome

Use Less to style your React Native apps.

Behind the scenes the .less files are transformed to react native style objects (look at the examples).

This transformer can be used together with React Native CSS modules.

How does it work?

Your App.less file might look like this:

@nice-blue: #5b83ad;
@light-blue: @nice-blue + #111;

.myClass {
  color: @light-blue;
}
.myOtherClass {
  color: red;
}
.my-dashed-class {
  color: green;
}

When you import your stylesheet:

import styles from "./App.less";

Your imported styles will look like this:

var styles = {
  myClass: {
    color: "#6c94be"
  },
  myOtherClass: {
    color: "red"
  },
  "my-dashed-class": {
    color: "green"
  }
};

You can then use that style object with an element:

Plain React Native:

<MyElement style={styles.myClass} />

<MyElement style={styles["my-dashed-class"]} />

React Native CSS modules using className property:

<MyElement className={styles.myClass} />

<MyElement className={styles["my-dashed-class"]} />

React Native CSS modules using styleName property:

<MyElement styleName="myClass my-dashed-class" />

Installation and configuration

Minimum React Native version for this transformer is 0.52. If you are using an older version, please update to a newer React Native version before trying to use this transformer.

Step 1: Install

yarn add --dev react-native-less-transformer less

Step 2: Configure the react native packager

For React Native v0.57 or newer / Expo SDK v31.0.0 or newer

Add this to metro.config.js in your project's root (create the file if it does not exist already):

const { getDefaultConfig } = require("metro-config");

module.exports = (async () => {
  const {
    resolver: { sourceExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("react-native-less-transformer")
    },
    resolver: {
      sourceExts: [...sourceExts, "less"]
    }
  };
})();

If you are using Expo, you also need to add this to app.json:

{
  "expo": {
    "packagerOpts": {
      "config": "metro.config.js",
      "sourceExts": ["js", "jsx", "less"]
    }
  }
}

For React Native v0.56 or older

If you are using React Native without Expo, add this to rn-cli.config.js in your project's root (create the file if you don't have one already):

module.exports = {
  getTransformModulePath() {
    return require.resolve("react-native-less-transformer");
  },
  getSourceExts() {
    return ["js", "jsx", "less"];
  }
};

For Expo SDK v30.0.0 or older

If you are using Expo, instead of adding the rn-cli.config.js file, you need to add this to app.json:

{
  "expo": {
    "packagerOpts": {
      "sourceExts": ["js", "jsx", "less"],
      "transformer": "node_modules/react-native-less-transformer/index.js"
    }
  }
}

LESS options

If you need to pass options (e.g. plugins) to less, you can do so by creating a transformer.js file and doing the following:

// For React Native version 0.73 or later
var upstreamTransformer = require("@react-native/metro-babel-transformer");

// For React Native version 0.59 or later
// var upstreamTransformer = require("metro-react-native-babel-transformer");

// For React Native version 0.56-0.58
// var upstreamTransformer = require("metro/src/reactNativeTransformer");

// For React Native version 0.52-0.55
// var upstreamTransformer = require("metro/src/transformer");

// For React Native version 0.47-0.51
// var upstreamTransformer = require("metro-bundler/src/transformer");

// For React Native version 0.46
// var upstreamTransformer = require("metro-bundler/build/transformer");

var lessTransformer = require("react-native-less-transformer");

module.exports.transform = function ({ src, filename, options }) {
  if (filename.endsWith(".less")) {
    var opts = Object.assign(options, {
      lessOptions: {
        plugins: [require("less-plugin-glob")]
      }
    });
    return lessTransformer.transform({ src, filename, options: opts });
  } else {
    return upstreamTransformer.transform({ src, filename, options });
  }
};

After that in metro.config.js point the babelTransformerPath to that file:

const { getDefaultConfig } = require("metro-config");

module.exports = (async () => {
  const {
    resolver: { sourceExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("./transformer.js")
    },
    resolver: {
      sourceExts: [...sourceExts, "less"]
    }
  };
})();

CSS Custom Properties (CSS variables)

You need version 1.2.1 or newer

:root {
  --text-color: blue;
}

.blue {
  color: var(--text-color);
}

CSS variables are not supported by default, but you can add support for them by using PostCSS and postcss-css-variables plugin.

Start by installing dependencies:

yarn add postcss postcss-css-variables react-native-postcss-transformer --dev

Add postcss-css-variables to your PostCSS configuration with one of the supported config formats, e.g. package.json, .postcssrc, postcss.config.js, etc.

After that create a transformer.js file and do the following:

// For React Native version 0.73 or later
var upstreamTransformer = require("@react-native/metro-babel-transformer");

// For React Native version 0.59 or later
// var upstreamTransformer = require("metro-react-native-babel-transformer");

// For React Native version 0.56-0.58
// var upstreamTransformer = require("metro/src/reactNativeTransformer");

// For React Native version 0.52-0.55
// var upstreamTransformer = require("metro/src/transformer");

// For React Native version 0.47-0.51
// var upstreamTransformer = require("metro-bundler/src/transformer");

// For React Native version 0.46
// var upstreamTransformer = require("metro-bundler/build/transformer");

var lessTransformer = require("react-native-less-transformer");
var postCSSTransformer = require("react-native-postcss-transformer");

module.exports.transform = function ({ src, filename, options }) {
  if (filename.endsWith(".less")) {
    return lessTransformer
      .renderToCSS({ src, filename, options })
      .then((css) =>
        postCSSTransformer.transform({ src: css, filename, options })
      );
  } else {
    return upstreamTransformer.transform({ src, filename, options });
  }
};

After that in metro.config.js point the babelTransformerPath to that file:

const { getDefaultConfig } = require("metro-config");

module.exports = (async () => {
  const {
    resolver: { sourceExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve("./transformer.js")
    },
    resolver: {
      sourceExts: [...sourceExts, "less"]
    }
  };
})();

Dependencies

This library has the following Node.js modules as dependencies:

react-native-less-transformer's People

Contributors

dependabot[bot] avatar kristerkari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-native-less-transformer's Issues

do i need a StyleSheet.create wrapper?

Hi, do I need a wrapper around the StyleSheet.create?
Or does react-native-svg-transformer do it itself?

image

This code doesn't really work ((
but I would like to know whether it is necessary or not.
If necessary, how can you add it?

react-native-web support

Hi, is it possible to make this plugin work for react-native-web? Since necolas/react-native-web#1146 prevents classname from working I wanted to use babel-plugin-react-native-classname-to-style to convert className to style so I can still use css modules for React Native and React JS. But I also need react-native-less-transformer to work in React JS for that to work. Hope you can help me.

if i want to add a plugins `less-plugin-functions` to handler function in transfromer.js,how do i do ?

I try to do it like this ,but it don't work
less-plugin-functions

var lessTransformer = require("react-native-less-transformer");

module.exports.transform = function({ src, filename, options }) {
  if (filename.endsWith(".less")) {
    var opts = Object.assign(options, {
      lessOptions: {
        plugins: [require("less-plugin-functions")]
      }
    });
    return lessTransformer.transform({ src, filename, options: opts });
  } else {
    return upstreamTransformer.transform({ src, filename, options });
  }
};

semver issue

using in expo and facing error while starting server
Failed to construct transformer: TypeError: semver is not a function

console.log says that semver is object. so looks like we need change line
var reactNativeMinorVersion = semver(reactNativeVersionString).minor;
to
var reactNativeMinorVersion = semver.minor(reactNativeVersionString);

i have "react-native-less-transformer": "^1.4.0",
inside have dependency semver v7.5.1.

Error with Jest

Hi!

I have an error when I try to run my Jest tests.
I've followed the instructions and everything works fine for development.

But when I run my tests, I have this error:

tests/views/Login.test.js
  ● Test suite failed to run

    /app/styles/less/common.less:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){@backgroundColor: #fff;
                                                                                             ^

    SyntaxError: Invalid or unexpected token

      1 | import {StyleSheet} from "react-native";
    > 2 | import extraStyles from "./less/common.less";
        | ^
      3 | 
      4 | export const styles = StyleSheet.create({});
      5 | 

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
      at Object.<anonymous> (app/styles/common.js:2:1)
      at Object.<anonymous> (app/views/Login.js:5:1)

I tried to configure Jest in my package.json:

"jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "less",
      "js"
    ],
    "transform": {
      "^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
      "\\.(less)$": "<rootDir>/node_modules/react-native-less-transformer"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(less|js)$",
    "testPathIgnorePatterns": [
      "\\.snap$",
      "<rootDir>/node_modules/"
    ],
    "cacheDirectory": ".jest/cache"
  },

But it raised another error:

TypeError: Jest: a transform must export a `process` function.

      1 | import {StyleSheet} from "react-native";
    > 2 | import extraStyles from "./less/common.less";
        |               ^
      3 | 
      4 | export const styles = StyleSheet.create({});
      5 | 

      at ScriptTransformer._getTransformer (node_modules/jest-runtime/build/script_transformer.js:236:15)
      at Object.<anonymous> (app/styles/common.js:2:15)

My dependencies are:

"dependencies": {
    "axios": "^0.18.0",
    "prop-types": "^15.6.2",
    "react": "16.3.1",
    "react-native": "~0.55.2",
    "react-native-config": "^0.11.5",
    "react-navigation": "^2.10.0",
    "react-redux": "^5.0.7",
    "redux": "^4.0.0"
  },
"devDependencies": {
    "babel-eslint": "^8.2.6",
    "babel-preset-react-native-stage-0": "^1.0.1",
    "eslint": "^5.2.0",
    "eslint-plugin-jest": "^21.18.0",
    "eslint-plugin-react": "^7.10.0",
    "faker": "^4.1.0",
    "jest": "^23.4.2",
    "jest-react-native": "^18.0.0",
    "jscpd": "^0.6.22",
    "less": "^3.8.1",
    "react-native-cli": "^2.0.1",
    "react-native-less-transformer": "^1.1.2",
    "react-test-renderer": "16.3.1"
  },
  • Is there a way of using this library with Jest?

Not working on RN 0.59

After updating the project to 0.59, I get the error:
error: bundling failed: TypeError: Cannot destructure property src of 'undefined' or 'null'. at renderToCSS (node_modules/react-native-less-transformer/index.js:32:21)

On version 0.58 everything is ok

UPD:
You can fix it by change this line
return renderToCSS().then(css => {
to
return renderToCSS({ src, filename, options }).then(css => {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.