@xiaoqq
2018-08-10T08:09:18.000000Z
字数 1667
阅读 1391
Webpack
参考文章:
http://www.css88.com/doc/webpack/development/how-to-write-a-plugin/
var path = require('path');
var webpack = require('webpack');
var MyExampleWebpackPlugin = require('./wxplugin/index');
var config = {
devtool: "source-map",
entry: {
'redpacket': './application/pages/activity180420/redPacket.js'
},
output: {
path: path.resolve(__dirname, 'webapp/script'),
filename: '[name].js'
},
module: {
rules: [{
test: /\.js/,
use: [{
loader: 'babel-loader',
query: {
presets: ['es2015', 'stage-0']
}
},
path.resolve('./wxloader/wxjs.js')
]
}]
},
plugins: [
new MyExampleWebpackPlugin()
]
};
module.exports = config;
node-nightly
"scripts": {
"debug": "node-nightly --inspect-brk ./node_modules/webpack/bin/webpack.js"
},
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"debug"
],
"port": 9229
}
插件是webpack的核心功能,需要理解一些 webpack 底层的内部特性来做相应的勾子。
function HelloWorldPlugin(options) {
// 使用配置(options)设置插件实例
}
HelloWorldPlugin.prototype.apply = function(compiler) {
compiler.plugin('done', function() {
console.log('Hello World!');
});
};
module.exports = HelloWorldPlugin;
执行顺序 | Hook | 作用 |
---|---|---|
1 | environment | |
2 | afterEnvironment | |
3 | entryOption | 初始化入口选项 |
4 | afterPlugins | |
5 | afterResolvers | |
6 | beforeRun | |
7 | run | 开始编译 |
8 | normalModuleFactory | |
9 | contextModuleFactory | |
10 | beforeCompile | |
11 | compile | |
12 | thisCompilation | |
13 | compilation | |
14 | make | 从entry开始递归的分析依赖,对每个依赖模块进行build |
15 | afterCompile | |
16 | shouldEmit | |
17 | emit | 把各个chunk输出到结果文件 |
18 | afterEmit | |
19 | done |
watch执行顺序
执行顺序 | Hook | 作用 |
---|---|---|
6 | invalid | |
7 | watchRun | |
8 | normalModuleFactory | |
9 | contextModuleFactory | |
10 | beforeCompile | |
11 | compile | |
12 | thisCompilation | |
13 | compilation | |
14 | make | |
15 | afterCompile | |
16 | shouldEmit | |
17 | emit | |
18 | afterEmit | |
19 | done |