Skip to contentpackage
react-native-postcss-transformer
v1.2.1PostCSS transformer for react-native
node any3 depscjsopen source
- license
- MIT
- deps
- 3
- unpacked
- 7.9 kB
- files
- 5
- node
- any
$npm install react-native-postcss-transformer@1.2.1 README
GitHub-flavored package documentation.
downloads
Across all versions, last 30 days.
2,048weekly+153.5%
4,635 in 30 days
Oct 16 → Nov 14hover or arrows
compatibility
Runtime metadata.
install size
Estimated unpacked size across 13 packages.
610.8 kB
Includes dependencies.
dependencies
2 direct · 1 outdated
versions
Latest published May 16, 2024
contents
react-native-postcss-transformer
Feb 10, 2023
maintainers
Registry package owners.
Use PostCSS to style your React Native apps.
How does it work?
:root {
--blue-color: blue;
}
.myClass {
color: var(--blue-color);
}
.myOtherClass {
color: red;
}
.my-dashed-class {
color: green;
}
When you import your stylesheet:
import styles from "./App.css";
Your imported styles will look like this:
var styles = {
myClass: {
color: "blue"
},
myOtherClass: {
color: "red"
},
"my-dashed-class": {
color: "green"
}
};
You can then use that style object with an element:
<MyElement style={styles.myClass} />
<MyElement style={styles["my-dashed-class"]} />
<MyElement className={styles.myClass} />
<MyElement className={styles["my-dashed-class"]} />
<MyElement styleName="myClass my-dashed-class" />
Installation and configuration
Step 1: Install
npm install --save-dev react-native-postcss-transformer postcss
yarn add --dev react-native-postcss-transformer postcss
Step 2: Add your PostCSS config and install your PostCSS plugins
For Expo SDK v41.0.0 or newer
Merge the contents from your project's metro.config.js file with this config (create the file if it does not exist already).
const { getDefaultConfig } = require("expo/metro-config");
module.exports = (() => {
const config = getDefaultConfig(__dirname);
const { transformer, resolver } = config;
config.transformer = {
...transformer,
babelTransformerPath: require.resolve("./postcss-transformer.js")
};
config.resolver = {
...resolver,
sourceExts: [...sourceExts, "css", "pcss"]
};
return config;
})();
For React Native v0.72.1 or newer
Merge the contents from your project's metro.config.js file with this config (create the file if it does not exist already).
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;
/**
* Metro configuration
* https://reactnative.dev/docs/metro
*
* @type {import('metro-config').MetroConfig}
*/
const config = {
transformer: {
babelTransformerPath: require.resolve("./postcss-transformer.js")
},
resolver: {
sourceExts: [...sourceExts, "css", "pcss"]
}
};
module.exports = mergeConfig(defaultConfig, config);
Create postcss-transformer.js file to your project's root and specify supported extensions:
const upstreamTransformer = require("@react-native/metro-babel-transformer");
const postcssTransformer = require("react-native-postcss-transformer");
const postCSSExtensions = ["css", "pcss"]; // <-- Add other extensions if needed.
module.exports.transform = function ({ src, filename, ...rest }) {
if (postCSSExtensions.some((ext) => filename.endsWith("." + ext))) {
return postcssTransformer.transform({ src, filename, ...rest });
}
return upstreamTransformer.transform({ src, filename, ...rest });
};
Dependencies
This library has the following Node.js modules as dependencies:
TODO