All Projects → michalkvasnicak → Babel Plugin Css Modules Transform

michalkvasnicak / Babel Plugin Css Modules Transform

Licence: mit
Extract css class names from required css module files, so we can render it on server.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Babel Plugin Css Modules Transform

core-web
like core-js but for Web APIs (based on polyfill.io)
Stars: ✭ 34 (-89.31%)
Mutual labels:  babel, babel-plugin
babel-plugin-transform-html-import-to-string
Turn HTML imports (and export from) into constant strings
Stars: ✭ 22 (-93.08%)
Mutual labels:  babel, babel-plugin
babel-plugin-source-map-support
A Babel plugin which automatically makes stack traces source-map aware
Stars: ✭ 41 (-87.11%)
Mutual labels:  babel, babel-plugin
Xwind
Tailwind CSS as a templating language in JS and CSS-in-JS
Stars: ✭ 249 (-21.7%)
Mutual labels:  babel, babel-plugin
Babel Blade
(under new management!) ⛸️Solve the Double Declaration problem with inline GraphQL. Babel plugin/macro that works with any GraphQL client!
Stars: ✭ 266 (-16.35%)
Mutual labels:  babel, babel-plugin
S2s
Coding time Compile. A tool to write code fastest.
Stars: ✭ 254 (-20.13%)
Mutual labels:  babel, babel-plugin
Seek Style Guide
Living style guide for SEEK, powered by React, webpack, CSS Modules and Less.
Stars: ✭ 302 (-5.03%)
Mutual labels:  babel, css-modules
Babel Plugin React Intl Auto
i18n for the component age. Auto management react-intl ID.
Stars: ✭ 203 (-36.16%)
Mutual labels:  babel, babel-plugin
Grafoo
A GraphQL Client and Toolkit
Stars: ✭ 264 (-16.98%)
Mutual labels:  babel, babel-plugin
babel-plugin-syntax-pipeline
Allow parsing of pipeline operator |>
Stars: ✭ 23 (-92.77%)
Mutual labels:  babel, babel-plugin
React
Extremely simple boilerplate, easiest you can find, for React application including all the necessary tools: Flow | React 16 | redux | babel 6 | webpack 3 | css-modules | jest | enzyme | express + optional: sass/scss
Stars: ✭ 244 (-23.27%)
Mutual labels:  babel, css-modules
Babel Plugin Module Resolver
Custom module resolver plugin for Babel
Stars: ✭ 3,134 (+885.53%)
Mutual labels:  babel, babel-plugin
Eslint Import Resolver Babel Module
Custom eslint resolve for babel-plugin-module-resolver
Stars: ✭ 236 (-25.79%)
Mutual labels:  babel, babel-plugin
babel-plugin-proposal-pattern-matching
the minimal grammar, high performance JavaScript pattern matching implementation
Stars: ✭ 34 (-89.31%)
Mutual labels:  babel, babel-plugin
Front End Guide
📚 Study guide and introduction to the modern front end stack.
Stars: ✭ 14,073 (+4325.47%)
Mutual labels:  babel, css-modules
babel-plugin-transform-replace-expressions
A Babel plugin for replacing expressions with other expressions
Stars: ✭ 23 (-92.77%)
Mutual labels:  babel, babel-plugin
Typescript Webpack React Redux Boilerplate
React and Redux with TypeScript
Stars: ✭ 182 (-42.77%)
Mutual labels:  babel, css-modules
Babel Plugin Macros
🎣 Allows you to build simple compile-time libraries
Stars: ✭ 2,366 (+644.03%)
Mutual labels:  babel, babel-plugin
babel-plugin-remove-test-ids
🐠 Babel plugin to strip `data-test-id` HTML attributes
Stars: ✭ 40 (-87.42%)
Mutual labels:  babel, babel-plugin
Babel Plugin Console
Babel Plugin that adds useful build time console functions 🎮
Stars: ✭ 278 (-12.58%)
Mutual labels:  babel, babel-plugin

babel-plugin-css-modules-transform Circle CI

