简体   繁体   English

如何在不缩小的情况下构建 React 的生产版本?

[英]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.我一直在或多或少地遵循官方指南来使用 React 设置本地开发环境,它似乎使用了create-react-app ,它确实设置了很多。

Now, if I run npm run build I get a minified version of everything in the build folder.现在,如果我运行npm run build我会得到build文件夹中所有内容的缩小版本。 If I, however, run npm start the version NodeJS serves does not seem to have any modifications.但是,如果我运行npm start服务的版本似乎没有任何修改。 But I cannot see these files.但是我看不到这些文件。

Question问题

So either:所以要么:

  • Can I access the files generated by npm start somewhere?我可以从某处npm start生成的文件吗? As these seem to be unmodified.因为这些似乎是未修改的。 ( build is never modified there) build永远不会在那里修改)
  • Or can I somehow run npm run build , so it does a "development" build with unminimized files?或者我可以以某种方式运行npm run build ,以便它使用未最小化的文件进行“开发”构建吗?

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

> example-project@0.1.0 build [...]
> react-scripts build

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

System系统

My package.json has the default scripts:我的package.json有默认脚本:

  "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, eg debugging or this specific use case .我可能想要这个的原因有很多,例如调试或这个特定的用例

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要更改 webpack 配置和构建脚本,您必须从 create-react-app 中弹出(我不建议执行此步骤,因为它会破坏未来的兼容性),或者您可以使用 rewire 之类的工具来覆盖某些设置

Take a look at this https://github.com/timarney/react-app-rewired看看这个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创建一个 build.js
  2. Change your package.json so that it runs build.js更改您的 package.json 以便它运行 build.js

build.js构建.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 中,我更改了这样的 npm 脚本链接(将运行 build.js 脚本的节点构建)

package.json包.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所以如果你真的想从 create-react-app 中弹出,你所要做的就是运行

npm run-script eject

Then you will get a new folder with all configs used by create-react-app然后您将获得一个包含 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如果有帮助,请不要忘记标记为答案干杯和成功

Why can't you see the source files?为什么看不到源文件? Here is what I would try:这是我会尝试的:

  1. Start your react app with npm run start使用npm run start启动你的npm run start应用npm run start
  2. Open your browser to http://localhost:3000打开浏览器访问http://localhost:3000
  3. Open Developer tools and inspect the created chunked bundles by the webpack-dev server .打开开发人员工具并检查 webpack-dev 服务器创建的分块包 In Chrome on a mac, you can do the following: cmd+option+j will open developer tools.在 Mac 上的 Chrome 中,您可以执行以下操作: cmd+option+j将打开开发人员工具。 Then click the sources tab: within this tab you will s ee the bundles created by react's build configuration .然后单击选项卡:在此选项卡中,您将看到由 react 的构建配置创建的包 Now the output of these bundles might not be pretty but it's all there.现在这些包的输出可能不漂亮,但它就在那里。

Alternatively, all your application's build configuration settings are contained within your webpack.config.js file even when you use create-react-app .或者,即使您使用create-react-app ,您的应用程序的所有构建配置设置都包含在您的webpack.config.js文件中。 As this configuration is just encapsulated within the react-scripts node module .由于此配置仅封装在react-scripts节点模块中 So maybe you could try editing this file directly, without ejecting: <base_path>/node_modules/react-scripts/config/webpack.config.js .因此,也许您可​​以尝试直接编辑此文件,而无需弹出: <base_path>/node_modules/react-scripts/config/webpack.config.js Although you need to be careful as to not break an existing configuration setting.尽管您需要小心不要破坏现有的配置设置。 You probably want to mess with the source-map settings for production builds.您可能想弄乱生产版本的source-map设置。 At least this way if you ruin this file you can always just remove and reinstall react-scripts and be back to your initial configuration.至少这样,如果你破坏了这个文件,你总是可以删除并重新安装react-scripts并返回到你的初始配置。 This will also allow you to play around with your customizations in 'semi-safe' sandboxed environment.这也将允许您在“半安全”沙盒环境中进行自定义。 Remember, there is no magic that create-react-app is providing rather it's just making useful defaults for your build configuration.请记住, create-react-app没有提供任何魔法,而只是为您的构建配置设置有用的默认值。

Lastly, as @xzesstence pointed out you can try out the react-app-rewired module.最后,正如@xzesstence 指出的,您可以尝试react-app-rewired模块。

Hopefully that helps!希望这有帮助!

I wanted the unobfuscated code of a React app - mostly of curiosity, I had the source - while having the job of rewriting it in Angular (producing a far more maintainable app 5% of the size and 1% dependencies).我想要一个 React 应用程序的未混淆代码——主要是出于好奇,我有源代码——同时有在 Angular 中重写它的工作(生成一个更易于维护的应用程序,其大小为 5%,依赖项为 1%)。

I've never used React but discovered by modifying the file我从未使用过 React 但通过修改文件发现

<base_path>/node_modules/react-scripts/config/webpack.config.prod.js <base_path>/node_modules/react-scripts/config/webpack.config.prod.js

and replacing the large optimization config item, under module.exports, with the following...并用以下内容替换 module.exports 下的大型优化配置项...

