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

Commit

Permalink
feat: upgrade core-utils (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Oct 7, 2022
1 parent d516785 commit e7ba7ca
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 63 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
installSourceMapSupport,
resolveTsPath,
transformDynamicImport,
applySourceMap,
compareNodeVersion,
} from '@esbuild-kit/core-utils';
import {
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -75,7 +74,7 @@ function transformer(
},
);

code = applySourceMap(transformed, filePath, sourcemaps);
code = applySourceMap(transformed, filePath);
}

module._compile(code, filePath);
Expand Down
9 changes: 8 additions & 1 deletion tests/fixtures/lib/cjs-ext-cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/lib/cjs-ext-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/lib/esm-ext-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion tests/fixtures/lib/esm-ext-mjs/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/lib/ts-ext-cts/index.cts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/lib/ts-ext-jsx/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/lib/ts-ext-mts/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
60 changes: 33 additions & 27 deletions tests/fixtures/lib/ts-ext-ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,61 @@
async function test(description: string, testFunction: () => any | Promise<any>) {
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(
Expand Down
40 changes: 23 additions & 17 deletions tests/fixtures/lib/ts-ext-ts/index.tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,59 @@ async function test(description: string, testFunction: () => any | Promise<any>)
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(
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/lib/ts-ext-tsx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/tsconfig/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('Should not run');
console.log('Should not run');

0 comments on commit e7ba7ca

Please sign in to comment.