Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate single physical javascript file using create-react-app

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?

like image 583
Ajit Goel Avatar asked Mar 06 '18 03:03

Ajit Goel


People also ask

How do you generate js file with React?

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.

How do I bundle the React app in one 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.

Is create React app a single page app?

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.

What is create react app?

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.

How to combine JS and CSS files in a react app?

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.

How do I build a react app using NPM?

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!

What is the best way to build a website with react?

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.


1 Answers

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",     ...   } } 
like image 136
phdesign Avatar answered Sep 19 '22 22:09

phdesign