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

fix: TypeScript file detection #56

Draft
wants to merge 39 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1df22cf
wip
privatenumber Feb 8, 2023
82a55f5
wip
privatenumber Feb 12, 2023
12625ec
wip
privatenumber Feb 12, 2023
b262062
wip
privatenumber Feb 12, 2023
948c9fb
wip
privatenumber Feb 12, 2023
de68642
wip
privatenumber Feb 12, 2023
bac43c9
wip
privatenumber Feb 12, 2023
31311d3
wip
privatenumber Feb 12, 2023
292ae99
wip
privatenumber Feb 12, 2023
8383d7c
Merge branch 'develop' of github.com:esbuild-kit/esm-loader into impr…
privatenumber Feb 13, 2023
18f564f
Merge branch 'develop' into improve-ts-detect
privatenumber Jul 17, 2023
02e715a
Merge branch 'develop' of github.com:esbuild-kit/esm-loader into impr…
privatenumber Jul 17, 2023
cfb1a3f
wip
privatenumber Jul 17, 2023
5bff63b
wip
privatenumber Jul 17, 2023
0791bde
wip
privatenumber Jul 17, 2023
5cc0a47
wip
privatenumber Jul 17, 2023
f504e63
wip
privatenumber Jul 17, 2023
e368abb
wip
privatenumber Jul 17, 2023
2c44903
wip
privatenumber Jul 18, 2023
8194b5e
wip
privatenumber Jul 18, 2023
b2f335d
wip
privatenumber Jul 18, 2023
6bbc406
wip
privatenumber Jul 18, 2023
a89cf29
wip
privatenumber Jul 18, 2023
1a2c42c
wip
privatenumber Jul 18, 2023
67fb65c
wip
privatenumber Jul 18, 2023
b77ba28
wip
privatenumber Jul 18, 2023
ffd4662
wip
privatenumber Jul 18, 2023
68c4083
wip
privatenumber Jul 18, 2023
acdd2f0
wip
privatenumber Jul 18, 2023
74876c8
wip
privatenumber Jul 18, 2023
7153606
wip
privatenumber Jul 18, 2023
11d988b
wip
privatenumber Jul 18, 2023
cc1d61c
Merge branch 'develop' into improve-ts-detect
privatenumber Aug 27, 2023
8140d85
Merge branch 'develop' into improve-ts-detect
privatenumber Aug 27, 2023
0692321
cleanup fixtures
privatenumber Aug 28, 2023
f078eed
Merge branch 'develop' into improve-ts-detect
privatenumber Aug 28, 2023
99a7d25
wip
privatenumber Aug 28, 2023
bb64eef
Merge branch 'develop' into improve-ts-detect
privatenumber Aug 28, 2023
261f177
Merge branch 'develop' into improve-ts-detect
privatenumber Sep 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/loaders-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const _transformSource: transformSource = async function (

if (
url.endsWith('.json')
|| url.endsWith('.jsx')
|| tsExtensionsPattern.test(url)
) {
const transformed = await transform(
Expand Down
9 changes: 8 additions & 1 deletion src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ export const resolve: resolve = async function (
/**
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
*/
if (tsExtensionsPattern.test(context.parentURL!)) {
if (
context.parentURL
&& (
tsExtensionsPattern.test(context.parentURL)
|| fileMatcher?.(fileURLToPath(context.parentURL))
)
) {
const tsPath = resolveTsPath(specifier);

if (tsPath) {
Expand Down Expand Up @@ -219,6 +225,7 @@ export const load: LoadHook = async function (

if (
loaded.format === 'json'
|| url.endsWith('.jsx')
|| tsExtensionsPattern.test(url)
) {
const transformed = await transform(
Expand Down
20 changes: 16 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ const tsconfig = (
path: path.resolve(process.env.ESBK_TSCONFIG_PATH),
config: parseTsconfig(process.env.ESBK_TSCONFIG_PATH),
}
: getTsconfig()
: (
getTsconfig()
?? {
path: process.cwd(),
config: {},
}
)
);

export const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);

export const fileProtocol = 'file://';

export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)$/;
export const tsExtensionsPattern = /\.([cm]?ts|tsx)$/;

const getFormatFromExtension = (fileUrl: string): ModuleFormat | undefined => {
const extension = path.extname(fileUrl);
Expand All @@ -43,15 +49,21 @@ const getFormatFromExtension = (fileUrl: string): ModuleFormat | undefined => {
}
};

export const knownExtensions = /\.[tj]sx?$/;

export const getFormatFromFileUrl = (fileUrl: string) => {
const format = getFormatFromExtension(fileUrl);

if (format) {
return format;
}

// ts, tsx, jsx
if (tsExtensionsPattern.test(fileUrl)) {
/**
* Since this is only called when Node.js can't figure
* out the type, we only need to implement it for the
* extensions it can't handle (e.g. ts, tsx, jsx)
*/
if (knownExtensions.test(fileUrl)) {
return getPackageType(fileUrl);
}
};
Expand Down
22 changes: 19 additions & 3 deletions tests/fixtures/package-module/lib/cjs-ext-cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,25 @@ test(
'sourcemaps',
() => {
const { stack } = new Error();
const pathIndex = stack.indexOf(__filename + ':33:');
const previousCharacter = stack[pathIndex - 1];
return pathIndex > -1 && previousCharacter !== ':';
const errorPosition = ':33:';
const isWindows = process.platform === 'win32';
let pathname = __filename;
if (isWindows) {
// Remove drive letter
pathname = pathname.slice(2);
}

let pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
if (
pathIndex === -1
&& isWindows
) {
// Convert backslash to slash
pathname = pathname.replace(/\\/g, '/');
pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
}

return pathIndex > -1;
},
);

Expand Down
27 changes: 19 additions & 8 deletions tests/fixtures/package-module/lib/cjs-ext-js/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fileURLToPath } from 'node:url';

async function test(description, testFunction) {
try {
const result = await testFunction();
Expand Down Expand Up @@ -31,16 +33,25 @@ test(
'sourcemaps',
() => {
const { stack } = new Error();
let { pathname } = new URL(import.meta.url);
if (process.platform === 'win32') {
pathname = pathname.slice(1);
const errorPosition = ':35:';
const isWindows = process.platform === 'win32';
let pathname = fileURLToPath(import.meta.url);
if (isWindows) {
// Remove drive letter
pathname = pathname.slice(2);
}
let pathIndex = stack.indexOf(`${pathname}:33:`);
if (pathIndex === -1) {
pathIndex = stack.indexOf(`${pathname.toLowerCase()}:33:`);

let pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
if (
pathIndex === -1
&& isWindows
) {
// Convert backslash to slash
pathname = pathname.replace(/\\/g, '/');
pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
}
const previousCharacter = stack[pathIndex - 1];
return pathIndex > -1 && previousCharacter !== ':';

return pathIndex > -1;
},
);

Expand Down
27 changes: 19 additions & 8 deletions tests/fixtures/package-module/lib/esm-ext-js/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fileURLToPath } from 'node:url';

async function test(description, testFunction) {
try {
const result = await testFunction();
Expand Down Expand Up @@ -31,16 +33,25 @@ test(
'sourcemaps',
() => {
const { stack } = new Error();
let { pathname } = new URL(import.meta.url);
if (process.platform === 'win32') {
pathname = pathname.slice(1);
const errorPosition = ':35:';
const isWindows = process.platform === 'win32';
let pathname = fileURLToPath(import.meta.url);
if (isWindows) {
// Remove drive letter
pathname = pathname.slice(2);
}
let pathIndex = stack.indexOf(`${pathname}:33:`);
if (pathIndex === -1) {
pathIndex = stack.indexOf(`${pathname.toLowerCase()}:33:`);

let pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
if (
pathIndex === -1
&& isWindows
) {
// Convert backslash to slash
pathname = pathname.replace(/\\/g, '/');
pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
}
const previousCharacter = stack[pathIndex - 1];
return pathIndex > -1 && previousCharacter !== ':';

return pathIndex > -1;
},
);

Expand Down
27 changes: 19 additions & 8 deletions tests/fixtures/package-module/lib/esm-ext-mjs/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fileURLToPath } from 'node:url';

async function test(description, testFunction) {
try {
const result = await testFunction();
Expand Down Expand Up @@ -31,16 +33,25 @@ test(
'sourcemaps',
() => {
const { stack } = new Error();
let { pathname } = new URL(import.meta.url);
if (process.platform === 'win32') {
pathname = pathname.slice(1);
const errorPosition = ':35:';
const isWindows = process.platform === 'win32';
let pathname = fileURLToPath(import.meta.url);
if (isWindows) {
// Remove drive letter
pathname = pathname.slice(2);
}
let pathIndex = stack.indexOf(pathname + ':33:');
if (pathIndex === -1) {
pathIndex = stack.indexOf(pathname.toLowerCase() + ':33:');

let pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
if (
pathIndex === -1
&& isWindows
) {
// Convert backslash to slash
pathname = pathname.replace(/\\/g, '/');
pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
}
const previousCharacter = stack[pathIndex - 1];
return pathIndex > -1 && previousCharacter !== ':';

return pathIndex > -1;
},
);

Expand Down
24 changes: 21 additions & 3 deletions tests/fixtures/package-module/lib/ts-ext-cts/index.cts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { fileURLToPath } = require('node:url');

async function test(description: string, testFunction: () => any | Promise<any>) {
try {
const result = await testFunction();
Expand Down Expand Up @@ -31,9 +33,25 @@ test(
'sourcemaps',
() => {
const stack = (new Error()).stack!;
const pathIndex = stack.indexOf(`${__filename}:33:`);
const previousCharacter = stack[pathIndex - 1];
return pathIndex > -1 && previousCharacter !== ':';
const errorPosition = ':35:';
const isWindows = process.platform === 'win32';
let pathname = fileURLToPath(import.meta.url);
if (isWindows) {
// Remove drive letter
pathname = pathname.slice(2);
}

let pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
if (
pathIndex === -1
&& isWindows
) {
// Convert backslash to slash
pathname = pathname.replace(/\\/g, '/');
pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
}

return pathIndex > -1;
},
);

Expand Down
28 changes: 17 additions & 11 deletions tests/fixtures/package-module/lib/ts-ext-jsx/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,25 @@ test(
'sourcemaps',
() => {
const { stack } = new Error();
let { pathname } = new URL(import.meta.url);
if (process.platform === 'win32') {
pathname = pathname.slice(1);
const errorPosition = ':35:';
const isWindows = process.platform === 'win32';
let pathname = fileURLToPath(import.meta.url);
if (isWindows) {
// Remove drive letter
pathname = pathname.slice(2);
}
let pathIndex = stack.indexOf(`${pathname}:35:`);
if (pathIndex === -1) {
pathIndex = stack.indexOf(`${pathname.toLowerCase()}:35:`);
}
if (pathIndex === -1) {
pathIndex = stack.indexOf(`${fileURLToPath(import.meta.url).toLowerCase()}:35:`);

let pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
if (
pathIndex === -1
&& isWindows
) {
// Convert backslash to slash
pathname = pathname.replace(/\\/g, '/');
pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
}
const previousCharacter = stack[pathIndex - 1];
return pathIndex > -1 && previousCharacter !== ':';

return pathIndex > -1;
},
);

Expand Down
28 changes: 17 additions & 11 deletions tests/fixtures/package-module/lib/ts-ext-mts/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,25 @@ test(
'sourcemaps',
() => {
const stack = (new Error()).stack!;
let { pathname } = new URL(import.meta.url);
if (process.platform === 'win32') {
pathname = pathname.slice(1);
const errorPosition = ':35:';
const isWindows = process.platform === 'win32';
let pathname = fileURLToPath(import.meta.url);
if (isWindows) {
// Remove drive letter
pathname = pathname.slice(2);
}
let pathIndex = stack.indexOf(`${pathname}:35:`);
if (pathIndex === -1) {
pathIndex = stack.indexOf(`${pathname.toLowerCase()}:35:`);
}
if (pathIndex === -1) {
pathIndex = stack.indexOf(`${fileURLToPath(import.meta.url).toLowerCase()}:35:`);

let pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
if (
pathIndex === -1
&& isWindows
) {
// Convert backslash to slash
pathname = pathname.replace(/\\/g, '/');
pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
}
const previousCharacter = stack[pathIndex - 1];
return pathIndex > -1 && previousCharacter !== ':';

return pathIndex > -1;
},
);

Expand Down
28 changes: 17 additions & 11 deletions tests/fixtures/package-module/lib/ts-ext-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,25 @@ test(
'sourcemaps',
() => {
const stack = (new Error()).stack!;
let { pathname } = new URL(import.meta.url);
if (process.platform === 'win32') {
pathname = pathname.slice(1);
const errorPosition = ':35:';
const isWindows = process.platform === 'win32';
let pathname = fileURLToPath(import.meta.url);
if (isWindows) {
// Remove drive letter
pathname = pathname.slice(2);
}
let pathIndex = stack.indexOf(`${pathname}:35:`);
if (pathIndex === -1) {
pathIndex = stack.indexOf(`${pathname.toLowerCase()}:35:`);
}
if (pathIndex === -1) {
pathIndex = stack.indexOf(`${fileURLToPath(import.meta.url).toLowerCase()}:35:`);

let pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
if (
pathIndex === -1
&& isWindows
) {
// Convert backslash to slash
pathname = pathname.replace(/\\/g, '/');
pathIndex = stack.indexOf(`${pathname}${errorPosition}`);
}
const previousCharacter = stack[pathIndex - 1];
return pathIndex > -1 && previousCharacter !== ':';

return pathIndex > -1;
},
);

Expand Down
Loading