Skip to content

Commit

Permalink
fix: apply tsconfig only to matching files (#310)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `tsconfig.json` is now only applied to files it matches (via `include`/`exclude`/`files`)
  • Loading branch information
privatenumber committed Feb 8, 2023
1 parent c6bb06e commit 7c68769
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 45 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"dependencies": {
"esbuild": "^0.16.17",
"get-tsconfig": "^4.3.0",
"get-tsconfig": "github:privatenumber/get-tsconfig#npm/file-matcher",
"loader-utils": "^2.0.0",
"tapable": "^2.2.0",
"webpack-sources": "^1.4.3"
Expand All @@ -70,7 +70,7 @@
"memfs": "^3.4.13",
"mini-css-extract-plugin": "^1.6.2",
"pkgroll": "^1.8.0",
"tsx": "^3.12.2",
"tsx": "github:esbuild-kit/tsx#npm/ts-file-matcher",
"typescript": "^4.9.4",
"webpack": "^4.44.2",
"webpack-cli": "^4.10.0",
Expand Down
70 changes: 41 additions & 29 deletions pnpm-lock.yaml

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

9 changes: 6 additions & 3 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { transform as defaultEsbuildTransform } from 'esbuild';
import {
transform as defaultEsbuildTransform,
type TransformOptions,
} from 'esbuild';
import { getOptions } from 'loader-utils';
import webpack from 'webpack';
import type { LoaderOptions } from './types.js';
import { tsconfig } from './tsconfig.js';
import { fileMatcher } from './tsconfig.js';

async function ESBuildLoader(
this: webpack.loader.LoaderContext,
Expand Down Expand Up @@ -35,7 +38,7 @@ async function ESBuildLoader(
};

if (!('tsconfigRaw' in transformOptions)) {
transformOptions.tsconfigRaw = tsconfig;
transformOptions.tsconfigRaw = fileMatcher?.(this.resourcePath) as TransformOptions['tsconfigRaw'];
}

try {
Expand Down
5 changes: 0 additions & 5 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import webpack5 from 'webpack5';
import { matchObject } from 'webpack/lib/ModuleFilenameHelpers.js';
import { version } from '../package.json';
import type { EsbuildPluginOptions } from './types.js';
import { tsconfig } from './tsconfig.js';

type Compiler = webpack4.Compiler | webpack5.Compiler;
type Compilation = webpack4.compilation.Compilation | webpack5.Compilation;
Expand Down Expand Up @@ -137,10 +136,6 @@ export default function EsbuildPlugin(
options.minify = true;
}

if (!('tsconfigRaw' in options)) {
options.tsconfigRaw = tsconfig;
}

return {
apply(compiler: Compiler) {
if (!('format' in options)) {
Expand Down
7 changes: 6 additions & 1 deletion src/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { getTsconfig } from 'get-tsconfig';
import {
getTsconfig,
createFilesMatcher,
} from 'get-tsconfig';
import type { TransformOptions } from 'esbuild';

const foundTsconfig = getTsconfig();

export const tsconfig = foundTsconfig?.config as (TransformOptions['tsconfigRaw'] | undefined);

export const fileMatcher = foundTsconfig && createFilesMatcher(foundTsconfig);
32 changes: 27 additions & 5 deletions tests/specs/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import path from 'path';
import { createRequire } from 'node:module';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { execa } from 'execa';

const webpackCli = path.resolve('node_modules/webpack-cli/bin/cli.js');
const esbuildLoader = path.resolve('dist/index.cjs');

const detectStrictMode = `
(function (isStrict) {
arguments[0] = false;
return isStrict;
})(true)
`;

export default testSuite(({ describe }) => {
describe('tsconfig', ({ describe }) => {
describe('loader', ({ test }) => {
test('finds tsconfig.json and applies strict mode', async () => {
const fixture = await createFixture({
src: {
'index.ts': 'console.log(1)',
'index.ts': `module.exports = [${detectStrictMode}, require("./strict.ts")];`,
'strict.ts': `module.exports = ${detectStrictMode}`,
},
'webpack.config.js': `
module.exports = {
Expand All @@ -36,28 +45,41 @@ export default testSuite(({ describe }) => {
},
entry: './src/index.ts',
output: {
libraryTarget: 'commonjs2',
},
};
`,
'tsconfig.json': JSON.stringify({
compilerOptions: {
strict: true,
},
include: [
'src/strict.ts',
],
}),
});

await execa(webpackCli, {
cwd: fixture.path,
});

const code = await fixture.readFile('dist/main.js', 'utf8');
expect(code).toMatch('use strict');
const require = createRequire(import.meta.url);
expect(
require(path.join(fixture.path, 'dist/main.js')),
).toStrictEqual([false, true]);

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

describe('plugin', ({ test }) => {
test('finds tsconfig.json and applies strict mode', async () => {
/**
* Since the plugin applies on distribution assets, it should not apply
* any tsconfig settings.
*/
test('should not detect tsconfig.json and apply strict mode', async () => {
const fixture = await createFixture({
src: {
'index.js': 'console.log(1)',
Expand Down Expand Up @@ -86,7 +108,7 @@ export default testSuite(({ describe }) => {
});

const code = await fixture.readFile('dist/main.js', 'utf8');
expect(code).toMatch('use strict');
expect(code).not.toMatch('use strict');

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

0 comments on commit 7c68769

Please sign in to comment.