inject-css-in-js-webpack-plugin
A webpack plugin to convert an external stylesheet to an embedded stylesheet in JS
html-inline-css-webpack-plugin
A webpack plugin to convert an external stylesheet to an embedded stylesheet in JS. Heavily inspired by HtmlInlineCSSWebpackPlugin which is for making CSS internal in HTML files as opposed to JS files.
<link rel="stylesheet" /> => document.createTextNode(document.createElement('style'))
Requires mini-css-extract-plugin
If you are using html-webpack-plugin, whatever CSS bundle you want needs to have inject: false
for the HtmlWebpackPlugin for that chunk.
Install
NPM
npm i inject-css-in-js-webpack-plugin -D
Yarn
yarn add inject-css-in-js-webpack-plugin -D
Example
const MiniCssExtractPlugin = ;const HtmlWebpackPlugin = ;const InjectCssInJsWebpackPlugin = default; moduleexports = plugins: filename: "[name].css" inject: false { return filename; // use filter if you only want to inline css from the CSS file with bundle in the filename } replace: fileTarget: 'bundle' // this is the built file we'll look for the target to replace with the style tag target: '<span data-css="true"></span>' // this will be replaced with the style tag module: rules: test: /\.css$/ use: MiniCssExtractPluginloader "css-loader" }
Config
filter(optional)
filter?
Return true
to make current file internal, otherwise ignore current file.
example
{ return filename }
replace(optional)
replace?:
fileTarget
A file to search for the target ie bundle.js
target
A target in the generated Javascript for adding the internal stylesheet
example
ReactDOM
... new InjectCssInJsWebpackPlugin,...