Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit ccb92a3

Browse files
elianivaantfu
andauthored
feat: add support for node >=16.12.0 (#19)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 71a4d75 commit ccb92a3

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
strategy:
1717
matrix:
18-
node-version: [12.x, 14.x, 16.x]
18+
node-version: [12.x, 14.x, 16.x, 16.11, 16.12]
1919
os: [ubuntu-latest, windows-latest, macos-latest]
2020
fail-fast: false
2121

loader.mjs

+54-20
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,37 @@ import { transformSync } from 'esbuild'
55
const baseURL = pathToFileURL(`${process.cwd()}/`).href
66
const isWindows = process.platform === 'win32'
77

8-
const extensionsRegex = /\.(tsx?|json)$/;
8+
const extensionsRegex = /\.(tsx?|json)$/
99
const excludeRegex = /^\w+:/
1010

11+
function esbuildTransformSync(rawSource, filename, url, format) {
12+
const {
13+
code: js,
14+
warnings,
15+
map: jsSourceMap,
16+
} = transformSync(rawSource.toString(), {
17+
sourcefile: filename,
18+
sourcemap: 'both',
19+
loader: new URL(url).pathname.match(extensionsRegex)[1],
20+
target: `node${process.versions.node}`,
21+
format: format === 'module' ? 'esm' : 'cjs',
22+
})
23+
24+
if (warnings && warnings.length > 0) {
25+
for (const warning of warnings) {
26+
console.warn(warning.location)
27+
console.warn(warning.text)
28+
}
29+
}
30+
31+
return { js, jsSourceMap }
32+
}
33+
1134
export function resolve(specifier, context, defaultResolve) {
1235
const { parentURL = baseURL } = context
1336
const url = new URL(specifier, parentURL)
1437
if (extensionsRegex.test(url.pathname))
15-
return { url: url.href }
38+
return { url: url.href, format: 'module' }
1639

1740
// ignore `data:` and `node:` prefix etc.
1841
if (!excludeRegex.test(specifier)) {
@@ -22,14 +45,39 @@ export function resolve(specifier, context, defaultResolve) {
2245
url.pathname = `${pathname}.${ext}`
2346
const path = fileURLToPath(url.href)
2447
if (fs.existsSync(path))
25-
return { url: url.href }
48+
return {
49+
url: url.href,
50+
format: extensionsRegex.test(url.pathname) && 'module',
51+
}
2652
}
2753
}
2854

2955
// Let Node.js handle all other specifiers.
3056
return defaultResolve(specifier, context, defaultResolve)
3157
}
3258

59+
// New hook starting from Node v16.12.0
60+
// See: https://github.com/nodejs/node/pull/37468
61+
export function load(url, context, defaultLoad) {
62+
if (extensionsRegex.test(new URL(url).pathname)) {
63+
const { format } = context
64+
65+
let filename = url
66+
if (!isWindows) filename = fileURLToPath(url)
67+
68+
const rawSource = fs.readFileSync(new URL(url), { encoding: 'utf8' })
69+
const { js } = esbuildTransformSync(rawSource, filename, url, format)
70+
71+
return {
72+
format: 'module',
73+
source: js,
74+
}
75+
}
76+
77+
// Let Node.js handle all other format / sources.
78+
return defaultLoad(url, context, defaultLoad)
79+
}
80+
3381
export function getFormat(url, context, defaultGetFormat) {
3482
if (extensionsRegex.test(new URL(url).pathname)) {
3583
return {
@@ -46,23 +94,9 @@ export function transformSource(source, context, defaultTransformSource) {
4694

4795
if (extensionsRegex.test(new URL(url).pathname)) {
4896
let filename = url
49-
if (!isWindows)
50-
filename = fileURLToPath(url)
51-
52-
const { code: js, warnings, map: jsSourceMap } = transformSync(source.toString(), {
53-
sourcefile: filename,
54-
sourcemap: 'both',
55-
loader: new URL(url).pathname.match(extensionsRegex)[1],
56-
target: `node${process.versions.node}`,
57-
format: format === 'module' ? 'esm' : 'cjs',
58-
})
59-
60-
if (warnings && warnings.length > 0) {
61-
for (const warning of warnings) {
62-
console.warn(warning.location)
63-
console.warn(warning.text)
64-
}
65-
}
97+
if (!isWindows) filename = fileURLToPath(url)
98+
99+
const { js } = esbuildTransformSync(source, filename, url, format)
66100

67101
return {
68102
source: js,

0 commit comments

Comments
 (0)