transform the node module to the dest path
node_modules/lib/index.js
const main = require('./main');
main();
node_modules/lib/main.js
module.exports = () => {
console.log('main');
}
index.js
require('lib');
console.log('other...')
const path = require('path');
const transformer = require('module-transformer');
transformer.transform({
context: process.cwd(),
input: [
'index.js'
],
output: {
path: 'dist',
moduleDir: '.modules',
namedModule: 'id'
},
exclude: [],
alias: {
'@': path.join(__dirname, 'src')
},
cache: false,
plugins: [
transformer.plugins.emitFile()
],
advanced: {
parseOptions: {
ecmaVersion: 2020
}
}
})
├── dist
| ├── .modules
| | ├── 2.js
| | └── 3.js
| └── index.js
dist/.modules/2.js
const main = require('./3.js');
main();
dist/.modules/3.js
module.exports = () => {
console.log('main');
}
dist/index.js
require('./.modules/2.js');
console.log('other...')
- 加载配置
- 加载插件
- 遍历解析入口
- 按规则递归解析 module,生成模块依赖关系图
- 根据依赖关系生成资源树
- 编译完成
function testPlugin(options) {
return (compiler) => {
compiler.hook('beforeCompile', async () => {
console.log(compiler);
})
compiler.hook('done', async (stat) => {
console.log(compiler);
})
}
}
// 配置
const options = {
plugins: [
testPlugin({
// ...
})
]
}