Webpack 入门指南 - 1.安装

Webpack 是目前流行的打包工具,如何安装它呢?

 

1. 安装 Node Js

首先,Webpack 是基于 NodeJs 的工具,你必须首先安装 NodeJs. NodeJs 仅仅只需要在你的系统中安装一次就可以了。

 

2. 全局安装 Webpack

我们希望能够在系统的任何文件夹中使用 Webpack,使用的方式是通过 Webpack 命令来完成的,这需要我们全局安装 Webpack。这也只需要安装一次,以后每个项目就不需要重新全局安装了。

成功安装之后,你应该能够在任何目录中执行 webpack 命令,如果你还没有项目的配置文件的话,应该会看到当前的 Webpack 版本和一个命令的帮助列表。

$ npm install webpack -g

注意,最后还提示你,当前没有找到 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 是一个很复杂的工具。它依赖很多的其它工具。

这时候,你再检查一下 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, Webpack

4.1 打包普通脚本文件

写一段普通的脚本,例如,一个 Hellowrold 脚本。当然了,这里应该是 Hello,Webpack.

function hello(){
    alert("Hello, Webpack!");
}

保存到你的项目根目录中,文件名就叫 hello.js

4.2 创建 Webpack 配置文件

Webpack 根据配置文件的配置来完成打包,默认的配置文件名称是 webpack.config.js。

Webpack 的工作很简单,就是打包,你需要告诉它打包的内容,还有输出到哪里。entry 就是入口,显然 output 就是输出。

我们让 Webpack 将 hello.js 文件打包后输出到 bundle.js 文件中。

$ webpack

 在命令窗口,输入 webpack 回车执行。应该会看到如下的输出。

> webpack
Hash: 05c39d9076887c35f015
Version: webpack 1.13.2
Time: 59ms
    Asset     Size  Chunks             Chunk Names
bundle.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

配置 Webpack 使用这个插件,帮助我们生成一个网页,然后将打包的脚本自动插入到这个网页中。

var HtmlwebpackPlugin = require('html-webpack-plugin');

module.exports = {
  // 入口
  entry: ".",
  // 输出的文件名
  output: {
    filename: 'bundle.js'
  },
  // 添加我们的插件 会自动生成一个html文件
  plugins: [
    new HtmlwebpackPlugin({
      title: 'Hello Webpack'
    })
  ]
};

其实,这个配置文件就是一段程序,注意第一行,一定要加上。

重新执行 webpack 命令。你会看到多生成了一个名为 index.html 的网页。

> webpack
Hash: 05c39d9076887c35f015
Version: webpack 1.13.2
Time: 560ms
     Asset       Size  Chunks             Chunk Names
 bundle.js    1.44 kB       0  [emitted]  main
index.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 的打包工具,我们可以使用它帮助我们将脚本打包,它还可以帮助我们生成宿主网页,并自动将打包之后的脚本嵌入到这个网页中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值