用npm安装和设置webpack

安装Webpack

npm i webpack webpack-cli -D

 

设置Webpack

在该文件夹创建文件package.json, 粘贴以下的内容:

{
  "name": "xxxxxx",
  "version": "1.0.0",
  "description": "blah blah blah",
  "main": "index.js",
  "scripts": {
    "start": "webpack --mode development",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^4.0.1",
    "webpack-cli": "^2.0.10"
  }
}

然后建立一个文件夹叫 “src”, 在里面创建一个文件叫index.js. (当然名字可以不一定是index.js, 是由”main”的值而定.

 

使用Webpack

npm run start

这指令会生成代码并保存在”dist”的文件夹里面.

 

参考链接:

https://medium.freecodecamp.org/part-1-react-app-from-scratch-using-webpack-4-562b1d231e75