diff --git a/src/loader.ts b/src/loader.ts index c8453b3..90db8b7 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -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; + // 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) { diff --git a/tests/specs/tsconfig.ts b/tests/specs/tsconfig.ts index b005224..9a5e518 100644 --- a/tests/specs/tsconfig.ts +++ b/tests/specs/tsconfig.ts @@ -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'); @@ -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": { + "index.js": `export function testFn() { return "Hi!" }`, + "package.json": JSON.stringify({ + name: "fake-lib", + exports: { + ".": "./index.js", + } + }), + '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 }) => {