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 import from module=none of an module=es2022 via barrel reemits it as commonjs #61316

Open
HolgerJeromin opened this issue Feb 28, 2025 · 0 comments

Comments

@HolgerJeromin
Copy link
Contributor

HolgerJeromin commented Feb 28, 2025

πŸ”Ž Search Terms

module import commonjs barrel

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried. Tried 3.3, 5.7.3 and 5.9-nightly.

⏯ Playground Link

https://github.com/HolgerJeromin/ts-issues/tree/ts-61316

πŸ’» Code

Folder jsModule with tsconfig and one module file and a barrel file:

file jsModule/jsModule.ts

 export class TreeView {
  constructor() {}
}

file jsModule/services.ts

export {TreeView} from "./jsModule.js"

jsModule/tsconfig.json

{
  "compilerOptions": {
    "composite": true,
    "module": "ESNext",
    "target": "ESNext", 
    "strict": true
  },
  "files": ["jsModule.ts", "services.ts"]
}

And a folder src with module=none compile:

file src/main.ts

// both of these two imports trigger the bug:
type TreeView = import("../jsModule/services.js").TreeView;
import("../jsModule/services.js").then((module) => {
  const _TreeView = module.TreeView;
});

src/tsconfig.json

{
  "compilerOptions": {
    "module": "None",
    "target": "ESNext",
    "types": [],
    "strict": true
  },
  "files": ["main.ts"]
}

πŸ™ Actual behavior

Build jsModule folder:

.\node_modules\.bin\tsc.cmd --build  .\jsModule\ --verbose --force

Results in correct jsModule/jsModule.js

export class TreeView {
    constructor() { }
}

Build src folder:

.\node_modules\.bin\tsc.cmd --build  .\src\ --verbose --force

Changes existing jsModule/jsModule.js to commonJs output:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TreeView = void 0;
class TreeView {
    constructor() { }
}
exports.TreeView = TreeView;

--explainFiles shows this:

...
jsModule/jsModule.ts
  Imported via "./jsModule.js" from file 'jsModule/services.ts'
jsModule/services.ts
  Imported via "../jsModule/services.js" from file 'src/main.ts'
  Imported via "../jsModule/services.js" from file 'src/main.ts'
src/main.ts
  Part of 'files' list in tsconfig.json

πŸ™‚ Expected behavior

The emit of commonJs is completely surprising.
Expected would be one of

  • The files are not included (or referenced) in src/tsconfig.json so they should not be rebuild (but only .d.ts files consumed) or
  • if this needs to be rebuild it should be correctly based on its tsconfig.json

Additional information about the issue

With my real code these folders are different VS 2022 projects and therefore have no references.

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

No branches or pull requests

1 participant