Previous answers don't work because html-webpack-inline-source-plugin
is no longer supported by the author, replaced by official plugins html-inline-script-webpack-plugin
and html-inline-css-webpack-plugin
. The following method works with the latest React 17 as of October 2021. Steps:
Create a React app
create-react-app my-app
cd my-app
yarn
yarn start
Make sure it's working, you're happy with the output. Then, eject your config (I think this can be done without ejecting config, but I can't be bothered to look up how to configure CRA)
yarn eject
rm -rf build node_modules
yarn
yarn build
Then, add the Webpack plugins (Assuming you want both CSS and scripts embedded here, just remove one if you only want the other)
yarn add html-inline-css-webpack-plugin -D
yarn add html-inline-script-webpack-plugin -D
Finally, add these to the Webpack config config/webpack.config.js
. Firstly declare them at the top of the file:
const HTMLInlineCSSWebpackPlugin = require('html-inline-css-webpack-plugin').default;
const HtmlInlineScriptPlugin = require('html-inline-script-webpack-plugin');
Then add them to the plugins: [...]
collection. Search for new InlineChunkHtmlPlugin
and add them just after that:
isEnvProduction &&
shouldInlineRuntimeChunk &&
new HTMLInlineCSSWebpackPlugin(),
isEnvProduction &&
shouldInlineRuntimeChunk &&
new HtmlInlineScriptPlugin(),
Note: It is important to include the isEnvProduction
/ shouldInlineRuntimeChunk
checks! If you skip these the hot refresh won't work when you run yarn start
. You only want to embed the scripts in production mode, not during development.
Now, if you run yarn build
you'll find all the CSS and JS embedded in build/index.html
. If you run yarn start
it'll start a hot-reloading dev environment you can develop in.