【问题标题】:vue test utils SyntaxError: Unexpected token importvue test utils SyntaxError: Unexpected token import
【发布时间】:2018-11-24 01:50:56
【问题描述】:

我在尝试通过延迟加载方法加载组件的索引文件中收到错误 SyntaxError: Unexpected token import

const templates = () => import('@/pages/templates');

如果使用语法,它可以正常工作:

import templates from '@/pages/templates';

【问题讨论】:

    标签: jestjs vue-test-utils


    【解决方案1】:

    动态 import 语句是 Webpack 的一项功能*,因此当您使用 Jest 编译代码时,动态 import 将未定义。你可以通过在你的 babel 配置中使用 babel-plugin-dynamic-import-node 来解决这个问题:

    // .babelrc
    {
      "env": {
        "test": {
          "presets": [
            ["es2015", { "modules": false }],
            "react",
            "stage-0"
          ],
          "plugins": [
            "transform-es2015-modules-commonjs",
            "dynamic-import-node"
          ]
        }
      }
    }
    

    *虽然它符合TC39 spec

    【讨论】:

      猜你喜欢
      • 2019-04-24
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 2019-06-20
      • 2018-12-15
      • 2021-10-08
      • 2018-09-11
      • 2016-10-04
      相关资源
      最近更新 更多