{"id":13808980,"url":"https://github.com/kristerkari/react-native-sass-transformer","last_synced_at":"2025-05-15T12:02:57.918Z","repository":{"id":28302676,"uuid":"116600598","full_name":"kristerkari/react-native-sass-transformer","owner":"kristerkari","description":"Use Sass to style your React Native apps.","archived":false,"fork":false,"pushed_at":"2025-02-17T18:12:24.000Z","size":768,"stargazers_count":224,"open_issues_count":22,"forks_count":18,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-09T08:40:39.230Z","etag":null,"topics":["loader","react-native","sass","scss","transform"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kristerkari.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-07T21:29:23.000Z","updated_at":"2025-04-17T22:19:41.000Z","dependencies_parsed_at":"2023-01-14T08:35:16.320Z","dependency_job_id":"99422082-c48d-490e-82fd-b26766c388d3","html_url":"https://github.com/kristerkari/react-native-sass-transformer","commit_stats":{"total_commits":156,"total_committers":5,"mean_commits":31.2,"dds":0.4423076923076923,"last_synced_commit":"1f1b8ee3ddab8570972bbc65858c7f003d40c954"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kristerkari%2Freact-native-sass-transformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kristerkari%2Freact-native-sass-transformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kristerkari%2Freact-native-sass-transformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kristerkari%2Freact-native-sass-transformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kristerkari","download_url":"https://codeload.github.com/kristerkari/react-native-sass-transformer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254133192,"owners_count":22020293,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["loader","react-native","sass","scss","transform"],"created_at":"2024-08-04T01:01:56.943Z","updated_at":"2025-05-15T12:02:57.885Z","avatar_url":"https://github.com/kristerkari.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# react-native-sass-transformer [![NPM version](http://img.shields.io/npm/v/react-native-sass-transformer.svg)](https://www.npmjs.org/package/react-native-sass-transformer) [![Downloads per month](https://img.shields.io/npm/dm/react-native-sass-transformer.svg)](http://npmcharts.com/compare/react-native-sass-transformer?periodLength=30) [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github)\n\nUse [Sass](https://sass-lang.com/) to style your React Native apps.\n\nBehind the scenes the Sass files are transformed to [react native style objects](https://facebook.github.io/react-native/docs/style.html) (look at the examples).\n\n\u003e This transformer can be used together with [React Native CSS modules](https://github.com/kristerkari/react-native-css-modules).\n\n## How does it work?\n\nYour `App.scss` file might look like this:\n\n```scss\n%blue {\n  color: blue;\n}\n.myClass {\n  @extend %blue;\n}\n.myOtherClass {\n  color: red;\n}\n.my-dashed-class {\n  color: green;\n}\n```\n\nWhen you import your stylesheet:\n\n```js\nimport styles from \"./App.scss\";\n```\n\nYour imported styles will look like this:\n\n```js\nvar styles = {\n  myClass: {\n    color: \"blue\"\n  },\n  myOtherClass: {\n    color: \"red\"\n  },\n  \"my-dashed-class\": {\n    color: \"green\"\n  }\n};\n```\n\nYou can then use that style object with an element:\n\n**Plain React Native:**\n\n```jsx\n\u003cMyElement style={styles.myClass} /\u003e\n\n\u003cMyElement style={styles[\"my-dashed-class\"]} /\u003e\n```\n\n**[React Native CSS modules](https://github.com/kristerkari/react-native-css-modules) using [className](https://github.com/kristerkari/babel-plugin-react-native-classname-to-style) property:**\n\n```jsx\n\u003cMyElement className={styles.myClass} /\u003e\n\n\u003cMyElement className={styles[\"my-dashed-class\"]} /\u003e\n```\n\n**[React Native CSS modules](https://github.com/kristerkari/react-native-css-modules) using [styleName](https://github.com/kristerkari/babel-plugin-react-native-stylename-to-style) property:**\n\n```jsx\n\u003cMyElement styleName=\"myClass my-dashed-class\" /\u003e\n```\n\n_Please use the `.scss` file extension for SCSS syntax and the `.sass` file extension for indented Sass syntax._\n\n## Installation and configuration\n\n### Step 1: Install\n\n```sh\nyarn add --dev react-native-sass-transformer sass\n```\n\n### Step 2: Configure the react native packager\n\n#### For Expo SDK v41.0.0 or newer\n\nMerge the contents from your project's `metro.config.js` file with this config (create the file if it does not exist already).\n\n`metro.config.js`:\n\n```js\nconst { getDefaultConfig } = require(\"expo/metro-config\");\n\nmodule.exports = (() =\u003e {\n  const config = getDefaultConfig(__dirname);\n\n  const { transformer, resolver } = config;\n\n  config.transformer = {\n    ...transformer,\n    babelTransformerPath: require.resolve(\"react-native-sass-transformer\")\n  };\n  config.resolver = {\n    ...resolver,\n    sourceExts: [...resolver.sourceExts, \"scss\", \"sass\"]\n  };\n\n  return config;\n})();\n```\n\n---\n\n#### For React Native v0.72.1 or newer\n\nMerge the contents from your project's `metro.config.js` file with this config (create the file if it does not exist already).\n\n`metro.config.js`:\n\n```js\nconst { getDefaultConfig, mergeConfig } = require(\"@react-native/metro-config\");\n\nconst defaultConfig = getDefaultConfig(__dirname);\nconst { assetExts, sourceExts } = defaultConfig.resolver;\n\n/**\n * Metro configuration\n * https://reactnative.dev/docs/metro\n *\n * @type {import('metro-config').MetroConfig}\n */\nconst config = {\n  transformer: {\n    babelTransformerPath: require.resolve(\"react-native-sass-transformer\")\n  },\n  resolver: {\n    sourceExts: [...sourceExts, \"scss\", \"sass\"]\n  }\n};\n\nmodule.exports = mergeConfig(defaultConfig, config);\n```\n\n## Platform specific extensions\n\nIf you need [React Native's platform specific extensions](https://facebook.github.io/react-native/docs/platform-specific-code.html#platform-specific-extensions) for your Sass files, you can use [babel-plugin-react-native-platform-specific-extensions](https://github.com/kristerkari/babel-plugin-react-native-platform-specific-extensions). Platform specific extensions for files imported using Sass' `@import` are supported by default.\n\n## Sass options\n\nIf you need to pass options (e.g. functions) to `sass`, you can do so by creating a `transformer.js` file and doing the following:\n\n```js\nconst upstreamTransformer = require(\"@react-native/metro-babel-transformer\");\nconst sassTransformer = require(\"react-native-sass-transformer\");\n\nmodule.exports.transform = function ({ src, filename, options, ...rest }) {\n  if (filename.endsWith(\".scss\") || filename.endsWith(\".sass\")) {\n    const opts = Object.assign(options, {\n      sassOptions: {\n        functions: {\n          \"rem($px)\": (px) =\u003e {\n            px.setValue(px.getValue() / 16);\n            px.setUnit(\"rem\");\n            return px;\n          }\n        }\n      }\n    });\n    return sassTransformer.transform({ src, filename, options: opts, ...rest });\n  } else {\n    return upstreamTransformer.transform({ src, filename, options, ...rest });\n  }\n};\n```\n\nAfter that in `metro.config.js` point the `babelTransformerPath` to that file:\n\n```js\nconst { getDefaultConfig, mergeConfig } = require(\"@react-native/metro-config\");\n\nconst defaultConfig = getDefaultConfig(__dirname);\nconst { assetExts, sourceExts } = defaultConfig.resolver;\n\n/**\n * Metro configuration\n * https://reactnative.dev/docs/metro\n *\n * @type {import('metro-config').MetroConfig}\n */\nconst config = {\n  transformer: {\n    babelTransformerPath: require.resolve(\"./transformer.js\")\n  },\n  resolver: {\n    sourceExts: [...sourceExts, \"scss\", \"sass\"]\n  }\n};\n\nmodule.exports = mergeConfig(defaultConfig, config);\n```\n\n## CSS Custom Properties (CSS variables)\n\n_You need version 1.4.0 or newer_\n\n```css\n:root {\n  --text-color: blue;\n}\n\n.blue {\n  color: var(--text-color);\n}\n```\n\nCSS variables are not supported by default, but you can add support for them by using [PostCSS](https://postcss.org/) and [postcss-css-variables](https://github.com/MadLittleMods/postcss-css-variables#readme) plugin.\n\nStart by installing dependencies:\n\n```sh\nyarn add postcss postcss-css-variables react-native-postcss-transformer --dev\n```\n\nAdd `postcss-css-variables` to your PostCSS configuration with [one of the supported config formats](https://github.com/michael-ciniawsky/postcss-load-config), e.g. `package.json`, `.postcssrc`, `postcss.config.js`, etc.\n\nAfter that create a `transformer.js` file and do the following:\n\n```js\nconst upstreamTransformer = require(\"@react-native/metro-babel-transformer\");\nconst sassTransformer = require(\"react-native-sass-transformer\");\nconst postCSSTransformer = require(\"react-native-postcss-transformer\");\n\nmodule.exports.transform = function ({ src, filename, ...rest }) {\n  if (filename.endsWith(\".scss\") || filename.endsWith(\".sass\")) {\n    return sassTransformer\n      .renderToCSS({ src, filename, options })\n      .then((css) =\u003e\n        postCSSTransformer.transform({ src: css, filename, ...rest })\n      );\n  } else {\n    return upstreamTransformer.transform({ src, filename, ...rest });\n  }\n};\n```\n\nAfter that in `metro.config.js` point the `babelTransformerPath` to that file:\n\n```js\nconst { getDefaultConfig, mergeConfig } = require(\"@react-native/metro-config\");\n\nconst defaultConfig = getDefaultConfig(__dirname);\nconst { assetExts, sourceExts } = defaultConfig.resolver;\n\n/**\n * Metro configuration\n * https://reactnative.dev/docs/metro\n *\n * @type {import('metro-config').MetroConfig}\n */\nconst config = {\n  transformer: {\n    babelTransformerPath: require.resolve(\"./transformer.js\")\n  },\n  resolver: {\n    sourceExts: [...sourceExts, \"scss\", \"sass\"]\n  }\n};\n\nmodule.exports = mergeConfig(defaultConfig, config);\n```\n\n## Dependencies\n\nThis library has the following Node.js modules as dependencies:\n\n- [app-root-path](https://github.com/inxilpro/node-app-root-path)\n- [css-to-react-native-transform](https://github.com/kristerkari/css-to-react-native-transform)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkristerkari%2Freact-native-sass-transformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkristerkari%2Freact-native-sass-transformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkristerkari%2Freact-native-sass-transformer/lists"}