Webpack 入门指南

本文发布时间: 2019-Mar-22
Webpack 是目前流行的打包工具,如何安装它呢?1. 安装 Node Js首先,Webpack 是基于 NodeJs 的工具,你必须首先安装 NodeJs. NodeJs 仅仅只需要在你的系统中安装一次就可以了。2. 全局安装 Webpack我们希望能够在系统的任何文件夹中使用 Webpack,使用的方式是通过 Webpack 命令来完成的,这需要我们全局安装 Webpack。这也只需要安装一次,以后每个项目就不需要重新全局安装了。$ npm install webpack -g成功安装之后,你应该能够在任何目录中执行 webpack 命令,如果你还没有项目的配置文件的话,应该会看到当前的 Webpack 版本和一个命令的帮助列表。> webpackwebpack 1.12.12Usage: https://webpack.github.io/docs/cli.htmlOptions: --help, -h, -? --config --context --entry --module-bind --module-bind-post --module-bind-pre --output-path --output-file --output-chunk-file --output-named-chunk-file --output-source-map-file --output-public-path --output-jsonp-function --output-pathinfo --output-library --output-library-target --records-input-path --records-output-path --records-path --define --target --cache [default: true] --watch, -w --watch which closes when stdin ends --watch-aggregate-timeout --watch-poll --hot --debug --devtool --progress --resolve-alias --resolve-loader-alias --optimize-max-chunks --optimize-min-chunk-size --optimize-minimize --optimize-occurence-order --optimize-dedupe --prefetch --provide --labeled-modules --plugin --bail --profile -d shortcut for --debug --devtool sourcemap --output-pathinfo -p shortcut for --optimize-minimize --json, -j --colors, -c --sort-modules-by --sort-chunks-by --sort-assets-by --hide-modules --display-exclude --display-modules --display-chunks --display-error-details --display-origins --display-cached --display-cached-assets --display-reasons, --verbose, -vOutput filename not configured.PS C:studywebpackw1>注意,最后还提示你,当前没有找到 webpack 配置文件。3. 在项目中安装 Webpack最好在你的项目中也包含一份独立的 Webpack,这样你更方便管理你的项目。为什么又是全局安装,又是局部安装呢?可以参考这里的说明。3.1 确认创建 NPM 项目文件首先,你需要已经创建了 NPM 的项目文件,如果没有的话,使用 init 命令创建它。 npm init你需要回答一系列问题。最终你会得到一个名为 package.json 的 NPM 项目文件,如果你愿意的话,手工创建它也不错。甚至你可以直接复制一个过来。一个新创建的 package.json 内容应该如下所示。{ 'name': 'w1', 'version': '1.0.0', 'description': '', 'main': 'index.js', 'dependencies': {}, 'devDependencies': {}, 'scripts': { 'test': 'echo 'Error: no test specified' && exit 1' }, 'keywords': [], 'author': '', 'license': 'ISC'}3.2 在项目中安装 Webpack现在,可以在项目中安装 Webpack 了,直接使用 NPM 的 install 命令。npm install webpack --save-dev--save-dev 的意思是说 webpack 是一个开发工具,我们需要将这一点保存到 package.Json 文件中。install 命令可以简化为单个字符 i,注意是小写的 i。--save-dev 还可以简化为大写的 S,写成 -S,你可以在这里查看 instal 的更详细使用说明。npm i webpack --S你应该看到一个长长的输出,这是由于 Webpack 是一个很复杂的工具。它依赖很多的其它工具。> npm install webpack [email protected] w1`-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] | +-- [email protected] | `-- [email protected] +-- [email protected] +-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | `-- [email protected] +-- [email protected] | +-- [email protected] | | `-- [email protected] | `-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | `-- [email protected] +-- [email protected] | `-- [email protected] +-- [email protected] | +-- [email protected] | +-- [email protected] | | `-- [email protected] | +-- [email protected] | | +-- [email protected] | | `-- [email protected] | +-- [email protected] | | `-- [email protected] | +-- [email protected] | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | `-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | | `-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | | `-- [email protected] | +-- [email protected] | | `-- [email protected] | | `-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | | +-- [email protected] | | `-- [email protected] | +-- [email protected] | | `-- [email protected] | `-- [email protected] | `-- [email protected] +-- [email protected] | `-- [email protected] +-- [email protected] | `-- [email protected] +-- [email protected] +-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | `-- [email protected] | +-- [email protected] | +-- [email protected] | | +-- [email protected] | | | +-- [email protected] | | | | +-- [email protected] | | | | `-- [email protected] | | | `-- [email protected] | | +-- [email protected] | | `-- [email protected] | +-- [email protected] | `-- [email protected] +-- [email protected] | +-- [email protected] | `-- [email protected] | +-- [email protected] | | +-- [email protected] | | `-- [email protected] | | +-- [email protected] | | | `-- [email protected] | | +-- [email protected] | | +-- [email protected] | | | +-- [email protected] | | | | `-- [email protected] | | | | +-- [email protected] | | | | +-- [email protected] | | | | `-- [email protected] | | | +-- [email protected] | | | `-- [email protected] | | +-- [email protected] | | | `-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | | `-- [email protected] | | +-- [email protected] | | +-- [email protected] | | | +-- [email protected] | | | | `-- [email protected] | | | `-- [email protected] | | +-- [email protected] | | | +-- [email protected] | | | `-- [email protected] | | `-- [email protected] | | +-- [email protected] | | `-- [email protected] | +-- [email protected] | +-- [email protected] | +-- [email protected] | | `-- [email protected] | +-- [email protected] | | `-- [email protected] | +-- [email protected] | `-- [email protected] | +-- [email protected] | | `-- [email protected] | | +-- [email protected] | | `-- [email protected] | `-- [email protected] `-- [email protected] +-- [email protected] `-- [email protected] `-- [email protected] WARN optional Skipping failed optional dependency /chokidar/fsevents:npm WARN notsup Not compatible with your operating system or architecture: [email protected] WARN [email protected] No descriptionnpm WARN [email protected] No repository field.>这时候,你再检查一下 package.json 文件,它应该多了三行。{ 'name': 'w1', 'version': '1.0.0', 'description': '', 'main': 'index.js', 'dependencies': {}, 'devDependencies': { 'webpack': '^1.13.2' }, 'scripts': { 'test': 'echo 'Error: no test specified' && exit 1' }, 'keywords': [], 'author': '', 'license': 'ISC'}4. Hello, Webpack4.1 打包普通脚本文件写一段普通的脚本,例如,一个 Hellowrold 脚本。当然了,这里应该是 Hello,Webpack.function hello(){ alert('Hello, Webpack!');}保存到你的项目根目录中,文件名就叫 hello.js4.2 创建 Webpack 配置文件Webpack 根据配置文件的配置来完成打包,默认的配置文件名称是 webpack.config.js。Webpack 的工作很简单,就是打包,你需要告诉它打包的内容,还有输出到哪里。entry 就是入口,显然 output 就是输出。我们让 Webpack 将 hello.js 文件打包后输出到 bundle.js 文件中。module.exports = { // 入口 entry: './hello.js', // 输出的文件名 output: { filename: 'bundle.js' }};在命令窗口,输入 webpack 回车执行。应该会看到如下的输出。> webpackHash: 05c39d9076887c35f015Version: webpack 1.13.2Time: 59ms Asset Size Chunks Chunk Namesbundle.js 1.44 kB 0 [emitted] main [0] ./hello.js 51 bytes {0} [built]>默认的入口文件名为 index.js,如果你将 hello.js 改名为 index.js,还可以直接使用目录名,不用提供文件名。module.exports = { // 入口,默认入口文件名为 index.js entry: '.', // 输出的文件名 output: { filename: 'bundle.js' }};生成的 bundle.js 文件内容为/******/ (function(modules) { // webpackBootstrap/******/ // The module cache/******/ var installedModules = {};/******/ // The require function/******/ function __webpack_require__(moduleId) {/******/ // Check if module is in cache/******/ if(installedModules[moduleId])/******/ return installedModules[moduleId].exports;/******/ // Create a new module (and put it into the cache)/******/ var module = installedModules[moduleId] = {/******/ exports: {},/******/ id: moduleId,/******/ loaded: false/******/ };/******/ // Execute the module function/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);/******/ // Flag the module as loaded/******/ module.loaded = true;/******/ // Return the exports of the module/******/ return module.exports;/******/ }/******/ // expose the modules object (__webpack_modules__)/******/ __webpack_require__.m = modules;/******/ // expose the module cache/******/ __webpack_require__.c = installedModules;/******/ // __webpack_public_path__/******/ __webpack_require__.p = '';/******/ // Load entry module and return exports/******/ return __webpack_require__(0);/******/ })/************************************************************************//******/ ([/* 0 *//***/ function(module, exports) { function hello(){ alert('Hello, Webpack!'); }/***/ }/******/ ]);最后就是我们的脚本。4.2 将脚本嵌入到网页中刚刚只是一段脚本,还需要放到网页中才能执行。我们可以安装一个自动帮我们生成宿主网页的 webpack 插件 html-webpack-plugin 来帮助我们。npm install html-webpack-plugin --save-dev应该会看到如下的输出。> npm install html-webpack-plugin [email protected] w1`-- [email protected] +-- [email protected] +-- [email protected] | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | +-- [email protected] | | `-- [email protected] | +-- [email protected] | | +-- [email protected] | | `-- [email protected] | +-- [email protected] | | `-- [email protected] | +-- [email protected] | +-- [email protected] | | `-- [email protected] | +-- [email protected] | `-- [email protected] | `-- [email protected] +-- [email protected] +-- [email protected] | +-- [email protected] | | +-- [email protected] | | | +-- [email protected] | | | +-- [email protected] | | | +-- [email protected] | | | | `-- [email protected] | | | | +-- [email protected] | | | | `-- [email protected] | | | `-- [email protected] | | +-- [email protected] | | | `-- [email protected] | | +-- [email protected] | | | +-- [email protected] | | | +-- [email protected] | | | +-- [email protected] | | | `-- [email protected] | | | `-- [email protected] | | +-- [email protected] | | | `-- [email protected] | | `-- [email protected] | `-- [email protected] `-- [email protected] WARN optional Skipping failed optional dependency /chokidar/fsevents:npm WARN notsup Not compatible with your operating system or architecture: [email protected] WARN [email protected] No descriptionnpm WARN [email protected] No repository field.>这说明这个插件安装好了。配置 Webpack 使用这个插件,帮助我们生成一个网页,然后将打包的脚本自动插入到这个网页中。var HtmlwebpackPlugin = require('html-webpack-plugin');module.exports = { // 入口 entry: '.', // 输出的文件名 output: { filename: 'bundle.js' }, // 添加我们的插件 会自动生成一个html文件 plugins: [ new HtmlwebpackPlugin({ title: 'Hello Webpack' }) ]};其实,这个配置文件就是一段程序,注意第一行,一定要加上。重新执行 webpack 命令。你会看到多生成了一个名为 index.html 的网页。> webpackHash: 05c39d9076887c35f015Version: webpack 1.13.2Time: 560ms Asset Size Chunks Chunk Names bundle.js 1.44 kB 0 [emitted] mainindex.html 184 bytes [emitted] [0] ./index.js 51 bytes {0} [built]Child html-webpack-plugin for 'index.html': + 3 hidden modules>打开这个网页,检查插入的脚本引用。<!DOCTYPE html><html> <head> <meta charset='UTF-8'> <title>Hello Webpack</title> </head> <body> <script type='text/javascript' src='bundle.js'></script></body></html>5. 总结Webpack 是一个基于 NodeJs 的打包工具,我们可以使用它帮助我们将脚本打包,它还可以帮助我们生成宿主网页,并自动将打包之后的脚本嵌入到这个网页中。


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2020-Jul-13 01:11am
栏目列表