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

Add support for ES Module and CommonJS output, and <script type="module"> #3545

Merged
merged 53 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
c2ef5a7
[WIP] ESModule output in scope hoisting
devongovett Sep 14, 2019
0029200
First try at <script type="module"> support
devongovett Sep 14, 2019
e96669b
Fix mime types
devongovett Sep 15, 2019
5285cd5
Add env to bundle hash in case it ends up with duplicate content
devongovett Sep 15, 2019
b1be160
Turn off module output with scope hoisting
devongovett Sep 15, 2019
5562a09
Move addExports to its own file
devongovett Sep 15, 2019
3b45796
Use blacklist for esmodule target
devongovett Sep 15, 2019
bb27787
Add link
devongovett Sep 15, 2019
48b70d5
Fix $parcel$global in strict mode
devongovett Sep 15, 2019
135da44
Add a dynamic import() polyfill if all target browsers don't support it
devongovett Sep 15, 2019
8e7dc77
Fix adding prelude when needed
devongovett Sep 15, 2019
5684e90
wip
devongovett Sep 17, 2019
62a635c
Fix non-module builds
devongovett Sep 18, 2019
52145e7
Support multiple output formats
devongovett Sep 18, 2019
cd43058
commonjs output
devongovett Sep 18, 2019
5dcbfa9
Add defaults for output format
devongovett Sep 19, 2019
ee32e90
Don’t execute entry bundle in async bundles (non scope hoisting)
devongovett Sep 19, 2019
22d72e1
Remove useBuiltins: usage
devongovett Sep 19, 2019
f6ee6fb
Disable node_modules by default in library targets
devongovett Sep 20, 2019
ca213fa
Wrap require in a promise
devongovett Sep 20, 2019
e880f83
Simplify runtimes a bit
devongovett Sep 20, 2019
68028f1
Get resolved exported symbols from bundle graph
devongovett Sep 21, 2019
5c0e6b0
Support external modules in esm and cjs
devongovett Sep 22, 2019
c799e61
Fix naming with multiple targets
devongovett Sep 22, 2019
452de4a
Don't add loaders for library targets. Instead, add references to bun…
devongovett Sep 22, 2019
b9af9f9
Make flow happy
devongovett Sep 22, 2019
b8e5239
Support imports in CSS modules
devongovett Sep 22, 2019
36e1f83
Fix integration tests
devongovett Sep 22, 2019
9066dce
Fix unit tests
devongovett Sep 22, 2019
19d5357
Begin tests
devongovett Sep 22, 2019
7a94b70
More tests and fixes
devongovett Sep 23, 2019
73e270e
Add some comments
devongovett Sep 23, 2019
7304d1c
Lint
devongovett Sep 23, 2019
a1f242a
Merge branch 'v2' of github.com:parcel-bundler/parcel into esmodule-o…
devongovett Sep 25, 2019
bd37165
Merge branch 'v2' into esmodule-output
Sep 26, 2019
034b2ff
Merge branch 'esmodule-output' of github.com:parcel-bundler/parcel in…
devongovett Sep 27, 2019
c9e20c0
Fix tests
devongovett Sep 27, 2019
474cd7a
review comments
devongovett Sep 27, 2019
3650d28
Use esmodule output settings in terser optimizer
devongovett Sep 27, 2019
58cadbe
Move assertString outside
devongovett Sep 27, 2019
7e93344
Ensure targets.browsers is an array before passing to babel
devongovett Sep 27, 2019
4ee4ac7
Merge branch 'v2' into esmodule-output
devongovett Sep 28, 2019
c354cd6
Move environment engine checking to a util
devongovett Sep 28, 2019
0b1e59b
Generate multiple variable declarations for old engines without destr…
devongovett Sep 28, 2019
eba9a02
Merge branch 'esmodule-output' of github.com:parcel-bundler/parcel in…
devongovett Sep 28, 2019
e4c8cb9
Move matchesEngines to a method on Environment objects
devongovett Sep 28, 2019
4d0b32c
Merge branch 'v2' into esmodule-output
devongovett Sep 28, 2019
22fbe72
Apply suggestions from code review
devongovett Sep 28, 2019
b455388
Apply shouldIncludeNodeModule suggestion
devongovett Sep 28, 2019
212e62b
Fix unit test
devongovett Sep 28, 2019
3549183
Convert win32 paths to posix
devongovett Sep 29, 2019
065f391
Merge branch 'v2' of github.com:parcel-bundler/parcel into esmodule-o…
devongovett Sep 29, 2019
b8b7444
Fix merge
devongovett Sep 29, 2019
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
24 changes: 23 additions & 1 deletion packages/core/core/src/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type Graph from './Graph';
import invariant from 'assert';
import crypto from 'crypto';
import nullthrows from 'nullthrows';
import {flatMap} from '@parcel/utils';
import {flatMap, objectSortedEntriesDeep} from '@parcel/utils';

