本質上,webpack 是一個現代 JavaScript 應用程序的靜態模塊打包器(module bundler)。當 webpack 處理應用程序時,它會遞歸地構建一個依賴關係圖(dependency graph),其中包含應用程序須要的每一個模塊,而後將全部這些模塊打包成一個或多個 bundle。javascript
Webpack 是當下最熱門的前端資源模塊化管理和打包工具,它能夠將許多鬆散耦合的模塊按照依賴和規則打包成符合生產環境部署的前端資源。還能夠將按需加載的模塊進行代碼分離,等到實際須要時再異步加載。經過 loader 轉換,任何形式的資源均可以當作模塊,好比 CommonsJS、AMD、ES六、CSS、JSON、CoffeeScript、LESS 等;css
WebPack 是一款模塊加載器兼打包工具,它能把各類資源,如 JS、JSX、ES六、SASS、LESS、圖片等都做爲模塊來處理和使用。html
Webpack是一個前端資源的打包工具,它能夠將is、image、sss等資源當成一個模塊進行打包(下圖展現了全部的js打包成一個js,css打包成一個css等)前端
<script src="module1.js"></scirpt> <script src="module2.js"></scirpt> <script src="module3.js"></scirpt> <script src="module4.js"></scirpt>
這是最原始的 JavaScript 文件加載方式,若是把每個文件看作是一個模塊,那麼他們的接口一般是暴露在全局做用域下,也就是定義在 window
對象中,不一樣模塊的調用都是一個做用域。vue
這種原始的加載方式暴露了一些顯而易見的弊端:java
<script>
的書寫順序進行加載 服務器端的 NodeJS 遵循 CommonsJS 規範,該規範核心思想是容許模塊經過 require
方法來同步加載所需依賴的其它模塊,而後經過 exports
或 module.exports
來導出須要暴露的接口。node
require("module"); var test = require("../module.js"); test.test(); export.doStuff = function() {}; module.exports = someValue;
Asynchronous Module Definition 規範其實主要一個主要接口 define(id?, dependencies?, factory);
它要在聲明模塊的時候指定全部的依賴 dependencies
,而且還要當作形參傳到 factory
中,對於依賴的模塊提早執行。python
Asynchronous Module Definition 規範其實主要一個主要接口 define(id?, dependencies?, factory);
它要在聲明模塊的時候指定全部的依賴 dependencies
,而且還要當作形參傳到 factory
中,對於依賴的模塊提早執行。jquery
define("module", ["dep1", "dep2"], function(d1, d2) { return someExportedValue; }); require(["module", "../file.js"], function(module, file) {});
Commons Module Definition 規範和 AMD 很類似,儘可能保持簡單,並與 CommonsJS 和 NodeJS 的 Modules 規範保持了很大的兼容性。webpack
define(function(require, exports, module) { var $ = require("jquery"); var Spinning = require("./spinning"); exports.doSomething = ...; module.exports = ...; });
EcmaScript6 標準增長了 JavaScript 語言層面的模塊體系定義。 ES6 模塊的設計思想,是儘可能靜態化,使編譯時就能肯定模塊的依賴關係,以及輸入和輸出的變量。CommonsJS 和 AMD 模塊,都只能在運行時肯定這些東西。
import "jquery"; export function doStuff() {} module "localModule" {}
能夠兼容多種模塊風格,儘可能能夠利用已有的代碼,不只僅只是 JavaScript 模塊化,還有 CSS、圖片、字體等資源也須要模塊化。\
用webpack實現
Node.js是一個Javascript運行環境(runtime environment),發佈於2009年5月,由Ryan Dah開發,實質是對Chrome V8引擎進行了封裝。Node js對一些特殊用例進行優化,提供替代的API,使得V8在非瀏覽器環境下運行得更好。
V8引擎執行Javascript的速度很是快,性能很是好。Nodejs是一個基於Chrome JavaScript運行時創建的平臺,用於方便地搭建響應速度快、易於擴展的網絡應用。Nodejs使用事件驅動,非阻塞/O模型而得以輕量和高效,很是適合在分佈式設備上運行數據密集型的實時應用。
npm config set prefix "D:\nodejs\npm_modules" npm config set cache "D:\nodejs\npm_cache"
將D:\nodejs\npm_modules添加到環境變量中
npm install -g cnpm --registry=https://registry.npm.taobao.org
查看cnpm版本
cnpm -v
切換cnpm鏡像地址(執行下面命令須要去下載好的nrm目錄,若是沒有添加環境變量)
cnpm install -g nrm //安裝 nrm ls //查詢當前指向的鏡像地址 nrm use taobao //切換到淘寶鏡像
npm install webpack -g npm install webpack-cli -g
建立 webpack.config.js
配置文件
module.exports = { entry: "", //指定入口文件(main.js) output: { path: "", //須要是絕對路徑,默認是./dist/filename.js filename: "" }, module: { loaders: [ {test: /\.js$/, loader: ""} ] }, plugins: {}, resolve: {}, watch: true }
直接運行 webpack
命令打包
modules
的目錄,用於放置 JS 模塊等資源文件hello.js
,用於編寫 JS 模塊相關代碼main.js
的入口文件,用於打包時設置 entry
屬性webpack.config.js
配置文件,使用 webpack
命令打包index.html
,導入 WebPack 打包後的 JS 文件須要將某些被其餘js須要的方法導出(exports是ES5語法)
exports.sayHi = function () { document.write("<div>Hello WebPack</div>"); };
若是須要將多個方法導出
var function01 = funciotn(){}; var function02 = function(){}; exports.function01 = function01; 方式1:直接一個一個的導出 exports.function02 = function02; 方式2: exports = {function01,function02}
設置它爲入口
導入須要的js
var hello = require("./hello"); //導入hellow.js(能夠不加後綴) hello.sayHi();
webpack.config.js
的配置文件主要目的:將上面的兩個js打包成一個js,因爲main.js依賴了hellow.js,因此他們會打包在一塊
module.exports = { entry: "./modules/main.js", output: { filename: "./js/bundle.js" } };
index.html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script src="dist/js/bundle.js"></script> </body> </html>
# 用於監聽變化 webpack --watch //在webpack.config.js 中的配置能夠省略watch=true配置
運行 HTML 文件,你會在瀏覽器看到:
Hello WebPack
model01.js
var add = function (x, y) { return x+y; } var add2 = function (x, y) { return x+y+2; } module.exports.add = add; // module.exports ={add,add2};//若是有多個方法這樣導出 // module.exports.add2 = add2//若是有多個方法也能夠這樣導出
main.js(主文件)
//導入model01.js var {add} = require("./model01.js") var Vue = require("./vue.min.js") var VM = new Vue({ el:'#app',//vm接管了app區域的管理 //model數據 data:{ num1:0, num2:0 } });
webpack.config.js(必定須要添加否則報錯)
module.exports = { entry: "./main.js", output: { filename: "build.js" } };
打包命令
webpack
效果
build.js並無真正生成;
推薦使用webpack-dev-server開發服務器,它的功能能夠實現熱加載而且,自動刷新瀏覽器。
此時咱們在開發中就能夠再也不使用nginx作服務器了
cnpm install webpack@3.6.0 webpack-dev-server@2.9.1 html-webpack-plugin@2.30.1 --save-dev
npm init
"scripts": { "dev": "webpack-dev-server --inline --hot --open --port 5008" },
·
//引用html-webpack-plugin插件,做用是根據html模板(vue_02.html)在內存生成html文件,它的工做原理是根據模板文件在內存中生成一個index.html文件。 var htmlwp = require('html-webpack-plugin'); module.exports={ entry:'./src/main.js', //指定打包的入口文件 output:{ path: __dirname+'/dist', // 注意:__dirname表示webpack.config.js(當前文件)所在目錄的絕對路徑(默認也是這個路徑) filename:'build.js' //輸出文件(沒有真正的生成) }, //devtool: 'eval-source-map', plugins:[ new htmlwp({ title: '首頁', //生成的頁面標題<head><title>首頁</title></head> filename: 'index.html', //webpack-dev-server在內存中生成的文件名稱,自動將build注入到這個頁面底部,才能實現自動刷新功能 template: 'vue_02.html' //根據vue_02.html這個模板來生成(這個文件請程序員本身生成) }) ] }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>vue.js經常使用指令的測試</title> </head> <body> <div id="app"> <input type="text" v-model="num1"/> + <input type="text" v-model="num2"/>= <span v-text="Number.parseInt(num1)+Number.parseInt(num2)"></span> <span v-text="result"></span> <button v-on:click="change">計算</button> </div> </body> <!--<script src="vue.min.js"></script>--> <!--<script src="dist/build.js"></script>--> </html>
var {add} = require("./model01") var Vue = require("./vue.min") var VM = new Vue({ el:'#app',//vm接管了app區域的管理 data:{//model數據 num1:0, num2:0, result:0 }, methods:{ change:function () { //必需要有this(會修改data中的result數據) this.result = add(Number.parseInt(this.num1),Number.parseInt(this.num2)) } } });
model01.js
var add = function (x, y) { return x+y; } var add2 = function (x, y) { return x+y+2; } module.exports.add = add; // module.exports ={add,add2};//若是有多個方法這樣導出 // module.exports.add2 = add2//若是有多個方法也能夠這樣導出
一、在webpack.config.js中添加 devtool: 'eval-source-map',
二、在須要打上斷點的js中添加debugger
methods:{ change:function () { debugger //調試 this.result = add(Number.parseInt(this.num1),Number.parseInt(this.num2)) } }
效果