Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
css-modules-require-hook
Advanced tools
The css-modules-require-hook package is a tool that allows you to use CSS Modules in Node.js by transforming CSS files into JavaScript objects. This is particularly useful for server-side rendering (SSR) and testing environments where you need to handle CSS files in a Node.js context.
Basic Setup
This feature allows you to set up the css-modules-require-hook with a custom naming pattern for the generated CSS class names. The code sample demonstrates how to configure the hook and require a CSS file, which will be transformed into a JavaScript object.
const hook = require('css-modules-require-hook');
hook({
generateScopedName: '[name]__[local]___[hash:base64:5]'
});
const styles = require('./styles.css');
console.log(styles);
Custom Preprocessor
This feature allows you to use a custom preprocessor like Sass. The code sample shows how to configure the hook to preprocess SCSS files using node-sass before transforming them into JavaScript objects.
const hook = require('css-modules-require-hook');
const sass = require('node-sass');
hook({
extensions: ['.scss'],
preprocessCss: (data, filename) => {
return sass.renderSync({
data,
file: filename
}).css;
}
});
const styles = require('./styles.scss');
console.log(styles);
Custom CamelCase
This feature enables the conversion of CSS class names to camelCase in the resulting JavaScript object. The code sample demonstrates how to configure the hook to use camelCase and access the class names in camelCase format.
const hook = require('css-modules-require-hook');
hook({
camelCase: true
});
const styles = require('./styles.css');
console.log(styles);
console.log(styles.someClassName); // Access class names in camelCase
babel-plugin-react-css-modules is a Babel plugin that allows you to use CSS Modules in React components with a more seamless integration. Unlike css-modules-require-hook, which is used in a Node.js context, this plugin is specifically designed for use with Babel and React.
postcss-modules is a PostCSS plugin that enables the use of CSS Modules. It provides more flexibility and can be integrated into a broader PostCSS setup, making it suitable for build processes that already use PostCSS. Unlike css-modules-require-hook, it is not limited to Node.js environments.
css-modules-loader-core is a low-level library for loading and processing CSS Modules. It provides the core functionality needed to implement CSS Modules in various environments. While css-modules-require-hook is a higher-level tool for Node.js, css-modules-loader-core offers more granular control and can be used to build custom solutions.
The require hook compiles CSS Modules in runtime. This is similar to Babel's babel/register.
A CSS Module is a CSS file in which all class names and animation names are scoped locally by default. Learn more in the article CSS Modules - Welcome to the Future by Glen Maddern.
Compiling in runtime, universal usage.
To use this tool we require Node.js v0.12.x (or higher) and several modules to be installed.
$ npm i css-modules-require-hook
In this section I've tried to cover the common cases of usage.
Usually, Node.js caches all the require
calls by default. In order to invalidate cache for the purpose of development you should set the environment variable NODE_ENV
to development
. For example:
$ NODE_ENV=development node server.js
Still you can use devMode
option (see below) to override behavior which is imposed by environment variable.
Basically to attach the require hook you need to require this module. If you need to adjust it see the tuning section below.
require('css-modules-require-hook');
// var styles = require('./icon.css');
var hook = require('css-modules-require-hook');
var cssnext = require('cssnext');
hook({
prepend: [
// adding CSS Next plugin
cssnext(),
],
});
var hook = require('css-modules-require-hook');
hook({
generateScopedName: '[name]__[local]___[hash:base64:5]',
});
var hook = require('css-modules-require-hook');
var stylus = require('stylus');
hook({
extensions: ['.styl'],
preprocessCss: function (css, filename) {
return stylus(css)
.set('filename', filename)
.render();
},
});
// var styles = require('./demo.styl');
To adjust the require hook you need to provide params to the exported function.
var hook = require('css-modules-require-hook');
hook({
// append: [],
// generateScopedName: function () {},
// or any other options
// see the list below
});
append
arrayAppends custom plugins to the end of the PostCSS pipeline.
prepend
arrayPrepends custom plugins to the beginning of the PostCSS pipeline.
use
arrayProvides the full list of PostCSS plugins to the pipeline. Providing this cancels append
, prepend
, createImportedName
, generateScopedName
options.
preprocessCss
functionIn rare cases you may want to precompile styles, before they will be passed to the PostCSS pipeline. You should use synchronous transformations, since require
function is synchronous.
hook({
/**
* @param {string} css
* @param {string} filepath Absolute path to the file
* @return {string}
*/
preprocessCss: function (css, filepath) {
return css;
}
});
processCss
functionIn rare cases you may want to get compiled styles in runtime, so providing this option helps.
hook({
/**
* @param {string} css
* @param {string} filepath Absolute path to the file
*/
processCss: function (css, filepath) { /* */ }
});
devMode
booleanHelps you to invalidate cache of all require
calls. Usually used for the development purpose. Also overrides behavior, imposed by NODE_ENV
environment variable. For example:
hook({
devMode: false,
});
extensions
arrayAttach the require hook to additional file extensions (for example ['.scss']
).
rootDir
stringProvides absolute path to the project directory. Providing this will result in better generated class names. It can be obligatory, if you run require hook and build tools (like css-modulesify) from different working directories.
to
stringProvides to
option to the LazyResult instance.
createImportedName
functionShort alias for the postcss-modules-extract-imports plugin's createImportedName
option.
generateScopedName
functionShort alias for the postcss-modules-scope plugin's option. Helps you to specify the custom way to build generic names for the class selectors. You may also use a string pattern similar to the webpack's css-loader.
hook({
generateScopedName: '[name]__[local]___[hash:base64:5]'
});
or
hook({
/**
* @param {string} name Usually a class name
* @param {string} filepath
* @param {string} css
* @return {string}
*/
generateScopedName: function (name, filepath, css) {
return name;
}
});
mode
stringShort alias for the postcss-modules-local-by-default plugin's option.
debug package is used for debugging. So to turn it on simply specify the DEBUG environment variable:
DEBUG=css-modules:fetch
— to see resolved paths to the files.DEBUG=css-modules:setup
— to see the new options list.DEBUG=css-modules:*
— to see everything.FAQs
A require hook to compile CSS Modules on the fly
The npm package css-modules-require-hook receives a total of 188,668 weekly downloads. As such, css-modules-require-hook popularity was classified as popular.
We found that css-modules-require-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.