這篇文章主要介紹了webpack怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
webpack是基于node的。先安裝最新的node。
1.初始化
安裝node后,新建一個目錄,比如html5。cmd中切到當前文件夾。
npm init -y
這個命令會創建一個默認的package.json。它包含了項目的一些配置參數,通過它可以進行初始安裝。詳細參數:https://docs.npmjs.com/files/package.json。
不要y參數的話,會在命令框中設置各項參數,但覺得沒啥必要。
2.安裝webpack
npm install webpack --save-dev
將webpack安裝到當前目錄。雖然npm install webpack -g 可以講webpack安裝到全局,但是容易出現一些模塊找不到的錯誤,所以最好還是安裝到當前目錄下。
3.目錄結構
webpack是一款模塊加載各種資源并打包的工具。所以先建一個如下的目錄結構:

app包含的開發中的js文件,一個組件,一個入口。build中就是用來存放打包之后的文件的。webpack.config.js 顧名思義用來配置webpack的。package.json就不用說了。
component.js
export default function () {
var element = document.createElement('h2');
element.innerHTML = 'Hello world';
return element;
}component.js 是輸出一個內容為h2元素。export default 是ES6語法,表示指定默認輸出。import的時候不用帶大括號。
index.js
import component from './component'; document.body.appendChild(component());
index.js 的作用就是引用Component模塊,并在頁面上輸出一個h2元素。但完成這個還需要一個插件,因為目前我們還沒有index.html文件。
npm install html-webpack-plugin --save-dev
html-webpack-plugin的用來生成html,將其也安裝到開發目錄下面。
4.設置 webpack 配置文件
我們需要通過webpack.config.js文件告訴webpack如何開始。配置文件至少需要一個入口和一個輸出。多個頁面就需要多個入口。node的path模塊
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const PATHS = {
app: path.join(__dirname, 'app'),
build: path.join(__dirname, 'build'),
};
module.exports = {
entry: {
app: PATHS.app,
},
output: {
path: PATHS.build,
filename: '[name].js',
},
plugins: [
new HtmlWebpackPlugin({
title: 'Webpack demo',
}),
],
};第一次看到這個配置文件是有點懵,主要是exports,分三個部分,一個入口,一個輸出,一個插件。入口指向了app文件夾。默認會把包含"index.js"的文件作為入口。輸出指定了build地址和一個文件名;[name]這兒表示占位符,可以看成webpack提供的一個變量。這個具體后面再看。而HtmlWebpackPlugin會生成一個默認的html文件。
5.打包
有了以上準備,直接輸入 webpack 就能運行了。

這個輸出包含了Hash(每次打包值都不同),Version,Time(耗時)。以及輸出的文件信息。這時打開build文件夾,發現多了一個app.js和index.html文件,雙擊index.html:

也可以修改下package.json
{
"name": "Html5",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"html-webpack-plugin": "^2.28.0",
"webpack": "^2.2.1"
}
}指定build。在cmd中執行npm run build 得到同樣的結果

出現helloword。再看下文件內容
index.html:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Webpack demo</title> </head> <body> <script type="text/javascript" src="app.js"></script></body> </html>
默認引用了app.js。
6、解析
app.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] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.l = 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;
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony default export */ __webpack_exports__["a"] = function () {
var element = document.createElement('h2');
element.innerHTML = 'Hello world';
return element;
};
/***/ }),
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__component__ = __webpack_require__(0);
document.body.appendChild(__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__component__["a" /* default */])());
/***/ })
/******/ ]);而app.js內容比較多了。整體是一個匿名函數。
(function(module) {
})([(function (){}), function() {}])app文件夾中的兩個js文件成了這兒的兩個模塊。函數最開始是從__webpack_require__開始
return __webpack_require__(__webpack_require__.s = 1);
這里指定從模塊1執行(賦值語句的返回值為其值)。而模塊1的調用是通過__webpack_require__的這句執行的。
代碼如下:
modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
通過call調用模塊的主要作用是為了把參數傳過去。
(function(module, __webpack_exports__, __webpack_require__) {
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__component__ = __webpack_require__(0);
document.body.appendChild(__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__component__["a" /* default */])());
/***/ })__webpack_require__ 每加載一個模塊都會先去模塊緩存中找,沒有就新建一個module對象:
var module = installedModules[moduleId] = {
i: moduleId,
l: false,
exports: {}
};模塊1中加載了模塊0,
var __WEBPACK_IMPORTED_MODULE_0__component__ = __webpack_require__(0);
__WEBPACK_IMPORTED_MODULE_0__component__ 返回的是這個模塊0的exports部分。而之前Component.js的默認方法定義成了
__webpack_exports__["a"] = function () {
var element = document.createElement('h2');
element.innerHTML = 'Hello world';
return element;
}所以再模塊1的定義通過"a“來獲取這個方法:
代碼如下:
document.body.appendChild(__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__component__["a" /* default */])());
這樣就完整了,但這里使用了__webpack_require__.i 將原值返回。
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };感謝你能夠認真閱讀完這篇文章,希望小編分享的“webpack怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。