Giter VIP home page Giter VIP logo

Comments (6)

rbuckton avatar rbuckton commented on September 27, 2024

It looks like this hasn't worked since --module preserve was added.

@andrewbranch in createExternalHelpersImportDeclarationIfNeeded we always return an ESM import-style declaration, even though we always use tslib's CommonJS exports. Would you expect under --module preserve that we continue to inject import ... from "tslib", or that we emit it as a require call?

If we go with require, I can modify createExternalHelpersImportDeclarationIfNeeded to return an ImportEqualsDeclaration and have the es module transformer handle the downleveling to require. If we stick with import then I'll need to modify createExternalHelpersImportDeclarationIfNeeded to treat Preserve like it does ESNext.

from typescript.

andrewbranch avatar andrewbranch commented on September 27, 2024

Hmm, I guess I would expect an ImportEqualsDeclaration if the file extension is .cts, ImportDeclaration otherwise (this should be reflected by program.getImpliedNodeFormatForEmit in that module mode).

even though we always use tslib's CommonJS exports

I would also expect module resolution to work correctly once the synthetic import has the correct syntaxβ€”i.e., if we generate an ESM import, module resolution in --module preserve --moduleResolution bundler should set the import condition, because it can see that the emitted syntax will be an import. Vice versa if the synthetic import is an ImportEqualsDeclaration.

from typescript.

rbuckton avatar rbuckton commented on September 27, 2024

getImpliedNodeFormatForEmit doesn't detect "type": "commonjs" or "type": "module" for a .ts file when using preserve because getImpliedNodeFormatForFileWorker never hits the branch that populates sourceFile.packageJsonCache.

from typescript.

rbuckton avatar rbuckton commented on September 27, 2024

I'm mostly tempted to make it always import= under preserve because it is consistent and the transformer for preserve already properly handles import=. Using the implied node format means that a .ts file in a package with "type": "commonjs" ends up with invalid import syntax.

from typescript.

rbuckton avatar rbuckton commented on September 27, 2024

Though, I suppose preserve really isn't meant to work with "type": "commonjs" since we don't have a CJS-only variant for export class et al.

from typescript.

andrewbranch avatar andrewbranch commented on September 27, 2024

getImpliedNodeFormatForEmit doesn't detect "type": "commonjs" or "type": "module" for a .ts file when using preserve because getImpliedNodeFormatForFileWorker never hits the branch that populates sourceFile.packageJsonCache.

Yeah, this is expected behavior. After community feedback, we decided that .mts and .cts should be universal signals of module format, but package.json "type" should only be used inside node_modules or --module nodenext.

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.