webpack 4 入门指南 系列二(入门篇)

2018-07-08  本文已影响0人  yfmei

基本设置

# win10 ps执行连续命令用 ; 替换 &&
mkdir webpack-demo && webpack-demo
npm init -y
npm install webpack webpack-cli --save-dev

project

   webpack-demo
  |- package.json
+ |- index.html
+ |- /src
+   |- index.js

src/index.js

function component() {
  var element = document.createElement('div');

  // 为了使 _ 生效,需要 Lodash 库, 现在是通过 script标签引用的
  element.innerHTML = _.join(['Hello', 'webpack'], ' ');

  return element;
}

document.body.appendChild(component());    

index.html

<!doctype html>
<html>
  <head>
    <title>Getting Started</title>
    <script src="https://unpkg.com/lodash@4.16.6"></script>
  </head>
  <body>
    <script src="./src/index.js"></script>
  </body>
</html>

package.json

  {
    "name": "webpack-demo",
    "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.0.1",
      "webpack-cli": "^2.0.9"
    },
    "dependencies": {}
  }

打包依赖(bundle)

project

  webpack-demo
  |- package.json
+ |- /dist
+   |- index.html
- |- index.html
  |- /src
    |- index.js
npm install --save lodash

安装软件包到生产环境时,应该使用npm install --save。如果安装软件包是为了开发环境(比如 linter,测试包等等),那你就需要使用npm install --save-dev

src/index.js

+ import _ from 'lodash';
+
  function component() {
    var element = document.createElement('div');

-   // Lodash, currently included via a script, is required for this line to work
+   // Lodash, now imported by this script
    element.innerHTML = _.join(['Hello', 'webpack'], ' ');

    return element;
  }

  document.body.appendChild(component());
  <!doctype html>
  <html>
   <head>
     <title>Getting Started</title>
-    <script src="https://unpkg.com/lodash@4.16.6"></script>
   </head>
   <body>
-    <script src="./src/index.js"></script>
+    <script src="main.js"></script>
   </body>
  </html>
npx webpack

Hash: f786199817e635507cff
Version: webpack 4.15.1
Time: 3785ms
Built at: 2018-07-08 13:07:30
  Asset      Size  Chunks             Chunk Names
main.js  70.5 KiB       0  [emitted]  main
[1] (webpack)/buildin/module.js 497 bytes {0} [built]
[2] (webpack)/buildin/global.js 489 bytes {0} [built]
[3] ./src/index.js 222 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/

你的输出可能会有所不同,但如果构建成功,那么你可以继续往下走了。另外,不要担心警告,我们稍后会解决。

模块

importexport语句已经在ES2J015中标准化,并且在大多数浏览器中都收支持。一些旧的浏览器仍然很落后不支持,但webpack支持开箱即用的模块。

使用配置文件

projcet

  webpack-demo
  |- package.json
+ |- webpack.config.js
  |- /dist
    |- index.html
  |- /src
    |- index.js

webpack.config.js

const path = require("path")

module.exports = {
  entry: "./src/index.js",
  output:{
    filename: "main.js",
    path: path.resolve(__dirname, "dist")
  }
}
npx webpack --config webpack.config.js
Hash: 17e2e6a019ab95135618
Version: webpack 4.15.1
Time: 3792ms
Built at: 2018-07-08 13:34:51
  Asset      Size  Chunks             Chunk Names
main.js  70.5 KiB       0  [emitted]  app
[1] (webpack)/buildin/module.js 497 bytes {0} [built]
[2] (webpack)/buildin/global.js 489 bytes {0} [built]
[3] ./src/index.js 224 bytes {0} [built]
    + 1 hidden module

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this v
alue. 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://w
ebpack.js.org/concepts/mode/

注意,windows上通过其路径调用webpack时必须使用反斜杠,例如node_modules.bin\webpack --config webpack.config.js

只要存在webpack.config.js,webpack命令就会默认使用它。我们这里使用config选项只是为了告诉你,你可以传递任意名字的配置文件。在配置项很复杂需要拆分到多个文件的时候,这是很有用的。

npm 脚本

  {
    "name": "webpack-demo",
    "version": "1.0.0",
    "description": "",
    "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

> webpack-demo@1.0.0 build C:\Users\PC\workspace\IntellijProjects\branches\bleview\c
li\webpack-demo
> webpack

Hash: 17e2e6a019ab95135618
Version: webpack 4.15.1
Time: 441ms
Built at: 2018-07-08 13:50:09
  Asset      Size  Chunks             Chunk Names
main.js  70.5 KiB       0  [emitted]  app
[1] (webpack)/buildin/module.js 497 bytes {0} [built]
[2] (webpack)/buildin/global.js 489 bytes {0} [built]
[3] ./src/index.js 224 bytes {0} [built]
    + 1 hidden module

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this v
alue. 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://w
ebpack.js.org/concepts/mode/

通过在npm run build和你的参数之间使用两个破折号可以传递自定义参数。例如 npm run build -- --colors

总结

webpack-demo
|- package.json
|- webpack.config.js
|- /dist
  |- main.js
  |- index.html
|- /src
  |- index.js
|- /node_modules

如果使用npm5, 你还可能看到package-lock.json文件。

上一篇下一篇

猜你喜欢

热点阅读