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

feat: add support for node >=16.12.0 #19

Merged
merged 7 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 54 additions & 20 deletions loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,37 @@ import { transformSync } from 'esbuild'
const baseURL = pathToFileURL(`${process.cwd()}/`).href
const isWindows = process.platform === 'win32'

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

function esbuildTransformSync(rawSource, filename, url, format) {
const {
code: js,
warnings,
map: jsSourceMap,
} = transformSync(rawSource.toString(), {
sourcefile: filename,
sourcemap: 'both',
loader: new URL(url).pathname.match(extensionsRegex)[1],
target: `node${process.versions.node}`,
format: format === 'module' ? 'esm' : 'cjs',
})

if (warnings && warnings.length > 0) {
for (const warning of warnings) {
console.warn(warning.location)
console.warn(warning.text)
}
}

return { js, jsSourceMap }
}

export function resolve(specifier, context, defaultResolve) {
const { parentURL = baseURL } = context
const url = new URL(specifier, parentURL)
if (extensionsRegex.test(url.pathname))
return { url: url.href }
return { url: url.href, format: 'module' }

// ignore `data:` and `node:` prefix etc.
if (!excludeRegex.test(specifier)) {
Expand All @@ -22,14 +45,39 @@ export function resolve(specifier, context, defaultResolve) {
url.pathname = `${pathname}.${ext}`
const path = fileURLToPath(url.href)
if (fs.existsSync(path))
return { url: url.href }
return {
url: url.href,
format: extensionsRegex.test(url.pathname) && 'module',
}
}
}

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

// New hook starting from Node v16.12.0
// See: https://github.com/nodejs/node/pull/37468
export function load(url, context, defaultLoad) {
if (extensionsRegex.test(new URL(url).pathname)) {
const { format } = context

let filename = url
if (!isWindows) filename = fileURLToPath(url)

const rawSource = fs.readFileSync(new URL(url), { encoding: 'utf8' })
const { js } = esbuildTransformSync(rawSource, filename, url, format)

return {
format: 'module',
source: js,
}
}

// Let Node.js handle all other format / sources.
return defaultLoad(url, context, defaultLoad)
}

export function getFormat(url, context, defaultGetFormat) {
if (extensionsRegex.test(new URL(url).pathname)) {
return {
Expand All @@ -46,23 +94,9 @@ export function transformSource(source, context, defaultTransformSource) {

if (extensionsRegex.test(new URL(url).pathname)) {
let filename = url
if (!isWindows)
filename = fileURLToPath(url)

const { code: js, warnings, map: jsSourceMap } = transformSync(source.toString(), {
sourcefile: filename,
sourcemap: 'both',
loader: new URL(url).pathname.match(extensionsRegex)[1],
target: `node${process.versions.node}`,
format: format === 'module' ? 'esm' : 'cjs',
})

if (warnings && warnings.length > 0) {
for (const warning of warnings) {
console.warn(warning.location)
console.warn(warning.text)
}
}
if (!isWindows) filename = fileURLToPath(url)

const { js } = esbuildTransformSync(source, filename, url, format)

return {
source: js,
Expand Down
Loading