【发布时间】:2018-09-18 22:03:15
【问题描述】:
我直接从文档 (https://babeljs.io/docs/en/plugins#plugin-development) 中获取了这个插件示例
export default function() {
return {
visitor: {
Identifier(path) {
const name = path.node.name;
// reverse the name: JavaScript -> tpircSavaJ
path.node.name = name.split("").reverse().join("");
}
}
};
}
我的babel.config.js 文件是这样的:
module.exports = {
plugins: [
'transform-es2015-modules-commonjs',
'./babelPlugin.js',
],
presets: [
'@babel/env'
]
} ;
我使用命令 babel testInput.js -o testOutput.js 运行 Babel
但我收到此错误:
D:\Projects\Babel plugin test\babelPlugin.js:1
(function (exports, require, module, __filename, __dirname) { export default function() {
^^^^^^
SyntaxError: Unexpected token export
如果我使用module.exports = 而不是export default,则一切正常。
为什么export default 会导致语法错误?
【问题讨论】:
-
问题是你的 babel 的配置,因为这个语法很好用。