此错误发生时,我正在编写装载程序:
ERROR in ./src/components/notFound.vue
Module build failed (from ./loader/cssExchangeLoader.js):
/Users/laiyinan/Project/前端开发/blog/loader/cssExchangeLoader.js:3
export default function(source){
^^^^^^
SyntaxError: Unexpected token export
loader(ALL):
var loaderUtils = require('loader-utils');
export default function(source){
let options = loaderUtils.getOptions(this);
console.log(options);
console.log(source.substr(0,10));
return `export default ${JSON.stringify(source)}`;
}
webpack.config(部分):
{
test: /(\.css$)|(\.scss$)|(\.vue$)/,
use: [
{
loader: path.resolve(root,'loader/cssExchangeLoader.js'),
options: {
target: 'red',
alternative: 'green'
}
}
],
exclude: [/node_modules/,/loader/]
}
为什么运行webpack时会发生模块构建错误?以及如何解决?预先感谢!
答案 0 :(得分:1)
默认情况下,Webpack(嗯,实际上不是Webpack本身,而是Node.js)仅支持CJS导入。该代码将起作用:
var loaderUtils = require('loader-utils');
module.exports = function(source){
let options = loaderUtils.getOptions(this);
console.log(options);
console.log(source.substr(0,10));
return `export default ${JSON.stringify(source)}`;
}