简体   繁体   English

create-react-app webpack 配置和文件在哪里?

[英]where is create-react-app webpack config and files?

I create a ReactJS project with the create-react-app package and that worked well, but I cannot find webpack files and configurations.我使用create-react-app包创建了一个 ReactJS 项目并且运行良好,但是我找不到 webpack 文件和配置。

How does react-create-app work with webpack? react-create-app 如何与 webpack 配合使用? Where are the webpack configuration files located in a default installation with create-react-app ?使用create-react-app的默认安装中的 webpack 配置文件在哪里? I'm unable to find configuration files in my project's folders.我无法在我的项目文件夹中找到配置文件。

I have not created an override config file.我还没有创建覆盖配置文件。 I can manage config settings with other articles but I want to find the conventional config file(s).我可以使用其他文章管理配置设置,但我想找到传统的配置文件。

If you want to find webpack files and configurations go to your package.json file and look for scripts如果您想查找 webpack 文件和配置,请转到您的 package.json 文件并查找脚本

图片

You will find that scripts object is using a library react-scripts你会发现脚本对象正在使用一个库react-scripts

Now go to node_modules and look for react-scripts folder react-script-in-node-modules现在转到 node_modules 并查找 react-scripts 文件夹react-script-in-node-modules

This react-scripts/scripts and react-scripts/config folder contains all the webpack configurations.这个react-scripts/scriptsreact-scripts/config文件夹包含所有的 webpack 配置。

The files are located in your node_modules/react-scripts folder:这些文件位于您的node_modules/react-scripts文件夹中:

Webpack config:网络包配置:

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/webpack.config.js https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/webpack.config.js

Start Script:启动脚本:

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/start.js https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/start.js

Build Script:构建脚本:

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/build.js https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/build.js

Test Script:测试脚本:

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/test.js https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/test.js

and so on ...等等...

Now, the purpose of CRA is not to worry about these.现在,CRA 的目的不是担心这些。

From the documentation :文档

You don't need to install or configure tools like Webpack or Babel.您不需要安装或配置 Webpack 或 Babel 之类的工具。 They are preconfigured and hidden so that you can focus on the code.它们是预先配置和隐藏的,以便您可以专注于代码。

If you want to have access to the config files, you need to eject by running:如果你想访问配置文件,你需要通过运行来弹出

npm run eject

Note: this is a one-way operation.注意:这是一种单向操作。 Once you eject, you can't go back!一旦弹出,就无法返回!

In most scenarios, it is best not to eject and try to find a way to make it work for you in another way.在大多数情况下,最好不要弹出并尝试以另一种方式找到适合您的方法。

If you need to override some of the config options, you can have a look at https://github.com/gsoft-inc/craco如果您需要覆盖某些配置选项,您可以查看https://github.com/gsoft-inc/craco

A lot of people come to this page with the goal of finding the webpack config and files in order to add their own configuration to them.很多人来到这个页面的目的是找到 webpack 配置和文件,以便将他们自己的配置添加到它们中。 Another way to achieve this without running npm run eject is to use react-app-rewired .另一种无需运行npm run eject即可实现此目的的方法是使用react-app-rewired This allows you to overwrite your webpack config file without ejecting.这允许您在不弹出的情况下覆盖您的 webpack 配置文件。

Assuming you don't want to eject and you just want to look at the config you will find them in /node_modules/react-scripts/config假设您不想弹出并且只想查看配置,您会在/node_modules/react-scripts/config找到它们

webpack.config.dev.js. //used by `npm start`
webpack.config.prod.js //used by `npm run build`

create-react-app使用的 Webpack 配置在这里: https : //github.com/facebook/create-react-app/tree/master/packages/react-scripts/config

You can find it inside the /config folder.您可以在/config文件夹中找到它。

When you eject you get a message like:当您弹出时,您会收到如下消息:

 Adding /config/webpack.config.dev.js to the project
 Adding /config/webpack.config.prod.js to the project

Webpack configuration is being handled by react-scripts . Webpack 配置由react-scripts处理。 You can find all webpack config inside node_modules react-scripts/config .您可以在 node_modules react-scripts/config 中找到所有 webpack配置

And If you want to customize webpack config, you can follow this customize-webpack-config如果你想自定义 webpack 配置,你可以按照这个custom-webpack-config

Try ejecting the config files, by running:尝试弹出配置文件,运行:

npm run eject

then you'll find a config folder created in your project.然后你会发现在你的项目中创建了一个 config 文件夹。 You will find your webpack config files init.你会发现你的 webpack 配置文件 init.d 。

Ejecting is not the best option as well as editing something under node_modules .弹出不是最好的选择,也不是在node_modules下编辑node_modules react-app-rewired is not maintained and has the warning: react-app-rewired未维护并有警告:

...you now "own" the configs. ...您现在“拥有”配置。 No support will be provided.将不提供任何支持。 Proceed with caution...谨慎行事...

Solution - use craco .解决方案 -使用 craco

I know it's pretty late, but for future people stumbling upon this issue, if you want to have access to the webpack config of CRA, there's no other way except you have to run:我知道现在已经很晚了,但是对于未来遇到这个问题的人来说,如果您想访问 CRA 的 webpack 配置,除了必须运行之外别无他法:

$ npm run eject

However, with ejection, you'll strip away yourself from CRA pipeline of updates, therefore from the point of ejection, you have to maintain it yourself.但是,通过弹出,您将脱离 CRA 更新管道,因此从弹出的角度来看,您必须自己维护它。

I have come across this issue many times, and therefore I've created a template for react apps which have most of the same config as CRA, but also additional perks (like styled-components, jest unit test, Travis ci for deployments, prettier, ESLint, etc...) to make the maintenance easier.我多次遇到过这个问题,因此我为 React 应用程序创建了一个模板,该模板与 CRA 的配置大部分相同,但还有额外的好处(如样式组件、jest 单元测试、用于部署的 Travis ci,更漂亮、ESLint 等...)使维护更容易。 Check out the repo .查看repo

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM