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 flag not respected #9174

Closed
Larsundso opened this issue Jul 7, 2024 · 3 comments
Closed

Dynamic Imports flag not respected #9174

Larsundso opened this issue Jul 7, 2024 · 3 comments
Labels

Comments

@Larsundso
Copy link

Larsundso commented Jul 7, 2024

Describe the bug

Dynamically importing files and modules in the code will lead swc to believe the module or file is unused and omit it during compilation. Even if dynamicImport is set to true in parser options.

Input code

// index.ts
(async () => {
 const r = await import("./lang.json");
 console.log(r);
})();

// lang.json
{
 "hi": "hi"
}

Config

{
 "$schema": "https://swc.rs/schema.json",
 "jsc": {
  "parser": {
   "syntax": "typescript",
   "tsx": false,
   "decorators": true,
   "dynamicImport": true,
   "preserveAllComments": false,
   "topLevelAwait": true,
   "resolveFully": true
  },
  "target": "esnext"
 },
 "module": {
  "type": "nodenext"
 },
 "minify": true,
 "sourceMaps": true,
 "isModule": true
}

Playground link (or link to the minimal reproduction)

https://github.com/Larsundso/swc-dynamic-import

SWC Info output

-- OS 1

Operating System:
    Platform: win32
    Arch: x64
    Machine Type: x86_64
    Version: Windows 11 Enterprise
    CPU: (24 cores)
        Models: AMD Ryzen 9 5900X 12-Core Processor

Binaries:
    Node: 22.4.0
    npm: N/A
    Yarn: N/A
    pnpm: N/A

Relevant Packages:
    @swc/core: 1.6.12-nightly-20240706.1
    @swc/helpers: N/A
    @swc/types: N/A


SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

-- OS 2

Operating System:
    Platform: linux
    Arch: x64
    Machine Type: x86_64
    Version: #122-Ubuntu SMP Thu May 23 07:48:21 UTC 2024
    CPU: (8 cores)
        Models: AMD EPYC 7282 16-Core Processor

Binaries:
    Node: 22.3.0
    npm: 10.8.1
    Yarn: N/A
    pnpm: 9.4.0

Relevant Packages:
    @swc/core: 1.6.12-nightly-20240706.1
    @swc/helpers: N/A
    @swc/types: N/A


SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

Expected behavior

lang.json should be compiled into /dist
the program should log its contents into the console

Actual behavior

lang.json is missing from /dist
the program fails with the following error

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/swc-dynamic-import/dist/lang.json' imported from /swc-dynamic-import/dist/index.js

Version

1.6.12-nightly-20240706.1

Additional context

No response

@Larsundso Larsundso added the C-bug label Jul 7, 2024
@kdy1 kdy1 modified the milestone: Planned Jul 7, 2024
@kdy1
Copy link
Member

kdy1 commented Jul 7, 2024

lang.json should be copied by other means. e.g. --copy-files
swc does not have a project mode of tsc so swc does not transform dependencies.

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 7, 2024
@ab-geoff-plitt
Copy link

@kdy1 That works for JSON files, but is there any way to do dynamic import of code? Was hoping to use await import() with an ESM module

@kdy1
Copy link
Member

kdy1 commented Jul 17, 2024

You can compile directory instead of specifying only entry file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants