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 14, 2024
1 parent 351fc82 commit b95fd2b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ 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) {
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': {
'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 (error) {
result = error 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 b95fd2b

Please sign in to comment.