Code Monkey home page Code Monkey logo

Comments (3)

privatenumber avatar privatenumber commented on May 18, 2024 2

This problem is happening because Node.js is unable to detect whether a .ts file is commonjs/module.

I worked on a fix in esbuild-kit/esm-loader#2, which detects the package type (by detecting nearest package.json), and skips it if it's commonjs, so that it can be handled by cjs-loader.

When the fix is merged, named imports on JSON will work for CommonJS contexts.

However, it still won't work in Module contexts because native JSON modules don't support named imports by spec. We can technically override this behavior to support it but I'm not sure if we should because we don't back-port things like __filename/__dirname.

What do you think?

from tsx.

antfu avatar antfu commented on May 18, 2024 1

I think using named import json is quite a common convention supported by most of the bundlers. Importing a single field of a json would also enable tree-shaking for a smaller footprint. I would expect this as I might run the .ts file with the runtime in development and use bundler for production. Without this, it would be quite a hassle to deal with. I am not sure if sacrificing this to match the spec is worth it here.

from tsx.

privatenumber avatar privatenumber commented on May 18, 2024

Fair point. I actually encountered that exact problem when importing version from package.json, which works in Rollup/Vite.

I'm OK with overriding JSON modules to add support in esm-loader as well.

For reference on bundlers:

from tsx.

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.