Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ignore node modules #128

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/witty-dolls-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tsconfck': major
---

breaking(find): replace scanNodeModules with ignoreNodeModules
6 changes: 4 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ interface TSConfckFindOptions {
root?: string;

/**
* set to true if you want to find tsconfig.json files inside node_modules
* set to true if you don't want to find tsconfig for files inside node_modules
*
* This is useful if you want to use the output with esbuild.transform as esbuild itself also ignores node_modules
*
* @default false
*/
scanNodeModules?: boolean;
ignoreNodeModules?: boolean;
}
```

Expand Down
19 changes: 12 additions & 7 deletions packages/tsconfck/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,22 @@ see [API-DOCS](docs/api.md)

## Advanced

### finding tsconfig.json files inside node_modules
### ignoring tsconfig.json for files inside node_modules

By default, tsconfck ignores tsconfig.json files inside node_modules, similar to esbuild.
If you want to include tsconfig.json files inside node_modules in your find/parse results, set `scanNodeModules`
esbuild ignores node_modules so when you want to use tsconfck with esbuild, you can set `ignoreNodeModules: true`

```js
import { find } from 'tsconfck';
// does not return some-lib/tsconfig.json but first tsconfig outside of node_modules
import { find, parse } from 'tsconfck';
// returns some-lib/tsconfig.json
const fooTSConfig = await find('node_modules/some-lib/src/foo.ts');
// returns some-lib/tsconfig.json if it exists otherwise continues finding up the tree
const fooResult = await find('node_modules/some-lib/src/foo.ts', { scanNodeModules: true });

// returns null
const fooTSConfigIgnored = await find('node_modules/some-lib/src/foo.ts', {
ignoreNodeModules: true
});

