Skip to content

Allow imports with .js extension for package.json "type": "module" esm compatibility. #22671

Closed as not planned
@jamie-pate

Description

@jamie-pate

🚀 Feature request

Reconsider #4591 which would require a webpack extension to allow developers to import modules using the '.js' file extension even though they are '.ts' files before compilation.

Command (mark with an x)

  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • extract-i18n
  • run
  • config
  • help
  • version
  • doc

Description

I am working on a split back-end/front-end project and trying to use "type":"module" in both package.json files but I'm currently unable to update to es modules on the client side because webpack cannot resolve typescript modules if the import ends with '.js' instead.

See https://nodejs.org/api/esm.html#mandatory-file-extensions

Since TS 2.0, it's possible to :
import { Something } from './something.js';// <- this is actually ./something.ts in the working directory.

Module identifiers allow for .js extension

Before TypeScript 2.0, a module identifier was always assumed to be extension-less; for instance, given an import as import d from "./moduleA.js", the compiler looked up the definition of "moduleA.js" in ./moduleA.js.ts or ./moduleA.js.d.ts. This made it hard to use bundling/loading tools like SystemJS that expect URI's in their module identifier.

With TypeScript 2.0, the compiler will look up definition of "moduleA.js" in ./moduleA.ts or ./moduleA.d.ts.

It's also important to be compatible with ES6 modules, which require the .js extension, as <script type="module"></script> is standard in evergreen browsers, and as for now TS doesn't provide an option to add the .js extension in transpiled files (see microsoft/TypeScript#13422).

Describe the solution you'd like

Add the resolve-typescript-plugin (or have an option to enable it) to the generated webpack config.

Describe alternatives you've considered

Currently I'm just running a split project where the server package requires '.js' extensions and the 'client' package does not. This is obviously not ideal.

Custom webpack builder can probably do this with this plugin: resolve-typescript-plugin

Nodejs can apparently be configured to experimentally customize the resolution algorithm

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions