Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions knip.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"c8",
// Optional peer dependency used for loading TypeScript configuration files
"jiti",
"jiti-v2.0",
"jiti-v2.1",
],
},
"docs": {
Expand Down
34 changes: 20 additions & 14 deletions lib/config/config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ function isNativeTypeScriptSupportEnabled() {
* @since 9.24.0
*/
async function loadTypeScriptConfigFileWithJiti(filePath, fileURL, mtime) {
// eslint-disable-next-line no-use-before-define -- `ConfigLoader.loadJiti` can be overwritten for testing
const { createJiti } = await ConfigLoader.loadJiti().catch(() => {
throw new Error(
"The 'jiti' library is required for loading TypeScript configuration files. Make sure to install it.",
);
});
const { createJiti, version: jitiVersion } =
// eslint-disable-next-line no-use-before-define -- `ConfigLoader.loadJiti` can be overwritten for testing
await ConfigLoader.loadJiti().catch(() => {
throw new Error(
"The 'jiti' library is required for loading TypeScript configuration files. Make sure to install it.",
);
});

// `createJiti` was added in jiti v2.
if (typeof createJiti !== "function") {
Expand All @@ -155,11 +156,15 @@ async function loadTypeScriptConfigFileWithJiti(filePath, fileURL, mtime) {
* Disabling `moduleCache` allows us to reload a
* config file when the last modified timestamp changes.
*/

const jiti = createJiti(__filename, {
const jitiOptions = {
moduleCache: false,
interopDefault: false,
});
};

if (jitiVersion.startsWith("2.1.")) {
jitiOptions.interopDefault = false;
}

const jiti = createJiti(__filename, jitiOptions);
const config = await jiti.import(fileURL.href);

importedConfigFileModificationTime.set(filePath, mtime);
Expand Down Expand Up @@ -494,11 +499,12 @@ class ConfigLoader {

/**
* Used to import the jiti dependency. This method is exposed internally for testing purposes.
* @returns {Promise<Record<string, unknown>>} A promise that fulfills with a module object
* or rejects with an error if jiti is not found.
* @returns {Promise<{createJiti: Function|undefined, version: string;}>} A promise that fulfills with an object containing the jiti module's createJiti function and version.
*/
static loadJiti() {
return import("jiti");
static async loadJiti() {
const { createJiti } = await import("jiti");
const version = require("jiti/package.json").version;
return { createJiti, version };
}

/**
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@
"globals": "^15.0.0",
"got": "^11.8.3",
"gray-matter": "^4.0.3",
"jiti": "^2.1.0",
"jiti": "^2.2.0",
"jiti-v2.0": "npm:jiti@2.0.x",
"jiti-v2.1": "npm:jiti@2.1.x",
"knip": "^5.32.0",
"lint-staged": "^11.0.0",
"load-perf": "^0.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import plugin from "./plugin";

export default plugin.configs.recommended;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
configs: {
recommended: {
rules: {
"no-undef": "error",
},
},
},
};

// The following string will confuse jiti when interopDefault is false:
// @foo export {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
Loading
Loading