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 generation plugin to Fluxer frontend (#3848)

toonlink7241f48c7473
diff --git a/fluxer_app/package.json b/fluxer_app/package.json
index 3c9c97b26..5c6207213 100644
--- a/fluxer_app/package.json
+++ b/fluxer_app/package.json
@@ -244,6 +244,7 @@
},
"devDependencies": {
"@babel/parser": "8.0.8",
+ "@babel/types": "^8.1.0",
"@lingui/cli": "catalog:",
"@lingui/format-po": "catalog:",
"@lingui/swc-plugin": "catalog:",
diff --git a/fluxer_app/rspack.config.mjs b/fluxer_app/rspack.config.mjs
index c767d3096..889c57187 100644
--- a/fluxer_app/rspack.config.mjs
+++ b/fluxer_app/rspack.config.mjs
@@ -16,6 +16,7 @@ import {
} from '@rspack/core';
import {getLinguiSwcPluginConfig} from './scripts/build/rspack/lingui.mjs';
import {staticFilesPlugin} from './scripts/build/rspack/static-files.mjs';
+import {moduleMapPlugin} from './scripts/module-map/index.ts';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
@@ -775,6 +776,14 @@ export default () => {
});
},
},
+ ...(isProduction
+ ? [
+ moduleMapPlugin({
+ root: MONOREPO_ROOT,
+ build: {...buildInfo, desktopRenderer: isDesktopRenderer},
+ }),
+ ]
+ : []),
],
optimization: {
splitChunks: isProduction
diff --git a/fluxer_app/scripts/module-map/exports.ts b/fluxer_app/scripts/module-map/exports.ts
new file mode 100644
index 000000000..d225056db
--- /dev/null
+++ b/fluxer_app/scripts/module-map/exports.ts
@@ -0,0 +1,274 @@
+import {parse} from '@babel/parser';
+import * as t from '@babel/types';
+import type {Compilation} from '@rspack/core';
+
+type CompilationModule = Parameters<Compilation['chunkGraph']['getModuleId']>[0];
+
+interface GeneratedExport {
+ name: string;
+ binding?: string;
+ isDefaultBinding: boolean;
+}
+
+type AstNode = t.Node;
+
+const parseModuleSource = (code: string) =>
+ parse(code, {
+ sourceType: 'unambiguous',
+ allowReturnOutsideFunction: true,
+ plugins: ['jsx', 'typescript'],
+ });
+
+const getExportName = (node: AstNode | null | undefined): string | undefined => {
+ if (t.isIdentifier(node)) return node.name;
+ if (t.isStringLiteral(node)) return node.value;
+ return undefined;
+};
+
+const getObjectPropertyName = (property: t.ObjectProperty): string | undefined => {
+ const key = property.key;
+
+ if (t.isIdentifier(key)) return key.name;
+ if (t.isStringLiteral(key)) return key.value;
+ if (t.isNumericLiteral(key)) return String(key.value);
+
+ return undefined;
+};
+
+const getGeneratedBinding = (node: AstNode | null | undefined): string | undefined => {
+ if (t.isIdentifier(node)) return node.name;
+
+ if (t.isArrowFunctionExpression(node)) {
+ if (t.isBlockStatement(node.body)) {
+ const [statement] = node.body.body;
+
+ if (node.body.body.length === 1 && t.isReturnStatement(statement)) {
+ return getGeneratedBinding(statement.argument);
+ }
+
+ return undefined;
+ }
+
+ return getGeneratedBinding(node.body);
+ }
+
+ 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 getGeneratedExports = (code: string): Array<GeneratedExport> => {
+ const ast = parseModuleSource(code);
+ const exports: Array<GeneratedExport> = [];
+
+ const visit = (value: unknown): void => {
+ if (Array.isArray(value)) {
+ for (const item of value) {
+ visit(item);
+ }
+ return;
+ }
+
+ if (typeof value !== 'object' || value === null || !('type' in value)) {
+ return;
+ }
+
+ const node = value as AstNode;
+
+ if (t.isCallExpression(node)) {
+ const {callee, arguments: args} = node;
+
+ if (
+ t.isMemberExpression(callee) &&
+ t.isIdentifier(callee.object, {name: '__webpack_require__'}) &&
+ t.isIdentifier(callee.property, {name: 'd'})
+ ) {
+ for (const argument of args.slice(1, 3)) {
+ if (!t.isObjectExpression(argument)) continue;
+
+ for (const property of argument.properties) {
+ if (!t.isObjectProperty(property)) continue;
+
+ const name = getObjectPropertyName(property);
+ if (name === undefined) continue;
+
+ exports.push({
+ name,
+ binding: getGeneratedBinding(property.value),
+ isDefaultBinding: hasDefaultBindingComment(property),
+ });
+ }
+ }
+ }
+ }
+
+ for (const child of Object.values(node)) visit(child);
+ };
+
+ visit(ast);
+
+ return exports;
+};
+
+const collectExportBindings = (code: string): Map<string, Set<string>> => {
+ const ast = parseModuleSource(code);
+ const bindings = new Map<string, Set<string>>();
+
+ const addBinding = (local: string | undefined, exported: string | undefined) => {
+ if (!local || !exported) {
+ return;
+ }
+
+ const exports = bindings.get(local) ?? new Set<string>();
+ exports.add(exported);
+ bindings.set(local, exports);
+ };
+
+ const addPatternBindings = (pattern: AstNode | null | undefined): void => {
+ if (t.isIdentifier(pattern)) {
+ addBinding(pattern.name, pattern.name);
+ } else if (t.isRestElement(pattern)) {
+ addPatternBindings(pattern.argument);
+ } else if (t.isAssignmentPattern(pattern)) {
+ addPatternBindings(pattern.left);
+ } else if (t.isObjectPattern(pattern)) {
+ for (const property of pattern.properties) {
+ if (t.isRestElement(property)) {
+ addPatternBindings(property.argument);
+ } else if (t.isObjectProperty(property)) {
+ addPatternBindings(property.value);
+ }
+ }
+ } else if (t.isArrayPattern(pattern)) {
+ for (const element of pattern.elements) {
+ addPatternBindings(element);
+ }
+ }
+ };
+
+ for (const statement of ast.program.body) {
+ if (t.isExportNamedDeclaration(statement) && statement.source == null) {
+ const declaration = statement.declaration;
+
+ if (t.isFunctionDeclaration(declaration) || t.isClassDeclaration(declaration)) {
+ if (declaration.id) {
+ addBinding(declaration.id.name, declaration.id.name);
+ }
+ } else if (t.isVariableDeclaration(declaration)) {
+ for (const declarator of declaration.declarations) {
+ addPatternBindings(declarator.id);
+ }
+ }
+
+ for (const specifier of statement.specifiers) {
+ if (!t.isExportSpecifier(specifier)) continue;
+
+ addBinding(getExportName(specifier.local), getExportName(specifier.exported));
+ }
+ } else if (t.isExportDefaultDeclaration(statement)) {
+ const declaration = statement.declaration;
+
+ if ((t.isFunctionDeclaration(declaration) || t.isClassDeclaration(declaration)) && declaration.id) {
+ addBinding(declaration.id.name, 'default');
+ }
+ }
+ }
+
+ return bindings;
+};
+
+const mapGeneratedExports = (
+ generatedExports: Array<GeneratedExport>,
+ exportNames: Array<string>,
+ sourceBindings: Map<string, Set<string>>,
+): Record<string, string> | undefined => {
+ const result: Record<string, string> = {};
+ const mappedGeneratedNames = new Set<string>();
+
+ for (const generated of generatedExports) {
+ let originalName: string | undefined;
+
+ if (exportNames.includes(generated.name)) {
+ originalName = generated.name;
+ } else if (generated.isDefaultBinding && exportNames.includes('default')) {
+ originalName = 'default';
+ } else if (generated.binding) {
+ const candidates = [...(sourceBindings.get(generated.binding) ?? [])].filter((name) =>
+ exportNames.includes(name),
+ );
+
+ if (candidates.length === 1) {
+ originalName = candidates[0];
+ }
+ }
+
+ if (originalName) {
+ result[originalName] = generated.name;
+ mappedGeneratedNames.add(generated.name);
+ }
+ }
+
+ const unmappedOriginalNames = exportNames.filter((name) => !(name in result));
+ const unmappedGenerated = generatedExports.filter((generated) => !mappedGeneratedNames.has(generated.name));
+
+ if (unmappedOriginalNames.length === 1 && unmappedGenerated.length === 1) {
+ result[unmappedOriginalNames[0]] = unmappedGenerated[0].name;
+ }
+
+ return Object.keys(result).length === exportNames.length ? result : undefined;
+};
+
+export const getModuleExportMap = (
+ compilation: Compilation,
+ module: CompilationModule,
+): Record<string, string> | undefined => {
+ const runtimes = [
+ ...new Set([...compilation.chunkGraph.getModuleChunksIterable(module)].flatMap((chunk) => [...chunk.runtime])),
+ ];
+
+ if (!runtimes.length) return undefined;
+
+ const codeGenerationRuntime =
+ runtimes.find((runtime) => compilation.entries.has(runtime)) ?? runtimes[runtimes.length - 1];
+
+ if (!codeGenerationRuntime) return undefined;
+
+ const usedExports = compilation.moduleGraph.getUsedExports(module, runtimes);
+ const providedExports = compilation.moduleGraph.getProvidedExports(module);
+
+ let exportNames: Array<string> | null = null;
+
+ if (Array.isArray(usedExports)) {
+ exportNames = usedExports;
+ } else if (Array.isArray(providedExports)) {
+ if (usedExports === true) {
+ exportNames = providedExports;
+ }
+ if (usedExports === false) {
+ exportNames = [];
+ }
+ }
+
+ if (!exportNames?.length) return undefined;
+
+ const source = compilation.codeGenerationResults.get(module, codeGenerationRuntime).sources.get('javascript');
+
+ if (!source) return undefined;
+
+ const generatedExports = getGeneratedExports(source.source().toString());
+
+ if (generatedExports.length !== exportNames.length) return undefined;
+
+ const originalSource =
+ typeof module.type === 'string' && module.type.startsWith('javascript/') ? module.originalSource() : null;
+
+ const sourceBindings = originalSource
+ ? collectExportBindings(originalSource.source().toString())
+ : new Map<string, Set<string>>();
+
+ return mapGeneratedExports(generatedExports, exportNames, sourceBindings);
+};
diff --git a/fluxer_app/scripts/module-map/index.ts b/fluxer_app/scripts/module-map/index.ts
new file mode 100644
index 000000000..a100194f5
--- /dev/null
+++ b/fluxer_app/scripts/module-map/index.ts
@@ -0,0 +1,175 @@
+import crypto from 'node:crypto';
+import path from 'node:path';
+import {Compilation, type Compiler, sources} from '@rspack/core';
+
+import {getModuleExportMap} from './exports.ts';
+import {
+ getModuleFiles,
+ getModuleResource,
+ getNodeModuleInfo,
+ isAssetFile,
+ isInRoot,
+ normalizePath,
+ stripQuery,
+} from './modules.ts';
+
+interface ModuleMapPluginOptions {
+ root: string;
+ build?: Record<string, unknown>;
+ filename?: string;
+ includeDependencies?: boolean;
+}
+
+interface ModuleEntry {
+ id?: string | number;
+ files?: Array<string>;
+ concatenatedInto?: string | number;
+ exportMap?: Record<string, string>;
+}
+
+interface ModuleMapManifest {
+ build: Record<string, unknown>;
+ moduleIds: string | false;
+ chunkLoadingGlobal: string;
+ modules: Record<string, ModuleEntry>;
+ packages: Record<string, string>;
+ assets: Record<string, string>;
+}
+
+interface ConcatenatedModuleLike {
+ modules?: Set<{resource?: string | null}>;
+ rootModule?: {resource?: string | null} | null;
+}
+
+type CompilationModule = Parameters<Compilation['chunkGraph']['getModuleId']>[0];
+
+const getConcatenatedModules = (module: CompilationModule): ConcatenatedModuleLike | undefined => {
+ const concatenated = module as CompilationModule & ConcatenatedModuleLike;
+
+ return concatenated.modules?.size ? concatenated : undefined;
+};
+
+const collectModuleMetadata = (compilation: Compilation, root: string, includeDependencies: boolean) => {
+ const modules: Record<string, ModuleEntry> = {};
+ const packages: Record<string, string> = {};
+
+ for (const module of compilation.modules) {
+ const id = compilation.chunkGraph.getModuleId(module);
+ if (id == null) continue;
+
+ const resource = getModuleResource(module);
+ if (!resource || !path.isAbsolute(resource)) continue;
+
+ const moduleFilename = stripQuery(resource);
+ const nodeModuleInfo = getNodeModuleInfo(root, moduleFilename);
+ const packageName = nodeModuleInfo?.packageName;
+ const packagePath = nodeModuleInfo?.packagePath;
+
+ if (packageName ? !includeDependencies : !isInRoot(root, moduleFilename)) {
+ continue;
+ }
+
+ const key = packageName ? `npm/${packagePath}` : normalizePath(path.relative(root, moduleFilename));
+
+ const concatenated = getConcatenatedModules(module);
+
+ if (concatenated) {
+ const rootModule = concatenated.rootModule ?? [...concatenated.modules!][0];
+
+ for (const child of concatenated.modules!) {
+ if (!child.resource || child === rootModule) continue;
+
+ const childResource = stripQuery(child.resource);
+ const childRelative = path.relative(root, childResource);
+
+ if (isInRoot(root, childResource)) {
+ modules[normalizePath(childRelative)] = {
+ concatenatedInto: id,
+ };
+ }
+ }
+ }
+
+ let moduleKey = key;
+ if (!packageName && key.startsWith('apps/')) {
+ moduleKey = key.slice(5);
+ }
+
+ const exportMap = getModuleExportMap(compilation, module);
+
+ modules[moduleKey] = {
+ id,
+ files: getModuleFiles(compilation, module),
+ ...(exportMap ? {exportMap} : {}),
+ };
+
+ if (packageName) {
+ const isEntry = [...compilation.moduleGraph.getIncomingConnections(module)].some(
+ (connection) => connection.dependency?.request === packageName,
+ );
+
+ if (isEntry) {
+ packages[packageName] = key;
+ }
+ }
+ }
+
+ return {modules, packages};
+};
+
+const collectAssetHashes = (compilation: Compilation): Record<string, string> => {
+ const assets: Record<string, string> = {};
+
+ for (const asset of compilation.getAssets()) {
+ if (!isAssetFile(asset.name)) continue;
+
+ assets[asset.name] = crypto.createHash('sha256').update(asset.source.buffer()).digest('hex');
+ }
+
+ return assets;
+};
+
+const createManifest = (
+ compilation: Compilation,
+ build: Record<string, unknown>,
+ root: string,
+ includeDependencies: boolean,
+): ModuleMapManifest => {
+ const {modules, packages} = collectModuleMetadata(compilation, root, includeDependencies);
+
+ return {
+ build,
+ moduleIds: compilation.options.optimization.moduleIds ?? false,
+ chunkLoadingGlobal: compilation.outputOptions.chunkLoadingGlobal ?? '',
+ modules,
+ packages,
+ assets: collectAssetHashes(compilation),
+ };
+};
+
+export function moduleMapPlugin({
+ root,
+ build = {},
+ filename = 'module-map.json',
+ includeDependencies = true,
+}: ModuleMapPluginOptions) {
+ const resolvedRoot = path.resolve(root);
+
+ return {
+ apply(compiler: Compiler): void {
+ compiler.hooks.thisCompilation.tap('ModuleMapPlugin', (compilation) => {
+ compilation.hooks.processAssets.tap(
+ {
+ name: 'ModuleMapPlugin',
+ stage: Compilation.PROCESS_ASSETS_STAGE_REPORT,
+ },
+ () => {
+ const manifest = createManifest(compilation, build, resolvedRoot, includeDependencies);
+
+ compilation.emitAsset(filename, new sources.RawSource(JSON.stringify(manifest)));
+ },
+ );
+ });
+ },
+ };
+}
diff --git a/fluxer_app/scripts/module-map/modules.ts b/fluxer_app/scripts/module-map/modules.ts
new file mode 100644
index 000000000..fd247afb1
--- /dev/null
+++ b/fluxer_app/scripts/module-map/modules.ts
@@ -0,0 +1,55 @@
+import path from 'node:path';
+import type {Compilation} from '@rspack/core';
+
+type CompilationModule = Parameters<Compilation['chunkGraph']['getModuleId']>[0];
+
+const ASSET_FILE_REGEX = /\.(?:m?js|cjs|css)$/i;
+
+export const normalizePath = (value: string): string => value.replace(/\\/g, '/');
+
+export const stripQuery = (value: string): string => value.split('?')[0];
+
+export const isInRoot = (root: string, file: string): boolean => {
+ const relative = path.relative(root, file);
+
+ return !relative.startsWith('..') && !path.isAbsolute(relative);
+};
+
+export const getNodeModuleInfo = (root: string, file: string) => {
+ const relative = normalizePath(path.relative(root, file));
+ const nodeModulesIndex = relative.lastIndexOf('node_modules/');
+
+ if (nodeModulesIndex === -1) return undefined;
+
+ const packagePath = relative.slice(nodeModulesIndex + 'node_modules/'.length);
+ const segments = packagePath.split('/');
+ const packageName = segments[0]?.startsWith('@')
+ ? `${segments[0]}/${segments[1] ?? ''}`.replace(/\/$/, '')
+ : segments[0];
+
+ return packageName ? {packageName, packagePath: normalizePath(packagePath)} : undefined;
+};
+
+export const getModuleFiles = (compilation: Compilation, module: CompilationModule): Array<string> => {
+ const files = new Set<string>();
+
+ for (const chunk of compilation.chunkGraph.getModuleChunksIterable(module)) {
+ for (const file of chunk.files) {
+ if (ASSET_FILE_REGEX.test(file)) {
+ files.add(file);
+ }
+ }
+ }
+
+ return [...files];
+};
+
+export const getModuleResource = (module: CompilationModule): string | undefined => {
+ if (!('resource' in module) || typeof module.resource !== 'string') {
+ return undefined;
+ }
+
+ return module.resource;
+};
+
+export const isAssetFile = (file: string): boolean => ASSET_FILE_REGEX.test(file);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 296e03aa1..3dcda18d5 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1633,6 +1633,9 @@ importers:
'@babel/parser':
specifier: 8.0.8
version: 8.0.8
+ '@babel/types':
+ specifier: ^8.1.0
+ version: 8.1.0
'@lingui/cli':
specifier: 'catalog:'
@@ -3578,8 +3581,8 @@ packages:
resolution: {integrity: sha512-WCsemW40Cpc6gouC1SrY6PBaZ+oLRlEVY9XBvZBI7il/HyUrb2nJ505wE1MHEFyDZNnrWDEuy4jL50WVHfTTSQ==}
engines: {node: '>=6.9.0'}
- '@babel/[email protected]':
- resolution: {integrity: sha512-iXSSZAKQWmaeAwGtFw10v9fg5hUiUs0L+w+Z2K11kym3pDNK+SF0VcAQPsEFvWcTSZ7LMgClMXDBvTbH0IlICg==}
+ '@babel/[email protected]':
+ resolution: {integrity: sha512-8V8aSTDx012YPmDQF30vrokyxVFdRAU2YBS2VzrnfLAGkP8floVnjckwgFcD0Jrvf6GWMSRdCrYVsk3tyMwZRg==}
engines: {node: ^22.18.0 || >=24.11.0}
'@base-ui/[email protected]':
@@ -6379,6 +6382,7 @@ packages:
'@smithy/[email protected]':
resolution: {integrity: sha512-97imTcCyiPd8k0e9+ts/Sp0W65Dn9Ti357J7M09Ov4tI8Q1JDJVFSoAJtjEVKqfHEr3Pdo2jfolqKCaaiszH0g==}
engines: {node: '>=18.0.0'}
+ deprecated: Deprecated due to a bug in custom logger data redaction. See https://github.com/aws/aws-sdk-js-v3/issues/8393.
'@smithy/[email protected]':
resolution: {integrity: sha512-A9uSdn72ozbRUSit0eib0TW7nXuNPlaeM0zcGkJ+nE6tFcSDbnmtwoxbTCFBukVQcszDAyvsd7+rTduPTXpygg==}
@@ -12722,7 +12726,7 @@ snapshots:
dependencies:
- '@babel/types': 8.0.8
+ '@babel/types': 8.1.0
dependencies:
@@ -13309,7 +13313,7 @@ snapshots:
'@babel/helper-string-parser': 7.29.11
'@babel/helper-validator-identifier': 7.29.7
- '@babel/[email protected]':
+ '@babel/[email protected]':
dependencies:
'@babel/helper-string-parser': 8.0.8
'@babel/helper-validator-identifier': 8.0.6