Skip to content

Commit

Permalink
test: handle tsconfig for first file
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Feb 8, 2023
1 parent bd6310e commit d1ac67b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ async function ESBuildLoader(
};

if (!('tsconfigRaw' in transformOptions)) {
if (fileMatcher) {
transformOptions.tsconfigRaw = fileMatcher(this.resourcePath) as TransformOptions['tsconfigRaw'];
} else {
if (!fileMatcher) {
const tsconfigPath = tsconfig && path.resolve(tsconfig);
foundTsconfig = (
tsconfigPath
Expand All @@ -65,6 +63,13 @@ async function ESBuildLoader(
fileMatcher = createFilesMatcher(foundTsconfig);
}
}

if (fileMatcher) {
transformOptions.tsconfigRaw = fileMatcher(
// Doesn't include query
this.resourcePath,
) as TransformOptions['tsconfigRaw'];
}
}

try {
Expand Down
64 changes: 60 additions & 4 deletions tests/specs/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default testSuite(({ describe }) => {
test('finds tsconfig.json and applies strict mode', async () => {
const fixture = await createFixture({
src: {
'index.ts': `module.exports = [${detectStrictMode}, require("./strict.ts")];`,
'strict.ts': `module.exports = ${detectStrictMode}`,
'index.ts': `module.exports = [${detectStrictMode}, require("./not-strict.ts")];`,
'not-strict.ts': `module.exports = ${detectStrictMode}`,
},
'webpack.config.js': `
module.exports = {
Expand Down Expand Up @@ -56,7 +56,7 @@ export default testSuite(({ describe }) => {
strict: true,
},
include: [
'src/strict.ts',
'src/index.ts',
],
}),
});
Expand All @@ -68,7 +68,63 @@ export default testSuite(({ describe }) => {
const require = createRequire(import.meta.url);
expect(
require(path.join(fixture.path, 'dist/main.js')),
).toStrictEqual([false, true]);
).toStrictEqual([true, false]);

await fixture.rm();
});

test('handles resource with query', async () => {
const fixture = await createFixture({
src: {
'index.ts': `module.exports = [${detectStrictMode}, require("./not-strict.ts?some-query")];`,
'not-strict.ts': `module.exports = ${detectStrictMode}`,
},
'webpack.config.js': `
module.exports = {
mode: 'production',
optimization: {
minimize: false,
},
resolveLoader: {
alias: {
'esbuild-loader': ${JSON.stringify(esbuildLoader)},
},
},
module: {
rules: [{
test: /\\.ts$/,
loader: 'esbuild-loader',
}],
},
entry: './src/index.ts',
output: {
libraryTarget: 'commonjs2',
},
};
`,
'tsconfig.json': JSON.stringify({
compilerOptions: {
strict: true,
},
include: [
'src/index.ts',
],
}),
});

await execa(webpackCli, {
cwd: fixture.path,
});

const require = createRequire(import.meta.url);
expect(
require(path.join(fixture.path, 'dist/main.js')),
).toStrictEqual([true, false]);

await fixture.rm();
});
Expand Down

0 comments on commit d1ac67b

Please sign in to comment.