Skip to content

Commit

Permalink
fix: don't resolve tsconfig for externals
Browse files Browse the repository at this point in the history
Original issue: #363,

esbuild-loader would attempt to resolve typescript configs for external
sources under some conditions, and cause problems (in this case, the
resolved tsconfig.json referenced a config that is not in the dependency
tree and thus unresolvable)
  • Loading branch information
glsignal committed Jul 13, 2024
1 parent 351fc82 commit c62fc04
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ async function ESBuildLoader(
} else {
/* Detect tsconfig file */

// Don't look for tsconfig.json based on external sources (see
// https://github.com/privatenumber/esbuild-loader/issues/363)
if (resourcePath.match(/node_modules/) != null) return;

Check failure on line 86 in src/loader.ts

View workflow job for this annotation

GitHub Actions / Test

Use '===' to compare with null

Check failure on line 86 in src/loader.ts

View workflow job for this annotation

GitHub Actions / Test

Expected { after 'if' condition

// Webpack shouldn't be loading the same path multiple times so doesn't need to be cached
const tsconfig = getTsconfig(resourcePath, 'tsconfig.json', tsconfigCache);
if (tsconfig) {
Expand Down
74 changes: 73 additions & 1 deletion tests/specs/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import { createRequire } from 'node:module';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { execa } from 'execa';
import { execa, ExecaError } from 'execa';
import { tsconfigJson } from '../utils.js';

const webpackCli = path.resolve('node_modules/webpack-cli/bin/cli.js');
Expand Down Expand Up @@ -263,6 +263,78 @@ export default testSuite(({ describe }) => {
const code2 = await fixture.readFile('dist/index2.js', 'utf8');
expect(code2).toMatch('__publicField(this, "foo", 100);');
});

test('ignores tsconfig.json in third party dependencies', async () => {
await using fixture = await createFixture({
// Fake external dependency
node_modules: {
"fake-lib": {

Check failure on line 271 in tests/specs/tsconfig.ts

View workflow job for this annotation

GitHub Actions / Test

Strings must use singlequote
"index.js": `export function testFn() { return "Hi!" }`,

Check failure on line 272 in tests/specs/tsconfig.ts

View workflow job for this annotation

GitHub Actions / Test

Strings must use singlequote

Check failure on line 272 in tests/specs/tsconfig.ts

View workflow job for this annotation

GitHub Actions / Test

Strings must use singlequote
"package.json": JSON.stringify({

Check failure on line 273 in tests/specs/tsconfig.ts

View workflow job for this annotation

GitHub Actions / Test

Strings must use singlequote
name: "fake-lib",

Check failure on line 274 in tests/specs/tsconfig.ts

View workflow job for this annotation

GitHub Actions / Test

Strings must use singlequote
exports: {
".": "./index.js",

Check failure on line 276 in tests/specs/tsconfig.ts

View workflow job for this annotation

GitHub Actions / Test

Strings must use singlequote

Check failure on line 276 in tests/specs/tsconfig.ts

View workflow job for this annotation

GitHub Actions / Test

Strings must use singlequote
}

Check failure on line 277 in tests/specs/tsconfig.ts

View workflow job for this annotation

GitHub Actions / Test

Missing trailing comma
}),
'tsconfig.json': tsconfigJson({
extends: "something-imaginary",
}),
}
},
// Our project
src: {
'index.ts': `
import { testFn } from "fake-lib";
export default testFn;`,
},
'webpack.config.js': `
module.exports = {
mode: 'production',
optimization: {
minimize: false,
},
resolveLoader: {
alias: {
'esbuild-loader': ${JSON.stringify(esbuildLoader)},
},
},
module: {
rules: [
{
test: /\.[tj]sx?$/,
loader: 'esbuild-loader',
options: {
target: 'es2015',
}
}
],
},
entry: {
index: './src/index.ts',
},
};
`,
});

let result;

try {
result = await execa(webpackCli, {
cwd: fixture.path,
});
} catch (e) {
result = e as ExecaError;
}
const { exitCode, stderr } = result;

expect(stderr).not.toMatch("File 'something-imaginary' not found.")
expect(exitCode).toEqual(0);
});
});

describe('plugin', ({ test }) => {
Expand Down

0 comments on commit c62fc04

Please sign in to comment.