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 Sep 27, 2024
1 parent 2b550b0 commit e127e23
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions packages/knip/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/tmp
/node_modules
!/fixtures/**
.cache
2 changes: 1 addition & 1 deletion packages/knip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"easy-table": "1.2.0",
"enhanced-resolve": "^5.17.1",
"fast-glob": "^3.3.2",
"jiti": "^1.21.6",
"jiti": "^2.0.0",
"js-yaml": "^4.1.0",
"minimist": "^1.2.8",
"picocolors": "^1.0.0",
Expand Down
9 changes: 3 additions & 6 deletions packages/knip/src/util/jiti.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fileURLToPath } from 'node:url';
import createJITI, { type JITIOptions } from 'jiti';
import { type JitiOptions, createJiti } from 'jiti';
import { DEFAULT_EXTENSIONS } from '../constants.js';
import { join } from './path.js';

Expand All @@ -15,9 +15,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);
8 changes: 2 additions & 6 deletions packages/knip/src/util/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { timerify } from './Performance.js';
import { LoaderError } from './errors.js';
import { loadFile, loadJSON, loadTOML, loadYAML, parseJSON, parseYAML } from './fs.js';
import { isTypeModule } from './fs.js';
import { jitiCJS, jitiESM } from './jiti.js';
import { jiti } from './jiti.js';
import { extname, isInternal } from './path.js';

const load = async (filePath: string) => {
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
6 changes: 6 additions & 0 deletions packages/knip/test/util/find-and-parse-gitignores.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ test('findAndParseGitignores', async () => {
assert.deepEqual(gitignore, {
gitignoreFiles: ['../../.gitignore', '../../../../.gitignore', '.gitignore', 'a/.gitignore', 'a/b/.gitignore'],
ignores: new Set([
'**/.cache',
'**/.cache/**',
'**/.idea',
'**/.idea/**',
'.git',
Expand All @@ -34,6 +36,8 @@ test('findAndParseGitignores (/a)', async () => {
'.yarn',
'**/b/c',
'**/b/c/**',
'**/.cache',
'**/.cache/**',
'**/.idea',
'**/.idea/**',
]),
Expand All @@ -59,6 +63,8 @@ test('findAndParseGitignores (/a/b', async () => {
'.yarn',
'**/c',
'**/c/**',
'**/.cache',
'**/.cache/**',
'**/.idea',
'**/.idea/**',
]),
Expand Down

0 comments on commit e127e23

Please sign in to comment.