// returns empty config
const { tsconfig } = await parse('node_modules/some-lib/src/foo.ts', { ignoreNodeModules: true });
```

### caching
Expand Down
6 changes: 3 additions & 3 deletions packages/tsconfck/src/find-native.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path';
import { loadTS, stripNodeModules } from './util.js';
import { isInNodeModules, loadTS } from './util.js';

/**
* find the closest tsconfig.json file using native ts.findConfigFile
Expand All @@ -12,8 +12,8 @@ import { loadTS, stripNodeModules } from './util.js';
*/
export async function findNative(filename, options) {
let dir = path.dirname(path.resolve(filename));
if (!options?.scanNodeModules) {
dir = stripNodeModules(dir);
if (options?.ignoreNodeModules && isInNodeModules(dir)) {
return null;
}
const cache = options?.cache;
const root = options?.root ? path.resolve(options.root) : undefined;
Expand Down
8 changes: 4 additions & 4 deletions packages/tsconfck/src/find.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import fs from 'node:fs';
import { makePromise, stripNodeModules } from './util.js';
import { isInNodeModules, makePromise } from './util.js';
/**
* find the closest tsconfig.json file
*
Expand All @@ -9,11 +9,11 @@ import { makePromise, stripNodeModules } from './util.js';
* @returns {Promise<string|null>} absolute path to closest tsconfig.json or null if not found
*/
export async function find(filename, options) {
const cache = options?.cache;
let dir = path.dirname(path.resolve(filename));
if (!options?.scanNodeModules) {
dir = stripNodeModules(dir);
if (options?.ignoreNodeModules && isInNodeModules(dir)) {
return null;
}
const cache = options?.cache;
if (cache?.hasTSConfigPath(dir)) {
return cache.getTSConfigPath(dir);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/tsconfck/src/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export interface TSConfckFindOptions {
root?: string;

/**
* set to true if you want to find tsconfig.json files inside node_modules
* set to true if you don't want to find tsconfig for files inside node_modules
*
* This is useful if you want to use the output with esbuild.transform as esbuild itself also ignores node_modules
*
* @default false
*/
scanNodeModules?: boolean;
ignoreNodeModules?: boolean;
}

export interface TSConfckParseOptions extends TSConfckFindOptions {
Expand Down
12 changes: 4 additions & 8 deletions packages/tsconfck/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,13 @@ export async function resolveTSConfigJson(filename, cache) {
}

/**
* remove path segments inside node_modules
*
* @param {string} dir an absolute directory path
* @returns {string} parent dir of node_modules if inside of node_modules, dir if not
* @returns {boolean} if dir path includes a node_modules segment
*/
export const stripNodeModules = IS_POSIX
? (dir) => {
const i = dir.indexOf('/node_modules/');
return i > -1 ? dir.slice(0, i) : dir;
}
: (dir) => dir.replace(/[/\\]node_modules[/\\].*$/, '');
export const isInNodeModules = IS_POSIX
? (dir) => dir.includes('/node_modules/')
: (dir) => dir.match(/[/\\]node_modules[/\\]/);

/**
* convert posix separator to native separator
Expand Down
9 changes: 4 additions & 5 deletions packages/tsconfck/tests/find-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,24 @@ describe('find-native', () => {
}
});

it('should ignore tsconfig in node_modules directory', async () => {
it('should ignore files in node_modules directory with ignoreNodeModules=true', async () => {
const fixtureDir = 'find/a';
const expected = native2posix(absFixture(`${fixtureDir}/tsconfig.json`));
const relativeTS = relFixture(`${fixtureDir}/node_modules/some-lib/src/foo.ts`);
const absoluteTS = absFixture(`${fixtureDir}/node_modules/some-lib/src/foo.ts`);
const inputs = [relativeTS, `./${relativeTS}`, absoluteTS];
for (const input of inputs) {
expect(await findNative(input), `input: ${input}`).toBe(expected);
expect(await findNative(input, { ignoreNodeModules: true }), `input: ${input}`).toBe(null);
}
});

it('should find tsconfig in node_modules directory with scanNodeModules=true', async () => {
it('should find tsconfig in node_modules', async () => {
const fixtureDir = 'find/a';
const expected = native2posix(absFixture(`${fixtureDir}/node_modules/some-lib/tsconfig.json`));
const relativeTS = relFixture(`${fixtureDir}/node_modules/some-lib/src/foo.ts`);
const absoluteTS = absFixture(`${fixtureDir}/node_modules/some-lib/src/foo.ts`);
const inputs = [relativeTS, `./${relativeTS}`, absoluteTS];
for (const input of inputs) {
expect(await findNative(input, { scanNodeModules: true }), `input: ${input}`).toBe(expected);
expect(await findNative(input), `input: ${input}`).toBe(expected);
}
});

Expand Down
9 changes: 4 additions & 5 deletions packages/tsconfck/tests/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,24 @@ describe('find', () => {
}
});

it('should ignore tsconfig in node_modules directory', async () => {
it('should ignore files in node_modules directory with ignoreNodeModules=true', async () => {
const fixtureDir = 'find/a';
const expected = absFixture(`${fixtureDir}/tsconfig.json`);
const relativeTS = relFixture(`${fixtureDir}/node_modules/some-lib/src/foo.ts`);
const absoluteTS = absFixture(`${fixtureDir}/node_modules/some-lib/src/foo.ts`);
const inputs = [relativeTS, `./${relativeTS}`, absoluteTS];
for (const input of inputs) {
expect(await find(input), `input: ${input}`).toBe(expected);
expect(await find(input, { ignoreNodeModules: true }), `input: ${input}`).toBe(null);
}
});

it('should find tsconfig in node_modules directory with scanNodeModules=true', async () => {
it('should find tsconfig in node_modules', async () => {
const fixtureDir = 'find/a';
const expected = absFixture(`${fixtureDir}/node_modules/some-lib/tsconfig.json`);
const relativeTS = relFixture(`${fixtureDir}/node_modules/some-lib/src/foo.ts`);
const absoluteTS = absFixture(`${fixtureDir}/node_modules/some-lib/src/foo.ts`);
const inputs = [relativeTS, `./${relativeTS}`, absoluteTS];
for (const input of inputs) {
expect(await find(input, { scanNodeModules: true }), `input: ${input}`).toBe(expected);
expect(await find(input), `input: ${input}`).toBe(expected);
}
});

Expand Down
6 changes: 4 additions & 2 deletions packages/tsconfck/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ declare module 'tsconfck' {
root?: string;

/**
* set to true if you want to find tsconfig.json files inside node_modules
* set to true if you don't want to find tsconfig for files inside node_modules
*
* This is useful if you want to use the output with esbuild.transform as esbuild itself also ignores node_modules
*
* @default false
*/
scanNodeModules?: boolean;
ignoreNodeModules?: boolean;
}

interface TSConfckParseOptions extends TSConfckFindOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/tsconfck/types/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@
null,
null
],
"mappings": ";;;;;;;;iBAUsBA,IAAIA;;;;;;;;iBCWJC,OAAOA;;;;;;;iBCRbC,MAAMA;;;;;;;;;;iBCDAC,UAAUA;cCXnBC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC0BJC,KAAKA;cAwTdC,kBAAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCxTTC,WAAWA;cA6NpBC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;;;WCtPpBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;WAuBnBC,oBAAoBA;;;;WAIpBC,sBAAsBA;;;;;;;;;WAStBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA6BnBC,0BAA0BA;;;;;;;;;;;;WAY1BC,yBAAyBA"
"mappings": ";;;;;;;;iBAUsBA,IAAIA;;;;;;;;iBCWJC,OAAOA;;;;;;;iBCRbC,MAAMA;;;;;;;;;;iBCDAC,UAAUA;cCXnBC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC0BJC,KAAKA;cAwTdC,kBAAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCxTTC,WAAWA;cA6NpBC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;;;WCtPpBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;WAyBnBC,oBAAoBA;;;;WAIpBC,sBAAsBA;;;;;;;;;WAStBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA6BnBC,0BAA0BA;;;;;;;;;;;;WAY1BC,yBAAyBA"
}