This is similar to this question however neither of the answers solves the problem.
After running npm run build
the resultant index.html
looks similar to:
<script>!function (i) { function e(e) { for //rest omitted
<script src="/static/js/2.3f294f32.chunk.js"></script>
<script src="/static/js/main.7b9daa35.chunk.js"></script>
The first <script>
element is inlined javascript that i have extracted to a file called loader.js
<script src="/loader.js"></script>
<script src="/static/js/2.3f294f32.chunk.js"></script>
<script src="/static/js/main.7b9daa35.chunk.js"></script>
this works but I would like to combine all 3 files into a single file
I've tried filesmerge.com to combine the JS files but this results in an error when referencing the single file:
output.min.js:1 Uncaught TypeError: (intermediate value)(...) is not a function
at output.min.js:1
I then tried combining using jscompress.com and whilst this does not produce any errors the react root element is not rendered
I've also tried this solution suggested on the create-react-app repo which does not work. No error is produced but no react element is rendered (page remains blank)
Bundling. Most React apps will have their files “bundled” using tools like Webpack, Rollup or Browserify. Bundling is the process of following imported files and merging them into a single file: a “bundle”. This bundle can then be included on a webpage to load an entire app at once.
To create a new app/project using this tool, all we need to do is run the command "create-react-app" followed by the app name. After running the above command, a new folder called "my-sample-app" will get created and that would have all of our application code.
If the device we're trying to interface can understand JavaScript, we can use React to describe a User Interface for it.
Javascript. export default App; Now run the Nodejs process npm run dev in one terminal and in another terminal start Reactjs using npm start simultaneously. Output: We see react output we see a button “Connect” we have to click it.
If you are to combine all these JS and CSS files of React App in a single bundle, you can use gulp. Here’s how: Go to the command line and install the gulp packages as dev dependencies in your package.json file. Next, create a .env file in your project root folder and set the following environment variable to disable source maps.
Using the above file, you can play around with React however you like and open the html file in your browser to view the UI. We are doing the same for babel. Write the React component inside of the tag and render in the place of a placeholder div by using getElementById! And that's it. Let your react development Journey continue.
Learn how to bundle all the JavaScript and CSS files generated by the React App build and combine them into a single file. 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.
Write the React component inside of the tag and render in the place of a placeholder div by using getElementById! And that's it. Let your react development Journey continue. If you liked that, check out some other stuff you might find interesting: Cheers! Nice job Kapeel, many developers are not aware that you can do this.
In short: It's possible, but not very practical. Why? Your application will no longer be performant as your single bundle file grows. A single large request, instead of smaller requests, will inevitably lead to slower web performance and potentially wasted bandwidth.
On that same note, I'd highly advise against using the CRA for your single-bundled application. While the CRA is a great boilerplate geared toward a DX friendly approach to React with Webpack, it does contain a lot of dependencies that may be unnecessarily bundled with your app.
As such, I'd highly recommend building your own Webpack configuration (it's relatively simple with the help of the Webpack documentation combined with the CRA Webpack notes) or consider alternatives like rollup, gulp, microbundle, or browserify to name a few.
The following procedure below will inevitably become outdated as the CRA gets updated. Therefore, use these instructions at your own risk.
CRA Version: v4.0.3
You'll first want to eject: yarn eject
or npm run eject
-- you can probably use some 3rd party packages to override without ejecting, but I'll leave that up to you to figure out.
Then, you'll need to go to the config/webpack.config.js
file and change the following:
isEnvProduction && shouldInlineRuntimeChunk && new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime-.+[.]js/])
as it creates a chunk file list inside the index.html
file when buildingfilename: "static/css/bundle.min.css"
and removing the chunkFileName option.filename: "static/js/bundle.min.js"
to output to a single filename for production.runtimeChunk: false
to avoid creating a runtime.chunk.js
filenew webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 })
to limit outputted chunks to 1CRA Version: v4.0.3 (demo updated as May 25th, 2021)
Working repo: https://github.com/mattcarlotta/cra-single-bundle
This configuration will inevitably become outdated as the dev world adopts Webpack 5
Matt's solution is the closest one I got by now. After a few hours of searching, I found a way to force Webpack to output one js file. Just put it here for future reference or anyone gets this problem.
plugins: [
isEnvProduction &&
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
}),
...
]
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