webpack 初步:安裝

?Webpack is used to compile JavaScript modules. Once installed, you can interface with webpack either from its CLI or API.

1 基本設(shè)置

  • npm初始化
    npm init -y
  • webpack安裝
    npm install --save-dev webpack
  • webpack-cli安裝
    ?webpack CLI provides a flexible set of commands for developers to increase speed when setting up a custom webpack project, providing a set of tools to improve the setup of custom webpack configuration.
    npm install --save-dev webpack-cli
  • webpack運行
"scripts": {
    "build": "webpack --config webpack.config.js"
}

1.1 Project

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

src/index.js

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

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

  return element;
}

document.body.appendChild(component());

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.20.2",
    "webpack-cli": "^3.1.2"
    },
    "dependencies": {}
  }

存在的問題

  • It is not immediately apparent that the script depends on an external library.
  • If a dependency is missing, or included in the wrong order, the application will not function properly.
  • If a dependency is included but not used, the browser will be forced to download unnecessary code.

2 Creating a Bundle

??分離source與dist.
??The "source" code is the code that we'll write and edit.
??The "distribution" code is the minimized and optimized output of our build process that will eventually be loaded in the browser:

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

??To bundle the lodash dependency with index.js, we'll need to install the library locally:
npm install --save lodash

src/index.js

??index.js explicitly requires lodash to be present, and binds it as "_"(no global scope pollution).
??By stating what dependencies a module needs, webpack can use this information to build adependency graph. It then uses the graph to generate an optimized bundle where scripts will be executed in the correct order.

import _ from "lodash"; // 在npm 安裝了loadsh
function component() {
  let element = document.createElement("div");
  // 這里直接使用
  element.innerHTML = _.join(["Hello", "webpack"], " ");
  return element;
}
document.body.appendChild(component());

dist/index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- <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>

運行

??run npx webpack, which will take our script at src/index.js as the entry point, and will generate dist/main.js as the output.
??The npx command, which ships with Node 8.2/npm 5.2.0 or higher, runs the webpack binary (./node_modules/.bin/webpack) of the webpack package we installed in the beginning:

> npx webpack
Hash: da7648cab100b6222869
Version: webpack 4.29.0
Time: 857ms
Built at: 2019-01-27 10:36:40
  Asset    Size  Chunks             Chunk Names
main.js  19 KiB       0  [emitted]  main
Entrypoint main = main.js
[18] ./src/index.js 249 bytes {0} [built]
    + 51 hidden modules

3 Modules

??The import and export statements have been standardized in ES2015 and are supported in most browsers. Some older browsers still lag behind but webpack supports modules out of the box.Behind the scenes, webpack actually "transpiles" the code so that older browsers can also run it.
??Note that webpack will not alter any code other than import and export statements. If you are using other ES2015 features, make sure to use a transpiler such as Babel or Bublé via webpack's loader system.

4 Using a Configuration

??See the configuration documentation to learn more.

webpack.config.js

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

package.json

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "main": "index.js",
  "scripts": {
    "dev": "webpack --config webpack.config.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^4.29.0",
    "webpack-cli": "^3.2.1"
  },
  "dependencies": {
    "lodash": "^4.17.11"
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • This project was bootstrapped with Create React App. Belo...
    unspecx閱讀 5,311評論 0 2
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • 如果說給高中教育松綁,大家也許會異口同聲地說,不行,有高考的壓力。 如果說給初中教育減壓,大家或許會不約而同地講,...
    湯勇曉語閱讀 1,948評論 0 11
  • 2018年12月19日 農(nóng)歷十一月十三 星期三 晴 學(xué)經(jīng)人員:琪佳媽、琪琪、佳佳。 寶貝年齡:琪琪10歲,佳佳9歲...
    順德琪佳媽閱讀 334評論 0 0

友情鏈接更多精彩內(nèi)容