Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

node.js - How to build a production version of React without minification?

Background

I've been following more or less the official guide to setup a local dev environment with react and it seems to use create-react-app, which sets up really a lot.

Now, if I run npm run build I get a minified version of everything in the build folder. If I, however, run npm start the version NodeJS serves does not seem to have any modifications. But I cannot see these files.

Question

So either:

  • Can I access the files generated by npm start somewhere? As these seem to be unmodified. (build is never modified there)
  • Or can I somehow run npm run build, so it does a "development" build with unminimized files?

Tries

My aim is just to get access to an unminimized version of react scripts.

As for the last question I've tried some parameters and enironmental variables as suggested in this question, but as you can see, it failed:

$ NODE_ENV=dev npm run build --dev --configuration=dev

> [email protected] build [...]
> react-scripts build

Creating an optimized production build...
[...]

System

My package.json has the default scripts:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

Note: Please do not ask why I am doing it or try to convince me that it is bad. There are many reasons why I'd maybe want this, e.g. debugging or this specific use case.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To change the webpack config and build scripts you have either to eject from create-react-app (i would not recommend this step, as it breaks future compatibility) or you can use tools like rewire to override some settings

Take a look at this https://github.com/timarney/react-app-rewired

I personally used just rewire

npm i rewire --save-dev

Here is a sample config i created for one of my projects in the past and it worked pretty good!

  1. Create a build.js
  2. Change your package.json so that it runs build.js

build.js

const rewire = require('rewire');
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 = '[name].js';
// CSS. "5" is MiniCssPlugin
config.plugins[5].options.filename = '[name].css';
config.plugins[5].options.publicPath = '../';

Then in my package.json i changed the npm script links like this (node build which will run the build.js script)

package.json

"scripts": {
    "start": "react-scripts start",
    "build": "node build && gulp",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

So if you really want to eject from create-react-app, all you have to do is to run

npm run-script eject

Then you will get a new folder with all configs used by create-react-app

But as i said before, there is no reason why not to use rewire and just override the config instead of ejecting

if it helps, please don't forget to mark as answer Cheers and good success


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...