Skip to content

Commit

Permalink
Migrate to jiti v2
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jul 3, 2024
1 parent 4c4e3e1 commit c373690
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 15 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/knip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@snyk/github-codeowners": "1.1.0",
"easy-table": "1.2.0",
"fast-glob": "^3.3.2",
"jiti": "^1.21.6",
"jiti": "2.0.0-beta.3",
"js-yaml": "^4.1.0",
"minimist": "^1.2.8",
"picocolors": "^1.0.0",
Expand Down
8 changes: 2 additions & 6 deletions packages/knip/src/util/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LoaderError } from './errors.js';
import { loadFile, loadJSON, loadTOML, loadYAML, parseJSON, parseYAML } from './fs.js';
import { isTypeModule } from './fs.js';
import { extname, isInternal } from './path.js';
import { jitiCJS, jitiESM } from './register.js';
import { jiti } from './register.js';

const load = async (filePath: string) => {
try {
Expand Down Expand Up @@ -45,11 +45,7 @@ const load = async (filePath: string) => {
return imported.default ?? imported;
}

if (ext === '.mts' || ((ext === '.ts' || ext === '.tsx') && isTypeModule(filePath))) {
return await jitiESM(filePath);
}

return await jitiCJS(filePath);
return await jiti(filePath);
} catch (error) {
throw new LoaderError(`Error loading ${filePath}`, { cause: error });
}
Expand Down
10 changes: 4 additions & 6 deletions packages/knip/src/util/register.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fileURLToPath } from 'node:url';
import createJITI, { type JITIOptions } from 'jiti';
// @ts-expect-error ts(2614)
import { type JitiOptions, createJiti } from 'jiti';
import { DEFAULT_EXTENSIONS } from '../constants.js';
import { join } from './path.js';

Expand All @@ -15,9 +16,6 @@ const options = {
},
};

// @ts-expect-error Our package.json has type=module (for globby, picocolors, etc), but here it confuses TypeScript
const createLoader = (options: JITIOptions) => createJITI(process.cwd(), options);
const createLoader = (options: JitiOptions) => createJiti(process.cwd(), options);

export const jitiCJS = createLoader(options);

export const jitiESM = createLoader({ ...options, esmResolve: true });
export const jiti = createLoader(options);
4 changes: 2 additions & 2 deletions packages/knip/src/util/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { timerify } from './Performance.js';
import { debugLog } from './debug.js';
import { getPackageNameFromModuleSpecifier } from './modules.js';
import { cwd, join, toPosix } from './path.js';
import { jitiCJS } from './register.js';
import { jiti as _jiti } from './register.js';

/*
* package.json#exports + self-referencing not supported in `resolve` package nor Bun:
Expand All @@ -18,7 +18,7 @@ const createRequire = (path?: string) => nodeCreateRequire(pathToFileURL(path ??
const require = createRequire();
export const _require = timerify(require);

const resolve = (specifier: string) => toPosix(jitiCJS.resolve(specifier));
const resolve = (specifier: string) => toPosix(_jiti.resolve(specifier));

const tryResolve = (specifier: string, from: string) => {
try {
Expand Down

0 comments on commit c373690

Please sign in to comment.