Skip to content

Commit

Permalink
Distinguish between module and classic scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Nov 29, 2020
1 parent 064ba0a commit ea9c296
Show file tree
Hide file tree
Showing 125 changed files with 2,141 additions and 358 deletions.
7 changes: 6 additions & 1 deletion flow-libs/posthtml.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ declare module 'posthtml' {
tag: string,
attrs?: {[string]: string, ...},
content?: Array<string>,
loc?: {
start: {|line: number, column: number|},
end: {|line: number, column: number|},
...
},
...
};

Expand All @@ -26,7 +31,7 @@ declare module 'posthtml' {
...
};

declare var walk: (fn: (node: PostHTMLNode) => PostHTMLNode) => void;
declare var walk: (fn: (node: PostHTMLNode) => PostHTMLNode | PostHTMLNode[]) => void;
declare var process: (
tree: PostHTMLTree | string,
options: ?PostHTMLOptions,
Expand Down
17 changes: 5 additions & 12 deletions packages/configs/default/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,11 @@
"url:*": ["@parcel/transformer-raw"]
},
"namers": ["@parcel/namer-default"],
"runtimes": {
"browser": [
"@parcel/runtime-js",
"@parcel/runtime-browser-hmr",
"@parcel/runtime-react-refresh"
],
"service-worker": ["@parcel/runtime-js"],
"web-worker": ["@parcel/runtime-js"],
"node": ["@parcel/runtime-js"],
"electron-renderer": ["@parcel/runtime-js"],
"electron-main": ["@parcel/runtime-js"]
},
"runtimes": [
"@parcel/runtime-js",
"@parcel/runtime-browser-hmr",
"@parcel/runtime-react-refresh"
],
"optimizers": {
"data-url:*": ["...", "@parcel/optimizer-data-url"],
"*.css": ["@parcel/optimizer-cssnano"],
Expand Down
9 changes: 2 additions & 7 deletions packages/core/core/src/AssetGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {md5FromObject} from '@parcel/utils';
import nullthrows from 'nullthrows';
import Graph, {type GraphOpts} from './Graph';
import {createDependency} from './Dependency';
import {getAssetGroupId} from './assetUtils';

type AssetGraphOpts = {|
...GraphOpts<AssetGraphNode>,
Expand Down Expand Up @@ -55,13 +56,7 @@ export function nodeFromDep(dep: Dependency): DependencyNode {

export function nodeFromAssetGroup(assetGroup: AssetGroup): AssetGroupNode {
return {
id: md5FromObject({
...assetGroup,
// only influences building the asset graph
canDefer: undefined,
// if only the isURL property is different, no need to re-run transformation.
isURL: undefined,
}),
id: getAssetGroupId(assetGroup),
type: 'asset_group',
value: assetGroup,
usedSymbolsDownDirty: true,
Expand Down
15 changes: 10 additions & 5 deletions packages/core/core/src/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import assert from 'assert';
import invariant from 'assert';
import crypto from 'crypto';
import nullthrows from 'nullthrows';
import {flatMap, objectSortedEntriesDeep, unique} from '@parcel/utils';
import {
flatMap,
objectSortedEntriesDeep,
unique,
isSubset,
} from '@parcel/utils';

import {getBundleGroupId, getPublicId} from './utils';
import Graph, {ALL_EDGE_TYPES, mapVisitor, type GraphOpts} from './Graph';
Expand Down Expand Up @@ -615,7 +620,7 @@ export default class BundleGraph {
visitedBundles.add(descendant);
if (
descendant.type !== bundle.type ||
descendant.env.context !== bundle.env.context
!isSubset(descendant.env.context, bundle.env.context)
) {
actions.skipChildren();
return;
Expand All @@ -630,7 +635,7 @@ export default class BundleGraph {
let similarSiblings = this.getSiblingBundles(descendant).filter(
sibling =>
sibling.type === bundle.type &&
sibling.env.context === bundle.env.context,
isSubset(sibling.env.context, bundle.env.context),
);
if (
similarSiblings.some(
Expand Down Expand Up @@ -713,7 +718,7 @@ export default class BundleGraph {
// Don't deduplicate when context changes
if (
node.type === 'bundle' &&
node.value.env.context !== bundle.env.context
!isSubset(node.value.env.context, bundle.env.context)
) {
actions.skipChildren();
}
Expand Down Expand Up @@ -766,7 +771,7 @@ export default class BundleGraph {
// Stop when context changes
if (
node.type === 'bundle' &&
node.value.env.context !== bundle.env.context
!isSubset(node.value.env.context, bundle.env.context)
) {
actions.skipChildren();
}
Expand Down
85 changes: 46 additions & 39 deletions packages/core/core/src/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function createEnvironment({
isLibrary = false,
scopeHoist = false,
sourceMap,
loc,
}: EnvironmentOpts = {}): Environment {
if (context == null) {
if (engines?.node) {
Expand All @@ -28,65 +29,71 @@ export function createEnvironment({
}
}

let ctx = new Set(typeof context === 'string' ? [context] : context);

if (engines == null) {
switch (context) {
case 'node':
case 'electron-main':
engines = {
node: DEFAULT_ENGINES.node,
};
break;
case 'browser':
case 'web-worker':
case 'service-worker':
case 'electron-renderer':
engines = {
browsers: DEFAULT_ENGINES.browsers,
};
break;
default:
engines = {};
engines = {};
for (let context of ctx) {
switch (context) {
case 'node':
case 'electron-main':
engines.node = DEFAULT_ENGINES.node;
break;
case 'browser':
case 'web-worker':
case 'service-worker':
case 'electron-renderer':
engines.browsers = DEFAULT_ENGINES.browsers;
break;
}
}
}

if (includeNodeModules == null) {
switch (context) {
case 'node':
case 'electron-main':
case 'electron-renderer':
includeNodeModules = false;
break;
case 'browser':
case 'web-worker':
case 'service-worker':
default:
includeNodeModules = true;
break;
includeNodeModules = true;
for (let context of ctx) {
switch (context) {
case 'node':
case 'electron-main':
case 'electron-renderer':
includeNodeModules = false;
break;
case 'browser':
case 'web-worker':
case 'service-worker':
default:
includeNodeModules = true;
break;
}
}
}

if (outputFormat == null) {
switch (context) {
case 'node':
case 'electron-main':
case 'electron-renderer':
outputFormat = 'commonjs';
break;
default:
outputFormat = 'global';
break;
outputFormat = 'global';
for (let context of ctx) {
switch (context) {
case 'node':
case 'electron-main':
case 'electron-renderer':
outputFormat = 'commonjs';
break;
default:
outputFormat = 'global';
break;
}
}
}

return {
context,
context: ctx,
engines,
includeNodeModules,
outputFormat,
isLibrary,
minify,
scopeHoist,
sourceMap,
loc,
};
}

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 @@ -421,7 +421,7 @@ export default class PackagerRunner {
sourceRoot = bundle.env.sourceMap.sourceRoot;
} else if (
this.options.serve &&
bundle.target.env.context === 'browser'
bundle.target.env.context.has('browser')
) {
sourceRoot = '/__parcel_source_root';
}
Expand All @@ -431,7 +431,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.has('node')) {
// inlining should only happen in production for browser targets by default
inlineSources = this.options.mode === 'production';
}
Expand Down
11 changes: 4 additions & 7 deletions packages/core/core/src/ParcelConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
Bundler,
Namer,
Runtime,
EnvironmentContext,
PackageName,
Optimizer,
Packager,
Expand Down Expand Up @@ -52,7 +51,7 @@ export default class ParcelConfig {
transformers: GlobMap<ExtendableParcelConfigPipeline>;
bundler: ?ParcelPluginNode;
namers: PureParcelConfigPipeline;
runtimes: {[EnvironmentContext]: PureParcelConfigPipeline, ...};
runtimes: PureParcelConfigPipeline;
packagers: GlobMap<ParcelPluginNode>;
validators: GlobMap<ExtendableParcelConfigPipeline>;
optimizers: GlobMap<ExtendableParcelConfigPipeline>;
Expand All @@ -71,7 +70,7 @@ export default class ParcelConfig {
this.filePath = config.filePath;
this.resolvers = config.resolvers || [];
this.transformers = config.transformers || {};
this.runtimes = config.runtimes || {};
this.runtimes = config.runtimes || [];
this.bundler = config.bundler;
this.namers = config.namers || [];
this.packagers = config.packagers || {};
Expand Down Expand Up @@ -266,10 +265,8 @@ export default class ParcelConfig {
return this.loadPlugins<Namer>(this.namers);
}

getRuntimes(
context: EnvironmentContext,
): Promise<Array<LoadedPlugin<Runtime>>> {
let runtimes = this.runtimes[context];
getRuntimes(): Promise<Array<LoadedPlugin<Runtime>>> {
let runtimes = this.runtimes;
if (!runtimes) {
return Promise.resolve([]);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/ParcelConfig.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default {
packagers: (mapStringSchema('packager', 'packagers'): SchemaEntity),
optimizers: (mapPipelineSchema('optimizer', 'optimizers'): SchemaEntity),
reporters: (pipelineSchema('reporter', 'reporters'): SchemaEntity),
runtimes: (mapPipelineSchema('runtime', 'runtimes'): SchemaEntity),
runtimes: (pipelineSchema('runtime', 'runtimes'): SchemaEntity),
filePath: {
type: 'string',
},
Expand Down
12 changes: 11 additions & 1 deletion packages/core/core/src/Transformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,17 @@ export default class Transformation {
.filter(
asset =>
asset.ast != null &&
!(asset.value.type === 'js' && asset.value.env.scopeHoist),
!(
asset.value.type === 'js' &&
asset.value.env.scopeHoist &&
// HACK: also handle global scripts with no dependencies.
// JSPackager calls getCode() for these even with scope hoisting, so we
// need to generate here until we find a way to make generate happen on demand.
!(
asset.value.env.context.has('script') &&
asset.value.dependencies.size === 0
)
),
)
.map(async asset => {
if (asset.isASTDirty) {
Expand Down
13 changes: 10 additions & 3 deletions packages/core/core/src/applyRuntimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {setDifference} from '@parcel/utils';
import {PluginLogger} from '@parcel/logger';
import ThrowableDiagnostic, {errorToDiagnostic} from '@parcel/diagnostic';
import {dependencyToInternalDependency} from './public/Dependency';
import {mergeEnvironments} from './Environment';

type RuntimeConnection = {|
bundle: InternalBundle,
Expand All @@ -46,7 +47,7 @@ export default async function applyRuntimes({
let connections: Array<RuntimeConnection> = [];

for (let bundle of bundleGraph.getBundles()) {
let runtimes = await config.getRuntimes(bundle.env.context);
let runtimes = await config.getRuntimes();
for (let runtime of runtimes) {
try {
let applied = await runtime.plugin.apply({
Expand All @@ -62,11 +63,17 @@ export default async function applyRuntimes({

if (applied) {
let runtimeAssets = Array.isArray(applied) ? applied : [applied];
for (let {code, dependency, filePath, isEntry} of runtimeAssets) {
for (let {
code,
dependency,
filePath,
isEntry,
env,
} of runtimeAssets) {
let assetGroup = {
code,
filePath,
env: bundle.env,
env: mergeEnvironments(bundle.env, env),
// Runtime assets should be considered source, as they should be
// e.g. compiled to run in the target environment
isSource: true,
Expand Down
13 changes: 13 additions & 0 deletions packages/core/core/src/assetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
} from '@parcel/types';
import type {
Asset,
AssetGroup,
RequestInvalidation,
Dependency,
Environment,
Expand All @@ -24,6 +25,7 @@ import type {ConfigOutput} from '@parcel/utils';
import {Readable} from 'stream';
import crypto from 'crypto';
import {PluginLogger} from '@parcel/logger';
import {md5FromObject} from '@parcel/utils';
import nullthrows from 'nullthrows';
import CommittedAsset from './CommittedAsset';
import UncommittedAsset from './UncommittedAsset';
Expand Down Expand Up @@ -251,3 +253,14 @@ export async function getInvalidationHash(

return hash.digest('hex');
}

export function getAssetGroupId(assetGroup: AssetGroup): string {
return md5FromObject({
...assetGroup,
env: getEnvironmentHash(assetGroup.env),
// only influences building the asset graph
canDefer: undefined,
// if only the isURL property is different, no need to re-run transformation.
isURL: undefined,
});
}
Loading

0 comments on commit ea9c296

Please sign in to comment.