-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* @typedef {import('./types').Jiti} Jiti | ||
* @typedef {import('./types').JitiOptions} JitiOptions | ||
*/ | ||
|
||
/** | ||
* @param {string} parentURL | ||
* @param {JitiOptions} [_opts] | ||
* @returns {Jiti} | ||
*/ | ||
export function createJiti(parentURL = "./_.js", _opts) { | ||
if (isDir(parentURL)) { | ||
parentURL += "/_.js"; | ||
} | ||
|
||
/** @type {Jiti} */ | ||
function jiti() { | ||
throw unsupportedError( | ||
"`jiti()` is not supported in native mode, use `jiti.import()` instead.", | ||
); | ||
} | ||
|
||
jiti.resolve = () => { | ||
throw unsupportedError("`jiti.resolve()` is not supported in native mode."); | ||
}; | ||
|
||
jiti.esmResolve = (id, opts) => { | ||
try { | ||
console.log("resolve", { id, from: opts?.parentURL || parentURL }); | ||
return import.meta.resolve(id, opts?.parentURL || parentURL); | ||
} catch (error) { | ||
if (opts?.try) { | ||
return undefined; | ||
} else { | ||
throw error; | ||
} | ||
} | ||
}; | ||
|
||
jiti.import = async function (id, opts) { | ||
const resolved = await this.esmResolve(id, opts); | ||
if (!resolved) { | ||
if (opts?.try) { | ||
return undefined; | ||
} else { | ||
throw new Error(`Cannot resolve module '${id}'`); | ||
} | ||
} | ||
return import(resolved); | ||
}; | ||
|
||
jiti.transform = () => { | ||
throw unsupportedError( | ||
"`jiti.transform()` is not supported in native mode.", | ||
); | ||
}; | ||
|
||
jiti.evalModule = () => { | ||
throw unsupportedError( | ||
"`jiti.evalModule()` is not supported in native mode.", | ||
); | ||
}; | ||
|
||
jiti.main = undefined; | ||
jiti.extensions = Object.create(null); | ||
jiti.cache = Object.create(null); | ||
|
||
return jiti; | ||
} | ||
|
||
export default createJiti; | ||
|
||
/** | ||
* @param {string} message | ||
*/ | ||
function unsupportedError(message) { | ||
throw new Error( | ||
`[jiti] ${message} (import or require \`jiti/node\` for more features).`, | ||
); | ||
} | ||
|
||
function isDir(filename) { | ||
if (filename instanceof URL || filename.startsWith("file://")) { | ||
return false; | ||
} | ||
if (filename.endsWith("/")) { | ||
return true; | ||
} | ||
return !filename.split("/").pop().includes("."); | ||
} |
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,26 @@ | ||
import { fileURLToPath } from "node:url"; | ||
import { readdir } from "node:fs/promises"; | ||
// @ts-ignore | ||
import { test } from "bun:test"; | ||
|
||
import { createJiti } from "../lib/jiti-native.mjs"; | ||
|
||
const fixturesDir = fileURLToPath(new URL("fixtures", import.meta.url)); | ||
|
||
const fixtures = await readdir(fixturesDir); | ||
|
||
const _jiti = createJiti(fixturesDir); | ||
|
||
for (const fixture of fixtures) { | ||
if ( | ||
fixture === "error-runtime" || | ||
fixture === "error-parse" || | ||
fixture === "typescript" | ||
) { | ||
continue; | ||
} | ||
|
||
test("fixtures/" + fixture + " (ESM) (Native)", async () => { | ||
await _jiti.import("./" + fixture); | ||
}); | ||
} |
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