8000 consider TLA imports have higher execution priority by TrickyPi · Pull Request #5937 · rollup/rollup · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

consider TLA imports have higher execution priority #5937

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

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 1 addition & 3 deletions src/ast/ExecutionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ interface ControlFlowContext {

export interface InclusionContext extends ControlFlowContext {
includedCallArguments: Set<Entity>;
withinTopLevelAwait: boolean;
}

export interface HasEffectsContext extends ControlFlowContext {
Expand All @@ -39,8 +38,7 @@ export function createInclusionContext(): InclusionContext {
hasBreak: false,
hasContinue: false,
includedCallArguments: new Set(),
includedLabels: new Set(),
withinTopLevelAwait: false
includedLabels: new Set()
};
}

Expand Down
35 changes: 12 additions & 23 deletions src/ast/nodes/AwaitExpression.ts 10000
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,43 @@ import type { InclusionContext } from '../ExecutionContext';
import type { ObjectPath } from '../utils/PathTracker';
import ArrowFunctionExpression from './ArrowFunctionExpression';
import type * as NodeType from './NodeType';
import { Flag, isFlagSet, setFlag } from './shared/BitFlags';
import FunctionNode from './shared/FunctionNode';
import { type ExpressionNode, type IncludeChildren, type Node, NodeBase } from './shared/Node';

export default class AwaitExpression extends NodeBase {
declare argument: ExpressionNode;
declare type: NodeType.tAwaitExpression;

get isTopLevelAwait(): boolean {
return isFlagSet(this.flags, Flag.isTopLevelAwait);
}
set isTopLevelAwait(value: boolean) {
this.flags = setFlag(this.flags, Flag.isTopLevelAwait, value);
}

hasEffects(): boolean {
if (!this.deoptimized) this.applyDeoptimizations();
return true;
}

initialise(): void {
super.initialise();
let parent = this.parent;
do {
if (parent instanceof FunctionNode || parent instanceof ArrowFunctionExpression) return;
} while ((parent = (parent as Node).parent as Node));
this.scope.context.usesTopLevelAwait = true;
}

include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
if (!this.included) this.includeNode(context);
this.argument.include(
{ ...context, withinTopLevelAwait: this.isTopLevelAwait },
includeChildrenRecursively
);
this.argument.include(context, includeChildrenRecursively);
}

includeNode(context: InclusionContext) {
this.included = true;
if (!this.deoptimized) this.applyDeoptimizations();
checkTopLevelAwait: {
let parent = this.parent;
do {
if (parent instanceof FunctionNode || parent instanceof ArrowFunctionExpression)
break checkTopLevelAwait;
} while ((parent = (parent as Node).parent as Node));
this.scope.context.usesTopLevelAwait = true;
this.isTopLevelAwait = true;
}
// Thenables need to be included
this.argument.includePath(THEN_PATH, { ...context, withinTopLevelAwait: this.isTopLevelAwait });
this.argument.includePath(THEN_PATH, context);
}

includePath(path: ObjectPath, context: InclusionContext): void {
if (!this.deoptimized) this.applyDeoptimizations();
if (!this.included) this.includeNode(context);
this.argument.includePath(path, { ...context, withinTopLevelAwait: this.isTopLevelAwait });
this.argument.includePath(path, context);
}
}

Expand Down
30 changes: 25 additions & 5 deletions src/ast/nodes/ImportExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import MemberExpression from './MemberExpression';
import type * as NodeType from './NodeType';
import ObjectPattern from './ObjectPattern';
import { Flag, isFlagSet, setFlag } from './shared/BitFlags';
import FunctionNode from './shared/FunctionNode';
import type { Node } from './shared/Node';
import {
doNotDeoptimize,
type ExpressionNode,
Expand Down Expand Up @@ -209,19 +211,18 @@ export default class ImportExpression extends NodeBase {
}

include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
if (!this.included) this.includeNode(context);
if (!this.included) this.includeNode();
this.source.include(context, includeChildrenRecursively);
}

