所以我一直在努力让Sweet.js在我的ES6项目上工作,使用Webpack来编译它。我已经能够让每个人分开工作了,但无论我怎么试图将它们组合在一起它都会产生某种错误......我认为它主要与源地图有关,因为错误通常看起来像什么像这样:
Module build failed: Error: C:/Users/.../index.js: Invalid mapping: {"generated":{"line":4,"column":12,
"lastColumn":null},"source":null,"original":{"line":null,"column":null},"name":null}
这是失败的代码:
webpack.config.js:
module.exports = {
entry: './public_html/script/src/index.js',
// ...
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules.*\.js/, loader: 'babel!sweetjs?modules[]=./macros.sjs&sourceMap=true' },
// ...
]
},
// ...
devtool: 'source-map',
// ...
};
macros.sjs:
macro (=~) {
rule infix { $x | $y } => {
!!$y.test($x)
}
};
export (=~);
macro (!=~) {
rule infix { $x | $y } => {
!$y.test($x)
}
};
export (!=~);
index.js:
let x = "asdf", p = /a/i;
x =~ p;
反转加载器不起作用,因为Babel不知道如何处理这些宏。删除Babel加载器让它编译,但随后(更复杂的东西继续)Webpack失败,因为它不知道如何处理ES6的事情。
到目前为止,我还没有发现任何关于同时使用babel和sweetjs装载机的事情......它甚至可能吗?
编辑:所以我发现了一个关于sweetjs-loader的错误:https://github.com/jlongster/sweetjs-loader/issues/4
将true设置为false可修复问题,并创建另一个问题。 Sweet.js似乎将一些垃圾数据放入文件的底部,这与Babel和Webpack的模块加载混淆。每个版本会多次出现此错误:
ERROR in ./public_html/script/src/main-menu/index.js
Module build failed: Error: Line 56: Unexpected token ILLEGAL
[... } ) ; ...]