Error when migrating from Babel 5 to 6 (ReferenceError: exports is not defined)

Error when migrating from Babel 5 to 6 (ReferenceError: exports is not defined)

我正在使用 Gulp 和 Babel 将客户端 es6 代码编译为 es5。升级后出现此错误(在浏览器中): Uncaught ReferenceError: exports is not defined

出现此错误的原因是 Babel 将我的客户端脚本编译为 CommonJS 模块,并在每个文件的开头添加了以下行:

Object.defineProperty(exports, "__esModule", { // <-- ReferenceError: exports is not defined
  value: true
});

但是我没有在客户端上使用任何 UMD/CommonJS 模块加载器,所以这段代码会导致错误。对于 Babel 5,为了避免这种情况,我在我的 gulpfile 中使用了选项 modules: 'ignore'

return gulp.src(src, {base: 'src'})
   .pipe(babel({
        modules: 'ignore' // <-- dropped from Babel 6
   }))
   .pipe(gulp.dest(dest));

所以它按原样编译了我的脚本,原始且清晰。但是这个选项从 Babel 6 中删除了,现在它会导致错误 [ReferenceError: [BABEL] ..myscript.js: Unknown option: base.modules] , 所以我不得不评论这一行。

在 Babel 6 中是否有 modules: 'ignore' 的替代方案?

因为您使用的是默认启用的 es2015 this set of plugins。请注意 babel-plugin-transform-es2015-modules-commonjs 在那里。

如果您不想执行任何类型的模块转换,您需要明确列出您想要使用的插件,而不是使用 es2015