Skip to content

Commit

Permalink
fix: warn on tsconfig resolution error, continue transform
Browse files Browse the repository at this point in the history
  • Loading branch information
glsignal committed Jul 15, 2024
1 parent 6b20c7b commit c8c8668
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
19 changes: 13 additions & 6 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,21 @@ 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;
let tsconfig;

try {
// Webpack shouldn't be loading the same path multiple times so doesn't need to be cached
tsconfig = getTsconfig(resourcePath, 'tsconfig.json', tsconfigCache);
} catch (error) {
if (resourcePath.split(path.sep).includes('node_modules')) {
this.emitWarning(
new Error(`[esbuild-loader] Error while discovering tsconfig.json in dependency: "${error?.toString()}"`),
);
} else {
throw error;
}
}

// 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) {
const fileMatcher = createFilesMatcher(tsconfig);
transformOptions.tsconfigRaw = fileMatcher(resourcePath) as TransformOptions['tsconfigRaw'];
Expand Down
14 changes: 8 additions & 6 deletions tests/specs/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,9 @@ export default testSuite(({ describe }) => {
// Fake external dependency
node_modules: {
'fake-lib': {
'index.js': 'export function testFn() { return "Hi!" }',
'index.ts': 'export function testFn(): string { return "Hi!" }',
'package.json': JSON.stringify({
name: 'fake-lib',
exports: {
'.': './index.js',
},
}),
'tsconfig.json': tsconfigJson({
extends: 'something-imaginary',
Expand Down Expand Up @@ -302,6 +299,10 @@ export default testSuite(({ describe }) => {
},
},
resolve: {
extensions: ['.ts', '.js'],
},
module: {
rules: [
{
Expand Down Expand Up @@ -330,9 +331,10 @@ export default testSuite(({ describe }) => {
} catch (error) {
result = error as ExecaError;
}
const { exitCode, stderr } = result;
const { exitCode, stdout } = result;

expect(stderr).not.toMatch("File 'something-imaginary' not found.");
// We log this as a warning, and continue the transform
expect(stdout).toMatch("Error while discovering tsconfig.json in dependency: \"Error: File 'something-imaginary' not found.\"");
expect(exitCode).toEqual(0);
});
});
Expand Down

0 comments on commit c8c8668

Please sign in to comment.