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

Convert internal asset, dependency, and environment fields to numbers #9663

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/core/core/src/AssetGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import nullthrows from 'nullthrows';
import {ContentGraph} from '@parcel/graph';
import {createDependency} from './Dependency';
import {type ProjectPath, fromProjectPathRelative} from './projectPath';
import {DependencyFlags, EnvironmentFlags} from './types';

type InitOpts = {|
entries?: Array<ProjectPath>,
Expand Down Expand Up @@ -230,13 +231,14 @@ export default class AssetGraph extends ContentGraph<AssetGraphNode> {
env: target.env,
isEntry: true,
needsStableName: true,
symbols: target.env.isLibrary
? new Map([['*', {local: '*', isWeak: true, loc: null}]])
: undefined,
symbols:
target.env.flags & EnvironmentFlags.IS_LIBRARY
? new Map([['*', {local: '*', isWeak: true, loc: null}]])
: undefined,
}),
);

if (node.value.env.isLibrary) {
if (node.value.env.flags & EnvironmentFlags.IS_LIBRARY) {
// in library mode, all of the entry's symbols are "used"
node.usedSymbolsDown.add('*');
node.usedSymbolsUp.set('*', undefined);
Expand Down Expand Up @@ -401,7 +403,10 @@ export default class AssetGraph extends ContentGraph<AssetGraphNode> {
defer = deps.every(
d =>
d.symbols &&
!(d.env.isLibrary && d.isEntry) &&
!(
d.env.flags & EnvironmentFlags.IS_LIBRARY &&
d.flags & DependencyFlags.ENTRY
) &&
!d.symbols.has('*') &&
![...d.symbols.keys()].some(symbol => {
if (!resolvedAsset.symbols) return true;
Expand Down
15 changes: 11 additions & 4 deletions packages/core/core/src/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ import {ContentGraph, ALL_EDGE_TYPES, mapVisitor} from '@parcel/graph';
import {Hash, hashString} from '@parcel/rust';
import {DefaultMap, objectSortedEntriesDeep, getRootDir} from '@parcel/utils';

import {Priority, BundleBehavior, SpecifierType} from './types';
import {
Priority,
BundleBehavior,
SpecifierType,
AssetFlags,
EnvironmentFlags,
EnvironmentContextNames,
} from './types';
import {getBundleGroupId, getPublicId} from './utils';
import {ISOLATED_ENVS} from './public/Environment';
import {fromProjectPath, fromProjectPathRelative} from './projectPath';
Expand Down Expand Up @@ -201,7 +208,7 @@ export default class BundleGraph {
if (
node.type === 'dependency' &&
node.value.symbols != null &&
node.value.env.shouldScopeHoist &&
node.value.env.flags & EnvironmentFlags.SHOULD_SCOPE_HOIST &&
// Disable in dev mode because this feature is at odds with safeToIncrementallyBundle
isProduction
) {
Expand Down Expand Up @@ -528,7 +535,7 @@ export default class BundleGraph {
? BundleBehavior[opts.bundleBehavior]
: null,
isSplittable: opts.entryAsset
? opts.entryAsset.isBundleSplittable
? Boolean(opts.entryAsset.flags & AssetFlags.IS_BUNDLE_SPLITTABLE)
: opts.isSplittable,
isPlaceholder,
target,
Expand Down Expand Up @@ -1301,7 +1308,7 @@ export default class BundleGraph {
// If a bundle's environment is isolated, it can't access assets present
// in any ancestor bundles. Don't consider any assets reachable.
if (
ISOLATED_ENVS.has(bundle.env.context) ||
ISOLATED_ENVS.has(EnvironmentContextNames[bundle.env.context]) ||
!bundle.isSplittable ||
bundle.bundleBehavior === BundleBehavior.isolated ||
bundle.bundleBehavior === BundleBehavior.inline
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/src/CommittedAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SourceMap from '@parcel/source-map';
import {bufferStream, blobToStream, streamFromPromise} from '@parcel/utils';
import {generateFromAST} from './assetUtils';
import {deserializeRaw} from './serializer';
import {AssetFlags} from './types';

export default class CommittedAsset {
value: Asset;
Expand All @@ -27,7 +28,7 @@ export default class CommittedAsset {
getContent(): Blob | Promise<Buffer | string> {
if (this.content == null) {
if (this.value.contentKey != null) {
if (this.value.isLargeBlob) {
if (this.value.flags & AssetFlags.LARGE_BLOB) {
return this.options.cache.getStream(this.value.contentKey);
} else {
return this.options.cache.getBlob(this.value.contentKey);
Expand Down
54 changes: 45 additions & 9 deletions packages/core/core/src/Dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Priority,
BundleBehavior,
ExportsCondition,
DependencyFlags,
} from './types';

import {toInternalSourceLocation} from './utils';
Expand Down Expand Up @@ -63,20 +64,34 @@ export function createDependency(
(opts.packageConditions ? JSON.stringify(opts.packageConditions) : ''),
);

let {
placeholder,
promiseSymbol,
importAttributes,
isESM,
shouldWrap,
webworker,
...meta
} = opts.meta ?? {};

let dep: Dependency = {
id,
specifier: opts.specifier,
specifierType: SpecifierType[opts.specifierType],
priority: Priority[opts.priority ?? 'sync'],
needsStableName: opts.needsStableName ?? false,
bundleBehavior: opts.bundleBehavior
? BundleBehavior[opts.bundleBehavior]
: null,
isEntry: opts.isEntry ?? false,
isOptional: opts.isOptional ?? false,
: 255,
flags:
(opts.needsStableName ? DependencyFlags.NEEDS_STABLE_NAME : 0) |
(opts.isEntry ? DependencyFlags.ENTRY : 0) |
(opts.isOptional ? DependencyFlags.OPTIONAL : 0) |
(isESM ? DependencyFlags.IS_ESM : 0) |
(shouldWrap ? DependencyFlags.SHOULD_WRAP : 0) |
(webworker ? DependencyFlags.IS_WEBWORKER : 0),
loc: toInternalSourceLocation(projectRoot, opts.loc),
env: opts.env,
meta: opts.meta || {},
meta,
target: opts.target,
sourceAssetId: opts.sourceAssetId,
sourcePath: toProjectPath(projectRoot, opts.sourcePath),
Expand All @@ -98,6 +113,21 @@ export function createDependency(
pipeline: opts.pipeline,
};

if (typeof placeholder === 'string') {
dep.placeholder = placeholder;
}

if (typeof promiseSymbol === 'string') {
dep.promiseSymbol = promiseSymbol;
}

if (importAttributes && typeof importAttributes === 'object') {
dep.importAttributes = Object.entries(importAttributes).map(([k, v]) => ({
key: k,
value: (v: any),
}));
}

if (opts.packageConditions) {
convertConditions(opts.packageConditions, dep);
}
Expand All @@ -106,17 +136,23 @@ export function createDependency(
}

export function mergeDependencies(a: Dependency, b: Dependency): void {
let {meta, symbols, needsStableName, isEntry, isOptional, ...other} = b;
let {meta, symbols, flags, ...other} = b;
Object.assign(a, other);
Object.assign(a.meta, meta);
if (a.symbols && symbols) {
for (let [k, v] of symbols) {
a.symbols.set(k, v);
}
}
if (needsStableName) a.needsStableName = true;
if (isEntry) a.isEntry = true;
if (!isOptional) a.isOptional = false;
if (flags & DependencyFlags.NEEDS_STABLE_NAME) {
a.flags |= DependencyFlags.NEEDS_STABLE_NAME;
}
if (flags & DependencyFlags.ENTRY) {
a.flags |= DependencyFlags.ENTRY;
}
if (!(flags & DependencyFlags.OPTIONAL)) {
a.flags &= ~DependencyFlags.OPTIONAL;
}
}

function convertConditions(conditions: Array<string>, dep: Dependency) {
Expand Down
34 changes: 25 additions & 9 deletions packages/core/core/src/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import {hashString} from '@parcel/rust';
import {toInternalSourceLocation} from './utils';
import PublicEnvironment from './public/Environment';
import {environmentToInternalEnvironment} from './public/Environment';
import {
EnvironmentContext,
EnvironmentContextNames,
EnvironmentFlags,
OutputFormat,
OutputFormatNames,
SourceType,
SourceTypeNames,
} from './types';

const DEFAULT_ENGINES = {
browsers: ['> 0.25%'],
Expand Down Expand Up @@ -94,16 +103,19 @@ export function createEnvironment({
}
}

let flags =
(isLibrary ? EnvironmentFlags.IS_LIBRARY : 0) |
(shouldOptimize ? EnvironmentFlags.SHOULD_OPTIMIZE : 0) |
(shouldScopeHoist ? EnvironmentFlags.SHOULD_SCOPE_HOIST : 0);

let res: Environment = {
id: '',
context,
context: EnvironmentContext[context],
engines,
includeNodeModules,
outputFormat,
sourceType,
isLibrary,
shouldOptimize,
shouldScopeHoist,
outputFormat: OutputFormat[outputFormat],
sourceType: SourceType[sourceType],
flags,
sourceMap,
loc,
};
Expand All @@ -129,6 +141,12 @@ export function mergeEnvironments(
// $FlowFixMe - ignore the `id` that is already on a
return createEnvironment({
...a,
context: EnvironmentContextNames[a.context],
outputFormat: OutputFormatNames[a.outputFormat],
sourceType: SourceTypeNames[a.sourceType],
isLibrary: Boolean(a.flags & EnvironmentFlags.IS_LIBRARY),
shouldOptimize: Boolean(a.flags & EnvironmentFlags.SHOULD_OPTIMIZE),
shouldScopeHoist: Boolean(a.flags & EnvironmentFlags.SHOULD_SCOPE_HOIST),
...b,
loc: b.loc ? toInternalSourceLocation(projectRoot, b.loc) : a.loc,
});
Expand All @@ -142,9 +160,7 @@ function getEnvironmentHash(env: Environment): string {
env.includeNodeModules,
env.outputFormat,
env.sourceType,
env.isLibrary,
env.shouldOptimize,
env.shouldScopeHoist,
env.flags,
env.sourceMap,
]),
);
Expand Down
5 changes: 3 additions & 2 deletions packages/core/core/src/PackagerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {getInvalidationId, getInvalidationHash} from './assetUtils';
import {optionsProxy} from './utils';
import {invalidateDevDeps} from './requests/DevDepRequest';
import {tracer, PluginTracer} from '@parcel/profiler';
import {EnvironmentContext} from './types';

type Opts = {|
config: ParcelConfig,
Expand Down Expand Up @@ -584,7 +585,7 @@ export default class PackagerRunner {
sourceRoot = bundle.env.sourceMap.sourceRoot;
} else if (
this.options.serveOptions &&
bundle.target.env.context === 'browser'
bundle.target.env.context === EnvironmentContext.browser
) {
sourceRoot = '/__parcel_source_root';
}
Expand All @@ -594,7 +595,7 @@ export default class PackagerRunner {
bundle.env.sourceMap.inlineSources !== undefined
) {
inlineSources = bundle.env.sourceMap.inlineSources;
} else if (bundle.target.env.context !== 'node') {
} else if (bundle.target.env.context !== EnvironmentContext.node) {
// inlining should only happen in production for browser targets by default
inlineSources = this.options.mode === 'production';
}
Expand Down
7 changes: 4 additions & 3 deletions packages/core/core/src/SymbolPropagation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import nullthrows from 'nullthrows';
import {setEqual} from '@parcel/utils';
import logger from '@parcel/logger';
import {md, convertSourceLocationToHighlight} from '@parcel/diagnostic';
import {BundleBehavior} from './types';
import {AssetFlags, BundleBehavior} from './types';
import {fromProjectPathRelative, fromProjectPath} from './projectPath';

export function propagateSymbols({
Expand Down Expand Up @@ -150,7 +150,7 @@ export function propagateSymbols({
let depUsedSymbolsDown = new Set();
dep.usedSymbolsDown = depUsedSymbolsDown;
if (
assetNode.value.sideEffects ||
assetNode.value.flags & AssetFlags.SIDE_EFFECTS ||
// Incoming dependency with cleared symbols
addAll ||
// For entries, we still need to add dep.value.symbols of the entry (which are "used" but not according to the symbols data)
Expand Down Expand Up @@ -408,7 +408,8 @@ export function propagateSymbols({
let reexport = reexportedSymbols.get(s);
let v =
// Forward a reexport only if the current asset is side-effect free and not external
!assetNode.value.sideEffects && reexport != null
!(assetNode.value.flags & AssetFlags.SIDE_EFFECTS) &&
reexport != null
? reexport
: {
asset: assetNode.id,
Expand Down
7 changes: 4 additions & 3 deletions packages/core/core/src/Transformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
import {invalidateOnFileCreateToInternal, createInvalidations} from './utils';
import invariant from 'assert';
import {tracer, PluginTracer} from '@parcel/profiler';
import {AssetFlags} from './types';

type GenerateFunc = (input: UncommittedAsset) => Promise<GenerateOutput>;

Expand Down Expand Up @@ -176,7 +177,7 @@ export default class Transformation {

let pipeline = await this.loadPipeline(
this.request.filePath,
asset.value.isSource,
Boolean(asset.value.flags & AssetFlags.IS_SOURCE),
asset.value.pipeline,
);
let assets, error;
Expand Down Expand Up @@ -290,7 +291,7 @@ export default class Transformation {
if (asset.value.type !== initialType) {
nextPipeline = await this.loadNextPipeline({
filePath: initialAsset.value.filePath,
isSource: asset.value.isSource,
isSource: Boolean(asset.value.flags & AssetFlags.IS_SOURCE),
newType: asset.value.type,
newPipeline: asset.value.pipeline,
currentPipeline: pipeline,
Expand Down Expand Up @@ -363,7 +364,7 @@ export default class Transformation {
asset.value.type !== initialType &&
(await this.loadNextPipeline({
filePath: initialAsset.value.filePath,
isSource: asset.value.isSource,
isSource: Boolean(asset.value.flags & AssetFlags.IS_SOURCE),
newType: asset.value.type,
newPipeline: asset.value.pipeline,
currentPipeline: pipeline,
Expand Down
15 changes: 10 additions & 5 deletions packages/core/core/src/UncommittedAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {createDependency, mergeDependencies} from './Dependency';
import {mergeEnvironments} from './Environment';
import {PARCEL_VERSION} from './constants';
import {createAsset, createAssetIdFromOptions} from './assetUtils';
import {BundleBehaviorNames} from './types';
import {AssetFlags, BundleBehaviorNames} from './types';
import {invalidateOnFileCreateToInternal, createInvalidations} from './utils';
import {type ProjectPath, fromProjectPath} from './projectPath';

Expand Down Expand Up @@ -116,7 +116,9 @@ export default class UncommittedAsset {
this.value.stats.size = size;
}

this.value.isLargeBlob = this.content instanceof Readable;
if (this.content instanceof Readable) {
this.value.flags |= AssetFlags.LARGE_BLOB;
}
this.value.committed = true;
}

Expand Down Expand Up @@ -367,8 +369,9 @@ export default class UncommittedAsset {
? null
: BundleBehaviorNames[this.value.bundleBehavior]),
isBundleSplittable:
result.isBundleSplittable ?? this.value.isBundleSplittable,
isSource: this.value.isSource,
result.isBundleSplittable ??
Boolean(this.value.flags & AssetFlags.IS_BUNDLE_SPLITTABLE),
isSource: Boolean(this.value.flags & AssetFlags.IS_SOURCE),
env: mergeEnvironments(
this.options.projectRoot,
this.value.env,
Expand All @@ -391,7 +394,9 @@ export default class UncommittedAsset {
},
// $FlowFixMe
symbols: result.symbols,
sideEffects: result.sideEffects ?? this.value.sideEffects,
sideEffects:
result.sideEffects ??
Boolean(this.value.flags & AssetFlags.SIDE_EFFECTS),
uniqueKey: result.uniqueKey,
astGenerator: result.ast
? {type: result.ast.type, version: result.ast.version}
Expand Down