-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add experimental esm loader support (#266)
- Loading branch information
Showing
10 changed files
with
186 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { dirname, join } from "node:path"; | ||
import { fileURLToPath, pathToFileURL } from "node:url"; | ||
import { existsSync } from "node:fs"; | ||
import { readFile } from "node:fs/promises"; | ||
import { isBuiltin } from "node:module"; | ||
import { createJiti } from "./jiti.mjs"; | ||
|
||
let jiti; | ||
|
||
// https://nodejs.org/api/module.html#initialize | ||
export async function initialize() { | ||
jiti = createJiti(); | ||
} | ||
|
||
// https://nodejs.org/api/module.html#resolvespecifier-context-nextresolve | ||
export async function resolve(specifier, context, nextResolve) { | ||
if (_shouldSkip(specifier)) { | ||
return nextResolve(specifier, context); | ||
} | ||
const resolvedPath = jiti.importResolve(specifier, context?.parentURL, { | ||
conditions: context?.conditions, | ||
}); | ||
return { | ||
url: pathToFileURL(resolvedPath).href, | ||
shortCircuit: true, | ||
}; | ||
} | ||
|
||
// https://nodejs.org/api/module.html#loadurl-context-nextload | ||
export async function load(url, context, nextLoad) { | ||
if (_shouldSkip(url)) { | ||
return nextLoad(url, context); | ||
} | ||
|
||
const filename = fileURLToPath(url); | ||
|
||
if (url.endsWith(".js")) { | ||
const pkg = await _findClosestPackageJson(dirname(filename)); | ||
if (pkg && pkg.type === "module") { | ||
return nextLoad(url, context); | ||
} | ||
} | ||
|
||
const rawSource = await readFile(filename, "utf8"); | ||
|
||
if (url.endsWith(".json")) { | ||
return { | ||
source: `export default ${rawSource}`, | ||
format: "module", | ||
shortCircuit: true, | ||
}; | ||
} | ||
|
||
const transpiledSource = jiti.transform({ | ||
source: rawSource, | ||
filename: filename, | ||
ts: url.endsWith("ts"), | ||
retainLines: true, | ||
async: true, | ||
}); | ||
|
||
if (url.endsWith(".js") && !transpiledSource.includes("jitiImport")) { | ||
return { | ||
source: transpiledSource, | ||
format: "commonjs", | ||
shortCircuit: true, | ||
}; | ||
} | ||
|
||
return { | ||
source: _wrapSource(transpiledSource, filename), | ||
format: "module", | ||
shortCircuit: true, | ||
}; | ||
} | ||
|
||
function _wrapSource(source, filename) { | ||
const _jitiPath = new URL("jiti.mjs", import.meta.url).href; | ||
return /*js*/ `import { createJiti as __createJiti__ } from ${JSON.stringify(_jitiPath)};async function _module(exports, require, module, __filename, __dirname, jitiImport) { ${source}\n}; | ||
// GENERATED BY JITI ESM LOADER | ||
const filename = ${JSON.stringify(filename)}; | ||
const dirname = ${JSON.stringify(dirname(filename))}; | ||
const jiti = __createJiti__(filename); | ||
const module = { exports: Object.create(null) }; | ||
await _module(module.exports, jiti, module, filename, dirname, jiti.import); | ||
if (module.exports && module.exports.__JITI_ERROR__) { | ||
const { filename, line, column, code, message } = | ||
module.exports.__JITI_ERROR__; | ||
const loc = [filename, line, column].join(':'); | ||
const err = new Error(code + ": " + message + " " + loc); | ||
Error.captureStackTrace(err, _module); | ||
throw err; | ||
} | ||
export default module.exports; | ||
`; | ||
} | ||
|
||
function _shouldSkip(url) { | ||
return ( | ||
!jiti || | ||
url.endsWith(".mjs") || | ||
url.endsWith(".cjs") || | ||
(!url.startsWith("./") && !url.startsWith("file://")) || | ||
isBuiltin(url) | ||
); | ||
} | ||
|
||
async function _findClosestPackageJson(dir) { | ||
if (dir === "/") return null; | ||
const packageJsonPath = join(dir, "package.json"); | ||
if (existsSync(packageJsonPath)) { | ||
return JSON.parse(await readFile(packageJsonPath, "utf8")); | ||
} | ||
return _findClosestPackageJson(dirname(dir)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// https://nodejs.org/api/module.html#moduleregisterspecifier-parenturl-options | ||
import { register } from "node:module"; | ||
|
||
register("./jiti-hooks.mjs", import.meta.url, {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import "jiti/register"; | ||
import { fileURLToPath } from "node:url"; | ||
import { readdir } from "node:fs/promises"; | ||
import { test } from "node:test"; | ||
import assert from "node:assert"; | ||
|
||
const fixturesDir = fileURLToPath(new URL("fixtures", import.meta.url)); | ||
|
||
const fixtures = await readdir(fixturesDir); | ||
|
||
for (const fixture of fixtures) { | ||
if (fixture === "typescript") { | ||
continue; // .mts support | ||
} | ||
test("fixtures/" + fixture + " (ESM)", async () => { | ||
const promise = import(`./fixtures/${fixture}`); | ||
const shouldReject = | ||
fixture === "error-parse" || fixture === "error-runtime"; | ||
(await shouldReject) | ||
? assert.rejects(promise) | ||
: assert.doesNotReject(promise); | ||
}); | ||
} |