上一章講的是definePlugin的用法,和這一章依舊沒有任何關係,這一章講的時候providerPlugin
。html
就是提供全局變量啦node
jquery
栗子初始化項目jquery
+ 0x006-provider-plugin + src - index.js - webpack.config.js
安裝依賴包webpack
$ cnpm init -y $ cnpm install webpack --save-dev $ cnpm install jquery --save
編寫webpack.config.js
git
var path = require('path') module.exports = { entry : ['./src/index.js'], output : { path : path.resolve(__dirname, 'dist'), filename: 'index.js' } }
添加插件,並定義jQuery
github
var path = require('path') var webpack = require('webpack') module.exports = { entry : ['./src/index.js'], output : { path : path.resolve(__dirname, 'dist'), filename: 'index.js' }, plugins: [ new webpack.ProvidePlugin({ $ : 'jquery', jQuery: 'jquery' }) ] }
調用jquery
web
// ./src/index.js $(document).ready(function () { console.log($('#name')[0].innerHTML) }) // ./src/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>providerPlugin</title> </head> <body> <p id="name">followWinter</p> </body> <script src="./../dist/index.js"></script> </html>
./src/index.html
,查看控制檯
添加定義npm
timestamp: [path.resolve(__dirname, 'src/utils'), 'default']
添加文件./src/utils
segmentfault
export default function () { console.log(new Date().getTime()) }
調用瀏覽器
// ./src/index.js timestamp()
打包並執行
$ webpack $ node ./dist/index.js # 輸出 1509977476685