Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(main): still load tsconfig-paths-webpack-plugin when baseUrl misses from tsconfig #819

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/main/resolve-options/normalize.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,20 @@ function pushPlugin(pPlugins, pPluginToPush) {
return (pPlugins || []).concat(pPluginToPush);
}

function isTsConfigPathsEligible(pTSConfig) {
return has(pTSConfig, "options.baseUrl");
}

// eslint-disable-next-line max-lines-per-function
async function compileResolveOptions(
pResolveOptions,
pTSConfig,
pResolveOptionsFromDCConfig
) {
let lResolveOptions = {};

// TsConfigPathsPlugin requires a baseUrl to be present in the tsconfig,
// otherwise it prints scary messages that it didn't and read the tsConfig
// (potentially making users think it's dependency-cruiser disregarding the
// tsconfig). Hence: only load TsConfigPathsPlugin when an options.baseUrl exists
// Also: there's a performance impact of ~1 ms per resolve even when there
// There's a performance impact of ~1 ms per resolve even when there
// are 0 paths in the tsconfig, so not loading it when not necessary
// will be a win.
// Also: requiring the plugin only when it's necessary will save some
// startup time (especially on a cold require cache)
if (pResolveOptions.tsConfig && isTsConfigPathsEligible(pTSConfig)) {
if (pResolveOptions.tsConfig) {
const { default: TsConfigPathsPlugin } = await import(
"tsconfig-paths-webpack-plugin"
);
Expand All @@ -92,6 +85,29 @@ async function compileResolveOptions(
// @ts-expect-error TS2351 "TsConfPathsPlugin is not constructable" - is unjustified
new TsConfigPathsPlugin({
configFile: pResolveOptions.tsConfig,
// TsConfigPathsPlugin requires a baseUrl to be present in the tsconfig,
// otherwise it prints scary messages that it didn't and read the tsConfig
// (potentially making users think it's dependency-cruiser disregarding the
// tsconfig). Hence up till version 13.0.4 dependency-cruiser only loaded
// TsConfigPathsPlugin when an options.baseUrl existed. However, this
// isn't necessary anymore:
// - [tsconfig#baseUrl documentation](https://www.typescriptlang.org/tsconfig#baseUrl)
// UNrecommends the use of the baseUrl for non-AMD projects
// - [tsconfig-paths PR #207](https://github.com/dividab/tsconfig-paths/pull/208)
// 'tolerates' undefined baseUrls
//
// Hence, until
// [tpwp issue #99](https://github.com/dividab/tsconfig-paths-webpack-plugin/issues/99)
// lands:
// - pass a default baseUrl to TsConfigPathsPlugin if the baseUrl isn't available
// - pass undefined in all other cases; TsConfigPathsPlugin will read
// it from the tsconfig.json in that case. Passing the processed baseUrl
// (pTSConfig?.options?.baseUrl) instead would've been more obvious, but
// doesn't work, as that is an absolute path and tsconfig-paths(-wpp)
// seems to process that again resulting in invalid paths and unresolved
// or erroneous dependencies
// eslint-disable-next-line no-undefined
baseUrl: pTSConfig?.options?.baseUrl ? undefined : "./",
// TsConfigPathsPlugin doesn't (can't) read enhanced-resolve's
// list of extensions, and the default it uses for extensions
// so we do it ourselves - either with the extensions passed
Expand Down
27 changes: 24 additions & 3 deletions test/main/resolve-options/normalize.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ describe("[I] main/resolve-options/normalize", () => {
expect(lNormalizedOptions.useSyncFileSystemCalls).to.equal(true);
});

it("does not add the typescript paths plugin to the plugins if a tsConfig is specified without a baseUrl", async () => {
it("does not add the typescript paths plugin to the plugins if no tsConfig is specified", async () => {
const lNormalizedOptions = await normalizeResolveOptions(
{},
normalizeCruiseOptions({
ruleSet: { options: { tsConfig: { fileName: TEST_TSCONFIG } } },
ruleSet: { options: {} },
}),
lTsconfigContents
);
Expand All @@ -44,14 +44,35 @@ describe("[I] main/resolve-options/normalize", () => {
lDefaultNoOfResolveOptions
);
expect(lNormalizedOptions.symlinks).to.equal(false);
expect(lNormalizedOptions.tsConfig).to.equal(TEST_TSCONFIG);
expect(lNormalizedOptions.tsConfig).to.equal(null);
expect(lNormalizedOptions.combinedDependencies).to.equal(false);
expect(lNormalizedOptions).to.ownProperty("extensions");
expect(lNormalizedOptions).to.ownProperty("fileSystem");
expect((lNormalizedOptions.plugins || []).length).to.equal(0);
expect(lNormalizedOptions.useSyncFileSystemCalls).to.equal(true);
});

it("adds the typescript paths plugin to the plugins if a tsConfig is specified, even without a baseUrl", async () => {
const lNormalizedOptions = await normalizeResolveOptions(
{},
normalizeCruiseOptions({
ruleSet: { options: { tsConfig: { fileName: TEST_TSCONFIG } } },
}),
lTsconfigContents
);

expect(Object.keys(lNormalizedOptions).length).to.equal(
lDefaultNoOfResolveOptions + 1
);
expect(lNormalizedOptions.symlinks).to.equal(false);
expect(lNormalizedOptions.tsConfig).to.equal(TEST_TSCONFIG);
expect(lNormalizedOptions.combinedDependencies).to.equal(false);
expect(lNormalizedOptions).to.ownProperty("extensions");
expect(lNormalizedOptions).to.ownProperty("fileSystem");
expect((lNormalizedOptions.plugins || []).length).to.equal(1);
expect(lNormalizedOptions.useSyncFileSystemCalls).to.equal(true);
});

it("adds the typescript paths plugin to the plugins if a tsConfig is specified with a baseUrl and actual paths", async () => {
const lNormalizedOptions = await normalizeResolveOptions(
{},
Expand Down