Skip to content

@vue/babel - dynamic import fail #922

Closed
@maxpou

Description

@maxpou

Version

3.0.0-beta.2

Reproduction link

maxpou/gitvub#43

Steps to reproduce

  1. vue create test-dyn-import
  2. pick vue-router, vuex, sass, babel, pwa, eslint, unit-jest
  3. add new file users.json under test/unit
{
  "data": ["bob", "max"]
}
  1. add the following test
  it('should import my file', async () => {
    const userFile = './users.json'
    const users = await import(userFile)
    console.log(users)
  })
  1. run npm run test

What is expected?

test should be green and print something like this { data: [ 'bob', 'max' ] }

What is actually happening?

~/sites/lab/test-dyn-import(master*) » npm run test

> test-dyn-import@0.1.0 test /home/maxence/sites/lab/test-dyn-import
> vue-cli-service test

 FAIL  tests/unit/HelloWorld.spec.js
  ● Test suite failed to run

    /home/maxence/sites/lab/test-dyn-import/tests/unit/HelloWorld.spec.js:35
                return import(userFile).then(function (someModule) {
                       ^^^^^^

    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:316:17)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.488s
Ran all test suites.
 ERROR  jest exited with code 1.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test-dyn-import@0.1.0 test: `vue-cli-service test`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the test-dyn-import@0.1.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/maxence/.npm/_logs/2018-03-02T17_51_40_312Z-debug.log

I'm trying to switch to the new file architecture for one of my side project (https://github.com/maxpou/gitvub).
I solved this issue with the past architecture by the following babelrc file

{
  "presets": [
    ["env", {
      "modules": false,
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2"
  ],
  "plugins": ["transform-runtime"],
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": ["transform-es2015-modules-commonjs", "dynamic-import-node"]
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions