运行 mocha 时仍然收到使用 babel-plugin-syntax-dynamic-import 进行动态导入的语法错误

Still receive syntax error for dynamic import with babel-plugin-syntax-dynamic-import when running with mocha

所有 babel modules/plugins 都在最新版本的 babel v6 上。 Mocha 在 v4.0.1 上。

设置:

// .babelrc
{
   presets: [
      "stage-3",
      [
         "env",
         {
           "targets": {
              "browsers": [
                "last 2 versions"
              ],
              "node": "current"
           }
         }
      ]
      "react"
   ],
   plugins: ["transform-runtime", "syntax-dynamic-import"]
}


// mocha.opts
--recursive
--reporter progress
--require babel-polyfill
--require babel-register
--timeout 20000

所以当我 运行 我的测试套件时,我仍然得到: error stack trace.

我不得不想象它与 babel-register 和 syntax-dynamic-import 不能正确协同工作有关。因为当我在 webpack 中将它与 babel-loader 一起使用时,我在 transpile 上没有遇到任何语法错误。

来自 Satyajit Sahoo 的关于 babeljs slack 的回答为我解决了问题:

syntax-dynamic-import will only enable the syntax in babylon, which means it babylon can parse the code without syntax error. the code import(...) is still there, which node doesn’t understand and you still need to transpile it. dynamic-import-node transpiles the import(...) to require which node understands. On client, the transpilation step is not necessary since Webpack understands the syntax and does the bundling.