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

Import assertions are skipped when transpiled from TS to JS #3110

Closed
andreubotella opened this issue Dec 24, 2021 · 6 comments
Closed

Import assertions are skipped when transpiled from TS to JS #3110

andreubotella opened this issue Dec 24, 2021 · 6 comments
Labels

Comments

@andreubotella
Copy link
Contributor

Describe the bug

Import assertions affect whether a module is valid in the module graph, depending on the JS runtime, and so should not be stripped when transpiling.

Input code

import data from "./test.json" assert { type: "json" };

console.log(data);

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.2.122&code=H4sIAAAAAAAAA8vMLcgvKlFISSxJVEgrys9VUNLTL0ktLtHLKs7PU1JILC5OBUpXK5RUFqRaKShBRGutubiS8%2FOK83NS9XLy0zVAujWtAVSAl05LAAAA&config=H4sIAAAAAAAAA0WNSwrDMAxE76J1FsWLLnKHHsK4SnDxD40CDcZ3rx1cspNmnp4qfeBorVSsgGVMOJPaL62kZ2E48UVpIUWPNhvArS9WdtaOMMzDmF6HnMETWCj65LdzyFyORRi4K5v28Cdbd8X8PkZQr3%2BX80ntdsw7j9cEVQ5uPx%2Bqcl%2B4AAAA

Expected behavior

import data from "./test.json" assert { type: "json" };
console.log(data);

Actual behavior

import data from "./test.json";
console.log(data);

Version

1.2.122

Additional context

No response

@kdy1 kdy1 added this to the v1.2.123 milestone Dec 24, 2021
andreubotella pushed a commit to andreubotella/swc that referenced this issue Dec 24, 2021
swc_ecma_codegen:

    * Implement codegen for static import assertions.

swc_ecma_parser:

    * Make static import assertions a syntax error for ES versions under ES2022

Fixes swc-project#3110.
@kdy1
Copy link
Member

kdy1 commented Dec 25, 2021

Closing as duplicate of #2802

@kdy1 kdy1 closed this as completed Dec 25, 2021
@kdy1 kdy1 removed this from the v1.2.123 milestone Dec 25, 2021
@kdy1 kdy1 added invalid and removed C-bug labels Dec 25, 2021
@wycats
Copy link
Contributor

wycats commented May 29, 2022

@kdy1 It seems like the solution in #2802 involved a new flag that only applies to the ECMAScript parser.

Also, maybe I'm doing something wrong, but when I attempted to add the flag to the example in the playground in the JSON editor (after updating the version to the latest available version and switching to ECMAScript), it still seems to strip it out.

@kdy1
Copy link
Member

kdy1 commented May 30, 2022

@wycats TypeScript supports it unconditionally.

pub fn import_assertions(self) -> bool {
match self {
Syntax::Es(EsConfig {
import_assertions, ..
}) => import_assertions,
Syntax::Typescript(_) => true,
}
}

@kdy1
Copy link
Member

kdy1 commented May 30, 2022

@wycats
Copy link
Contributor

wycats commented Jun 21, 2022

Great! Consider this solved.

I'm not sure why it was causing me trouble in my real app (it wasn't this problem), but I can dig in if I reproduce it again.

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 16, 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 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

4 participants