确实,如果您的代码被转译,那么您将需要源映射。但是有可能绕过捆绑。是的,webpack 确实总是会尝试打包,但是使用插件可以将代码从包中取出并放在输出目录中,就像它只是通过转译器运行一样。
我有一个节点应用程序,我想简单地逐个文件地转换为 ES5,而不是捆绑任何东西。所以我的配置大致是这样的:
let config = {
entry: [
glob.sync(srcDir + '/**/*.js') // get all .js files from the source dir
],
output : {
filename : '[name].rem.js', // webpack wants to bundle - it can bundle here ;)
path: outDir
},
resolve: {
alias: {
'app': appDir
}
},
plugins: [
new RemoveEmptyScriptsPlugin({extensions: ['js'], scriptExtensions: /\.rem\.js/}) // for all .js source files that get bundled remove the bundle .rem.js file
],
module: {
rules:[{
test: /\.jsx?$/,
type: 'asset/resource', // get webpack to take it out instead of bundling
generator: {
filename: ({filename}) => filename // return full file name so directory structure is preserved
},
use: {
loader: 'babel-loader',
options: {
targets: { node: 16 },
presets: [
['@babel/preset-env', { modules: 'commonjs' /* transpile import/export */}],
]
}
}
}]
}
};
// Since the code does not go through the full pipeline and imports are not getting resolved, aliases will remain in the code.
// To resolve them it takes to hack the aliases object into the babel config
config.module.rules[0].use.options.plugins.push(['babel-plugin-webpack-alias-7', {config: {resolve: {alias: config.resolve.alias}}}];
但后来看来,当前发布的babel-plugin-webpack-alias-7 不支持为config 选项提供Object,所以我不得不修补插件https://github.com/shortminds/babel-plugin-webpack-alias-7/pull/22
啊,然后webpack-remove-empty-scripts 插件对我的想法有问题,所以我也不得不修补它https://github.com/webdiscus/webpack-remove-empty-scripts/pull/6