Skip to content

Commit

Permalink
Add support for worklets and fix pipeline bugs (parcel-bundler#6495)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored Jun 24, 2021
1 parent 41a9a11 commit dee58e9
Show file tree
Hide file tree
Showing 47 changed files with 631 additions and 71 deletions.
21 changes: 16 additions & 5 deletions packages/bundlers/default/src/DefaultBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ export default (new Bundler({
let bundle = bundleGraph.createBundle({
entryAsset: asset,
needsStableName:
dependency.bundleBehavior === 'inline' ||
asset.bundleBehavior === 'inline'
? false
: dependency.isEntry || dependency.needsStableName,
bundleBehavior: dependency.bundleBehavior ?? asset.bundleBehavior,
target: bundleGroup.target,
});
bundleByType.set(bundle.type, bundle);
Expand Down Expand Up @@ -166,11 +168,12 @@ export default (new Bundler({
target: bundleGroup.target,
needsStableName:
asset.bundleBehavior === 'inline' ||
dependency.bundleBehavior === 'inline' ||
(dependency.priority === 'parallel' &&
!dependency.needsStableName)
? false
: parentBundle.needsStableName,
isInline: asset.bundleBehavior === 'inline',
bundleBehavior: dependency.bundleBehavior ?? asset.bundleBehavior,
isSplittable: asset.isBundleSplittable ?? true,
pipeline: asset.pipeline,
});
Expand Down Expand Up @@ -208,7 +211,12 @@ export default (new Bundler({

// Step 2: Remove asset graphs that begin with entries to other bundles.
bundleGraph.traverseBundles(bundle => {
if (bundle.isInline || !bundle.isSplittable || bundle.env.isIsolated()) {
if (
bundle.bundleBehavior === 'inline' ||
bundle.bundleBehavior === 'isolated' ||
!bundle.isSplittable ||
bundle.env.isIsolated()
) {
return;
}

Expand All @@ -232,7 +240,8 @@ export default (new Bundler({
// to predict and reference.
// TODO: reconsider this. This is only true for the global output format.
!containingBundle.needsStableName &&
!containingBundle.isInline &&
containingBundle.bundleBehavior !== 'inline' &&
containingBundle.bundleBehavior !== 'isolated' &&
containingBundle.isSplittable,
);

Expand All @@ -245,7 +254,8 @@ export default (new Bundler({
group =>
bundleGraph
.getBundlesInBundleGroup(group)
.filter(b => !b.isInline).length < config.maxParallelRequests,
.filter(b => b.bundleBehavior !== 'inline').length <
config.maxParallelRequests,
)
) {
bundleGraph.createBundleReference(candidate, bundle);
Expand Down Expand Up @@ -399,7 +409,8 @@ export default (new Bundler({
group =>
bundleGraph
.getBundlesInBundleGroup(group)
.filter(b => !b.isInline).length < config.maxParallelRequests,
.filter(b => b.bundleBehavior !== 'inline').length <
config.maxParallelRequests,
)
) {
eligibleSourceBundles.add(bundle);
Expand Down
6 changes: 5 additions & 1 deletion packages/configs/default/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"types:*.{ts,tsx}": ["@parcel/transformer-typescript-types"],
"bundle-text:*": ["...", "@parcel/transformer-inline-string"],
"data-url:*": ["...", "@parcel/transformer-inline-string"],
"worklet:*.{js,mjs,jsm,jsx,es6,cjs,ts,tsx}": [
"@parcel/transformer-worklet",
"..."
],
"*.{js,mjs,jsm,jsx,es6,cjs,ts,tsx}": [
"@parcel/transformer-babel",
"@parcel/transformer-js",
Expand Down Expand Up @@ -35,7 +39,7 @@
"style:*.vue": ["@parcel/transformer-vue"],
"custom:*.vue": ["@parcel/transformer-vue"],
"url:*.{png,jpg,jpeg,webp}": ["@parcel/transformer-image"],
"url:*": ["@parcel/transformer-raw"]
"url:*": ["...", "@parcel/transformer-raw"]
},
"namers": ["@parcel/namer-default"],
"runtimes": [
Expand Down
1 change: 1 addition & 0 deletions packages/configs/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@parcel/transformer-typescript-types": "2.0.0-beta.3.1",
"@parcel/transformer-vue": "2.0.0-beta.3.1",
"@parcel/transformer-webmanifest": "2.0.0-beta.3.1",
"@parcel/transformer-worklet": "2.0.0-beta.3.1",
"@parcel/transformer-yaml": "2.0.0-beta.3.1"
},
"peerDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/configs/webextension/index.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "@parcel/config-default",
"transformers": {
"manifest.json": ["@parcel/transformer-webextension"]
"manifest.json": ["@parcel/transformer-webextension"],
"raw:*": ["@parcel/transformer-raw"]
},
"packagers": {
"manifest.json": "@parcel/packager-raw-url"
Expand Down
21 changes: 14 additions & 7 deletions packages/core/core/src/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import invariant from 'assert';
import nullthrows from 'nullthrows';
import {objectSortedEntriesDeep} from '@parcel/utils';
import {Hash, hashString} from '@parcel/hash';
import {Priority} from './types';
import {Priority, BundleBehavior} from './types';

import {getBundleGroupId, getPublicId} from './utils';
import {ALL_EDGE_TYPES, mapVisitor} from './Graph';
Expand Down Expand Up @@ -785,7 +785,8 @@ export default class BundleGraph {
if (
new Environment(bundle.env).isIsolated() ||
!bundle.isSplittable ||
bundle.isInline
bundle.bundleBehavior === BundleBehavior.isolated ||
bundle.bundleBehavior === BundleBehavior.inline
) {
return false;
}
Expand All @@ -811,7 +812,10 @@ export default class BundleGraph {
// Check that every parent bundle has a bundle group in its ancestry that contains the asset.
return parentBundleNodes.every(bundleNodeId => {
let bundleNode = nullthrows(this._graph.getNode(bundleNodeId));
if (bundleNode.type === 'root') {
if (
bundleNode.type === 'root' ||
bundleNode.value.bundleBehavior === BundleBehavior.isolated
) {
return false;
}

Expand All @@ -836,7 +840,10 @@ export default class BundleGraph {
let childBundles = this.getBundlesInBundleGroup(node.value);
if (
childBundles.some(
b => b.id !== bundle.id && this.bundleHasAsset(b, asset),
b =>
b.id !== bundle.id &&
b.bundleBehavior !== BundleBehavior.isolated &&
this.bundleHasAsset(b, asset),
)
) {
actions.skipChildren();
Expand Down Expand Up @@ -1440,7 +1447,7 @@ export default class BundleGraph {

let referencedBundles = this.getReferencedBundles(bundle);
for (let referenced of referencedBundles) {
if (referenced.isInline) {
if (referenced.bundleBehavior === BundleBehavior.inline) {
bundles.push(referenced);
addReferencedBundles(referenced);
}
Expand All @@ -1450,7 +1457,7 @@ export default class BundleGraph {
addReferencedBundles(bundle);

this.traverseBundles((childBundle, _, traversal) => {
if (childBundle.isInline) {
if (childBundle.bundleBehavior === BundleBehavior.inline) {
bundles.push(childBundle);
} else if (childBundle.id !== bundle.id) {
traversal.skipChildren();
Expand All @@ -1472,7 +1479,7 @@ export default class BundleGraph {
}

for (let referencedBundle of this.getReferencedBundles(bundle)) {
if (!referencedBundle.isInline) {
if (referencedBundle.bundleBehavior !== BundleBehavior.inline) {
hash.writeString(referencedBundle.id);
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/core/core/src/Dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import type {
Meta,
DependencySpecifier,
Symbol,
BundleBehavior as IBundleBehavior,
} from '@parcel/types';
import type {Dependency, Environment, Target} from './types';
import {hashString} from '@parcel/hash';
import {SpecifierType, Priority} from './types';
import {SpecifierType, Priority, BundleBehavior} from './types';

type DependencyOpts = {|
id?: string,
Expand All @@ -17,6 +18,7 @@ type DependencyOpts = {|
specifierType: $Keys<typeof SpecifierType>,
priority?: $Keys<typeof Priority>,
needsStableName?: boolean,
bundleBehavior?: ?IBundleBehavior,
isEntry?: boolean,
isOptional?: boolean,
loc?: SourceLocation,
Expand Down Expand Up @@ -48,6 +50,9 @@ export function createDependency(opts: DependencyOpts): Dependency {
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,
meta: opts.meta || {},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/PackagerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export default class PackagerRunner {
}
getSourceMapReference(bundle: NamedBundle, map: ?SourceMap): Async<?string> {
if (map && bundle.env.sourceMap && !bundle.isInline) {
if (map && bundle.env.sourceMap && bundle.bundleBehavior !== 'inline') {
if (bundle.env.sourceMap && bundle.env.sourceMap.inline) {
return this.generateSourceMap(bundleToInternalBundle(bundle), map);
} else {
Expand Down Expand Up @@ -338,7 +338,7 @@ export default class PackagerRunner {
bundle: BundleType,
bundleGraph: BundleGraphType<NamedBundleType>,
) => {
if (!bundle.isInline) {
if (bundle.bundleBehavior !== 'inline') {
throw new Error(
'Bundle is not inline and unable to retrieve contents',
);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/dumpGraphToGraphViz.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default async function dumpGraphToGraphViz(
} else if (node.type === 'bundle') {
let parts = [];
if (node.value.needsStableName) parts.push('stable name');
if (node.value.isInline) parts.push('inline');
if (node.value.bundleBehavior) parts.push(node.value.bundleBehavior);
if (parts.length) label += ' (' + parts.join(', ') + ')';
if (node.value.env) label += ` (${getEnvDescription(node.value.env)})`;
// $FlowFixMe
Expand Down
7 changes: 5 additions & 2 deletions packages/core/core/src/public/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
Stats,
Target as ITarget,
GraphVisitor,
BundleBehavior,
} from '@parcel/types';
import type BundleGraph from '../BundleGraph';

Expand All @@ -28,6 +29,7 @@ import {mapVisitor} from '../Graph';
import Environment from './Environment';
import Dependency, {dependencyToInternalDependency} from './Dependency';
import Target from './Target';
import {BundleBehaviorNames} from '../types';

const internalBundleToBundle: DefaultWeakMap<
ParcelOptions,
Expand Down Expand Up @@ -119,8 +121,9 @@ export class Bundle implements IBundle {
return this.#bundle.needsStableName;
}

get isInline(): ?boolean {
return this.#bundle.isInline;
get bundleBehavior(): ?BundleBehavior {
let bundleBehavior = this.#bundle.bundleBehavior;
return bundleBehavior != null ? BundleBehaviorNames[bundleBehavior] : null;
}

get isSplittable(): ?boolean {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/core/src/public/Dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import type {
MutableDependencySymbols as IMutableDependencySymbols,
SpecifierType,
DependencyPriority,
BundleBehavior,
} from '@parcel/types';
import type {Dependency as InternalDependency} from '../types';
import {BundleBehaviorNames} from '../types';

import Environment from './Environment';
import Target from './Target';
Expand Down Expand Up @@ -75,6 +77,11 @@ export default class Dependency implements IDependency {
return this.#dep.needsStableName;
}

get bundleBehavior(): ?BundleBehavior {
let bundleBehavior = this.#dep.bundleBehavior;
return bundleBehavior == null ? null : BundleBehaviorNames[bundleBehavior];
}

get isEntry(): boolean {
return this.#dep.isEntry;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/core/core/src/public/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ export const BROWSER_ENVS: Set<string> = new Set<string>([
'browser',
'web-worker',
'service-worker',
'worklet',
'electron-renderer',
]);
const ELECTRON_ENVS = new Set(['electron-main', 'electron-renderer']);
const NODE_ENVS = new Set(['node', ...ELECTRON_ENVS]);
const WORKER_ENVS = new Set(['web-worker', 'service-worker']);
const ISOLATED_ENVS = WORKER_ENVS;
const ISOLATED_ENVS = new Set([...WORKER_ENVS, 'worklet']);

const ALL_BROWSERS = [
'chrome',
Expand Down Expand Up @@ -181,6 +182,10 @@ export default class Environment implements IEnvironment {
return WORKER_ENVS.has(this.#environment.context);
}

isWorklet(): boolean {
return this.#environment.context === 'worklet';
}

matchesEngines(
minVersions: VersionMap,
defaultValue?: boolean = false,
Expand Down
14 changes: 7 additions & 7 deletions packages/core/core/src/public/MutableBundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export default class MutableBundleGraph extends BundleGraph<IBundle>
throw new Error('Dependency not found');
}

invariant(dependencyNode.type === 'dependency');

let resolved = this.#graph.getDependencyResolution(
dependencyToInternalDependency(dependency),
);
Expand Down Expand Up @@ -108,10 +110,7 @@ export default class MutableBundleGraph extends BundleGraph<IBundle>
this.#graph._graph.addEdge(dependencyNodeId, resolvedNodeId, 'references');
this.#graph._graph.removeEdge(dependencyNodeId, resolvedNodeId);

if (
dependency.isEntry ||
resolved.bundleBehavior === BundleBehavior.isolated
) {
if (dependency.isEntry) {
this.#graph._graph.addEdge(
nullthrows(this.#graph._graph.rootNodeId),
bundleGroupNodeId,
Expand Down Expand Up @@ -194,9 +193,10 @@ export default class MutableBundleGraph extends BundleGraph<IBundle>
mainEntryId: entryAsset?.id,
pipeline: opts.entryAsset ? opts.entryAsset.pipeline : opts.pipeline,
needsStableName: opts.needsStableName,
isInline: opts.entryAsset
? opts.entryAsset.bundleBehavior === 'inline'
: opts.isInline,
bundleBehavior:
opts.bundleBehavior != null
? BundleBehavior[opts.bundleBehavior]
: null,
isSplittable: opts.entryAsset
? opts.entryAsset.isBundleSplittable
: opts.isSplittable,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/src/requests/WriteBundlesRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import path from 'path';
import {hashString} from '@parcel/hash';
import {createPackageRequest} from './PackageRequest';
import createWriteBundleRequest from './WriteBundleRequest';
import {BundleBehavior} from '../types';

type WriteBundlesRequestInput = {|
bundleGraph: BundleGraph,
Expand Down Expand Up @@ -79,7 +80,7 @@ async function run({input, api, farm, options}: RunInput) {
}

// skip inline bundles, they will be processed via the parent bundle
return !bundle.isInline;
return bundle.bundleBehavior !== BundleBehavior.inline;
});

try {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export type Dependency = {|
specifierType: $Values<typeof SpecifierType>,
priority: $Values<typeof Priority>,
needsStableName: boolean,
bundleBehavior: ?$Values<typeof BundleBehavior>,
isEntry: boolean,
isOptional: boolean,
loc: ?SourceLocation,
Expand Down Expand Up @@ -435,7 +436,7 @@ export type Bundle = {|
entryAssetIds: Array<ContentKey>,
mainEntryId: ?ContentKey,
needsStableName: ?boolean,
isInline: ?boolean,
bundleBehavior: ?$Values<typeof BundleBehavior>,
isSplittable: ?boolean,
isPlaceholder?: boolean,
target: Target,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/test/PublicBundle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Public Bundle', () => {
publicId: null,
pipeline: null,
needsStableName: null,
isInline: null,
bundleBehavior: null,
isSplittable: true,
target: {
env,
Expand Down
Loading

0 comments on commit dee58e9

Please sign in to comment.