I want to build a react app and have the static css and js files effectively embedded into the index.html that gets produced when I call yarn build
.
I can obviously hack the output and replace the <script src"...
and <link href="/static/css/...
tags with inline versions but was hoping for a more elegant solution.
When you create a production build for your React App, the output folder contains the main index. html file and associated JavaScript and CSS files are added in the /static/js and /static/css folders respectively. If you are to combine all these JS and CSS files of React App in a single bundle, you can use gulp.
If you're hosting your build with a static hosting provider you can use react-snapshot or react-snap to generate HTML pages for each route, or relative link, in your application. These pages will then seamlessly become active, or “hydrated”, when the JavaScript bundle has loaded.
“run single spec file react” Code Answer n order to run a specific test, you'll need to use the jest command. npm test will not work. To access jest directly on the command line, install it via npm i -g jest-cli or yarn global add jest-cli. Then simply run your specific test with jest bar.
I got it working. For future reference (using react 16.7.0 & yarn 1.10.1)...
Create a react app:
npx create-react-app my-app
cd my-app
yarn build
All good? Cool, run eject
so that you have access to the webpack config:
yarn eject
rm -rf build node_modules
yarn install
yarn build
Add this project and update webpack:
yarn add [email protected]
yarn add [email protected]
Edit config/webpack.config.js
:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin'); <--- add this
...
plugins: [
new HtmlWebpackPlugin(
Object.assign(
...
isEnvProduction
? {
inlineSource: '.(js|css)$', <-- add this
minify: {
...
),
...
isEnvProduction &&
shouldInlineRuntimeChunk &&
new HtmlWebpackInlineSourcePlugin(HtmlWebpackPlugin), <--- add this
new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime~.+[.]js/]),
...
Run rm -rf build && yarn build && serve -s build
- the index.html
you load should now contain all the js and css stuff inline. Note that the static files are still created in the build dir, but they aren't used by the page.
There is a Webpack plugin that has been created for this purpose:
https://www.npmjs.com/package/html-webpack-inline-source-plugin
As the README specifies, this must be used with html-webpack-plugin, and you have to specify the inlineSource
that is passed in:
plugins: [
new HtmlWebpackPlugin({
inlineSource: '.(js|css)$' // embed all javascript and css inline
}),
new HtmlWebpackInlineSourcePlugin()
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With