Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

fix: apply tsconfig only to matched files #30

Merged
merged 14 commits into from
Feb 6, 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@esbuild-kit/core-utils": "^3.0.0",
"get-tsconfig": "^4.2.0"
"get-tsconfig": "^4.4.0"
},
"devDependencies": {
"@pvtnbr/eslint-config": "^0.30.1",
Expand All @@ -44,6 +44,7 @@
"esbuild": "^0.15.10",
"eslint": "^8.24.0",
"execa": "^6.1.0",
"fs-fixture": "^1.2.0",
"get-node": "^13.2.0",
"manten": "^0.3.0",
"pkgroll": "^1.4.0",
Expand Down
17 changes: 12 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getTsconfig,
parseTsconfig,
createPathsMatcher,
createFilesMatcher,
} from 'get-tsconfig';
import type { TransformOptions } from 'esbuild';

Expand All @@ -22,13 +23,13 @@ const nodeModulesPath = `${path.sep}node_modules${path.sep}`;
const tsconfig = (
process.env.ESBK_TSCONFIG_PATH
? {
path: process.env.ESBK_TSCONFIG_PATH,
path: path.resolve(process.env.ESBK_TSCONFIG_PATH),
config: parseTsconfig(process.env.ESBK_TSCONFIG_PATH),
}
: getTsconfig()
);

const tsconfigRaw = tsconfig?.config;
const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);

const applySourceMap = installSourceMapSupport();
Expand Down Expand Up @@ -70,7 +71,7 @@ function transformer(
code,
filePath,
{
tsconfigRaw: tsconfigRaw as TransformOptions['tsconfigRaw'],
tsconfigRaw: fileMatcher?.(filePath) as TransformOptions['tsconfigRaw'],
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"extends": "../tsconfig.json",
"compilerOptions": {
"jsxFactory": "console.error"
}
},
"include": [
"../src"
]
}
2 changes: 1 addition & 1 deletion tests/fixtures/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"p/*": ["utils/*"],
"*/s": ["utils/*"]
},
}
},
}
96 changes: 96 additions & 0 deletions tests/specs/typescript/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import type { NodeApis } from '../../utils/node-with-loader';

export default testSuite(async ({ describe }, node: NodeApis) => {
Expand All @@ -10,6 +11,101 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
expect(nodeProcess.stdout).toBe('div null hello world\nnull null goodbye world');
});

describe('scope', ({ test }) => {
const checkStrictMode = `
(function (param) {
param = 2;
const isStrictMode = arguments[0] !== 2;
console.log(isStrictMode ? 'strict mode' : 'not strict mode');
})(1);
`;
const checkJsx = 'export default (<div></div>)';

test('does not apply tsconfig to excluded', async () => {
const fixture = await createFixture({
'tsconfig.json': JSON.stringify({
compilerOptions: {
strict: true,
jsxFactory: 'console.log',
},
exclude: [
'excluded',
],
}),
included: {
'strict-mode-ts.ts': checkStrictMode,
'strict-mode-js.js': checkStrictMode,
'jsx.jsx': checkJsx,
'tsx.tsx': checkJsx,
},
excluded: {
'strict-mode-ts.ts': checkStrictMode,
'tsx.tsx': checkJsx,
},
});

const includedStrictTs = await node.load('./included/strict-mode-ts.ts', {
cwd: fixture.path,
});
expect(includedStrictTs.stdout).toBe('strict mode');

const includedStrictJs = await node.load('./included/strict-mode-js.js', {
cwd: fixture.path,
});
expect(includedStrictJs.stdout).toBe('not strict mode');

const includedJsxTs = await node.load('./included/tsx.tsx', {
cwd: fixture.path,
});
expect(includedJsxTs.stdout).toBe('div null');

const includedJsxJs = await node.load('./included/jsx.jsx', {
cwd: fixture.path,
});
expect(includedJsxJs.stderr).toMatch('ReferenceError: React is not defined');

const excludedStrictTs = await node.load('./excluded/strict-mode-ts.ts', {
cwd: fixture.path,
});
expect(excludedStrictTs.stdout).toBe('not strict mode');

const excludedJsxTs = await node.load('./excluded/tsx.tsx', {
cwd: fixture.path,
});
expect(excludedJsxTs.stderr).toMatch('ReferenceError: React is not defined');

await fixture.rm();
});

test('allowJs', async () => {
const fixture = await createFixture({
'tsconfig.json': JSON.stringify({
compilerOptions: {
strict: true,
allowJs: true,
jsxFactory: 'console.log',
},
}),
src: {
'strict-mode-js.js': checkStrictMode,
'jsx.jsx': checkJsx,
},
});

const strictJs = await node.load('./src/strict-mode-js.js', {
cwd: fixture.path,
});
expect(strictJs.stdout).toBe('strict mode');

const jsxJs = await node.load('./src/jsx.jsx', {
cwd: fixture.path,
});
expect(jsxJs.stdout).toBe('div null');

await fixture.rm();
});
});

test('Custom tsconfig.json path', async () => {
const nodeProcess = await node.load('./src/tsx.tsx', {
cwd: './tsconfig',
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/node-with-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function createNode(

args: [filePath],
nodePath: node.path,
cwd: path.join(fixturePath, options?.cwd ?? ''),
cwd: path.resolve(fixturePath, options?.cwd ?? ''),
env: options?.env,
nodeOptions: options?.nodeOptions,
},
Expand Down