Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

try to load typescript for specifier with extensions. #40

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

mshima
Copy link

@mshima mshima commented Oct 4, 2022

@mshima mshima changed the title try to load typescript fallback for specifier with extensions. try to load fallback for specifier with extensions. Oct 4, 2022
@mshima mshima changed the title try to load fallback for specifier with extensions. try to load typescript for specifier with extensions. Oct 4, 2022
@jraoult
Copy link

jraoult commented Oct 9, 2022

Hey @mshima, I was trying to use your patch to address a downstream issue: privatenumber/tsx#112.

From my quick hacking, you can add support for .tsx (mapped from .js) by simply adding it to your mappedExtensions object:

const mappedExtensions = {
	'.js': ['.ts', '.tsx'],
	'.cjs': ['.cts'],
	'.mjs': ['.mts'],
} as const;

Looking forward to seeing your PR merged 😀.

@karlhorky
Copy link

karlhorky commented Oct 9, 2022

Right, and as also mentioned in privatenumber/tsx#112, in addition to .tsx source files, also would be good to add support for .jsx import paths:

// Should resolve to a.tsx, a.ts, etc 
import a from './a.jsx'

@mshima
Copy link
Author

mshima commented Oct 9, 2022

@jraoult from the issue description, you will need to patch cjs-loader instead, so esbuild-kit/cjs-loader#23 may be relevant but I don't think it fixes the issue.
You will probably need to patch the resolveTsPath too and related mapper.
resolveTsPath will need to return an array so a core-utils breaking change.

tsx command is the same of node --require=@esbuild-kit/cjs-loader --loader=@esbuild-kit/esm-loader.

You can test using the git branch:

npm install mshima/cjs-loader#allowjs
npm install mshima/esm-loader#allowjs

There is already a mapped extension at https://github.com/esbuild-kit/core-utils/blob/a756b7b599cb92ac720c559bee8451f63b1643a1/src/resolve-ts-path.ts#L3-L7.
The PR should be change by exposing it to don't duplicate.

@mshima mshima force-pushed the patch-1 branch 2 times, most recently from b7ccd79 to c4b4e12 Compare October 9, 2022 13:51
src/loaders.ts Outdated Show resolved Hide resolved
@karlhorky
Copy link

karlhorky commented Oct 9, 2022

Just taking this part out of my comment above, to make it visible to anyone here:

See this Replit with tsc: https://replit.com/@karlhorky/TypeScript-tsc-extension-resolution-js-jsx-cjs-mjs#index.ts

All of these extensions work:

Screen Shot 2022-10-09 at 16 51 13

@karlhorky
Copy link

I also made a Replit with [email protected] on its own (no tsx or tsc) to test the resolution of all extensions (using --bundle mode), and it seems that they are all respected:

https://replit.com/@karlhorky/esbuild-extension-resolution-js-jsx-cjs-mjs#index.ts

Screen Shot 2022-10-09 at 18 16 41

@mshima mshima force-pushed the patch-1 branch 4 times, most recently from 73f899d to 0634f90 Compare October 9, 2022 20:39
@mshima
Copy link
Author

mshima commented Oct 13, 2022

@privatenumber would be really nice to have this feature.
We are having trouble to import a ts (commonjs) file from a mjs file. mjs files requires the extension, which breaks esm-loader.

@datner
Copy link

datner commented Jun 1, 2023

If I understand correctly, following down the line, merging this PR will allow in turn vite to correctly resolve the following:

// ComponentA.tsx
export const ComponentA = () => <div />

// ComponentB.tsx
import { ComponentA } from './ComponentA.js'

export const ComponentB = () => <ComponentA />

Right? Either way, how can we move this PR forward? I'm willing to help

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

Successfully merging this pull request may close these issues.

Importing a typescript file from a js file using extension fails.
4 participants