includeNode(context: InclusionContext) {
includeNode() {
this.included = true;
this.withinTopLevelAwait = context.withinTopLevelAwait;
this.scope.context.includeDynamicImport(this);
this.scope.addAccessedDynamicImport(this);
}

includePath(path: ObjectPath, context: InclusionContext): void {
if (!this.included) this.includeNode(context);
includePath(path: ObjectPath): void {
if (!this.included) this.includeNode();
// Technically, this is not correct as dynamic imports return a Promise.
if (this.hasUnknownAccessedKey) return;
if (path[0] === UnknownKey) {
Expand All @@ -236,6 +237,25 @@ export default class ImportExpression extends NodeBase {
initialise(): void {
super.initialise();
this.scope.context.addDynamicImport(this);
let parent = this.parent;
let withinAwaitExpression = false;
let withinTopLevelAwait = false;
do {
if (
withinAwaitExpression &&
(parent instanceof FunctionNode || parent instanceof ArrowFunctionExpression)
) {
withinTopLevelAwait = false;
}
if (parent instanceof AwaitExpression) {
withinAwaitExpression = true;
withinTopLevelAwait = true;
}
} while ((parent = (parent as Node).parent as Node));

if (withinAwaitExpression && withinTopLevelAwait) {
this.withinTopLevelAwait = true;
}
}

parseNode(esTreeNode: GenericEsTreeNode): this {
Expand Down
5 changes: 2 additions & 3 deletions src/ast/nodes/shared/BitFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ export const enum Flag {
destructuringDeoptimized = 1 << 24,
hasDeoptimizedCache = 1 << 25,
hasEffects = 1 << 26,
isTopLevelAwait = 1 << 27,
withinTopLevelAwait = 1 << 28,
checkedForWarnings = 1 << 29
withinTopLevelAwait = 1 << 27,
checkedForWarnings = 1 << 28
}

export function isFlagSet(flags: number, flag: Flag): boolean {
Expand Down
28 changes: 18 additions & 10 deletions src/utils/executionOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,33 @@ export function analyseModuleExecution(entryModules: readonly Module[]): {
const parents = new Map<Module | ExternalModule, Module | null>();
const orderedModules: Module[] = [];

const handleSyncLoadedModule = (module: Module | ExternalModule, parent: Module) => {
if (parents.has(module)) {
if (!analysedModules.has(module)) {
cyclePaths.push(getCyclePath(module as Module, parent, parents));
}
return;
}
parents.set(module, parent);
analyseModule(module);
};

const analyseModule = (module: Module | ExternalModule) => {
if (module instanceof Module) {
for (const dependency of module.dependencies) {
if (parents.has(dependency)) {
if (!analysedModules.has(dependency)) {
cyclePaths.push(getCyclePath(dependency as Module, module, parents));
}
continue;
}
parents.set(dependency, module);
analyseModule(dependency);
handleSyncLoadedModule(dependency, module);
}

for (const dependency of module.implicitlyLoadedBefore) {
dynamicImports.add(dependency);
}
for (const { resolution } of module.dynamicImports) {
for (const { resolution, node } of module.dynamicImports) {
if (resolution instanceof Module) {
dynamicImports.add(resolution);
if (node.withinTopLevelAwait) {
handleSyncLoadedModule(resolution, module);
} else {
dynamicImports.add(resolution);
}
}
}
orderedModules.push(module);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require('node:path');
const ID_MAIN = path.join(__dirname, 'main.js');
const ID_LIB = path.join(__dirname, 'lib.js');

module.exports = defineTest({
description: 'throw a warn when a cycle is detected which includes a top-level await import',
options: {
output: {
inlineDynamicImports: true
}
},
expectedWarnings: ['CIRCULAR_DEPENDENCY'],
logs: [
{
code: 'CIRCULAR_DEPENDENCY',
ids: [ID_MAIN, ID_LIB, ID_MAIN],
level: 'warn',
message: 'Circular dependency: main.js -> lib.js -> main.js'
}
]
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
setTimeout(() => {
console.log(main);
});

const square$1 = x => x;

var lib = /*#__PURE__*/Object.freeze({
__proto__: null,
square: square$1
});

const { square } = await Promise.resolve().then(function () { return lib; });

console.log(square(5));

const main = 'main';

export { main };
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { main } from './main.js';

setTimeout(() => {
console.log(main);
});

export const square = x => x;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { square } = await import('./lib.js');

console.log(square(5));

export const main = 'main';
22 changes: 11 additions & 11 deletions test/form/samples/no-treeshake/_expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ const quux = 1;

const other = () => quux;

const fred$1 = 1;

var dynamicImported = () => fred$1;

var dynamicImported$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
default: dynamicImported,
fred: fred$1
});

function baz() {
return foo + external.value;
}
Expand Down Expand Up @@ -54,16 +64,6 @@ try {
const x = 1;
} catch {}

const { fred: fred$1 } = await Promise.resolve().then(function () { return dynamicImported$1; });

const fred = 1;

var dynamicImported = () => fred;

var dynamicImported$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
default: dynamicImported,
fred: fred
});
const { fred } = await Promise.resolve().then(function () { return dynamicImported$1; });

export { create, getPrototypeOf, quux, quux as strange };
Loading
0