Skip to content

Commit b95fd2b

Browse files
committed
fix: don't resolve tsconfig for externals
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)
1 parent 351fc82 commit b95fd2b

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/loader.ts

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ async function ESBuildLoader(
8181
} else {
8282
/* Detect tsconfig file */
8383

84+
// Don't look for tsconfig.json based on external sources (see
85+
// https://github.com/privatenumber/esbuild-loader/issues/363)
86+
if (resourcePath.match(/node_modules/) !== null) {
87+
return;
88+
}
89+
8490
// Webpack shouldn't be loading the same path multiple times so doesn't need to be cached
8591
const tsconfig = getTsconfig(resourcePath, 'tsconfig.json', tsconfigCache);
8692
if (tsconfig) {

tests/specs/tsconfig.ts

+73-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'path';
22
import { createRequire } from 'node:module';
33
import { testSuite, expect } from 'manten';
44
import { createFixture } from 'fs-fixture';
5-
import { execa } from 'execa';
5+
import { execa, ExecaError } from 'execa';
66
import { tsconfigJson } from '../utils.js';
77

88
const webpackCli = path.resolve('node_modules/webpack-cli/bin/cli.js');
@@ -263,6 +263,78 @@ export default testSuite(({ describe }) => {
263263
const code2 = await fixture.readFile('dist/index2.js', 'utf8');
264264
expect(code2).toMatch('__publicField(this, "foo", 100);');
265265
});
266+
267+
test('ignores tsconfig.json in third party dependencies', async () => {
268+
await using fixture = await createFixture({
269+
// Fake external dependency
270+
node_modules: {
271+
'fake-lib': {
272+
'index.js': 'export function testFn() { return "Hi!" }',
273+
'package.json': JSON.stringify({
274+
name: 'fake-lib',
275+
exports: {
276+
'.': './index.js',
277+
},
278+
}),
279+
'tsconfig.json': tsconfigJson({
280+
extends: 'something-imaginary',
281+
}),
282+
},
283+
},
284+
// Our project
285+
src: {
286+
'index.ts': `
287+
import { testFn } from "fake-lib";
288+
289+
export default testFn;`,
290+
},
291+
'webpack.config.js': `
292+
module.exports = {
293+
mode: 'production',
294+
295+
optimization: {
296+
minimize: false,
297+
},
298+
299+
resolveLoader: {
300+
alias: {
301+
'esbuild-loader': ${JSON.stringify(esbuildLoader)},
302+
},
303+
},
304+
305+
module: {
306+
rules: [
307+
{
308+
test: /.[tj]sx?$/,
309+
loader: 'esbuild-loader',
310+
options: {
311+
target: 'es2015',
312+
}
313+
}
314+
],
315+
},
316+
317+
entry: {
318+
index: './src/index.ts',
319+
},
320+
};
321+
`,
322+
});
323+
324+
let result;
325+
326+
try {
327+
result = await execa(webpackCli, {
328+
cwd: fixture.path,
329+
});
330+
} catch (error) {
331+
result = error as ExecaError;
332+
}
333+
const { exitCode, stderr } = result;
334+
335+
expect(stderr).not.toMatch("File 'something-imaginary' not found.");
336+
expect(exitCode).toEqual(0);
337+
});
266338
});
267339

268340
describe('plugin', ({ test }) => {

0 commit comments

Comments
 (0)