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

Commit 5b374fb

Browse files
fix: support node:test in node 18 (#33)
1 parent 3d07291 commit 5b374fb

File tree

23 files changed

+634
-299
lines changed

23 files changed

+634
-299
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"test": "node --loader @esbuild-kit/esm-loader tests"
3232
},
3333
"dependencies": {
34-
"@esbuild-kit/core-utils": "^2.0.2",
34+
"@esbuild-kit/core-utils": "^2.1.0",
3535
"get-tsconfig": "^4.1.0"
3636
},
3737
"devDependencies": {

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/loaders-deprecated.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
transform,
88
transformDynamicImport,
99
applySourceMap,
10+
compareNodeVersion,
1011
} from '@esbuild-kit/core-utils';
1112
import {
1213
sourcemaps,
@@ -96,14 +97,7 @@ const _transformSource: transformSource = async function (
9697
return result;
9798
};
9899

99-
const loadersDeprecatedVersion = [16, 12, 0];
100-
const nodeVersion = process.version.slice(1).split('.').map(Number);
101-
102-
const nodeSupportsDeprecatedLoaders = (
103-
nodeVersion[0] - loadersDeprecatedVersion[0]
104-
|| nodeVersion[1] - loadersDeprecatedVersion[1]
105-
|| nodeVersion[2] - loadersDeprecatedVersion[2]
106-
) < 0;
100+
const nodeSupportsDeprecatedLoaders = compareNodeVersion([16, 12, 0]) < 0;
107101

108102
export const getFormat = nodeSupportsDeprecatedLoaders ? _getFormat : undefined;
109103
export const transformSource = nodeSupportsDeprecatedLoaders ? _transformSource : undefined;

src/loaders.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
transformDynamicImport,
66
applySourceMap,
77
resolveTsPath,
8+
compareNodeVersion,
89
} from '@esbuild-kit/core-utils';
910
import {
1011
sourcemaps,
@@ -83,6 +84,11 @@ async function tryDirectory(
8384
const fileProtocol = 'file://';
8485
const isPathPattern = /^\.{0,2}\//;
8586

87+
const supportsNodePrefix = (
88+
compareNodeVersion([14, 13, 1]) >= 0
89+
|| compareNodeVersion([12, 20, 0]) >= 0
90+
);
91+
8692
export const resolve: resolve = async function (
8793
specifier,
8894
context,
@@ -91,7 +97,7 @@ export const resolve: resolve = async function (
9197
) {
9298
// Added in v12.20.0
9399
// https://nodejs.org/api/esm.html#esm_node_imports
94-
if (specifier.startsWith('node:')) {
100+
if (!supportsNodePrefix && specifier.startsWith('node:')) {
95101
specifier = specifier.slice(5);
96102
}
97103

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
1-
const fs = require('node:fs');
2-
3-
console.log(
4-
'loaded cjs-ext-cjs/index.cjs',
5-
JSON.stringify({
6-
nodePrefix: Boolean(fs),
7-
hasDynamicImport: Boolean(import('fs')),
8-
...(() => {
9-
let nameInError;
10-
try {
11-
nameInError();
12-
} catch (error) {
13-
return {
14-
nameInError: error.message.includes('nameInError'),
15-
sourceMap: error.stack.includes(':11:5'),
16-
};
17-
}
18-
})(),
19-
}),
1+
async function test(description, testFunction) {
2+
try {
3+
const result = await testFunction();
4+
if (!result) { throw result; }
5+
console.log(`✔ ${description}`);
6+
} catch (error) {
7+
console.log(`✖ ${description}: ${error.toString().split('\n').shift()}`);
8+
}
9+
}
10+
11+
console.log('loaded cjs-ext-cjs/index.cjs');
12+
13+
test(
14+
'has CJS context',
15+
() => typeof require !== 'undefined' || typeof module !== 'undefined',
16+
);
17+
18+
test(
19+
'name in error',
20+
() => {
21+
let nameInError;
22+
try {
23+
nameInError();
24+
} catch (error) {
25+
return error.message.includes('nameInError');
26+
}
27+
},
28+
);
29+
30+
test(
31+
'sourcemaps',
32+
() => new Error().stack.includes(':32:'),
33+
);
34+
35+
test(
36+
'resolves optional node prefix',
37+
() => Boolean(require('node:fs')),
38+
);
39+
40+
test(
41+
'resolves required node prefix',
42+
() => Boolean(require('node:test')),
43+
);
44+
45+
test(
46+
'has dynamic import',
47+
() => import('fs').then(Boolean),
2048
);
2149

2250
module.exports = 1234;
Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1-
const fs = require('node:fs');
1+
async function test(description, testFunction) {
2+
try {
3+
const result = await testFunction();
4+
if (!result) { throw result; }
5+
console.log(`✔ ${description}`);
6+
} catch (error) {
7+
console.log(`✖ ${description}: ${error.toString().split('\n').shift()}`);
8+
}
9+
}
210

3-
console.log(
4-
'loaded cjs-ext-js/index.js',
5-
JSON.stringify({
6-
nodePrefix: Boolean(fs),
7-
hasDynamicImport: Boolean(import('fs')),
8-
...(() => {
9-
let nameInError;
10-
try {
11-
nameInError();
12-
} catch (error) {
13-
return {
14-
nameInError: error.message.includes('nameInError'),
15-
sourceMap: error.stack.includes(':11:5'),
16-
};
17-
}
18-
})(),
19-
}),
11+
console.log('loaded cjs-ext-js/index.js');
12+
13+
test(
14+
'has CJS context',
15+
() => typeof require !== 'undefined' || typeof module !== 'undefined',
16+
);
17+
18+
test(
19+
'name in error',
20+
() => {
21+
let nameInError;
22+
try {
23+
nameInError();
24+
} catch (error) {
25+
return error.message.includes('nameInError');
26+
}
27+
},
2028
);
2129

22-
module.exports = 1234;
30+
test(
31+
'sourcemaps',
32+
() => new Error().stack.includes(':32:'),
33+
);
34+
35+
test(
36+
'has dynamic import',
37+
() => import('fs').then(Boolean),
38+
);
Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
1-
import fs from 'node:fs';
1+
async function test(description, testFunction) {
2+
try {
3+
const result = await testFunction();
4+
if (!result) { throw result; }
5+
console.log(`✔ ${description}`);
6+
} catch (error) {
7+
console.log(`✖ ${description}: ${error.toString().split('\n').shift()}`);
8+
}
9+
}
210

3-
console.log(
4-
'loaded esm-ext-js/index.js',
5-
JSON.stringify({
6-
nodePrefix: Boolean(fs),
7-
hasDynamicImport: Boolean(import('fs')),
8-
...(() => {
9-
let nameInError;
10-
try {
11-
nameInError();
12-
} catch (error) {
13-
return {
14-
nameInError: error.message.includes('nameInError'),
15-
sourceMap: error.stack.includes(':11:5'),
16-
};
17-
}
18-
})(),
19-
}),
11+
console.log('loaded esm-ext-js/index.js');
12+
13+
test(
14+
'has CJS context',
15+
() => typeof require !== 'undefined' || typeof module !== 'undefined',
16+
);
17+
18+
test(
19+
'name in error',
20+
() => {
21+
let nameInError;
22+
try {
23+
nameInError();
24+
} catch (error) {
25+
return error.message.includes('nameInError');
26+
}
27+
},
28+
);
29+
30+
test(
31+
'sourcemaps',
32+
() => new Error().stack.includes(':32:'),
33+
);
34+
35+
test(
36+
'resolves optional node prefix',
37+
() => import('node:fs').then(Boolean),
38+
);
39+
40+
test(
41+
'resolves required node prefix',
42+
() => import('node:test').then(Boolean),
2043
);
2144

2245
export default 1234;
Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
1-
import fs from 'node:fs';
1+
async function test(description, testFunction) {
2+
try {
3+
const result = await testFunction();
4+
if (!result) { throw result; }
5+
console.log(`✔ ${description}`);
6+
} catch (error) {
7+
console.log(`✖ ${description}: ${error.toString().split('\n').shift()}`);
8+
}
9+
}
210

3-
console.log(
4-
'loaded esm-ext-mjs/index.mjs',
5-
JSON.stringify({
6-
nodePrefix: Boolean(fs),
7-
hasDynamicImport: Boolean(import('fs')),
8-
...(() => {
9-
let nameInError;
10-
try {
11-
nameInError();
12-
} catch (error) {
13-
return {
14-
nameInError: error.message.includes('nameInError'),
15-
sourceMap: error.stack.includes(':11:5'),
16-
};
17-
}
18-
})(),
19-
}),
11+
console.log('loaded esm-ext-mjs/index.mjs');
12+
13+
test(
14+
'has CJS context',
15+
() => typeof require !== 'undefined' || typeof module !== 'undefined',
16+
);
17+
18+
test(
19+
'name in error',
20+
() => {
21+
let nameInError;
22+
try {
23+
nameInError();
24+
} catch (error) {
25+
return error.message.includes('nameInError');
26+
}
27+
},
28+
);
29+
30+
test(
31+
'sourcemaps',
32+
() => new Error().stack.includes(':32:'),
33+
);
34+
35+
test(
36+
'resolves optional node prefix',
37+
() => import('node:fs').then(Boolean),
38+
);
39+
40+
test(
41+
'resolves required node prefix',
42+
() => import('node:test').then(Boolean),
2043
);
2144

2245
export default 1234;

0 commit comments

Comments
 (0)