Skip to content
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
4 changes: 2 additions & 2 deletions .c8rc.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"checkCoverage": true,
"statements": 99.96,
"statements": 99.97,
"branches": 98.7,
"functions": 100,
"lines": 99.96,
"lines": 99.97,
"exclude": [
"bin",
"configs",
Expand Down
38 changes: 8 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@
"rechoir": "0.8.0",
"safe-regex": "2.1.1",
"semver": "7.8.1",
"tsconfig-paths-webpack-plugin": "4.2.0",
"watskeburt": "5.0.3"
},
"devDependencies": {
Expand Down Expand Up @@ -374,4 +373,4 @@
"vue-template-compiler": ">=2.0.0 <3.0.0",
"@vue/compiler-sfc": ">=3.0.0 <4.0.0"
}
}
}
2 changes: 1 addition & 1 deletion src/config-utl/extract-depcruise-config/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default async function extractDepcruiseConfig(
const lResolvedFileName = resolve(
pConfigFileName,
pBaseDirectory,
await normalizeResolveOptions(
normalizeResolveOptions(
{
extensions: [".js", ".json", ".cjs", ".mjs"],
},
Expand Down
8 changes: 5 additions & 3 deletions src/extract/resolve/resolve.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve as resolvePath } from "node:path";
import enhancedResolve from "enhanced-resolve";
import { stripQueryParameters } from "../helpers.mjs";
import pathToPosix from "#utl/path-to-posix.mjs";

/** @import {IResolveOptions} from "../../../types/resolve-options.mjs" */

Expand Down Expand Up @@ -50,8 +50,10 @@ export function resolve(
return stripQueryParameters(
gResolvers.get(pCachingContext).resolveSync(
{},
// lookupStartPath
pathToPosix(pFileDirectory),
// lookupStartPath - must be absolute so enhanced-resolve classifies
// it correctly on Windows (relative paths with backslashes would be
// treated as PathType.Normal and POSIX-normalised, breaking ".." resolution)
resolvePath(pFileDirectory),
// request
pModuleName,
),
Expand Down
2 changes: 1 addition & 1 deletion src/main/cruise.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default async function cruise(
);

bus.summary("startup: get resolve options", c(5));
const lNormalizedResolveOptions = await normalizeResolveOptions(
const lNormalizedResolveOptions = normalizeResolveOptions(
pResolveOptions,
lCruiseOptions,
pTranspileOptions?.tsConfig,
Expand Down
63 changes: 10 additions & 53 deletions src/main/resolve-options/normalize.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "node:fs";
import { resolve as resolvePath } from "node:path";
import enhancedResolve from "enhanced-resolve";
import { scannableExtensions } from "#extract/transpile/meta.mjs";
import {
Expand Down Expand Up @@ -61,64 +62,20 @@ function getNonOverridableResolveOptions(pCacheDuration) {
};
}

function pushPlugin(pPlugins, pPluginToPush) {
return (pPlugins || []).concat(pPluginToPush);
}

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

// 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) {
const { default: TsConfigPathsPlugin } =
await import("tsconfig-paths-webpack-plugin");
lResolveOptions.plugins = pushPlugin(
lResolveOptions.plugins,
// @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
// or with the supported ones.
extensions:
pResolveOptionsFromDCConfig.extensions ||
pResolveOptions.extensions ||
DEFAULT_RESOLVE_OPTIONS.extensions,
}),
);
lResolveOptions.tsconfig = {
configFile: resolvePath(pResolveOptions.tsConfig),

// baseUrl: pTSConfig?.options?.baseUrl ? undefined : "./",
// references: pTSConfig?.options?.references ?? []
};
}

return {
Expand All @@ -140,14 +97,14 @@ async function compileResolveOptions(
* @returns
*/
// eslint-disable-next-line complexity
export default async function normalizeResolveOptions(
export default function normalizeResolveOptions(
pResolveOptions,
pOptions,
pTSConfig,
) {
const lRuleSet = pOptions?.ruleSet ?? {};

return await compileResolveOptions(
return compileResolveOptions(
{
// EnhancedResolve's symlinks:
// - true => symlinks are followed (vv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"baseUrl": "./",
"paths": {
"@modules/*": ["./libs/*"],
"@sister": ["./libs/zus/*"]
"@sister": ["./libs/zus/"]
},
"noEmit": true,
"skipLibCheck": true
Expand Down
8 changes: 4 additions & 4 deletions test/extract/get-dependencies.amd.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ describe("[I] extract/getDependencies - AMD - ", () => {
// amdFixtures.forEach((pFixture) => runFixture(pFixture, "tsc"));
});
describe("[I] extract/getDependencies - AMD - with bangs", () => {
it("splits extracts the module part of the plugin + module - regular requirejs", async () => {
it("splits extracts the module part of the plugin + module - regular requirejs", () => {
const lOptions = normalizeCruiseOptions({ moduleSystems: ["amd"] });
const lResolveOptions = await normalizeResolveOptions(
const lResolveOptions = normalizeResolveOptions(
{ bustTheCache: true },
lOptions,
);
Expand All @@ -62,9 +62,9 @@ describe("[I] extract/getDependencies - AMD - with bangs", () => {
);
});

it("splits bang!./blabla into bang and ./blabla - CommonJS wrapper", async () => {
it("splits bang!./blabla into bang and ./blabla - CommonJS wrapper", () => {
const lOptions = normalizeCruiseOptions({ moduleSystems: ["amd"] });
const lResolveOptions = await normalizeResolveOptions(
const lResolveOptions = normalizeResolveOptions(
{ bustTheCache: true },
lOptions,
);
Expand Down
12 changes: 6 additions & 6 deletions test/extract/get-dependencies.cjs.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ function runFixture(pFixture, pParser = "acorn") {
lOptions.preserveSymlinks = pFixture.input.preserveSymlinks;
}

it(`${pFixture.title} (with '${pParser}' as parser)`, async () => {
it(`${pFixture.title} (with '${pParser}' as parser)`, () => {
deepEqual(
extractDependencies(
pFixture.input.fileName,
normalizeCruiseOptions(lOptions),
await normalizeResolveOptions(
normalizeResolveOptions(
{ bustTheCache: true, resolveLicenses: true },
normalizeCruiseOptions(lOptions),
),
Expand Down Expand Up @@ -71,9 +71,9 @@ describe("[I] extract/getDependencies - CommonJS - ", () => {
});

describe("[I] extract/getDependencies - CommonJS - with bangs", () => {
it("strips the inline loader prefix from the module name when resolving", async () => {
it("strips the inline loader prefix from the module name when resolving", () => {
const lOptions = normalizeCruiseOptions({ moduleSystems: ["cjs"] });
const lResolveOptions = await normalizeResolveOptions(
const lResolveOptions = normalizeResolveOptions(
{ bustTheCache: true },
lOptions,
);
Expand Down Expand Up @@ -101,9 +101,9 @@ describe("[I] extract/getDependencies - CommonJS - with bangs", () => {
);
});

it("strips multiple inline loader prefixes from the module name when resolving", async () => {
it("strips multiple inline loader prefixes from the module name when resolving", () => {
const lOptions = normalizeCruiseOptions({ moduleSystems: ["cjs"] });
const lResolveOptions = await normalizeResolveOptions(
const lResolveOptions = normalizeResolveOptions(
{ bustTheCache: true },
lOptions,
);
Expand Down
Loading
Loading