Skip to content

Commit

Permalink
Fix perf of lazy mode glob matches
Browse files Browse the repository at this point in the history
  • Loading branch information
marcins committed Sep 21, 2023
1 parent b0feb72 commit 33b955a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
21 changes: 10 additions & 11 deletions packages/core/core/src/requests/AssetGraphRequest.js
Expand Up @@ -19,7 +19,7 @@ import type {Diagnostic} from '@parcel/diagnostic';

import invariant from 'assert';
import nullthrows from 'nullthrows';
import {PromiseQueue, setEqual, isGlobMatch} from '@parcel/utils';
import {PromiseQueue, setEqual} from '@parcel/utils';
import {hashString} from '@parcel/rust';
import ThrowableDiagnostic from '@parcel/diagnostic';
import {Priority} from '../types';
Expand All @@ -39,8 +39,8 @@ type AssetGraphRequestInput = {|
optionsRef: SharedReference,
name: string,
shouldBuildLazily?: boolean,
lazyIncludes?: string[],
lazyExcludes?: string[],
lazyIncludes?: RegExp[],
lazyExcludes?: RegExp[],
requestedAssetIds?: Set<string>,
|};

Expand Down Expand Up @@ -113,8 +113,8 @@ export class AssetGraphBuilder {
name: string;
cacheKey: string;
shouldBuildLazily: boolean;
lazyIncludes: string[];
lazyExcludes: string[];
lazyIncludes: RegExp[];
lazyExcludes: RegExp[];
requestedAssetIds: Set<string>;
isSingleChangeRebuild: boolean;
assetGroupsWithRemovedParents: Set<NodeId>;
Expand Down Expand Up @@ -329,17 +329,16 @@ export class AssetGraphBuilder {
// For conditional lazy building - if this node matches the `lazyInclude` globs that means we want
// only those nodes to be treated as lazy - that means if this node does _NOT_ match that glob, then we
// also consider it not lazy (so it gets marked as requested).
const relativePath = fromProjectPathRelative(node.value.filePath);
if (this.lazyIncludes.length > 0) {
isNodeLazy = isGlobMatch(
fromProjectPathRelative(node.value.filePath),
this.lazyIncludes,
isNodeLazy = this.lazyIncludes.some(lazyIncludeRegex =>
relativePath.match(lazyIncludeRegex),
);
}
// Excludes override includes, so a node is _not_ lazy if it is included in the exclude list.
if (this.lazyExcludes.length > 0 && isNodeLazy) {
isNodeLazy = !isGlobMatch(
fromProjectPathRelative(node.value.filePath),
this.lazyExcludes,
isNodeLazy = !this.lazyExcludes.some(lazyExcludeRegex =>
relativePath.match(lazyExcludeRegex),
);
}

Expand Down
17 changes: 14 additions & 3 deletions packages/core/core/src/resolveOptions.js
Expand Up @@ -14,7 +14,13 @@ import {hashString} from '@parcel/rust';
import {NodeFS} from '@parcel/fs';
import {LMDBCache, FSCache} from '@parcel/cache';
import {NodePackageManager} from '@parcel/package-manager';
import {getRootDir, relativePath, resolveConfig, isGlob} from '@parcel/utils';
import {
getRootDir,
relativePath,
resolveConfig,
isGlob,
globToRegex,
} from '@parcel/utils';
import loadDotEnv from './loadDotEnv';
import {toProjectPath} from './projectPath';
import {getResolveFrom} from './requests/ParcelConfigRequest';
Expand All @@ -30,6 +36,11 @@ function generateInstanceId(entries: Array<FilePath>): string {
);
}

// Compiles an array of globs to regex - used for lazy include/excludes
function compileGlobs(globs: string[]): RegExp[] {
return globs.map(glob => globToRegex(glob));
}

export default async function resolveOptions(
initialOptions: InitialParcelOptions,
): Promise<ParcelOptions> {
Expand Down Expand Up @@ -104,13 +115,13 @@ export default async function resolveOptions(
: undefined;

let shouldBuildLazily = initialOptions.shouldBuildLazily ?? false;
let lazyIncludes = initialOptions.lazyIncludes ?? [];
let lazyIncludes = compileGlobs(initialOptions.lazyIncludes ?? []);
if (lazyIncludes.length > 0 && !shouldBuildLazily) {
throw new Error(
'Lazy includes can only be provided when lazy building is enabled',
);
}
let lazyExcludes = initialOptions.lazyExcludes ?? [];
let lazyExcludes = compileGlobs(initialOptions.lazyExcludes ?? []);
if (lazyExcludes.length > 0 && !shouldBuildLazily) {
throw new Error(
'Lazy excludes can only be provided when lazy building is enabled',
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/types.js
Expand Up @@ -270,8 +270,8 @@ export type ParcelOptions = {|
shouldContentHash: boolean,
serveOptions: ServerOptions | false,
shouldBuildLazily: boolean,
lazyIncludes: string[],
lazyExcludes: string[],
lazyIncludes: RegExp[],
lazyExcludes: RegExp[],
shouldBundleIncrementally: boolean,
shouldAutoInstall: boolean,
logLevel: LogLevel,
Expand Down

0 comments on commit 33b955a

Please sign in to comment.