如何将 webpack 与 create-react-app 一起使用?

Posted

技术标签:

【中文标题】如何将 webpack 与 create-react-app 一起使用?【英文标题】:How to use webpack with create-react-app? 【发布时间】:2019-05-31 23:40:04 【问题描述】:

我在documentation provided by create-react-app 之后创建了一个create-react-app 项目

1) 我创建了我的项目并 cd 进入它

npx create-react-app my-app
cd my-app

2) 我已经按照getting started documentation 安装了webpack、webpack-cli 和 webpack-dev-server。

npm install --save-dev webpack webpack-dev-server webpack-cli

3) 考虑到需要 babel,我也按照this instruction 的这些步骤进行操作

npm install --save-dev @babel/core @babel/preset-env
npm install --save-dev babel-loader
npm install --save-dev @babel/preset-react

4) 我还在this instruction

之后创建了以下 webpack.config.js 文件
const path = require('path');

module.exports = 
  entry: './src/index.js',
  mode: 'production',
  module: 
    rules: [
        
          test: /\.js$/,
          exclude: /node_modules/,
          use: 
            loader: 'babel-loader',
            options: 
            "presets": ["@babel/preset-env","@babel/preset-react"]        
          
        
    ]
  ,
  output: 
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist')
  
;

下面是我的 package.json 文件


  "name": "my-app",
  "version": "0.1.0",
  "dependencies": 
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-scripts": "2.1.3"
  ,
  "scripts": 
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "start:dev": "webpack-dev-server",
    "build:dev": "webpack"
  ,
  "eslintConfig": 
    "extends": "react-app"
  ,
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": 
    "@babel/core": "^7.2.2",
    "@babel/preset-env": "^7.2.3",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.5",
    "webpack": "^4.28.3",
    "webpack-cli": "^3.2.0",
    "webpack-dev-server": "^3.1.14"
  

当我运行以下命令时

npm run start:dev

我收到以下错误

 webpack-dev-server

ℹ 「wds」: Project is running at http://localhost:8080/
ℹ 「wds」: webpack output is served from /
✖ 「wdm」: Hash: ebe0431a84f29f0e98e9
Version: webpack 4.28.3
Time: 5058ms
Built at: 01/05/2019 1:49:37 PM
 1 asset
Entrypoint main = main.js
 [0] ./node_modules/react/index.js 190 bytes 0 [built]
 [4] ./node_modules/react-dom/index.js 1.33 KiB 0 [built]
 [5] ./src/logo.svg 2.65 KiB 0 [built] [failed] [1 error]
 [6] ./src/serviceWorker.js 4.6 KiB 0 [built]
 [7] multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js 40 bytes 0 [built]
 [8] (webpack)-dev-server/client?http://localhost:8080 7.78 KiB 0 [built]
 [9] ./node_modules/url/url.js 22.8 KiB 0 [built]
[16] ./node_modules/strip-ansi/index.js 161 bytes 0 [built]
[18] ./node_modules/loglevel/lib/loglevel.js 7.68 KiB 0 [built]
[19] (webpack)-dev-server/client/socket.js 1.05 KiB 0 [built]
[21] (webpack)-dev-server/client/overlay.js 3.58 KiB 0 [built]
[26] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes 0 [built]
[28] (webpack)/hot/emitter.js 75 bytes 0 [built]
[34] ./src/index.css 168 bytes 0 [built] [failed] [1 error]
[37] ./src/index.js + 1 modules 3.58 KiB 0 [built]
     | ./src/index.js 472 bytes [built]
     | ./src/App.js 3.09 KiB [built]
    + 23 hidden modules

ERROR in ./src/App.css 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
> .App 
|   text-align: center;
| 
 @ ./src/App.js 21:0-19
 @ ./src/index.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js

ERROR in ./src/logo.svg 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3">
|     <g fill="#61DAFB">
|         <path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/>
 @ ./src/App.js 20:0-30 42:13-17
 @ ./src/index.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js

ERROR in ./src/index.css 1:5
Module parse failed: Unexpected token (1:5)
You may need an appropriate loader to handle this file type.
> body 
|   margin: 0;
|   padding: 0;
 @ ./src/index.js 3:0-21
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js
ℹ 「wdm」: Failed to compile.

我还运行了以下命令

npm run build:dev

这产生了这个错误:

> my-app@0.1.0 build:dev /Users/chriscruz/Desktop/my-app
> webpack

Hash: eff8d3ad52e3c88d156c
Version: webpack 4.28.3
Time: 2647ms
Built at: 01/05/2019 1:54:01 PM
 1 asset
Entrypoint main = main.js
 [3] ./src/logo.svg 2.65 KiB 0 [built] [failed] [1 error]
 [4] ./src/serviceWorker.js 4.6 KiB 0 [built]
 [9] (webpack)/buildin/global.js 472 bytes 0 [built]
[10] ./src/index.css 168 bytes 0 [built] [failed] [1 error]
[11] ./src/App.css 165 bytes 0 [built] [failed] [1 error]
[13] ./src/index.js + 1 modules 3.58 KiB 0 [built]
     | ./src/index.js 472 bytes [built]
     | ./src/App.js 3.09 KiB [built]
    + 8 hidden modules

ERROR in ./src/App.css 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
> .App 
|   text-align: center;
| 
 @ ./src/App.js 21:0-19
 @ ./src/index.js

ERROR in ./src/logo.svg 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3">
|     <g fill="#61DAFB">
|         <path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/>
 @ ./src/App.js 20:0-30 42:13-17
 @ ./src/index.js

ERROR in ./src/index.css 1:5
Module parse failed: Unexpected token (1:5)
You may need an appropriate loader to handle this file type.
> body 
|   margin: 0;
|   padding: 0;
 @ ./src/index.js 3:0-21
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! my-app@0.1.0 build:dev: `webpack`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the my-app@0.1.0 build:dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/chriscruz/.npm/_logs/2019-01-05T18_54_01_643Z-debug.log

【问题讨论】:

如果你使用 create-react-app 你可以简单地使用弹出...github.com/facebook/create-react-app/blob/master/packages/… 你的 create-react-app 版本是多少?您可以通过npx create-react-app -V了解 【参考方案1】:

当您使用create-react-app 时,您已经安装并预配置了 webpack,您可以使用npm run build 命令构建您的项目。如果你想使用非标准的 webpack 配置,可以运行npm run eject 命令,之后会在你的项目中创建配置文件,你可以更改它们。

【讨论】:

以上是关于如何将 webpack 与 create-react-app 一起使用?的主要内容,如果未能解决你的问题,请参考以下文章

如何将 webpack 与 express 一起使用?

如何将 Webpack 与 Angular + templateCache 一起使用?

如何将 bootstrap 5 与 webpack 加载器一起使用?

如何将 Rails 6 中的 JqTree 与 webpacker 集成,树不是函数

Javascript / webpack:如何将目录中的所有json文件与文件的自定义循环连接起来

如何将 VS Code 调试器与 webpack-dev-server 一起使用(忽略断点)