This is perhaps a newbie question. I have created a small reactjs app using create-react-app and I see that the bundle.js file is being served from http://localhost:3000/static/js/bundle.js. However, I do not see a physical "bundled" javascript file on my machine. How can I generate a physical bundled javascript file so I can "register" it in my wordpress php code? I'm building a small wordpress plugin that will use reactjs on the client side. Am I missing something obvious?
Run npm run build or npx react-scripts build to create an optimized production build for your React App and then run the command npx gulp to bundle all the JS and CSS files in the static build folder into the single main html 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. 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 learning React or creating a new single-page app, use Create React App. If you're building a server-rendered website with Node.js, try Next.js. If you're building a static content-oriented website, try Gatsby.
Whether you’re using React or another library, Create React App lets you focus on code, not build tools. Updating your build tooling is typically a daunting and time-consuming task.
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.
Run build From the terminal, run npm run build, which should produce the single JS bundle comprising React library code and your application code, ready to drop in as a single <script> reference as needed (as well as chunked build output from standard react-scripts build). Thanks for contributing an answer to Stack Overflow!
The React team primarily recommends these solutions: If you’re learning React or creating a new single-page app, use Create React App. If you’re building a server-rendered website with Node.js, try Next.js.
An alternative solution, as suggested here, is to use the rewire package to manipulate Create React App's webpack config, e.g.
Create a new file scripts/build.js
// npm install rewire const rewire = require('rewire'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const defaults = rewire('react-scripts/scripts/build.js'); const config = defaults.__get__('config'); // Consolidate chunk files instead config.optimization.splitChunks = { cacheGroups: { default: false, }, }; // Move runtime into bundle instead of separate file config.optimization.runtimeChunk = false; // JS config.output.filename = 'static/js/[name].js'; // CSS remove MiniCssPlugin config.plugins = config.plugins.filter(plugin => !(plugin instanceof MiniCssExtractPlugin)); // CSS replaces all MiniCssExtractPlugin.loader with style-loader config.module.rules[2].oneOf = config.module.rules[2].oneOf.map(rule => { if (!rule.hasOwnProperty('use')) return rule; return Object.assign({}, rule, { use: rule.use.map(options => /mini-css-extract-plugin/.test(options.loader) ? {loader: require.resolve('style-loader'), options: {}} : options) }); });
Edit package.json
{ "scripts": { ... "build": "npx ./scripts/build.js", ... } }
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