module.exports = {...

    optimization: {
            minimize: false,
            splitChunks: {
                chunks: 'all',
                name: true
            },
            runtimeChunk: true
        },

npm run build built unobfuscated, readable code that ran as expected, using no other modifications. npm run build构建了未混淆的、可读的代码,按预期运行,没有使用其他修改。 Used Gitbash only with the commands npm install , npm run build and npm start - Just thought someone may find that useful.仅将 Gitbash 与命令npm installnpm run buildnpm start 一起使用- 只是认为有人可能会觉得这很有用。

I don't recommend this because the code you want is still wrapped in a webpack eval mess.我不推荐这样做,因为你想要的代码仍然包裹在 webpack eval 中。 It's easier to pick the useful bits from the source or just rebuild the app.从源代码中选择有用的部分或重新构建应用程序会更容易。 At best, I got to see what a cluster react and webpack is.充其量,我必须了解集群 react 和 webpack 是什么。

The files are kept in the server process memory and not written to disk, unless you eject the scripts (or if it is possible to use a tool like 'rewire') and modify them to write it to disk using the writeToDisk option as described in the webpack DevServer docs .文件保存在服务器进程内存中并且不会写入磁盘,除非您弹出脚本(或者如果可以使用像“rewire”这样的工具)并修改它们以使用writeToDisk选项将其写入磁盘,如中所述webpack DevServer 文档

You can however get the actual file list/links by navigating to the webpack-dev-server endpoint under the server.但是,您可以通过导航到webpack-dev-server下的webpack-dev-server端点来获取实际的文件列表/链接。

For instance if using the default url at localhost:3000 then use the following url to see all files on the server:例如,如果在 localhost:3000 使用默认 url,则使用以下 url 查看服务器上的所有文件:

http://localhost:3000/webpack-dev-server

But what you really need is only the index.html (which is in general just a stub that loads the JS files) and the 3 following JS files which appear to be consistent on all create-react-app installments, along with their respective source map files.但您真正需要的只是 index.html(通常只是加载 JS 文件的存根)和以下 3 个 JS 文件,它们在所有 create-react-app 分期付款中似乎都一致,以及它们各自的源代码地图文件。

  1. main.chunk.js main.chunk.js
  2. bundle.js包.js
  3. vendors~main.chunk.js供应商~main.chunk.js

You can just right click on the links on the page and save them, or you can navigate direct the link or get them from the Chrome Dev Tools "sources" tab.您可以右键单击页面上的链接并保存它们,或者您可以直接导航链接或从 Chrome 开发工具“源”选项卡中获取它们。

Note that in general for code changes only the main.chunk.js file is updated, so for the average code change you might just fetch the updated main.chunk.js and main.chunk.js.map files.请注意,通常对于代码更改仅更新 main.chunk.js 文件,因此对于平均代码更改,您可能只获取更新的 main.chunk.js 和 main.chunk.js.map 文件。

I know it's way too late to answer this, but try this npm i -D cra-build-watch .我知道现在回答这个为时已晚,但试试这个npm i -D cra-build-watch

I feel this library is underrated but it just watch the changes in react app and does not re-build the whole package again and again.我觉得这个库被低估了,但它只是观察 react 应用程序中的变化,而不是一次又一次地重新构建整个包。

Although rewiring helps in making the build by not minifying it, however, still it goes through the whole process of building again and again.尽管重新布线有助于通过不缩小构建来进行构建,但是,它仍然会一次又一次地经历整个构建过程。

After spending a whole day on this problem, I could not find a way to get the none-minified version of the production code,在这个问题上花了一整天,我找不到办法得到生产代码的非缩小版本,

what I did was: open the dev tools on chrome, navigate to the sources tab;我所做的是:打开 chrome 上的开发工具,导航到源选项卡; now find the javascript file (in create-react-app it is usually in static > js > main.js)现在找到 javascript 文件(在 create-react-app 中它通常在 static > js > main.js 中)

when the javascript file is visible, at the bottom left of the screen a pair of curly braces appear (look at the image):当 javascript 文件可见时,在屏幕的左下角会出现一对大括号(看图片):

screenshot of the dev tools开发工具的屏幕截图

when you click on the curly braces, it beautifies the code, and then you can add breakpoints to the code, it is not convenient to deal with that code, but for now that is what worked for me.当你点击花括号时,它美化了代码,然后你可以在代码中添加断点,处理那段代码并不方便,但现在这对我有用。

hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应:无需构建即可运行生产 - React: Run on production without build 如何在反应中创建生产构建文件夹? - How to create production build folder in react? 有没有办法让我在不使用 Express 的情况下包含 React 生产构建文件? - Is there a way for me to include the React production build files without using Express? 如何使用Webpack 1.x在React应用程序中创建生产版本 - How to create production build in react application with Webpack 1.x 我如何更改 React 查找生产构建的目录? - How can I change the directory React looks for the production build? 对生产进行反应,资产未加载 - react production build, assets not loading 当我尝试使用 Parcel 构建在开发模式下运行良好的 React 应用程序的生产版本时,我得到“找不到模块 'sass'” - When I try to use Parcel to build a production version of a React app that works fine in development mode, I get “Cannot find module 'sass'” npm生产版本问题(npm版本内部版本-Pprod) - npm production build issue (npm version build -Pprod) 生产构建 React 上未加载 Multer 图像? - Multer Image Not loaded on Production Build React? 生产构建中未定义require(react.production.min.js) - require is not defined (react.production.min.js) on production build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM