Gathering detailed insights and metrics for @automattic/mini-css-extract-plugin-with-rtl
Gathering detailed insights and metrics for @automattic/mini-css-extract-plugin-with-rtl
Gathering detailed insights and metrics for @automattic/mini-css-extract-plugin-with-rtl
Gathering detailed insights and metrics for @automattic/mini-css-extract-plugin-with-rtl
mini-css-extract-plugin fork with support for WebpackRtlPlugin
npm install @automattic/mini-css-extract-plugin-with-rtl
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (86.52%)
CSS (9.75%)
HTML (3.73%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
6 Stars
121 Commits
2 Forks
3 Watchers
3 Branches
136 Contributors
Updated on May 05, 2024
Latest Version
0.8.0
Package Id
@automattic/mini-css-extract-plugin-with-rtl@0.8.0
Size
14.46 kB
NPM Version
6.9.0
Node Version
10.16.3
Published on
Nov 27, 2019
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
1
31
This plugin is a fork of mini-css-extract-plugin but with a support for WebpackRTLPlugin. Namely, it allows to load async CSS files depending of page's current direction. Please check mentioned packages to learn how to use them.
1npm install --save-dev mini-css-extract-plugin-with-rtl
webpack.config.js
1const MiniCssExtractPlugin = require("mini-css-extract-plugin-with-rtl"); 2const WebpackRTLPlugin = require("webpack-rtl-plugin"); 3 4module.exports = { 5 plugins: [ 6 new MiniCssExtractPlugin({ 7 // Options similar to the same options in webpackOptions.output 8 // both options are optional 9 filename: '[name].css', 10 chunkFilename: '[id].css', 11 rtlEnabled: true, 12 // Global variable (e.g., `window.pageDir`) whose value determines whether to load the LTR or 13 // RTL version of a CSS chunk at runtime. Value should be either `"ltr"` or `"rtl"`. If the 14 // `rtlGlobalVar` option is not specified, we check `document.dir` by default. 15 rtlGlobalVar: 'pageDir' 16 17 }), 18 new WebpackRTLPlugin() // You must not pass filename option 19 ], 20 module: { 21 rules: [ 22 { 23 test: /\.css$/, 24 use: [ 25 { 26 loader: MiniCssExtractPlugin.loader, 27 options: { 28 // you can specify a publicPath here 29 // by default it uses publicPath in webpackOptions.output 30 publicPath: '../', 31 hmr: process.env.NODE_ENV === 'development', 32 }, 33 }, 34 'css-loader', 35 ], 36 }, 37 ], 38 }, 39};
Originally created by Maxim Valenko as beshanoe/mini-css-extract-plugin-with-rtl. Automattic updated the plugin to use the latest mini-css-extract-plugin
upstream version, reorganized the commit structure to make the fork easier to maintain, and added support for data-webpack
attribute in the <link>
tags inserted into the HTML body.
Original mini-css-extract-plugin
README continues from here
This plugin extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS and SourceMaps.
It builds on top of a new webpack v4 feature (module types) and requires webpack 4 to work.
Compared to the extract-text-webpack-plugin:
1npm install --save-dev mini-css-extract-plugin
publicPath
Type: String|Function
Default: the publicPath
in webpackOptions.output
Specifies a custom public path for the target file(s).
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2module.exports = { 3 plugins: [ 4 new MiniCssExtractPlugin({ 5 // Options similar to the same options in webpackOptions.output 6 // all options are optional 7 filename: '[name].css', 8 chunkFilename: '[id].css', 9 ignoreOrder: false, // Enable to remove warnings about conflicting order 10 }), 11 ], 12 module: { 13 rules: [ 14 { 15 test: /\.css$/, 16 use: [ 17 { 18 loader: MiniCssExtractPlugin.loader, 19 options: { 20 // you can specify a publicPath here 21 // by default it uses publicPath in webpackOptions.output 22 publicPath: '../', 23 hmr: process.env.NODE_ENV === 'development', 24 }, 25 }, 26 'css-loader', 27 ], 28 }, 29 ], 30 }, 31};
publicPath
function examplewebpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2module.exports = { 3 plugins: [ 4 new MiniCssExtractPlugin({ 5 // Options similar to the same options in webpackOptions.output 6 // both options are optional 7 filename: '[name].css', 8 chunkFilename: '[id].css', 9 }), 10 ], 11 module: { 12 rules: [ 13 { 14 test: /\.css$/, 15 use: [ 16 { 17 loader: MiniCssExtractPlugin.loader, 18 options: { 19 publicPath: (resourcePath, context) => { 20 // publicPath is the relative path of the resource to the context 21 // e.g. for ./css/admin/main.css the publicPath will be ../../ 22 // while for ./css/main.css the publicPath will be ../ 23 return path.relative(path.dirname(resourcePath), context) + '/'; 24 }, 25 }, 26 }, 27 'css-loader', 28 ], 29 }, 30 ], 31 }, 32};
This plugin should be used only on production
builds without style-loader
in the loaders chain, especially if you want to have HMR in development
.
Here is an example to have both HMR in development
and your styles extracted in a file for production
builds.
(Loaders options left out for clarity, adapt accordingly to your needs.)
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2const devMode = process.env.NODE_ENV !== 'production'; 3 4module.exports = { 5 plugins: [ 6 new MiniCssExtractPlugin({ 7 // Options similar to the same options in webpackOptions.output 8 // both options are optional 9 filename: devMode ? '[name].css' : '[name].[hash].css', 10 chunkFilename: devMode ? '[id].css' : '[id].[hash].css', 11 }), 12 ], 13 module: { 14 rules: [ 15 { 16 test: /\.(sa|sc|c)ss$/, 17 use: [ 18 { 19 loader: MiniCssExtractPlugin.loader, 20 options: { 21 hmr: process.env.NODE_ENV === 'development', 22 }, 23 }, 24 'css-loader', 25 'postcss-loader', 26 'sass-loader', 27 ], 28 }, 29 ], 30 }, 31};
extract-mini-css-plugin supports hot reloading of actual css files in development. Some options are provided to enable HMR of both standard stylesheets and locally scoped CSS or CSS modules. Below is an example configuration of mini-css for HMR use with CSS modules.
While we attempt to hmr css-modules. It is not easy to perform when code-splitting with custom chunk names. reloadAll
is an option that should only be enabled if HMR isn't working correctly. The core challenge with css-modules is that when code-split, the chunk ids can and do end up different compared to the filename.
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2module.exports = { 3 plugins: [ 4 new MiniCssExtractPlugin({ 5 // Options similar to the same options in webpackOptions.output 6 // both options are optional 7 filename: '[name].css', 8 chunkFilename: '[id].css', 9 }), 10 ], 11 module: { 12 rules: [ 13 { 14 test: /\.css$/, 15 use: [ 16 { 17 loader: MiniCssExtractPlugin.loader, 18 options: { 19 // only enable hot in development 20 hmr: process.env.NODE_ENV === 'development', 21 // if hmr does not work, this is a forceful method. 22 reloadAll: true, 23 }, 24 }, 25 'css-loader', 26 ], 27 }, 28 ], 29 }, 30};
To minify the output, use a plugin like optimize-css-assets-webpack-plugin. Setting optimization.minimizer
overrides the defaults provided by webpack, so make sure to also specify a JS minimizer:
webpack.config.js
1const TerserJSPlugin = require('terser-webpack-plugin'); 2const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 3const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); 4module.exports = { 5 optimization: { 6 minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})], 7 }, 8 plugins: [ 9 new MiniCssExtractPlugin({ 10 filename: '[name].css', 11 chunkFilename: '[id].css', 12 }), 13 ], 14 module: { 15 rules: [ 16 { 17 test: /\.css$/, 18 use: [MiniCssExtractPlugin.loader, 'css-loader'], 19 }, 20 ], 21 }, 22};
The runtime code detects already added CSS via <link>
or <style>
tag.
This can be useful when injecting CSS on server-side for Server-Side-Rendering.
The href
of the <link>
tag has to match the URL that will be used for loading the CSS chunk.
The data-href
attribute can be used for <link>
and <style>
too.
When inlining CSS data-href
must be used.
Similar to what extract-text-webpack-plugin does, the CSS
can be extracted in one CSS file using optimization.splitChunks.cacheGroups
.
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2module.exports = { 3 optimization: { 4 splitChunks: { 5 cacheGroups: { 6 styles: { 7 name: 'styles', 8 test: /\.css$/, 9 chunks: 'all', 10 enforce: true, 11 }, 12 }, 13 }, 14 }, 15 plugins: [ 16 new MiniCssExtractPlugin({ 17 filename: '[name].css', 18 }), 19 ], 20 module: { 21 rules: [ 22 { 23 test: /\.css$/, 24 use: [MiniCssExtractPlugin.loader, 'css-loader'], 25 }, 26 ], 27 }, 28};
You may also extract the CSS based on the webpack entry name. This is especially useful if you import routes dynamically but want to keep your CSS bundled according to entry. This also prevents the CSS duplication issue one had with the ExtractTextPlugin.
1const path = require('path'); 2const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 3 4function recursiveIssuer(m) { 5 if (m.issuer) { 6 return recursiveIssuer(m.issuer); 7 } else if (m.name) { 8 return m.name; 9 } else { 10 return false; 11 } 12} 13 14module.exports = { 15 entry: { 16 foo: path.resolve(__dirname, 'src/foo'), 17 bar: path.resolve(__dirname, 'src/bar'), 18 }, 19 optimization: { 20 splitChunks: { 21 cacheGroups: { 22 fooStyles: { 23 name: 'foo', 24 test: (m, c, entry = 'foo') => 25 m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry, 26 chunks: 'all', 27 enforce: true, 28 }, 29 barStyles: { 30 name: 'bar', 31 test: (m, c, entry = 'bar') => 32 m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry, 33 chunks: 'all', 34 enforce: true, 35 }, 36 }, 37 }, 38 }, 39 plugins: [ 40 new MiniCssExtractPlugin({ 41 filename: '[name].css', 42 }), 43 ], 44 module: { 45 rules: [ 46 { 47 test: /\.css$/, 48 use: [MiniCssExtractPlugin.loader, 'css-loader'], 49 }, 50 ], 51 }, 52};
With the moduleFilename
option you can use chunk data to customize the filename. This is particularly useful when dealing with multiple entry points and wanting to get more control out of the filename for a given entry point/chunk. In the example below, we'll use moduleFilename
to output the generated css into a different directory.
1const miniCssExtractPlugin = new MiniCssExtractPlugin({ 2 moduleFilename: ({ name }) => `${name.replace('/js/', '/css/')}.css`, 3});
For long term caching use filename: "[contenthash].css"
. Optionally add [name]
.
For projects where css ordering has been mitigated through consistent use of scoping or naming conventions, the css order warnings can be disabled by setting the ignoreOrder flag to true for the plugin.
1new MiniCssExtractPlugin({ 2 ignoreOrder: true, 3}),
If you'd like to extract the media queries from the extracted CSS (so mobile users don't need to load desktop or tablet specific CSS anymore) you should use one of the following plugins:
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
no SAST tool detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
123 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
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 More