Skip to content

Commit b54131c

Browse files
refactor: reuse tsconfig util
1 parent eeaefd6 commit b54131c

File tree

16 files changed

+208
-207
lines changed

16 files changed

+208
-207
lines changed

src/cjs/api/module-extensions.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import fs from 'node:fs';
22
import Module from 'node:module';
3-
import { createFilesMatcher } from 'get-tsconfig';
43
import type { TransformOptions } from 'esbuild';
54
import { transformSync } from '../../utils/transform/index.js';
65
import { transformDynamicImport } from '../../utils/transform/transform-dynamic-import.js';
76
import { isESM } from '../../utils/esm-pattern.js';
87
import { shouldApplySourceMap, inlineSourceMap } from '../../source-map.js';
98
import { parent } from '../../utils/ipc/client.js';
10-
import { tsconfig } from './utils.js';
9+
import { fileMatcher } from '../../utils/tsconfig.js';
1110

1211
const typescriptExtensions = [
1312
'.cts',
@@ -23,8 +22,6 @@ const transformExtensions = [
2322
'.mjs',
2423
];
2524

26-
const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
27-
2825
// Clone Module._extensions with null prototype
2926
export const extensions = Object.assign(Object.create(null), Module._extensions);
3027

src/cjs/api/module-resolve-filename.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
import path from 'node:path';
22
import Module from 'node:module';
33
import { fileURLToPath } from 'node:url';
4-
import { createPathsMatcher } from 'get-tsconfig';
54
import { resolveTsPath } from '../../utils/resolve-ts-path.js';
65
import type { NodeError } from '../../types.js';
7-
import { isRelativePath } from '../../utils/path-utils.js';
8-
import { fileUrlPrefix } from '../../utils/file-url.js';
9-
import {
10-
isTsFilePatten,
11-
tsconfig,
12-
} from './utils.js';
6+
import { isRelativePath, fileUrlPrefix, tsExtensionsPattern } from '../../utils/path-utils.js';
7+
import { tsconfigPathsMatcher, allowJs } from '../../utils/tsconfig.js';
138

149
const nodeModulesPath = `${path.sep}node_modules${path.sep}`;
1510

16-
const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
17-
1811
type ResolveFilename = typeof Module._resolveFilename;
1912

2013
const defaultResolver = Module._resolveFilename.bind(Module);
@@ -33,8 +26,8 @@ const resolveTsFilename = (
3326
if (
3427
parent?.filename
3528
&& (
36-
isTsFilePatten.test(parent.filename)
37-
|| tsconfig?.config.compilerOptions?.allowJs
29+
tsExtensionsPattern.test(parent.filename)
30+
|| allowJs
3831
)
3932
&& tsPath
4033
) {

src/cjs/api/utils.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/esm/hook/load.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ import { inlineSourceMap } from '../../source-map.js';
77
import { isFeatureSupported, importAttributes } from '../../utils/node-features.js';
88
import { parent } from '../../utils/ipc/client.js';
99
import type { Message } from '../types.js';
10-
import {
11-
fileMatcher,
12-
tsExtensionsPattern,
13-
isJsonPattern,
14-
getNamespace,
15-
} from './utils.js';
10+
import { fileMatcher } from '../../utils/tsconfig.js';
11+
import { isJsonPattern, tsExtensionsPattern } from '../../utils/path-utils.js';
12+
import { getNamespace } from './utils.js';
1613
import { data } from './initialize.js';
1714

1815
const contextAttributesProperty = (

src/esm/hook/resolve.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ import type {
55
} from 'node:module';
66
import { resolveTsPath } from '../../utils/resolve-ts-path.js';
77
import type { NodeError } from '../../types.js';
8-
import { requestAcceptsQuery } from '../../utils/path-utils.js';
9-
import { fileUrlPrefix } from '../../utils/file-url.js';
8+
import { tsconfigPathsMatcher, allowJs } from '../../utils/tsconfig.js';
109
import {
11-
tsconfigPathsMatcher,
10+
requestAcceptsQuery,
11+
fileUrlPrefix,
1212
tsExtensionsPattern,
13+
isDirectoryPattern,
14+
} from '../../utils/path-utils.js';
15+
import {
1316
getFormatFromFileUrl,
14-
allowJs,
1517
namespaceQuery,
1618
getNamespace,
1719
type MaybePromise,
1820
} from './utils.js';
1921
import { data } from './initialize.js';
2022

21-
const isDirectoryPattern = /\/(?:$|\?)/;
22-
2323
type NextResolve = (
2424
specifier: string,
2525
context?: ResolveHookContext,

src/esm/hook/utils.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
11
import path from 'node:path';
22
import type { ModuleFormat } from 'node:module';
3-
import {
4-
getTsconfig,
5-
parseTsconfig,
6-
createPathsMatcher,
7-
createFilesMatcher,
8-
} from 'get-tsconfig';
3+
import { tsExtensionsPattern } from '../../utils/path-utils.js';
94
import { getPackageType } from './package-json.js';
105

11-
const tsconfig = (
12-
process.env.TSX_TSCONFIG_PATH
13-
? {
14-
path: path.resolve(process.env.TSX_TSCONFIG_PATH),
15-
config: parseTsconfig(process.env.TSX_TSCONFIG_PATH),
16-
}
17-
: getTsconfig()
18-
);
19-
20-
export const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
21-
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
22-
export const allowJs = tsconfig?.config.compilerOptions?.allowJs ?? false;
23-
24-
export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)($|\?)/;
25-
26-
export const isJsonPattern = /\.json(?:$|\?)/;
27-
286
const getFormatFromExtension = (fileUrl: string): ModuleFormat | undefined => {
29-
const extension = path.extname(fileUrl.split('?')[0]);
30-
7+
const queryIndex = fileUrl.indexOf('?');
8+
const extension = path.extname(
9+
queryIndex === -1
10+
? fileUrl
11+
: fileUrl.slice(0, queryIndex),
12+
);
3113
if (extension === '.json') {
3214
return 'json';
3315
}

src/utils/esm-pattern.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ const esmPattern = /\b(?:import|export)\b/;
2121
*/
2222

2323
export const isESM = (code: string) => {
24-
if (code.includes('import') || code.includes('export')) {
25-
try {
26-
const hasModuleSyntax = parseEsm(code)[3];
27-
return hasModuleSyntax;
28-
} catch {
29-
/**
30-
* If it fails to parse, there's a syntax error
31-
* Let esbuild handle it for better error messages
32-
*/
33-
return true;
34-
}
24+
if (!code.includes('import') && !code.includes('export')) {
25+
return false;
26+
}
27+
try {
28+
const hasModuleSyntax = parseEsm(code)[3];
29+
return hasModuleSyntax;
30+
} catch {
31+
/**
32+
* If it fails to parse, there's a syntax error
33+
* Let esbuild handle it for better error messages
34+
*/
35+
return true;
3536
}
36-
return false;
3737
};

src/utils/file-url.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/utils/path-utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ export const requestAcceptsQuery = (request: string) => {
4343
&& scheme !== 'node'
4444
);
4545
};
46+
47+
export const fileUrlPrefix = 'file://';
48+
49+
export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)($|\?)/;
50+
51+
export const isJsonPattern = /\.json($|\?)/;
52+
53+
export const isDirectoryPattern = /\/(?:$|\?)/;

src/utils/tsconfig.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import path from 'node:path';
2+
import {
3+
getTsconfig,
4+
parseTsconfig,
5+
createFilesMatcher,
6+
createPathsMatcher,
7+
} from 'get-tsconfig';
8+
9+
const tsconfig = (
10+
process.env.TSX_TSCONFIG_PATH
11+
? {
12+
path: path.resolve(process.env.TSX_TSCONFIG_PATH),
13+
config: parseTsconfig(process.env.TSX_TSCONFIG_PATH),
14+
}
15+
: getTsconfig()
16+
);
17+
18+
export const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
19+
20+
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
21+
22+
export const allowJs = tsconfig?.config.compilerOptions?.allowJs ?? false;

tests/fixtures.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import type { PackageJson, TsConfigJson } from 'type-fest';
2+
3+
export const createPackageJson = (packageJson: PackageJson) => JSON.stringify(packageJson);
4+
5+
export const createTsconfig = (tsconfig: TsConfigJson) => JSON.stringify(tsconfig);
6+
17
const cjsContextCheck = 'typeof module !== \'undefined\'';
28
const tsCheck = '1 as number';
39

@@ -85,8 +91,8 @@ const sourcemap = {
8591
};
8692

8793
export const expectErrors = {
88-
'expect-errors.js': `
89-
export const expectErrors = async (...assertions) => {
94+
'node_modules/expect-errors/index.js': `
95+
exports.expectErrors = async (...assertions) => {
9096
let errors = await Promise.all(
9197
assertions.map(async ([fn, expectedError]) => {
9298
let thrown;
@@ -227,13 +233,13 @@ export const files = {
227233

228234
node_modules: {
229235
'pkg-commonjs': {
230-
'package.json': JSON.stringify({
236+
'package.json': createPackageJson({
231237
type: 'commonjs',
232238
}),
233239
'index.js': syntaxLowering,
234240
},
235241
'pkg-module': {
236-
'package.json': JSON.stringify({
242+
'package.json': createPackageJson({
237243
type: 'module',
238244
main: './index.js',
239245
}),

tests/specs/api.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
tsxEsmApiCjsPath,
1010
type NodeApis,
1111
} from '../utils/tsx.js';
12+
import { createPackageJson } from '../fixtures.js';
1213

1314
const tsFiles = {
1415
'file.ts': `
@@ -21,7 +22,7 @@ const tsFiles = {
2122
`,
2223
'bar.ts': 'export type A = 1; export { bar } from "pkg"',
2324
'node_modules/pkg': {
24-
'package.json': JSON.stringify({
25+
'package.json': createPackageJson({
2526
name: 'pkg',
2627
type: 'module',
2728
exports: './index.js',
@@ -144,7 +145,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
144145
describe('module', ({ describe, test }) => {
145146
test('cli', async () => {
146147
await using fixture = await createFixture({
147-
'package.json': JSON.stringify({ type: 'module' }),
148+
'package.json': createPackageJson({ type: 'module' }),
148149
'index.ts': 'import { message } from \'./file\';\n\nconsole.log(message, new Error().stack);',
149150
...tsFiles,
150151
});
@@ -160,7 +161,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
160161
if (node.supports.moduleRegister) {
161162
test('module.register', async () => {
162163
await using fixture = await createFixture({
163-
'package.json': JSON.stringify({ type: 'module' }),
164+
'package.json': createPackageJson({ type: 'module' }),
164165
'module-register.mjs': `
165166
import { register } from 'node:module';
166167
@@ -190,7 +191,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
190191
describe('register / unregister', ({ test }) => {
191192
test('register / unregister', async () => {
192193
await using fixture = await createFixture({
193-
'package.json': JSON.stringify({ type: 'module' }),
194+
'package.json': createPackageJson({ type: 'module' }),
194195
'register.mjs': `
195196
import { register } from ${JSON.stringify(tsxEsmApiPath)};
196197
try {
@@ -235,7 +236,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
235236

236237
test('onImport', async () => {
237238
await using fixture = await createFixture({
238-
'package.json': JSON.stringify({ type: 'module' }),
239+
'package.json': createPackageJson({ type: 'module' }),
239240
'register.mjs': `
240241
import { register } from ${JSON.stringify(tsxEsmApiPath)};
241242
@@ -259,7 +260,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
259260

260261
test('namespace & onImport', async () => {
261262
await using fixture = await createFixture({
262-
'package.json': JSON.stringify({ type: 'module' }),
263+
'package.json': createPackageJson({ type: 'module' }),
263264
'register.mjs': `
264265
import { setTimeout } from 'node:timers/promises';
265266
import { register } from ${JSON.stringify(tsxEsmApiPath)};
@@ -290,7 +291,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
290291
describe('tsImport()', ({ test }) => {
291292
test('module', async () => {
292293
await using fixture = await createFixture({
293-
'package.json': JSON.stringify({ type: 'module' }),
294+
'package.json': createPackageJson({ type: 'module' }),
294295
'import.mjs': `
295296
import { tsImport } from ${JSON.stringify(tsxEsmApiPath)};
296297
@@ -321,7 +322,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
321322

322323
test('commonjs', async () => {
323324
await using fixture = await createFixture({
324-
'package.json': JSON.stringify({ type: 'module' }),
325+
'package.json': createPackageJson({ type: 'module' }),
325326
'import.cjs': `
326327
const { tsImport } = require(${JSON.stringify(tsxEsmApiCjsPath)});
327328
@@ -374,7 +375,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
374375

375376
test('namespace allows async nested calls', async () => {
376377
await using fixture = await createFixture({
377-
'package.json': JSON.stringify({ type: 'module' }),
378+
'package.json': createPackageJson({ type: 'module' }),
378379
'import.mjs': `
379380
import { tsImport } from ${JSON.stringify(tsxEsmApiPath)};
380381
tsImport('./file.ts', import.meta.url);
@@ -393,7 +394,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
393394

394395
test('onImport & doesnt cache files', async () => {
395396
await using fixture = await createFixture({
396-
'package.json': JSON.stringify({ type: 'module' }),
397+
'package.json': createPackageJson({ type: 'module' }),
397398
'import.mjs': `
398399
import { setTimeout } from 'node:timers/promises';
399400
import { tsImport } from ${JSON.stringify(tsxEsmApiPath)};
@@ -439,7 +440,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
439440
} else {
440441
test('no module.register error', async () => {
441442
await using fixture = await createFixture({
442-
'package.json': JSON.stringify({ type: 'module' }),
443+
'package.json': createPackageJson({ type: 'module' }),
443444
'register.mjs': `
444445
import { register } from ${JSON.stringify(tsxEsmApiPath)};
445446

tests/specs/loaders.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { testSuite, expect } from 'manten';
22
import { createFixture } from 'fs-fixture';
33
import type { NodeApis } from '../utils/tsx.js';
4+
import { createPackageJson } from '../fixtures.js';
45

56
export default testSuite(({ describe }, node: NodeApis) => {
67
describe('Loaders', ({ describe }) => {
78
describe('Hooks', async ({ test }) => {
89
const fixture = await createFixture({
9-
'package.json': JSON.stringify({ type: 'module' }),
10+
'package.json': createPackageJson({ type: 'module' }),
1011

1112
'ts.ts': `
1213
import fs from 'node:fs';
@@ -50,7 +51,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
5051

5152
describe('CJS patching', async ({ test }) => {
5253
const fixture = await createFixture({
53-
'package.json': JSON.stringify({ type: 'commonjs' }),
54+
'package.json': createPackageJson({ type: 'commonjs' }),
5455

5556
'ts.ts': `
5657
import fs from 'node:fs';

0 commit comments

Comments
 (0)