🎉 Babel 6 and Babel 7 compatible

⚠️ Babel 7 compatibility added in 1.4.0

This Babel plugin finds all requires for css module files and replace them with a hash where keys are class names and values are generated css class names.

This plugin is based on the fantastic css-modules-require-hook.

Warning

This plugin is experimental, pull requests are welcome.

Do not run this plugin as part of webpack frontend configuration. This plugin is intended only for backend compilation.

Example

/* test.css */

.someClass {
    color: red;
}
// component.js
const styles = require('./test.css');

console.log(styles.someClass);

// transformed file
const styles = {
    'someClass': 'Test__someClass___2Frqu'
}

console.log(styles.someClass); // prints Test__someClass___2Frqu

Installation

npm install --save-dev babel-plugin-css-modules-transform

Include plugin in .babelrc

{
    "plugins": ["css-modules-transform"]
}

With custom options css-modules-require-hook options

{
    "plugins": [
        [
            "css-modules-transform", {
                "append": [
                    "npm-module-name",
                    "./path/to/module-exporting-a-function.js"
                ],
                "camelCase": false,
                "createImportedName": "npm-module-name",
                "createImportedName": "./path/to/module-exporting-a-function.js",
                "devMode": false,
                "extensions": [".css", ".scss", ".less"], // list extensions to process; defaults to .css
                "generateScopedName": "[name]__[local]___[hash:base64:5]", // in case you don't want to use a function
                "generateScopedName": "./path/to/module-exporting-a-function.js", // in case you want to use a function
                "generateScopedName": "npm-module-name",
                "hashPrefix": "string",
                "ignore": "*css",
                "ignore": "./path/to/module-exporting-a-function-or-regexp.js",
                "preprocessCss": "./path/to/module-exporting-a-function.js",
                "preprocessCss": "npm-module-name",
                "processCss": "./path/to/module-exporting-a-function.js",
                "processCss": "npm-module-name",
                "processorOpts": "npm-module-name",
                "processorOpts": "./path/to/module/exporting-a-plain-object.js",
                "mode": "string",
                "prepend": [
                    "npm-module-name",
                    "./path/to/module-exporting-a-function.js"
                ],
                "extractCss": "./dist/stylesheets/combined.css"
            }
        ]
    ]
}

Using a preprocessor

When using this plugin with a preprocessor, you'll need to configure it as such:

// ./path/to/module-exporting-a-function.js
var sass = require('node-sass');
var path = require('path');

module.exports = function processSass(data, filename) {
    var result;
    result = sass.renderSync({
        data: data,
        file: filename
    }).css;
    return result.toString('utf8');
};

and then add any relevant extensions to your plugin config:

{
    "plugins": [
        [
            "css-modules-transform", {
                "preprocessCss": "./path/to/module-exporting-a-function.js",
                "extensions": [".css", ".scss"]
            }
        ]
    ]
}

Extract CSS Files

When you publish a library, you might want to ship compiled css files as well to help integration in other projects.

An more complete alternative is to use babel-plugin-webpack-loaders but be aware that a new webpack instance is run for each css file, this has a huge overhead. If you do not use fancy stuff, you might consider using babel-plugin-css-modules-transform instead.

To combine all css files in a single file, give its name:

{
    "plugins": [
        [
            "css-modules-transform", {
                "extractCss": "./dist/stylesheets/combined.css"
            }
        ]
    ]
}

To extract all files in a single directory, give an object:

{
    "plugins": [
        [
            "css-modules-transform", {
                "extractCss": {
                    "dir": "./dist/stylesheets/",
                    "relativeRoot": "./src/",
                    "filename": "[path]/[name].css"
                }
            }
        ]
    ]
}

Note that relativeRoot is used to resolve relative directory names, available as [path] in filename pattern.

Keeping import

To keep import statements you should set option keepImport to true. In this way, simultaneously with the converted values, the import will be described as unassigned call expression.

// before
const styles = require('./test.css');
// after
require('./test.css');

const styles = {
    'someClass': 'Test__someClass___2Frqu'
}

Alternatives

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].