Skip to content

Commit

Permalink
feat: jiti/native export (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Sep 12, 2024
1 parent 077a6ea commit 32b37b3
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
90 changes: 90 additions & 0 deletions lib/jiti-native.mjs
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(".");
}
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@
"types": "./lib/jiti-register.d.mts",
"import": "./lib/jiti-register.mjs"
},
"./native": {
"bun": "./lib/jiti-native.mjs",
"deno": "./lib/jiti-native.mjs",
"node": {
"import": {
"types": "./lib/jiti.d.mts",
"default": "./lib/jiti.mjs"
},
"require": {
"types": "./lib/jiti.d.cts",
"default": "./lib/jiti.cjs"
}
},
"default": "./lib/jiti-native.mjs"
},
"./package.json": "./package.json"
},
"main": "./lib/jiti.cjs",
Expand Down
26 changes: 26 additions & 0 deletions test/bun-native.test.ts
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);
});
}
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
exclude: [
"**/test.{ts,mjs,cjs,js}",
"node_modules/**/*",
"test/bun.test.ts",
"test/bun*.test.ts",
],
},
});

0 comments on commit 32b37b3

Please sign in to comment.