簡體   English   中英

從Babel 5遷移到6時出錯(ReferenceError:未定義導出)

[英]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,為了避免這種情況,我使用了選項modules: 'ignore'在我的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] ,所以我不得不評論這一行。

有什么替代modules: 'ignore' Babel 6中的modules: 'ignore'

由於您使用的是es2015 因此默認情況下啟用了這組插件 請注意, babel-plugin-transform-es2015-modules-commonjs就在那里。

如果您不希望執行任何類型的模塊轉換,則需要明確列出您要使用的插件,而不是使用es2015

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM