webpack的使用

來源:互聯網
上載者:User

標籤:

一、安裝使用

1.全域安裝webpack

npm install webpack -g

2.建立一個package.json

npm init

3.安裝webpack依賴

npm install webpack --save-dev

4.使用webpack開發工具,安裝webpack-dev-server此時的outpath中沒有輸出,需要webpack -p發布了才有

npm install webpack-dev-server --save-dev

註:使用--save-dev的目的是使將所安裝的依賴加入到package.json中。

 

二、使用

1.在不使用webpack.config.js的前提下可以直接使用命令

$ webpack entry.js bundle.js

以上代碼的含義為將entry.js檔案通過webpack打包成bundle.js檔案

 

2.webpack使用CommonJS的書寫形式:以下是我簡單的理解

CommonJS指一個檔案一個模組,但會一次性載入(即同步載入),但在瀏覽器端不適用這種方式,載入速率什麼的,於是引入了非同步載入,其中有CMD(sea.js)與AMD(require.js)。
CMD:define(function (require, exports, module) {})消極式載入的方式,但用到資源了再載入,
AMD:define("alpha", ["require", "exports", "beta"], function (require, exports, beta) {})提前載入的方式,用的時候資源已經載入完成

abc.js

document.write(require(‘./module.js‘)) // 添加模組

module.js

module.exports = ‘It works from module.js.‘

要上是引用模組的方式

 

三、webpack.config.js的常用功能及配置

1.entry:設定入口。存放主檔案

2.output:設定輸出

path:輸出檔案路徑

filename:輸出檔案名稱

publicPath:設定資源訪問路徑,即localhost:8080直接在該目錄下訪問,否則從根目錄下訪問

3.loader:Webpack 本身只能處理 JavaScript 模組,如果要處理其他類型的檔案,就需要使用 loader 進行轉換

test:匹配希望處理檔案的路徑
exclude:匹配不希望處理檔案的路徑
loader:此處xxx-loader可以簡寫為xxx,?後以query方式傳遞給loader參數

處理JS

{        test: /\.js$/,        exclude: /node_modules|vue\/src|vue-router\//,        loader: ‘babel‘        //babel可以讓我們在編寫代碼時使用ES6的版本}

處理css

{            test: /\.css$/,            loader: ExtractTextPlugin.extract(                "style-loader",                "css-loader?sourceMap"            )            //loader: "style!css" //用css-loader解析,再用style-loader解析        }, {            test: /\.less$/,            loader: ExtractTextPlugin.extract("style-loader", "css-loader!less-loader")            //loader: "style!css!less"//先用less-loader解析,再用css-loader解析,再用style-loader解析 }    

以上使用extract-text-webpack-plugin將css從js代碼中抽出併合並,如不需要則使用注釋的代碼

處理圖片與字型

{            test: /\.(png|jpg)$/,             loader: ‘url-loader?limit=8192‘ //  <= 8kb的圖片base64內聯        },            {                test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,                loader: ‘url?limit=10000&minetype=application/font-woff‘            },            {                test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,                loader: ‘url?limit=10&minetype=application/font-woff‘            },            {                test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,                loader: ‘url?limit=10&minetype=application/octet-stream‘            },            {                test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,                loader: ‘file‘            },            {                test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,                loader: ‘url?limit=10&minetype=image/svg+xml‘            }

解析vue

{      test: /\.vue$/,      loader: ‘vue-loader‘  }

四、外掛程式的使用

1.

 plugins: [    new webpack.BannerPlugin(‘This file is created by pcd‘)  ]

組建檔案的頂部會出現這句話"This file is created by pcd".

 

2.

plugins: [   new webpack.ProvidePlugin({       ‘Moment‘: ‘moment‘,       "$": "jquery",       "jQuery": "jquery",       "window.jQuery": "jquery"   })]

當每一個JS檔案都需要引用jquery庫時,就可以將其放在公用庫中。

$ webpack-dev-server --progress --colors

運行該命令可以對webpack進行監聽

也可以在oackage.json中加入

"scripts": {    "dev": "BUILD_DEV=1 webpack-dev-server --progress --colors",    "build": "BUILD_PRERELEASE=1 webpack -p"  }

開發

npm run dev

發布

npm run build

  

五、合并公用模組

var webpack            = require(‘webpack‘);var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin; ...entry: {   a: ‘./index/a.js‘,   b: ‘./idnex/b.js‘,   c: ‘./index/c.js‘,   d: ‘./index/d.js‘},...plugins: [   new CommonsChunkPlugin(‘part1.js‘, [‘a‘, ‘b‘]),   new CommonsChunkPlugin(‘common.js‘, [‘part1‘, ‘c‘])]...

 

webpack的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.