Installations
npm install babel-plugin-css-to-js-transform
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
15.3.0
NPM Version
7.0.15
Score
69.3
Supply Chain
96.5
Quality
73.6
Maintenance
25
Vulnerability
99.1
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (95.63%)
CSS (4.37%)
Developer
wapplr
Download Statistics
Total Downloads
2,198
Last Day
2
Last Week
6
Last Month
46
Last Year
482
GitHub Statistics
1 Stars
58 Commits
2 Watching
1 Branches
1 Contributors
Bundle Size
242.99 kB
Minified
76.23 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.2
Package Id
babel-plugin-css-to-js-transform@1.0.2
Unpacked Size
26.15 kB
Size
6.31 kB
File Count
4
NPM Version
7.0.15
Node Version
15.3.0
Total Downloads
Cumulative downloads
Total Downloads
2,198
Last day
100%
2
Compared to previous day
Last week
-57.1%
6
Compared to previous week
Last month
64.3%
46
Compared to previous month
Last year
-48.6%
482
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
2
Babel-plugin-css-to-js-transform
This Babel plugin finds all require
and all import
function for css files,
and replace them with a new file with this name [filename]_css.js
.
It is keeps the the require
and the import
.
In the file only replaces the file name with the new file name.
Then the transform generate a new file [filename]_css.js
from [filename].css
.
It inserts class names as default export
by css-modules
with css-modules-require-hook package,
and insert a getter _getCss
function to the default object like
some style loaders eg: isomorphic-style-loader
This plugin is based on the fantastic babel-plugin-css-modules-transform.
Why?
There are two reasons what the plugin was written:
- The exists plugins don't support async plugins for postcss
- Doesn't want insert class names to the file because it causes many duplicates and increases the bundle size.
These are especially interesting if you want to create a reusable high order component or module, and you want to add a css. When you create an end user type software use webpack and style-loaders.
Warning
This plugin is experimental, pull requests are welcome.
Example
1/* srcDir/test_require.css */ 2 3.someClass { 4 color: red; 5 display: flex; 6}
1// srcDir/component.js 2const styles = require("./test_import.css");
1// outDir/test_require_css.js 2"use strict"; 3 4Object.defineProperty(exports, "__esModule", { 5 value: true 6}); 7 8const tokens = { 9 "someClass":"test_require_31cRH" 10}; 11 12tokens._getCss = function () { 13 return `/* imported from test_require.css */ .test_require_31cRH { color: red; display: flex; } `; 14}; 15 16exports["default"] = tokens;
1// outDir/component.js 2var styles = require("dir/test_require_css.js")["default"];
Installation
1npm install --save-dev babel-plugin-css-to-js-transform
Include plugin in .babelrc
1{ 2 "plugins": ["css-to-js-transform"] 3}
With custom options
1module.exports = function (api, opts, env) { 2 return { 3 "plugins": [ 4 [ 5 require("babel-plugin-css-to-js-transform").default, 6 { 7 cssModulesOptions: { 8 generateScopedName: "[name]_[hash:base64:5]", 9 //more options here: [css-modules-require-hook](https://github.com/css-modules/css-modules-require-hook) 10 }, 11 alias: function alias({filePathOrModuleName, root, outDir, srcDir}) { 12 let relativeFromRoot = relative(root, filePathOrModuleName); 13 //if the processed css is exists the plugin read it from out folder. 14 if (relativeFromRoot.slice(0,3) === srcDir && existsSync(resolve(root, outDir + relativeFromRoot.slice(3)))){ 15 relativeFromRoot = outDir + relativeFromRoot.slice(3) 16 } 17 return resolve(root, relativeFromRoot); 18 }, 19 outDir: "dist" 20 } 21 ] 22 ] 23 } 24}
Using a processor
When using this plugin with a processor, run it before this plugin running. This example show you how create a build function with postcss.
You can try it in test package from command line: babel-plugin-test build
1// tools/build.js 2const postcss = require("postcss"); 3const path = require("path"); 4const fs = require("fs"); 5 6/*...*/ 7 8/**Create a postcss runner*/ 9async function processCssFunction(processCss) { 10 const plugins = [ 11 require("postcss-import")(), 12 require("postcss-calc")(), 13 require("pleeease-filters")(), 14 require("pixrem")(), 15 require("postcss-flexbugs-fixes")(), 16 require("postcss-preset-env")({ 17 stage: 3, 18 autoprefixer: { flexbox: "no-2009" }, 19 }), 20 ]; 21 const runner = postcss(plugins) 22 return await processCss({postcss, plugins, runner}); 23} 24 25/**Create the processCss function what find all css files in src folder, 26 * and it create generated css files to dist folder. 27 * You can set up root, src and dist folders 28 */ 29 30async function processCss(p = {}) { 31 32 const {rootPath, distPath, srcPath} = getPaths(p) 33 34 await processCssFunction(async function processCss({runner}) { 35 36 function recursiveReadDir(entriesPath, o = {}) { 37 fs.readdirSync(entriesPath).forEach(function(file){ 38 const curPath = path.resolve(entriesPath, file); 39 if(fs.lstatSync(curPath).isDirectory()) { 40 recursiveReadDir(curPath, o); 41 } else if (file.match(".css")){ 42 const srcRelative = path.relative(srcPath, curPath); 43 const rootRelative = path.relative(rootPath, curPath); 44 o[srcRelative] = "./"+rootRelative; 45 } 46 }); 47 } 48 49 const entries = {}; 50 recursiveReadDir(srcPath, entries); 51 52 await Promise.all(Object.keys(entries).map(async function (relativePath) { 53 return new Promise(async function(resolve, reject) { 54 try { 55 const from = path.resolve(srcPath, relativePath); 56 const to = path.resolve(distPath, relativePath); 57 const css = fs.readFileSync(from); 58 const result = await runner.process(css, {from: from, to: to}) 59 if (!fs.existsSync(path.dirname(to))){ 60 fs.mkdirSync(path.dirname(to), { recursive: true }); 61 } 62 if (!fs.existsSync(to)) { 63 fs.writeFileSync(to, result.css, function () { 64 return true; 65 }) 66 console.log("Css processed: " + to) 67 } else { 68 console.log("File aready exists, run clean script or delete it manually before process css: " + to) 69 } 70 return resolve(); 71 } catch (e) { 72 return reject(e) 73 } 74 }) 75 })) 76 77 }) 78} 79 80/*...*/ 81 82/** 83 * Build: first processCss, then babel 84 **/ 85async function build(p = {}) { 86 87 const {rootPath, distPath, srcPath} = getPaths(p) 88 89 await clean(p); 90 await processCss(p); 91 const exec = require("child_process").exec; 92 const execText = path.resolve(rootPath, "node_modules/.bin/babel") + " " + srcPath + " --presets=babel-preset-for-test --out-dir " + distPath; 93 console.log("Run babel: " + execText); 94 await exec(execText).stderr.pipe(process.stderr); 95} 96 97/*...*/ 98
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE.txt:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.txt:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to babel-plugin-css-to-js-transform
babel-plugin-transform-cssobj
Babel plugin to transform css into cssobj (CSS in JS engine), map class names into cssobj localized names
@diotoborg/architecto-at
> [Gulp](http://gulpjs.com) plugin to preprocess HTML, JavaScript, and other files based on custom context or environment configuration
@hishprorg/dolore-nam
## Note this is a fork of jsinspect that now supports ES2020 standard (and most proposed features), TS and TSX files. It uses Babel 8's parser, upgrading from babylon to the new babel/parser. Support for Flow has been removed in favour of TS.
@swenkerorg/sed-dolores
A code-generating [JSON Schema](https://json-schema.org/) validator that attempts to be reasonably secure.