Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic imports don't work even with jsc.parser.dynamicImport set to true #1271

Closed
mgol opened this issue Dec 12, 2020 · 8 comments · Fixed by #1287
Closed

Dynamic imports don't work even with jsc.parser.dynamicImport set to true #1271

mgol opened this issue Dec 12, 2020 · 8 comments · Fixed by #1287
Assignees
Labels
Milestone

Comments

@mgol
Copy link

mgol commented Dec 12, 2020

Describe the bug

Dynamic imports don't work even with jsc.parser.dynamicImport set to true. Meanwhile, the script below prints:

$ node ../../_swc-run.js 
error: import(...) expressions requires `jsc.parser.dynamicImport` to be true
 --> /Users/myuser/test.ts:1:38
  |
1 | export const importSomething = () => import('lodash');
  |                                      ^^^^^^

/Users/myuser/project/node_modules/@swc/core/index.js:116
        return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(options));
                        ^

Error: GenericFailure: failed to process js file

Input code

Save the following contents as /Users/myuser/test.ts (or any other path as long as you update it in the script as well):

export const importSomething = () => import('lodash');

and run the following code:

'use strict';

const fs = require('fs');
const swc = require('@swc/core');

const filePath = '/Users/myuser/test.ts';

const programmaticOptions = {
  jsc: {
    parser: {
      syntax: 'typescript',
      tsx: true,
      jsx: true,
      dynamicImport: true,
    },
    target: 'es2015',
  },
  sourceMaps: false,
  filename: filePath,
  sourceFileName: filePath,
};

const source = fs.readFileSync(filePath, 'utf-8');
const output = swc.transformSync(source, programmaticOptions);

console.log('output', output);

Config
Config is embedded in the script above.

Expected behavior
The code should compile.

Version
The version of @swc/core: 1.2.40

Additional context
Add any other context about the problem here.

@mgol mgol added the C-bug label Dec 12, 2020
@kdy1
Copy link
Member

kdy1 commented Dec 21, 2020

You should remove jsx: true.

Anyway, this should be error... But it will be fixed by #1244.

@kdy1 kdy1 closed this as completed Dec 21, 2020
@kdy1 kdy1 added this to the v1.2.41 milestone Dec 21, 2020
@kdy1 kdy1 self-assigned this Dec 21, 2020
kdy1 added a commit to kdy1/swc that referenced this issue Dec 21, 2020
kdy1 added a commit that referenced this issue Dec 21, 2020
swc_ecma_ast:
 - Add `Str.kind`.

swc_ecma_transforms:
 - Fix quotes in template literals. (#1280)

swc_ecma_codegen:
 - Fix escape of single quotes. (#1259)
 - Fix codegen of wrong escapes. (#1276)

swc:
 - Ensure that #1271 is about napi's deserialization.
@traverse
Copy link

traverse commented Jan 8, 2021

Am still running into this using the swc-loader with @swc/[email protected] not sure if I should've made an issue there or just here since this seems related. Tried different parser options but didn't find a working combination.

@kdy1
Copy link
Member

kdy1 commented Jan 13, 2021

@traverse Can you show me your config?

@traverse
Copy link

@kdy1 The config I was using is as follow

{
  loader: 'swc-loader',
  options: {
    jsc: {
      target: 'es2015',
      parser: {
        syntax: 'typescript',
        tsx: true,
        dynamicImport: true,
        classProperty: true,
        exportNamespaceFrom: true,
        exportDefaultFrom: true
      }
    }
  }
}

Like I said it's using Webpack with the swc-loader but I'd expect the config to be the same.

@kdy1
Copy link
Member

kdy1 commented Jan 13, 2021

@traverse I guess you should remove

        classProperty: true,
        exportNamespaceFrom: true,
        exportDefaultFrom: true

Seems like a bug of napi-rs.

@traverse
Copy link

I've tried several configurations also without those properties but it still kept showing the same error, I'll see if I can reproduce it again tomorrow.

@eliseumds
Copy link

I've tried several configurations also without those properties but it still kept showing the same error, I'll see if I can reproduce it again tomorrow.

Same here.

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 25, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

5 participants