我能够使用自定义加载程序来实现我的目标。这并不理想,但解析逻辑相当简单,可以随着时间的推移变得更强。使用此策略,我只能将其应用于项目文件(任何文件)
不
在里面
node_modules
).
{
test: /\.js$/,
loader: path.resolve('systemjs-loader.js'),
exclude: [
path.resolve('node_modules')
],
options: {
loaderMap: {
'systemjs-plugin-text': 'raw-loader'
}
}
}
... 和加载器代码:
const { getOptions } = require('loader-utils');
const escapeRegexp = require('escape-string-regexp');
/**
* Replaces systemjs-style loader syntax with webpack-style:
*
* ./some-module!systemjs-plugin -> webpack-plugin!./some-module
*/
module.exports = function (source) {
const options = getOptions(this);
const map = options.loaderMap;
if (!map) {
throw new Error('There is no need to use the systemjs loader without providing a `loaderMap`');
}
Object.keys(map).forEach(loader => {
// indexOf should be faster than regex.test
if (source.indexOf(loader) !== -1) {
const exp = new RegExp(`(['"])([^!]+)!${escapeRegexp(loader)}['"]`, 'g');
source = source.replace(exp, `$1${map[loader]}!$2$1`);
}
});
return source;
};