diff --git a/package.json b/package.json index 8e40640..9c581e3 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "prepack": "pnpm build && clean-pkg-json" }, "dependencies": { - "@esbuild-kit/core-utils": "^2.3.2", + "@esbuild-kit/core-utils": "^3.0.0", "get-tsconfig": "^4.2.0" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8f6a971..2ad6384 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,7 +1,7 @@ lockfileVersion: 5.4 specifiers: - '@esbuild-kit/core-utils': ^2.3.2 + '@esbuild-kit/core-utils': ^3.0.0 '@pvtnbr/eslint-config': ^0.30.1 '@types/node': ^18.7.23 '@types/semver': ^7.3.12 @@ -18,7 +18,7 @@ specifiers: typescript: ^4.8.4 dependencies: - '@esbuild-kit/core-utils': 2.3.2 + '@esbuild-kit/core-utils': 3.0.0 get-tsconfig: 4.2.0 devDependencies: @@ -59,8 +59,8 @@ packages: js-tokens: 4.0.0 dev: true - /@esbuild-kit/core-utils/2.3.2: - resolution: {integrity: sha512-aQwy1Hdd02ymjyMyyrhtyuZGv5W+mVZmj3DTKFV0TnB1AUgMBV40tXySpsGySe8vLvSe0c0TaqTc2FUo8/YlNQ==} + /@esbuild-kit/core-utils/3.0.0: + resolution: {integrity: sha512-TXmwH9EFS3DC2sI2YJWJBgHGhlteK0Xyu1VabwetMULfm3oYhbrsWV5yaSr2NTWZIgDGVLHbRf0inxbjXqAcmQ==} dependencies: esbuild: 0.15.10 source-map-support: 0.5.21 diff --git a/src/index.ts b/src/index.ts index ef3c79e..7f72ab4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,6 @@ import { installSourceMapSupport, resolveTsPath, transformDynamicImport, - applySourceMap, compareNodeVersion, } from '@esbuild-kit/core-utils'; import { @@ -32,7 +31,7 @@ const tsconfig = ( const tsconfigRaw = tsconfig?.config; const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig); -const sourcemaps = installSourceMapSupport(); +const applySourceMap = installSourceMapSupport(); const nodeSupportsImport = ( // v13.2.0 and higher @@ -62,9 +61,9 @@ function transformer( let code = fs.readFileSync(filePath, 'utf8'); if (filePath.endsWith('.cjs') && nodeSupportsImport) { - const transformed = transformDynamicImport(code); + const transformed = transformDynamicImport(filePath, code); if (transformed) { - code = applySourceMap(transformed, filePath, sourcemaps); + code = applySourceMap(transformed, filePath); } } else { const transformed = transformSync( @@ -75,7 +74,7 @@ function transformer( }, ); - code = applySourceMap(transformed, filePath, sourcemaps); + code = applySourceMap(transformed, filePath); } module._compile(code, filePath); diff --git a/tests/fixtures/lib/cjs-ext-cjs/index.cjs b/tests/fixtures/lib/cjs-ext-cjs/index.cjs index 2615e52..bb6b143 100644 --- a/tests/fixtures/lib/cjs-ext-cjs/index.cjs +++ b/tests/fixtures/lib/cjs-ext-cjs/index.cjs @@ -35,7 +35,14 @@ test( test( 'sourcemaps', - () => new Error().stack.includes(':38:'), + () => { + const { stack } = new Error(); + return ( + stack.includes(__filename + ':39:') + // TODO: Investigate why converting slashes is only needed for .cjs + || stack.includes(__filename.toLowerCase().replace(/\\/g, '/') + ':39:') + ); + }, ); test( diff --git a/tests/fixtures/lib/cjs-ext-js/index.js b/tests/fixtures/lib/cjs-ext-js/index.js index b247106..928c180 100644 --- a/tests/fixtures/lib/cjs-ext-js/index.js +++ b/tests/fixtures/lib/cjs-ext-js/index.js @@ -35,7 +35,13 @@ test( test( 'sourcemaps', - () => new Error().stack.includes(':38:'), + () => { + const { stack } = new Error(); + return ( + stack.includes(`${__filename}:39:`) + || stack.includes(`${__filename.toLowerCase()}:39:`) + ); + }, ); test( diff --git a/tests/fixtures/lib/esm-ext-js/index.js b/tests/fixtures/lib/esm-ext-js/index.js index 13a88c7..1d04ec6 100644 --- a/tests/fixtures/lib/esm-ext-js/index.js +++ b/tests/fixtures/lib/esm-ext-js/index.js @@ -34,7 +34,13 @@ test( test( 'sourcemaps', - () => new Error().stack.includes(':37:'), + () => { + const { stack } = new Error(); + return ( + stack.includes(`${__filename}:38:`) + || stack.includes(`${__filename.toLowerCase()}:38:`) + ); + }, ); test( diff --git a/tests/fixtures/lib/esm-ext-mjs/index.mjs b/tests/fixtures/lib/esm-ext-mjs/index.mjs index e2318d5..e0d37e9 100644 --- a/tests/fixtures/lib/esm-ext-mjs/index.mjs +++ b/tests/fixtures/lib/esm-ext-mjs/index.mjs @@ -34,7 +34,14 @@ test( test( 'sourcemaps', - () => new Error().stack.includes(':37:'), + () => { + const { stack } = new Error(); + const filePath = (typeof __filename === 'string') ? __filename : import.meta.url; + return ( + stack.includes(filePath + ':38:') + || stack.includes(filePath.toLowerCase() + ':38:') + ); + }, ); test( diff --git a/tests/fixtures/lib/ts-ext-cts/index.cts b/tests/fixtures/lib/ts-ext-cts/index.cts index 22aa437..18c0a8f 100644 --- a/tests/fixtures/lib/ts-ext-cts/index.cts +++ b/tests/fixtures/lib/ts-ext-cts/index.cts @@ -34,7 +34,13 @@ test( test( 'sourcemaps', - () => new Error().stack!.includes(':37:'), + () => { + const { stack } = new Error(); + return ( + stack!.includes(`${__filename}:38:`) + || stack!.includes(`${__filename.toLowerCase()}:38:`) + ); + }, ); test( diff --git a/tests/fixtures/lib/ts-ext-jsx/index.jsx b/tests/fixtures/lib/ts-ext-jsx/index.jsx index 3db8acc..da67288 100644 --- a/tests/fixtures/lib/ts-ext-jsx/index.jsx +++ b/tests/fixtures/lib/ts-ext-jsx/index.jsx @@ -34,7 +34,13 @@ test( test( 'sourcemaps', - () => new Error().stack.includes(':37:'), + () => { + const { stack } = new Error(); + return ( + stack.includes(`${__filename}:38:`) + || stack.includes(`${__filename.toLowerCase()}:38:`) + ); + }, ); test( diff --git a/tests/fixtures/lib/ts-ext-mts/index.mts b/tests/fixtures/lib/ts-ext-mts/index.mts index 1963784..6d32b18 100644 --- a/tests/fixtures/lib/ts-ext-mts/index.mts +++ b/tests/fixtures/lib/ts-ext-mts/index.mts @@ -34,7 +34,13 @@ test( test( 'sourcemaps', - () => new Error().stack!.includes(':37:'), + () => { + const { stack } = new Error(); + return ( + stack!.includes(`${__filename}:38:`) + || stack!.includes(`${__filename.toLowerCase()}:38:`) + ); + }, ); test( diff --git a/tests/fixtures/lib/ts-ext-ts/index.ts b/tests/fixtures/lib/ts-ext-ts/index.ts index 2606e9d..a84bd44 100644 --- a/tests/fixtures/lib/ts-ext-ts/index.ts +++ b/tests/fixtures/lib/ts-ext-ts/index.ts @@ -1,55 +1,61 @@ async function test(description: string, testFunction: () => any | Promise) { try { - const result = await testFunction(); - if (!result) { throw result; } - console.log(`✔ ${description}`); - } catch (error) { - console.log(`✖ ${description}: ${error.toString().split('\n').shift()}`); - } + const result = await testFunction(); + if (!result) { throw result; } + console.log(`✔ ${description}`); + } catch (error) { + console.log(`✖ ${description}: ${error.toString().split('\n').shift()}`); + } } console.log('loaded ts-ext-ts/index.ts'); test( - 'has CJS context', - () => typeof require !== 'undefined' || typeof module !== 'undefined', + 'has CJS context', + () => typeof require !== 'undefined' || typeof module !== 'undefined', ); test( - 'import.meta.url', - () => Boolean(import.meta.url), + 'import.meta.url', + () => Boolean(import.meta.url), ); test( - 'name in error', - () => { - let nameInError; - try { - nameInError(); - } catch (error) { - return error.message.includes('nameInError'); - } - }, + 'name in error', + () => { + let nameInError; + try { + nameInError(); + } catch (error) { + return error.message.includes('nameInError'); + } + }, ); test( - 'sourcemaps', - () => new Error().stack!.includes(':37:'), + 'sourcemaps', + () => { + const { stack } = new Error(); + return ( + stack!.includes(`${__filename}:38:`) + || stack!.includes(`${__filename.toLowerCase()}:38:`) + ); + }, ); test( - 'has dynamic import', - () => import('fs').then(Boolean), + 'has dynamic import', + () => import('fs').then(Boolean), ); test( - 'resolves optional node prefix', - () => import('node:fs').then(Boolean), + 'resolves optional node prefix', + () => import('node:fs').then(Boolean), ); test( - 'resolves required node prefix', - () => import('node:test').then(Boolean), + 'resolves required node prefix', + () => import('node:test').then(Boolean), ); test( diff --git a/tests/fixtures/lib/ts-ext-ts/index.tsx.ts b/tests/fixtures/lib/ts-ext-ts/index.tsx.ts index 6279601..aaed560 100644 --- a/tests/fixtures/lib/ts-ext-ts/index.tsx.ts +++ b/tests/fixtures/lib/ts-ext-ts/index.tsx.ts @@ -3,53 +3,59 @@ async function test(description: string, testFunction: () => any | Promise) const result = await testFunction(); if (!result) { throw result; } console.log(`✔ ${description}`); - } catch (error) { + } catch (error) { console.log(`✖ ${description}: ${error.toString().split('\n').shift()}`); - } + } } console.log('loaded ts-ext-ts/index.tsx.ts'); test( - 'has CJS context', - () => typeof require !== 'undefined' || typeof module !== 'undefined', + 'has CJS context', + () => typeof require !== 'undefined' || typeof module !== 'undefined', ); test( - 'import.meta.url', - () => Boolean(import.meta.url), + 'import.meta.url', + () => Boolean(import.meta.url), ); test( - 'name in error', - () => { + 'name in error', + () => { let nameInError; try { nameInError(); } catch (error) { return error.message.includes('nameInError'); } - }, + }, ); test( - 'sourcemaps', - () => new Error().stack!.includes(':37:'), + 'sourcemaps', + () => { + const { stack } = new Error(); + return ( + stack!.includes(`${__filename}:38:`) + || stack!.includes(`${__filename.toLowerCase()}:38:`) + ); + }, ); test( - 'has dynamic import', - () => import('fs').then(Boolean), + 'has dynamic import', + () => import('fs').then(Boolean), ); test( - 'resolves optional node prefix', - () => import('node:fs').then(Boolean), + 'resolves optional node prefix', + () => import('node:fs').then(Boolean), ); test( - 'resolves required node prefix', - () => import('node:test').then(Boolean), + 'resolves required node prefix', + () => import('node:test').then(Boolean), ); test( diff --git a/tests/fixtures/lib/ts-ext-tsx/index.tsx b/tests/fixtures/lib/ts-ext-tsx/index.tsx index c496b24..b7f23cb 100644 --- a/tests/fixtures/lib/ts-ext-tsx/index.tsx +++ b/tests/fixtures/lib/ts-ext-tsx/index.tsx @@ -34,7 +34,13 @@ test( test( 'sourcemaps', - () => new Error().stack!.includes(':37:'), + () => { + const { stack } = new Error(); + return ( + stack!.includes(`${__filename}:38:`) + || stack!.includes(`${__filename.toLowerCase()}:38:`) + ); + }, ); test( diff --git a/tests/fixtures/tsconfig/src/index.ts b/tests/fixtures/tsconfig/src/index.ts index 22c44c5..f9729c6 100644 --- a/tests/fixtures/tsconfig/src/index.ts +++ b/tests/fixtures/tsconfig/src/index.ts @@ -1 +1 @@ -console.log('Should not run'); \ No newline at end of file +console.log('Should not run');