import {getBundleGroupId} from './utils';
import {mapVisitor} from './Graph';
Expand Down Expand Up @@ -509,6 +509,27 @@ export default class BundleGraph {
return {asset, exportSymbol: symbol, symbol: identifier};
}

getExportedSymbols(asset: Asset) {
let symbols = [];

for (let symbol of asset.symbols.keys()) {
symbols.push(this.resolveSymbol(asset, symbol));
}

let deps = this.getDependencies(asset);
for (let dep of deps) {
if (dep.symbols.get('*') === '*') {
let resolved = nullthrows(this.getDependencyResolution(dep));
let exported = this.getExportedSymbols(resolved).filter(
s => s.exportSymbol !== 'default'
);
symbols.push(...exported);
}
}

return symbols;
}

getContentHash(bundle: Bundle): string {
let existingHash = this._bundleContentHashes.get(bundle.id);
if (existingHash != null) {
Expand All @@ -532,6 +553,7 @@ export default class BundleGraph {
hash.update(this.getContentHash(childBundle));
}, bundle);

hash.update(JSON.stringify(objectSortedEntriesDeep(bundle.env)));
return hash.digest('hex');
}
}
25 changes: 21 additions & 4 deletions packages/core/core/src/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const DEFAULT_ENGINES = {
export function createEnvironment({
context,
engines,
includeNodeModules
includeNodeModules,
outputFormat,
isLibrary = false
}: EnvironmentOpts = {}): Environment {
if (context == null) {
if (engines?.node) {
Expand Down Expand Up @@ -59,10 +61,25 @@ export function createEnvironment({
}
}

if (outputFormat == null) {
mischnic marked this conversation as resolved.
Show resolved Hide resolved
switch (context) {
case 'node':
case 'electron-main':
case 'electron-renderer':
outputFormat = 'commonjs';
break;
default:
outputFormat = 'global';
break;
}
}

return {
context,
engines,
includeNodeModules
includeNodeModules,
outputFormat,
isLibrary
};
}

Expand All @@ -75,8 +92,8 @@ export function mergeEnvironments(
return a;
}

return {
return createEnvironment({
...a,
...b
};
});
}
17 changes: 6 additions & 11 deletions packages/core/core/src/RequestGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ export default class RequestGraph extends Graph<RequestGraphNode> {
case 'dep_path_request':
promise = this.queue.add(() =>
this.resolvePath(requestNode.value).then(result => {
this.onDepPathRequestComplete(requestNode, result);
if (result) {
this.onDepPathRequestComplete(requestNode, result);
}
return result;
})
);
Expand Down Expand Up @@ -315,18 +317,11 @@ export default class RequestGraph extends Graph<RequestGraphNode> {
}

async resolvePath(dep: Dependency) {
try {
let assetRequest = await this.resolverRunner.resolve(dep);

let assetRequest = await this.resolverRunner.resolve(dep);
if (assetRequest) {
this.connectFile(nodeFromDepPathRequest(dep), assetRequest.filePath);
return assetRequest;
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND' && dep.isOptional) {
return null;
}

throw err;
}
return assetRequest;
}

async loadConfig(configRequest: ConfigRequest, parentNodeId: NodeId) {
Expand Down
16 changes: 13 additions & 3 deletions packages/core/core/src/ResolverRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class ResolverRunner {
this.pluginOptions = new PluginOptions(this.options);
}

async resolve(dependency: Dependency): Promise<AssetRequest> {
async resolve(dependency: Dependency): Promise<?AssetRequest> {
let dep = new PublicDependency(dependency);
report({
type: 'buildProgress',
Expand All @@ -39,14 +39,24 @@ export default class ResolverRunner {
options: this.pluginOptions
});

if (result) {
if (result && result.isExcluded) {
return null;
}

if (result && result.filePath) {
return {
...result,
filePath: result.filePath,
sideEffects: result.sideEffects,
code: result.code,
env: dependency.env
};
}
}

if (dep.isOptional) {
return null;
}

let dir = dependency.sourcePath
? path.dirname(dependency.sourcePath)
: '<none>';
Expand Down
45 changes: 28 additions & 17 deletions packages/core/core/src/TargetResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ export default class TargetResolver {
pkg.browser || pkgTargets.browser || (node && !browsers)
? 'node'
: 'browser';
let moduleContext =
pkg.browser || pkgTargets.browser ? 'browser' : mainContext;

let defaultEngines =
options.defaultEngines ??
(options.mode === 'production'
? DEFAULT_PRODUCTION_ENGINES
: DEFAULT_DEVELOPMENT_ENGINES);
let context = browsers || !node ? 'browser' : 'node';
if (context === 'browser' && pkgEngines.browsers == null) {
pkgEngines.browsers = defaultEngines.browsers;
} else if (context === 'node' && pkgEngines.node == null) {
pkgEngines.node = defaultEngines.node;
}

for (let targetName of COMMON_TARGETS) {
let targetDist;
Expand Down Expand Up @@ -204,8 +218,16 @@ export default class TargetResolver {
engines: descriptor.engines ?? pkgEngines,
context:
descriptor.context ??
(targetName === 'browser' ? 'browser' : mainContext),
includeNodeModules: descriptor.includeNodeModules
(targetName === 'browser'
? 'browser'
: targetName === 'module'
? moduleContext
: mainContext),
includeNodeModules: descriptor.includeNodeModules ?? false,
outputFormat:
descriptor.outputFormat ??
(targetName === 'module' ? 'esmodule' : 'commonjs'),
isLibrary: true
}),
sourceMap: descriptor.sourceMap
});
Expand All @@ -218,6 +240,7 @@ export default class TargetResolver {
continue;
}

let descriptor = pkgTargets[name];
let distPath = pkg[name];
let distDir;
let distEntry;
Expand All @@ -228,7 +251,6 @@ export default class TargetResolver {
distEntry = path.basename(distPath);
}

let descriptor = pkgTargets[name];
if (descriptor) {
targets.set(name, {
name,
Expand All @@ -238,7 +260,9 @@ export default class TargetResolver {
env: createEnvironment({
engines: descriptor.engines ?? pkgEngines,
context: descriptor.context,
includeNodeModules: descriptor.includeNodeModules
includeNodeModules: descriptor.includeNodeModules,
outputFormat: descriptor.outputFormat,
isLibrary: descriptor.isLibrary
}),
sourceMap: descriptor.sourceMap
});
Expand All @@ -247,19 +271,6 @@ export default class TargetResolver {

// If no explicit targets were defined, add a default.
if (targets.size === 0) {
let context = browsers || !node ? 'browser' : 'node';
let defaultEngines =
options.defaultEngines ??
(options.mode === 'production'
? DEFAULT_PRODUCTION_ENGINES
: DEFAULT_DEVELOPMENT_ENGINES);

if (context === 'browser' && pkgEngines.browsers == null) {
pkgEngines.browsers = defaultEngines.browsers;
} else if (context === 'node' && pkgEngines.node == null) {
pkgEngines.node = defaultEngines.node;
}

targets.set('default', {
name: 'default',
distDir: path.resolve(this.fs.cwd(), DEFAULT_DIST_DIRNAME),
Expand Down
16 changes: 9 additions & 7 deletions packages/core/core/src/Transformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,15 @@ class Pipeline {
preloadedConfig: ?Config
): Promise<Array<TransformerResult>> {
const resolve = async (from: FilePath, to: string): Promise<FilePath> => {
return (await this.resolverRunner.resolve(
createDependency({
env: asset.value.env,
moduleSpecifier: to,
sourcePath: from
})
)).filePath;
return nullthrows(
await this.resolverRunner.resolve(
createDependency({
env: asset.value.env,
moduleSpecifier: to,
sourcePath: from
})
)
).filePath;
};

// Load config for the transformer.
Expand Down
20 changes: 18 additions & 2 deletions packages/core/core/src/public/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type {
BundleGroup,
Dependency as IDependency,
GraphTraversalCallback,
Symbol
Symbol,
SymbolResolution
} from '@parcel/types';
import type {ParcelOptions} from '../types';
import type InternalBundleGraph from '../BundleGraph';
Expand Down Expand Up @@ -123,7 +124,7 @@ export default class BundleGraph implements IBundleGraph {
.map(bundle => new Bundle(bundle, this.#graph, this.#options));
}

resolveSymbol(asset: IAsset, symbol: Symbol) {
resolveSymbol(asset: IAsset, symbol: Symbol): SymbolResolution {
let res = this.#graph.resolveSymbol(
assetToInternalAsset(asset).value,
symbol
Expand All @@ -135,6 +136,15 @@ export default class BundleGraph implements IBundleGraph {
};
}

getExportedSymbols(asset: IAsset): Array<SymbolResolution> {
let res = this.#graph.getExportedSymbols(assetToInternalAsset(asset).value);
return res.map(e => ({
asset: assetFromValue(e.asset, this.#options),
exportSymbol: e.exportSymbol,
symbol: e.symbol
}));
}

traverseBundles<TContext>(
visit: GraphTraversalCallback<IBundle, TContext>
): ?TContext {
Expand All @@ -145,4 +155,10 @@ export default class BundleGraph implements IBundleGraph {
)
);
}

findBundlesWithAsset(asset: IAsset): Array<IBundle> {
return this.#graph
.findBundlesWithAsset(assetToInternalAsset(asset).value)
.map(bundle => new Bundle(bundle, this.#graph, this.#options));
}
}
Loading