Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(app): Add module map chunk IDs (#3894)

toonlink8b0de4f37e59
diff --git a/fluxer_app/scripts/module-map/exports.ts b/fluxer_app/scripts/module-map/exports.ts
index 14c101b61..9d6e1be18 100644
--- a/fluxer_app/scripts/module-map/exports.ts
+++ b/fluxer_app/scripts/module-map/exports.ts
@@ -35,6 +35,13 @@ const getObjectPropertyName = (property: t.ObjectProperty): string | undefined =
return undefined;
};
+const getMemberPropertyName = (member: t.MemberExpression): string | undefined => {
+ if (!member.computed && t.isIdentifier(member.property)) return member.property.name;
+ if (member.computed && t.isStringLiteral(member.property)) return member.property.value;
+ if (member.computed && t.isNumericLiteral(member.property)) return String(member.property.value);
+ return undefined;
+};
+
const getGeneratedBinding = (node: AstNode | null | undefined): string | undefined => {
if (t.isIdentifier(node)) return node.name;
@@ -55,11 +62,8 @@ const getGeneratedBinding = (node: AstNode | null | undefined): string | undefin
return undefined;
};
-const hasDefaultBindingComment = (node: t.ObjectProperty): boolean => {
- const comments = [...(node.leadingComments ?? []), ...(node.value.leadingComments ?? [])];
-
- return comments.some((comment) => comment.value.includes('export default binding'));
-};
+const hasDefaultBindingMarker = (comments: Array<t.Comment> | null | undefined): boolean =>
+ comments?.some((comment) => comment.value.trim() === 'export default') ?? false;
const getExportSourceModule = (module: CompilationModule): CompilationModule =>
(module as CompilationModule & {rootModule?: CompilationModule | null}).rootModule ?? module;
@@ -67,6 +71,7 @@ const getExportSourceModule = (module: CompilationModule): CompilationModule =>
const getGeneratedExports = (code: string): Array<GeneratedExport> => {
const ast = parseModuleSource(code);
const exports: Array<GeneratedExport> = [];
+ const defaultBindings = new Set<string>();
const visit = (value: unknown): void => {
if (Array.isArray(value)) {
@@ -82,6 +87,14 @@ const getGeneratedExports = (code: string): Array<GeneratedExport> => {
const node = value as AstNode;
+ if (t.isVariableDeclaration(node) && hasDefaultBindingMarker(node.leadingComments)) {
+ for (const declaration of node.declarations) {
+ if (t.isIdentifier(declaration.id)) {
+ defaultBindings.add(declaration.id.name);
+ }
+ }
+ }
+
if (t.isCallExpression(node)) {
const {callee, arguments: args} = node;
@@ -103,18 +116,37 @@ const getGeneratedExports = (code: string): Array<GeneratedExport> => {
exports.push({
name,
binding: getGeneratedBinding(property.value),
- isDefaultBinding: hasDefaultBindingComment(property),
+ isDefaultBinding: false,
});
}
}
}
}
+ if (
+ t.isAssignmentExpression(node) &&
+ t.isMemberExpression(node.left) &&
+ t.isIdentifier(node.left.object, {name: '__webpack_exports__'})
+ ) {
+ const name = getMemberPropertyName(node.left);
+ if (name !== undefined) {
+ exports.push({
+ name,
+ binding: getGeneratedBinding(node.right),
+ isDefaultBinding: false,
+ });
+ }
+ }
+
for (const child of Object.values(node)) visit(child);
};
visit(ast);
+ for (const generated of exports) {
+ generated.isDefaultBinding = generated.binding != null && defaultBindings.has(generated.binding);
+ }
+
return exports;
};
@@ -249,9 +281,13 @@ export const getModuleExportMap = (
let exportNames: Array<string> | null = null;
if (Array.isArray(usedExports)) {
- exportNames = usedExports;
+ if (usedExports.length) {
+ exportNames = usedExports;
+ } else if (Array.isArray(providedExports)) {
+ exportNames = providedExports;
+ }
} else if (Array.isArray(providedExports)) {
- if (usedExports === true) {
+ if (usedExports === true || usedExports === null) {
exportNames = providedExports;
}
if (usedExports === false) {
diff --git a/fluxer_app/scripts/module-map/index.ts b/fluxer_app/scripts/module-map/index.ts
index 7d9612750..18c79753a 100644
--- a/fluxer_app/scripts/module-map/index.ts
+++ b/fluxer_app/scripts/module-map/index.ts
@@ -22,6 +22,7 @@ interface ModuleMapPluginOptions {
interface ModuleEntry {
id?: string | number;
+ chunkIds?: Array<string | number>;
files?: Array<string>;
concatenatedInto?: string | number;
exportMap?: Record<string, string>;
@@ -47,6 +48,9 @@ interface ConcatenatedModuleLike {
type CompilationModule = Parameters<Compilation['chunkGraph']['getModuleId']>[0];
+const getModuleChunkIds = (compilation: Compilation, module: CompilationModule): Array<string | number> =>
+ [...compilation.chunkGraph.getModuleChunksIterable(module)].flatMap((chunk) => (chunk.id == null ? [] : [chunk.id]));
+
const getConcatenatedModules = (module: CompilationModule): Array<ModuleWithResource> | undefined => {
const {modules} = module as CompilationModule & ConcatenatedModuleLike;
@@ -72,7 +76,7 @@ const getModuleKey = (root: string, filename: string): {key: string; packageName
const collectModuleMetadata = (compilation: Compilation, root: string, includeDependencies: boolean) => {
const modules: Record<string, ModuleEntry> = {};
const packages: Record<string, string> = {};
- const concatenatedChildren = new Map<string, string | number>();
+ const concatenatedChildren = new Map<string, {id: string | number; chunkIds: Array<string | number>}>();
const concatenatedRoots = new Set<string>();
for (const module of compilation.modules) {
@@ -91,7 +95,10 @@ const collectModuleMetadata = (compilation: Compilation, root: string, includeDe
for (const child of concatenated) {
if (!child.resource || child === rootModule || !path.isAbsolute(child.resource)) continue;
- concatenatedChildren.set(stripQuery(child.resource), id);
+ concatenatedChildren.set(stripQuery(child.resource), {
+ id,
+ chunkIds: getModuleChunkIds(compilation, module),
+ });
}
}
@@ -111,10 +118,13 @@ const collectModuleMetadata = (compilation: Compilation, root: string, includeDe
if (id == null) {
if (concatenatedRoots.has(moduleFilename)) continue;
- const concatenatedInto = concatenatedChildren.get(moduleFilename);
- if (concatenatedInto === undefined) continue;
+ const concatenated = concatenatedChildren.get(moduleFilename);
+ if (concatenated === undefined) continue;
- modules[key] = {concatenatedInto};
+ modules[key] = {
+ concatenatedInto: concatenated.id,
+ chunkIds: concatenated.chunkIds,
+ };
continue;
}
@@ -123,6 +133,7 @@ const collectModuleMetadata = (compilation: Compilation, root: string, includeDe
modules[key] = {
id,
+ chunkIds: getModuleChunkIds(compilation, module),
files: getModuleFiles(compilation, module),
...(exportMap ? {exportMap} : {}),
};