@satilian/plugin-mf 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +522 -2
- package/dist/build/bootstrap.d.ts +9 -0
- package/dist/build/bootstrap.js +51 -0
- package/dist/build/build-expose.d.ts +11 -0
- package/dist/build/build-expose.js +142 -0
- package/dist/build/build-release.d.ts +12 -0
- package/dist/build/build-release.js +61 -0
- package/dist/build/build-types.d.ts +7 -0
- package/dist/build/build-types.js +88 -0
- package/dist/build/config.d.ts +14 -0
- package/dist/build/config.js +1 -0
- package/dist/build/format-stats.d.ts +8 -0
- package/dist/build/format-stats.js +35 -0
- package/dist/build/plugin.d.ts +3 -0
- package/dist/build/plugin.js +87 -0
- package/dist/build/resolver.d.ts +1 -0
- package/dist/build/resolver.js +13 -0
- package/dist/build/shared-dependencies.d.ts +2 -0
- package/dist/build/shared-dependencies.js +56 -0
- package/dist/build/sync-types.d.ts +7 -0
- package/dist/build/sync-types.js +202 -0
- package/dist/build/type-cache.d.ts +13 -0
- package/dist/build/type-cache.js +160 -0
- package/dist/build/types-cli.d.ts +2 -0
- package/dist/build/types-cli.js +99 -0
- package/dist/build/types-options.d.ts +11 -0
- package/dist/build/types-options.js +10 -0
- package/dist/client.d.ts +12 -0
- package/dist/client.js +6 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +2 -0
- package/dist/protocol/asset-path.d.ts +1 -0
- package/dist/protocol/asset-path.js +10 -0
- package/dist/protocol/load-error.d.ts +29 -0
- package/dist/protocol/load-error.js +17 -0
- package/dist/protocol/module.d.ts +12 -0
- package/dist/protocol/module.js +1 -0
- package/dist/protocol/network-policy.d.ts +5 -0
- package/dist/protocol/network-policy.js +1 -0
- package/dist/protocol/observability.d.ts +26 -0
- package/dist/protocol/observability.js +1 -0
- package/dist/protocol/preload.d.ts +16 -0
- package/dist/protocol/preload.js +1 -0
- package/dist/protocol/registry.d.ts +17 -0
- package/dist/protocol/registry.js +75 -0
- package/dist/protocol/release-manifest.d.ts +17 -0
- package/dist/protocol/release-manifest.js +29 -0
- package/dist/protocol/remote-manifest.d.ts +13 -0
- package/dist/protocol/remote-manifest.js +27 -0
- package/dist/protocol/shared-dependencies.d.ts +19 -0
- package/dist/protocol/shared-dependencies.js +20 -0
- package/dist/protocol/ssr-releases.d.ts +4 -0
- package/dist/protocol/ssr-releases.js +22 -0
- package/dist/protocol/type-manifest.d.ts +9 -0
- package/dist/protocol/type-manifest.js +19 -0
- package/dist/protocol/types-lock.d.ts +11 -0
- package/dist/protocol/types-lock.js +33 -0
- package/dist/release-manifest.test.d.ts +1 -0
- package/dist/release-manifest.test.js +119 -0
- package/dist/runtime/client/runtime.d.ts +12 -0
- package/dist/runtime/client/runtime.js +115 -0
- package/dist/runtime/client/runtime.js.LICENSE.txt +1 -0
- package/dist/runtime/core/manifest-loader.d.ts +6 -0
- package/dist/runtime/core/manifest-loader.js +29 -0
- package/dist/runtime/core/preload.d.ts +8 -0
- package/dist/runtime/core/preload.js +45 -0
- package/dist/runtime/core/remote-source.d.ts +32 -0
- package/dist/runtime/core/remote-source.js +159 -0
- package/dist/runtime/react/remote.d.ts +8 -0
- package/dist/runtime/react/remote.js +42 -0
- package/dist/runtime/server/cache/config.d.ts +11 -0
- package/dist/runtime/server/cache/config.js +1 -0
- package/dist/runtime/server/cache/disk-cache.d.ts +10 -0
- package/dist/runtime/server/cache/disk-cache.js +172 -0
- package/dist/runtime/server/cache/entry.d.ts +13 -0
- package/dist/runtime/server/cache/entry.js +47 -0
- package/dist/runtime/server/cache/lock.d.ts +3 -0
- package/dist/runtime/server/cache/lock.js +115 -0
- package/dist/runtime/server/cache/paths.d.ts +2 -0
- package/dist/runtime/server/cache/paths.js +17 -0
- package/dist/runtime/server/cache/prune.d.ts +4 -0
- package/dist/runtime/server/cache/prune.js +88 -0
- package/dist/runtime/server/loader.d.ts +11 -0
- package/dist/runtime/server/loader.js +49 -0
- package/dist/runtime/server/runtime.d.ts +18 -0
- package/dist/runtime/server/runtime.js +106 -0
- package/dist/server.d.ts +13 -0
- package/dist/server.js +6 -0
- package/dist/shared.d.ts +2 -0
- package/dist/shared.js +1 -0
- package/dist/sharing/registry.d.ts +13 -0
- package/dist/sharing/registry.js +69 -0
- package/dist/transport/loading.d.ts +16 -0
- package/dist/transport/loading.js +186 -0
- package/dist/transport/network-policy.d.ts +4 -0
- package/dist/transport/network-policy.js +30 -0
- package/dist/transport/observability.d.ts +9 -0
- package/dist/transport/observability.js +111 -0
- package/dist/transport/registry.d.ts +14 -0
- package/dist/transport/registry.js +96 -0
- package/dist/transport/release-loader.d.ts +4 -0
- package/dist/transport/release-loader.js +12 -0
- package/package.json +74 -6
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { rspack } from "@rsbuild/core";
|
|
2
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import node_path from "node:path";
|
|
5
|
+
import { formatStats } from "./format-stats.js";
|
|
6
|
+
function getMFOutputPath(config) {
|
|
7
|
+
const outputPath = config.output?.path;
|
|
8
|
+
if (!outputPath) throw new Error("[plugin-mf] Unable to determine MF output path");
|
|
9
|
+
return node_path.resolve(outputPath, "mf");
|
|
10
|
+
}
|
|
11
|
+
async function buildExpose({ entry, externals, splitChunks = false, releaseId, config: baseConfig }) {
|
|
12
|
+
const scope = "node" === baseConfig.name ? "server" : "client";
|
|
13
|
+
const outputPath = node_path.join(getMFOutputPath(baseConfig), releaseId, scope);
|
|
14
|
+
const config = {
|
|
15
|
+
...baseConfig,
|
|
16
|
+
name: `mf-${String(baseConfig.name || "bundle")}`,
|
|
17
|
+
entry,
|
|
18
|
+
output: {
|
|
19
|
+
...baseConfig.output,
|
|
20
|
+
path: outputPath,
|
|
21
|
+
publicPath: "auto",
|
|
22
|
+
filename: "[name].[contenthash:10].js",
|
|
23
|
+
cssFilename: "[name].[contenthash:10].css",
|
|
24
|
+
cssChunkFilename: "[name].[contenthash:10].css",
|
|
25
|
+
library: {
|
|
26
|
+
type: "module"
|
|
27
|
+
},
|
|
28
|
+
module: true
|
|
29
|
+
},
|
|
30
|
+
plugins: baseConfig.plugins?.map((plugin)=>plugin instanceof rspack.CssExtractRspackPlugin || plugin?.constructor.name === "CssExtractRspackPlugin" ? new rspack.CssExtractRspackPlugin({
|
|
31
|
+
...plugin.options,
|
|
32
|
+
filename: "[name].[contenthash:10].css",
|
|
33
|
+
chunkFilename: "[name].[contenthash:10].css"
|
|
34
|
+
}) : plugin),
|
|
35
|
+
optimization: {
|
|
36
|
+
...baseConfig.optimization,
|
|
37
|
+
runtimeChunk: false
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
config.output ??= {};
|
|
41
|
+
config.optimization ??= {};
|
|
42
|
+
const mergedExternals = [];
|
|
43
|
+
if (baseConfig.externals) mergedExternals.push(baseConfig.externals);
|
|
44
|
+
config.externals = mergedExternals.flat();
|
|
45
|
+
config.externals.unshift(({ request }, callback)=>{
|
|
46
|
+
if (request && externals && Object.hasOwn(externals, request)) return void callback(void 0, `var globalThis.__MF_GET_SHARED__(${JSON.stringify(request)}, "default", ${JSON.stringify(externals[request].requiredVersion)})`);
|
|
47
|
+
callback();
|
|
48
|
+
});
|
|
49
|
+
if ("web" === baseConfig.name) {
|
|
50
|
+
config.output.chunkFormat = "module";
|
|
51
|
+
config.output.chunkLoading = "import";
|
|
52
|
+
config.optimization.splitChunks = splitChunks ? {
|
|
53
|
+
chunks: "all",
|
|
54
|
+
minSize: 0
|
|
55
|
+
} : false;
|
|
56
|
+
}
|
|
57
|
+
if ("server" === scope) {
|
|
58
|
+
config.output.chunkFormat = "module";
|
|
59
|
+
config.output.chunkLoading = "import";
|
|
60
|
+
}
|
|
61
|
+
const compiler = rspack(config);
|
|
62
|
+
try {
|
|
63
|
+
const { manifest, stats } = await new Promise((resolve, reject)=>{
|
|
64
|
+
compiler.run((error, stats)=>{
|
|
65
|
+
if (error) return reject(error);
|
|
66
|
+
if (stats?.hasErrors()) {
|
|
67
|
+
console.error(stats.toString({
|
|
68
|
+
colors: true
|
|
69
|
+
}));
|
|
70
|
+
return reject(new Error("MF build failed"));
|
|
71
|
+
}
|
|
72
|
+
const manifest = {};
|
|
73
|
+
const files = "server" === scope ? stats?.compilation.getAssets().map((asset)=>asset.name).sort() : void 0;
|
|
74
|
+
for (const [name, entrypoint] of Object.entries(stats?.toJson({
|
|
75
|
+
entrypoints: true
|
|
76
|
+
}).entrypoints ?? {})){
|
|
77
|
+
const assets = entrypoint.assets?.map((asset)=>asset.name) ?? [];
|
|
78
|
+
const outputEntry = stats?.compilation.entrypoints.get(name)?.getEntrypointChunk();
|
|
79
|
+
const js = [
|
|
80
|
+
...outputEntry?.files ?? []
|
|
81
|
+
].filter((asset)=>/\.m?js$/.test(asset));
|
|
82
|
+
if (1 !== js.length) return reject(new Error(`[plugin-mf] Expected one JS entry for "${name}", got ${js.length}`));
|
|
83
|
+
const emitted = stats?.compilation.getAssets() ?? [];
|
|
84
|
+
manifest[name] = {
|
|
85
|
+
js: js[0],
|
|
86
|
+
css: assets.filter((asset)=>asset.endsWith(".css")),
|
|
87
|
+
...files ? {
|
|
88
|
+
files
|
|
89
|
+
} : {},
|
|
90
|
+
..."client" === scope ? {
|
|
91
|
+
assets: emitted.filter((asset)=>/\.(?:m?js|css)$/.test(asset.name)).map((asset)=>({
|
|
92
|
+
path: asset.name,
|
|
93
|
+
bytes: asset.source.size(),
|
|
94
|
+
type: asset.name.endsWith(".css") ? "css" : "js"
|
|
95
|
+
}))
|
|
96
|
+
} : {}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
resolve({
|
|
100
|
+
manifest,
|
|
101
|
+
stats
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
if (stats) console.log(await formatStats({
|
|
106
|
+
stats,
|
|
107
|
+
outputPath,
|
|
108
|
+
environment: config.name || ""
|
|
109
|
+
}));
|
|
110
|
+
if ("client" === scope && stats) {
|
|
111
|
+
const modules = stats.toJson({
|
|
112
|
+
modules: true,
|
|
113
|
+
nestedModules: true
|
|
114
|
+
}).modules ?? [];
|
|
115
|
+
const duplicates = modules.filter((module)=>(module.chunks?.length ?? 0) > 1).map((module)=>({
|
|
116
|
+
name: module.name,
|
|
117
|
+
bytes: module.size,
|
|
118
|
+
copies: module.chunks?.length
|
|
119
|
+
}));
|
|
120
|
+
await writeFile(node_path.join(outputPath, "bundle-report.json"), JSON.stringify({
|
|
121
|
+
splitChunks,
|
|
122
|
+
duplicates,
|
|
123
|
+
assets: stats.compilation.getAssets().map((asset)=>({
|
|
124
|
+
path: asset.name,
|
|
125
|
+
bytes: asset.source.size()
|
|
126
|
+
}))
|
|
127
|
+
}, null, 2), "utf8");
|
|
128
|
+
}
|
|
129
|
+
if ("server" === scope) {
|
|
130
|
+
for (const entry of Object.values(manifest))if ("string" != typeof entry) entry.integrity = Object.fromEntries(await Promise.all((entry.files ?? []).map(async (file)=>[
|
|
131
|
+
file,
|
|
132
|
+
createHash("sha256").update(await readFile(node_path.join(outputPath, file))).digest("hex")
|
|
133
|
+
])));
|
|
134
|
+
}
|
|
135
|
+
await writeFile(node_path.join(outputPath, "manifest.json"), JSON.stringify(manifest, null, 2), "utf8");
|
|
136
|
+
} finally{
|
|
137
|
+
await new Promise((resolve, reject)=>{
|
|
138
|
+
compiler.close((error)=>error ? reject(error) : resolve());
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export { buildExpose, getMFOutputPath };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Rspack } from "@rsbuild/core";
|
|
2
|
+
import type { SharedRequirements } from "../protocol/shared-dependencies.ts";
|
|
3
|
+
type BuildReleaseOptions = {
|
|
4
|
+
entry: Record<string, string>;
|
|
5
|
+
externals?: SharedRequirements;
|
|
6
|
+
splitChunks?: boolean;
|
|
7
|
+
nodeConfig?: Rspack.Configuration;
|
|
8
|
+
webConfig?: Rspack.Configuration;
|
|
9
|
+
rootPath: string;
|
|
10
|
+
};
|
|
11
|
+
export declare function buildRelease({ entry, externals, splitChunks, nodeConfig, webConfig, rootPath }: BuildReleaseOptions): Promise<void>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { rename, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { buildExpose, getMFOutputPath } from "./build-expose.js";
|
|
5
|
+
import { buildTypes } from "./build-types.js";
|
|
6
|
+
import { RELEASE_MANIFEST_VERSION } from "../protocol/release-manifest.js";
|
|
7
|
+
async function buildRelease({ entry, externals, splitChunks, nodeConfig, webConfig, rootPath }) {
|
|
8
|
+
const releaseId = randomUUID();
|
|
9
|
+
const tasks = [];
|
|
10
|
+
if (nodeConfig) tasks.push(buildExpose({
|
|
11
|
+
releaseId,
|
|
12
|
+
entry,
|
|
13
|
+
externals,
|
|
14
|
+
splitChunks,
|
|
15
|
+
config: nodeConfig
|
|
16
|
+
}));
|
|
17
|
+
if (webConfig) tasks.push(buildExpose({
|
|
18
|
+
releaseId,
|
|
19
|
+
entry,
|
|
20
|
+
externals,
|
|
21
|
+
splitChunks,
|
|
22
|
+
config: webConfig
|
|
23
|
+
}));
|
|
24
|
+
await Promise.all(tasks);
|
|
25
|
+
const outputConfig = webConfig ?? nodeConfig;
|
|
26
|
+
if (outputConfig) {
|
|
27
|
+
const outputPath = getMFOutputPath(outputConfig);
|
|
28
|
+
await buildTypes({
|
|
29
|
+
rootPath,
|
|
30
|
+
entry,
|
|
31
|
+
outputPath: node_path.join(outputPath, releaseId)
|
|
32
|
+
});
|
|
33
|
+
const manifest = {
|
|
34
|
+
version: RELEASE_MANIFEST_VERSION,
|
|
35
|
+
releaseId,
|
|
36
|
+
...nodeConfig ? {
|
|
37
|
+
server: `${releaseId}/server/manifest.json`
|
|
38
|
+
} : {},
|
|
39
|
+
...webConfig ? {
|
|
40
|
+
client: `${releaseId}/client/manifest.json`
|
|
41
|
+
} : {},
|
|
42
|
+
types: `${releaseId}/types/manifest.json`,
|
|
43
|
+
shared: {
|
|
44
|
+
client: externals ?? {},
|
|
45
|
+
server: externals ?? {}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const manifestSource = JSON.stringify(manifest, null, 2);
|
|
49
|
+
await writeFile(node_path.join(outputPath, releaseId, "pointer.json"), manifestSource, "utf8");
|
|
50
|
+
const temporaryPath = node_path.join(outputPath, `.manifest.${releaseId}.tmp`);
|
|
51
|
+
try {
|
|
52
|
+
await writeFile(temporaryPath, manifestSource, "utf8");
|
|
53
|
+
await rename(temporaryPath, node_path.join(outputPath, "manifest.json"));
|
|
54
|
+
} finally{
|
|
55
|
+
await rm(temporaryPath, {
|
|
56
|
+
force: true
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export { buildRelease };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import node_path from "node:path";
|
|
2
|
+
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import typescript from "typescript";
|
|
4
|
+
async function buildTypes({ entry, outputPath, rootPath }) {
|
|
5
|
+
const configPath = typescript.findConfigFile(rootPath, typescript.sys.fileExists, "tsconfig.json");
|
|
6
|
+
if (!configPath) throw new Error("[plugin-mf] Unable to find tsconfig.json for remote type generation");
|
|
7
|
+
const config = typescript.readConfigFile(configPath, typescript.sys.readFile);
|
|
8
|
+
if (config.error) throw new Error(formatDiagnostics([
|
|
9
|
+
config.error
|
|
10
|
+
]));
|
|
11
|
+
const configDirectory = node_path.dirname(configPath);
|
|
12
|
+
const parsed = typescript.parseJsonConfigFileContent(config.config, typescript.sys, configDirectory);
|
|
13
|
+
if (parsed.errors.length) throw new Error(formatDiagnostics(parsed.errors));
|
|
14
|
+
const typesPath = node_path.join(outputPath, "types");
|
|
15
|
+
const compilerOptions = {
|
|
16
|
+
...parsed.options,
|
|
17
|
+
declaration: true,
|
|
18
|
+
declarationMap: false,
|
|
19
|
+
emitDeclarationOnly: true,
|
|
20
|
+
listEmittedFiles: true,
|
|
21
|
+
noEmit: false,
|
|
22
|
+
outDir: typesPath
|
|
23
|
+
};
|
|
24
|
+
await rm(typesPath, {
|
|
25
|
+
recursive: true,
|
|
26
|
+
force: true
|
|
27
|
+
});
|
|
28
|
+
await mkdir(typesPath, {
|
|
29
|
+
recursive: true
|
|
30
|
+
});
|
|
31
|
+
const fileNames = Object.values(entry).map((request)=>node_path.resolve(configDirectory, request));
|
|
32
|
+
const declarationFiles = parsed.fileNames.filter((fileName)=>fileName.endsWith(".d.ts"));
|
|
33
|
+
const program = typescript.createProgram([
|
|
34
|
+
...new Set([
|
|
35
|
+
...fileNames,
|
|
36
|
+
...declarationFiles
|
|
37
|
+
])
|
|
38
|
+
], compilerOptions);
|
|
39
|
+
const emitResult = program.emit();
|
|
40
|
+
const diagnostics = typescript.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
41
|
+
const errors = diagnostics.filter((diagnostic)=>diagnostic.category === typescript.DiagnosticCategory.Error);
|
|
42
|
+
if (errors.length) throw new Error(formatDiagnostics(errors));
|
|
43
|
+
const commonSourceDirectory = compilerOptions.rootDir || findCommonDirectory(fileNames);
|
|
44
|
+
const checker = program.getTypeChecker();
|
|
45
|
+
const exposes = {};
|
|
46
|
+
for (const [name, request] of Object.entries(entry)){
|
|
47
|
+
const sourcePath = node_path.resolve(configDirectory, request);
|
|
48
|
+
const sourceFile = program.getSourceFile(sourcePath);
|
|
49
|
+
if (!sourceFile) throw new Error(`[plugin-mf] Unable to find source file for exposed module "${name}": ${request}`);
|
|
50
|
+
const sourceSymbol = checker.getSymbolAtLocation(sourceFile);
|
|
51
|
+
const symbol = sourceSymbol && checker.getExportsOfModule(sourceSymbol).find((item)=>item.name === name);
|
|
52
|
+
if (!symbol) throw new Error(`[plugin-mf] Exposed module "${name}" must have a named export "${name}"`);
|
|
53
|
+
const relativeSourcePath = node_path.relative(commonSourceDirectory, sourcePath);
|
|
54
|
+
const declarationPath = relativeSourcePath.replace(/\.[^.]+$/, ".d.ts").replace(/\\/g, "/");
|
|
55
|
+
exposes[name] = {
|
|
56
|
+
path: declarationPath,
|
|
57
|
+
export: name
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const files = emitResult.emittedFiles?.filter((filePath)=>filePath.endsWith(".d.ts")).map((filePath)=>node_path.relative(typesPath, filePath).replace(/\\/g, "/")).sort();
|
|
61
|
+
if (!files?.length) throw new Error("[plugin-mf] TypeScript did not emit declarations for exposed modules");
|
|
62
|
+
const manifest = {
|
|
63
|
+
version: 1,
|
|
64
|
+
files,
|
|
65
|
+
exposes
|
|
66
|
+
};
|
|
67
|
+
await writeFile(node_path.join(typesPath, "manifest.json"), JSON.stringify(manifest, null, 2), "utf8");
|
|
68
|
+
}
|
|
69
|
+
function formatDiagnostics(diagnostics) {
|
|
70
|
+
return typescript.formatDiagnosticsWithColorAndContext(diagnostics, {
|
|
71
|
+
getCanonicalFileName: (fileName)=>fileName,
|
|
72
|
+
getCurrentDirectory: process.cwd,
|
|
73
|
+
getNewLine: ()=>"\n"
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function findCommonDirectory(fileNames) {
|
|
77
|
+
const directories = fileNames.map((fileName)=>node_path.dirname(node_path.resolve(fileName)).split(node_path.sep));
|
|
78
|
+
const common = [
|
|
79
|
+
...directories[0]
|
|
80
|
+
];
|
|
81
|
+
for (const directory of directories.slice(1)){
|
|
82
|
+
let index = 0;
|
|
83
|
+
while(index < common.length && common[index] === directory[index])index += 1;
|
|
84
|
+
common.length = index;
|
|
85
|
+
}
|
|
86
|
+
return common.join(node_path.sep) || node_path.parse(process.cwd()).root;
|
|
87
|
+
}
|
|
88
|
+
export { buildTypes };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { LoadingOptions } from "../protocol/load-error.ts";
|
|
2
|
+
import type { TypesSyncOptions } from "./types-options.ts";
|
|
3
|
+
import type { SharedDependency } from "../protocol/shared-dependencies.ts";
|
|
4
|
+
import type { NetworkOptions } from "../protocol/network-policy.ts";
|
|
5
|
+
export type PluginMFConfig = {
|
|
6
|
+
splitChunks?: boolean;
|
|
7
|
+
network?: NetworkOptions;
|
|
8
|
+
types?: TypesSyncOptions;
|
|
9
|
+
loading?: LoadingOptions;
|
|
10
|
+
remotes?: Record<string, string>;
|
|
11
|
+
expose?: Record<string, string>;
|
|
12
|
+
externals?: SharedDependency[];
|
|
13
|
+
shared?: SharedDependency[];
|
|
14
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Rspack } from "@rsbuild/core";
|
|
2
|
+
type FormatStatsOptions = {
|
|
3
|
+
stats: Rspack.Stats;
|
|
4
|
+
outputPath: string;
|
|
5
|
+
environment: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function formatStats({ stats, outputPath, environment, }: FormatStatsOptions): Promise<string>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { gzipSync } from "node:zlib";
|
|
4
|
+
function formatSize(bytes) {
|
|
5
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
6
|
+
return `${(bytes / 1024).toFixed(1)} kB`;
|
|
7
|
+
}
|
|
8
|
+
async function formatStats({ stats, outputPath, environment }) {
|
|
9
|
+
const assets = stats.toJson({
|
|
10
|
+
assets: true
|
|
11
|
+
}).assets ?? [];
|
|
12
|
+
const rows = [];
|
|
13
|
+
let totalSize = 0;
|
|
14
|
+
let totalGzipSize = 0;
|
|
15
|
+
for (const asset of assets){
|
|
16
|
+
if (!asset.name) continue;
|
|
17
|
+
const filePath = node_path.join(outputPath, asset.name);
|
|
18
|
+
const file = await readFile(filePath);
|
|
19
|
+
const size = file.byteLength;
|
|
20
|
+
totalSize += size;
|
|
21
|
+
if ("web" === environment) {
|
|
22
|
+
const gzipSize = gzipSync(file).byteLength;
|
|
23
|
+
totalGzipSize += gzipSize;
|
|
24
|
+
rows.push(`${node_path.relative(process.cwd(), filePath).padEnd(48)}${formatSize(size).padEnd(12)}` + formatSize(gzipSize));
|
|
25
|
+
} else rows.push(`${node_path.relative(process.cwd(), filePath).padEnd(48)}` + formatSize(size));
|
|
26
|
+
}
|
|
27
|
+
const output = [
|
|
28
|
+
`File (${environment})`.padEnd(48) + "Size".padEnd("web" === environment ? 12 : 0) + ("web" === environment ? "Gzip" : ""),
|
|
29
|
+
...rows,
|
|
30
|
+
"",
|
|
31
|
+
"web" === environment ? "Total:".padEnd(48) + `${formatSize(totalSize).padEnd(12)}` + formatSize(totalGzipSize) : "Total:".padEnd(48) + formatSize(totalSize)
|
|
32
|
+
];
|
|
33
|
+
return output.join("\n") + "\n";
|
|
34
|
+
}
|
|
35
|
+
export { formatStats };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { rspack } from "@rsbuild/core";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { buildRelease } from "./build-release.js";
|
|
4
|
+
import { resolver } from "./resolver.js";
|
|
5
|
+
import { modifyEntry } from "./bootstrap.js";
|
|
6
|
+
import { syncTypes } from "./sync-types.js";
|
|
7
|
+
import { configureLoading } from "../transport/loading.js";
|
|
8
|
+
import { resolveShared } from "./shared-dependencies.js";
|
|
9
|
+
import { configureNetwork } from "../transport/network-policy.js";
|
|
10
|
+
const pluginMF = (mfConfig = {})=>({
|
|
11
|
+
name: "plugin-mf",
|
|
12
|
+
setup (api) {
|
|
13
|
+
if (mfConfig.network) configureNetwork(mfConfig.network);
|
|
14
|
+
const shared = resolveShared(mfConfig.shared, api.context.rootPath);
|
|
15
|
+
const externals = resolveShared(mfConfig.externals, api.context.rootPath);
|
|
16
|
+
if (mfConfig.loading) configureLoading(mfConfig.loading);
|
|
17
|
+
let nodeConfig;
|
|
18
|
+
let webConfig;
|
|
19
|
+
if (mfConfig.expose && Object.keys(mfConfig.expose).length) api.modifyEnvironmentConfig((config)=>{
|
|
20
|
+
const clean = config.output.cleanDistPath;
|
|
21
|
+
const options = "object" == typeof clean ? clean : {
|
|
22
|
+
enable: clean
|
|
23
|
+
};
|
|
24
|
+
const mfPath = node_path.resolve(api.context.rootPath, config.output.distPath.root ?? "dist", "mf").replace(/\\/g, "/");
|
|
25
|
+
const keepMF = new RegExp(`^${mfPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:/|$)`);
|
|
26
|
+
config.output.cleanDistPath = {
|
|
27
|
+
...options,
|
|
28
|
+
keep: [
|
|
29
|
+
...options.keep ?? [],
|
|
30
|
+
keepMF
|
|
31
|
+
]
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
api.onBeforeCreateCompiler(({ bundlerConfigs })=>{
|
|
35
|
+
bundlerConfigs.forEach((config)=>{
|
|
36
|
+
if (!nodeConfig && "node" === config.name) {
|
|
37
|
+
nodeConfig = config;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (!webConfig && "web" === config.name) webConfig = config;
|
|
41
|
+
});
|
|
42
|
+
if (!nodeConfig && webConfig) nodeConfig = {
|
|
43
|
+
...webConfig,
|
|
44
|
+
name: "node",
|
|
45
|
+
target: "node",
|
|
46
|
+
externalsPresets: {
|
|
47
|
+
...webConfig.externalsPresets,
|
|
48
|
+
node: true
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
api.onAfterBuild(async ()=>{
|
|
53
|
+
if (!mfConfig.expose || 0 === Object.keys(mfConfig.expose).length) return;
|
|
54
|
+
await buildRelease({
|
|
55
|
+
entry: mfConfig.expose,
|
|
56
|
+
externals,
|
|
57
|
+
splitChunks: mfConfig.splitChunks,
|
|
58
|
+
nodeConfig,
|
|
59
|
+
webConfig,
|
|
60
|
+
rootPath: api.context.rootPath
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
const remoteNames = Object.keys(mfConfig.remotes || {});
|
|
64
|
+
api.modifyRspackConfig((config)=>modifyEntry(config, mfConfig, shared));
|
|
65
|
+
if (remoteNames.length) {
|
|
66
|
+
api.onBeforeBuild(async ()=>{
|
|
67
|
+
await syncTypes({
|
|
68
|
+
remotes: mfConfig.remotes ?? {},
|
|
69
|
+
rootPath: api.context.rootPath,
|
|
70
|
+
types: mfConfig.types
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
api.modifyRspackConfig((config)=>{
|
|
74
|
+
config.plugins = config.plugins || [];
|
|
75
|
+
config.plugins.push(new rspack.DefinePlugin({
|
|
76
|
+
__MF_REMOTES__: JSON.stringify(mfConfig.remotes),
|
|
77
|
+
__MF_LOADING__: JSON.stringify(mfConfig.loading ?? {}),
|
|
78
|
+
__MF_NETWORK__: JSON.stringify(mfConfig.network ?? {})
|
|
79
|
+
}));
|
|
80
|
+
});
|
|
81
|
+
api.resolve(({ resolveData, environment })=>{
|
|
82
|
+
resolveData.request = resolver(resolveData.request, remoteNames, environment.name);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
export { pluginMF };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const resolver: (request: string, remoteNames: string[], env: string) => string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const resolver = (request, remoteNames, env)=>{
|
|
2
|
+
if (request.startsWith(".") || request.startsWith("/") || request.startsWith("data:")) return request;
|
|
3
|
+
const [maybeRemote, ...rest] = request.split("/");
|
|
4
|
+
if (!remoteNames.includes(maybeRemote)) return request;
|
|
5
|
+
const expose = rest.join("/") || "default";
|
|
6
|
+
const scope = "node" === env ? "server" : "client";
|
|
7
|
+
const code = `
|
|
8
|
+
import { createRemoteComponent } from "plugin-mf/${scope}";
|
|
9
|
+
export default createRemoteComponent(${JSON.stringify(maybeRemote)}, ${JSON.stringify(expose)});
|
|
10
|
+
`;
|
|
11
|
+
return `data:text/javascript,${encodeURIComponent(code)}`;
|
|
12
|
+
};
|
|
13
|
+
export { resolver };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { packageName, parseSharedRequirements } from "../protocol/shared-dependencies.js";
|
|
5
|
+
function installedVersion(request, root) {
|
|
6
|
+
const name = packageName(request);
|
|
7
|
+
const require = createRequire(node_path.join(root, "package.json"));
|
|
8
|
+
let directory = node_path.dirname(require.resolve(request));
|
|
9
|
+
while(true){
|
|
10
|
+
try {
|
|
11
|
+
const metadata = JSON.parse(readFileSync(node_path.join(directory, "package.json"), "utf8"));
|
|
12
|
+
if (metadata && "object" == typeof metadata && "name" in metadata && metadata.name === name && "version" in metadata && "string" == typeof metadata.version) return metadata.version;
|
|
13
|
+
} catch (error) {
|
|
14
|
+
if (!(error instanceof Error && "code" in error && "ENOENT" === error.code)) throw error;
|
|
15
|
+
}
|
|
16
|
+
const parent = node_path.dirname(directory);
|
|
17
|
+
if (parent === directory) throw new Error(`[plugin-mf] Cannot determine installed version: ${request}`);
|
|
18
|
+
directory = parent;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function resolveShared(dependencies = [], root) {
|
|
22
|
+
const options = dependencies.map((item)=>"string" == typeof item ? {
|
|
23
|
+
name: item
|
|
24
|
+
} : item);
|
|
25
|
+
const expanded = [
|
|
26
|
+
...options
|
|
27
|
+
];
|
|
28
|
+
if (options.some((item)=>"react" === item.name)) {
|
|
29
|
+
for (const name of [
|
|
30
|
+
"react/jsx-runtime",
|
|
31
|
+
"react/jsx-dev-runtime"
|
|
32
|
+
])if (!expanded.some((item)=>item.name === name)) expanded.push({
|
|
33
|
+
...options.find((item)=>"react" === item.name),
|
|
34
|
+
name
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const entries = expanded.map((item)=>{
|
|
38
|
+
const inherited = options.find((option)=>option.name === packageName(item.name));
|
|
39
|
+
const config = {
|
|
40
|
+
...inherited,
|
|
41
|
+
...item
|
|
42
|
+
};
|
|
43
|
+
const version = installedVersion(item.name, root);
|
|
44
|
+
if (void 0 !== config.version && config.version !== version) throw new Error(`[plugin-mf] ${item.name}: configured version ${config.version} differs from installed ${version}`);
|
|
45
|
+
return [
|
|
46
|
+
item.name,
|
|
47
|
+
{
|
|
48
|
+
version,
|
|
49
|
+
requiredVersion: config.requiredVersion ?? `^${version}`,
|
|
50
|
+
singleton: config.singleton ?? true
|
|
51
|
+
}
|
|
52
|
+
];
|
|
53
|
+
});
|
|
54
|
+
return parseSharedRequirements(Object.fromEntries(entries));
|
|
55
|
+
}
|
|
56
|
+
export { resolveShared };
|