欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【webpack】webpack4基本的构建过程

程序员文章站 2022-05-30 21:18:24
...

前提

安装node,npm(新版本的node安装时会同时安装npm)。如果想提高下载安装包时的速度,可以安装淘宝镜像cnpm。接下来,开始讲解webpack的基本构建过程。

过程

1.在项目目录下,执行npm init --yes,执行完生成一个package.json的文件。
2.安装webpack(以下两种方式自选):

npm install --save-dev webpack【本地安装,默认安装最新版本】
npm install --save-dev [email protected]<version> 【本地安装,安装特定版本,将version改为你需要的版本】

3.安装npm install --save-dev webpack-cli【由于我安装的是webpack^4.29.6版本,故需要安装webpack-cli。若是4.0以下版本的webpack,则不需要安装webpack-cli】

4.项目根目录下新建以下文件(带“+”号的):

 |-node_modules
 |-package.json
+|-index.html 
+|-src
+  |-index.js

index.html写入内容:

<!doctype html>
<html>
  <head>
    <title>起步</title>
    <script src="https://unpkg.com/[email protected]"></script>
  </head>
  <body>
    <script src="./src/index.js"></script>
  </body>
</html>

index.js中写入内容:

function component() {
    var element = document.createElement('div');
  
    // Lodash(目前通过一个 script 脚本引入)对于执行这一行是必需的
    element.innerHTML = _.join(['Hello', 'webpack'], ' ');
  
    return element;
  }
  
  document.body.appendChild(component());

5.我们还需要调整 package.json 文件,以便确保我们安装包是私有的(private),并且移除 main 入口。这可以防止意外发布你的代码。

{
  "name": "HelpBM",
  "version": "1.0.0",
  "description": "",
+  "private": true,
-  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0"
  }
}

调整目录结构:

  |-node_modules
  |-package.json
+ |-dist
    |-index.html 
  |-src
    |-index.js

6.要在 index.js 中打包 lodash 依赖,我们需要在本地安装 library:

npm install --save lodash

在安装一个要打包到生产环境的安装包时,你应该使用 npm install --save,如果你在安装一个用于开发环境的安装包(例如,linter, 测试库等),你应该使用 npm install --save-dev。请在 npm 文档 中查找更多信息。

也就是说,如果是项目上线时仍需要用到的安装包,我们用npm install --save安装,我们可以在package.jsondependencies中看到此项目依赖的安装包。如果只是开发阶段需要用到的安装包,我们用npm install --save-dev安装,可以在package.jsondevDependencies看到此项目以来的本地安装包。

7.由于之前未安装lodash,故只能通过script脚本引入;然后修改另一个 <script> 标签来加载 bundle,而不是原始的 /src 文件。修改index.html文件:

  <!doctype html>
  <html>
   <head>
     <title>起步</title>
-    <script src="https://unpkg.com/[email protected]"></script>
   </head>
   <body>
-    <script src="./src/index.js"></script>
+    <script src="main.js"></script>
   </body>
  </html>

8.执行 npx webpack(执行构建),会将我们的脚本作为入口起点,然后输出 为 main.js。得到如下结果:

Hash: 56e7f5843b82c7e95b31 Version: webpack 4.29.6 Time: 908ms Built
at: 2019-03-20 16:52:07 Asset Size Chunks Chunk
Names main.js 70.3 KiB 0 [emitted] main Entrypoint main =
main.js [1] ./src/index.js 286 bytes {0} [built] [2]
(webpack)/buildin/global.js 472 bytes {0} [built] [3]
(webpack)/buildin/module.js 497 bytes {0} [built]
+ 1 hidden module

WARNING in configuration The ‘mode’ option has not been set, webpack
will fallback to ‘production’ for this value. Set ‘mode’ option to
‘development’ or ‘production’ to enable defaults for each environment.
You can also set it to ‘none’ to disable any default behavior. Learn
more: https://webpack.js.org/concepts/mode/

在浏览器打开index.html,能看到页面上输出:Hello webpack

9.我们知道,在webpack4中,我们可以无须任何配置。但有时候又需要根据项目来作出一些其他配置,所以我们在项目根目录下新建webpack.config.js文件来替代一些默认配置。

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};

由于我们设置输出为bundle.js,故修改index.html文件为:

<!doctype html>
<html>
  <head>
    <title>起步</title>
  </head>
  <body>
    <script src="./bundle.js"></script>
  </body>
</html>

10.再次执行构建npx wepback,得到如下结果:

Hash: 56e7f5843b82c7e95b31
Version: webpack 4.29.6
Time: 937ms
Built at: 2019-03-20 17:12:52
Asset Size Chunks Chunk Names
main.js 70.3 KiB 0 [emitted] main
Entrypoint main = main.js
[1] ./src/index.js 286 bytes {0} [built]
[2] (webpack)/buildin/global.js 472 bytes {0} [built]
[3] (webpack)/buildin/module.js 497 bytes {0} [built]
+ 1 hidden module

WARNING in configuration The ‘mode’ option has not been set, webpack
will fallback to ‘production’ for this value. Set ‘mode’ option to
‘development’ or ‘production’ to enable defaults for each environment.
You can also set it to ‘none’ to disable any default behavior. Learn
more: https://webpack.js.org/concepts/mode/

11.使用npm run build来代替npx webpack,在package.json中作出修改:

 {
    "name": "webpack-demo",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
+     "build": "webpack"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {
      "webpack": "^4.0.1",
      "webpack-cli": "^2.0.9",
      "lodash": "^4.17.5"
    }
  }

修改完之后,执行npm run build会得到与npx webpack一样的结果。

结论

webpack的基本构建到此结束。最终项目目录结构应如下所示:

 |-node_modules
 |-package.json
 |-webpack.config.js
 |-dist
      |-index.html 
      |-bundle.js
 |-src
      |-index.js

参考文档:《webpack中文文档》

相关标签: webpack4 构建