@bleedingdev/modern-js-app-tools-extensions 3.8.3-ultramodern.6 → 3.9.0-ultramodern.10
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/dist/cjs/backend-federation-build/codegen.js +256 -0
- package/dist/cjs/backend-federation-build/config.js +189 -0
- package/dist/cjs/backend-federation-build/emit.js +150 -0
- package/dist/cjs/backend-federation-build/index.js +53 -0
- package/dist/cjs/build-config/build-environment.js +268 -0
- package/dist/cjs/build-config/precompress/plugin.js +124 -0
- package/dist/cjs/build-config/precompress/types.js +18 -0
- package/dist/cjs/build-config/public.js +46 -0
- package/dist/cjs/cloudflare/constants.js +1 -1
- package/dist/cjs/cloudflare/delivery-unit.js +7 -16
- package/dist/cjs/cloudflare-output-contract.js +37 -1
- package/dist/cjs/cloudflare-output-verifier/identity.js +6 -6
- package/dist/cjs/cloudflare-output-verifier/worker-bundles.js +4 -1
- package/dist/cjs/config.js +29 -1
- package/dist/cjs/deploy-output/npmAliases.js +226 -0
- package/dist/cjs/deploy-output/plugin.js +76 -0
- package/dist/cjs/deploy-output/target.js +48 -0
- package/dist/cjs/native-type-checker.js +193 -0
- package/dist/cjs/policy-defaults.js +101 -0
- package/dist/cjs/release-envelope/framework-output.js +77 -32
- package/dist/cjs/release-envelope/plugin.js +5 -3
- package/dist/cjs/release-identity.js +3 -6
- package/dist/cjs/runtime-package-resolution.js +118 -0
- package/dist/esm/backend-federation-build/codegen.mjs +205 -0
- package/dist/esm/backend-federation-build/config.mjs +116 -0
- package/dist/esm/backend-federation-build/emit.mjs +99 -0
- package/dist/esm/backend-federation-build/index.mjs +1 -0
- package/dist/esm/build-config/build-environment.mjs +224 -0
- package/dist/esm/build-config/precompress/plugin.mjs +76 -0
- package/dist/esm/build-config/precompress/types.mjs +0 -0
- package/dist/esm/build-config/public.mjs +1 -0
- package/dist/esm/cloudflare/constants.mjs +1 -1
- package/dist/esm/cloudflare/delivery-unit.mjs +1 -10
- package/dist/esm/cloudflare-output-contract.mjs +35 -2
- package/dist/esm/cloudflare-output-verifier/identity.mjs +1 -1
- package/dist/esm/cloudflare-output-verifier/worker-bundles.mjs +5 -2
- package/dist/esm/config.mjs +1 -0
- package/dist/esm/deploy-output/npmAliases.mjs +174 -0
- package/dist/esm/deploy-output/plugin.mjs +28 -0
- package/dist/esm/deploy-output/target.mjs +10 -0
- package/dist/esm/native-type-checker.mjs +142 -0
- package/dist/esm/policy-defaults.mjs +45 -0
- package/dist/esm/release-envelope/framework-output.mjs +51 -6
- package/dist/esm/release-envelope/plugin.mjs +5 -3
- package/dist/esm/release-identity.mjs +3 -6
- package/dist/esm/runtime-package-resolution.mjs +60 -0
- package/dist/esm-node/backend-federation-build/codegen.mjs +206 -0
- package/dist/esm-node/backend-federation-build/config.mjs +117 -0
- package/dist/esm-node/backend-federation-build/emit.mjs +100 -0
- package/dist/esm-node/backend-federation-build/index.mjs +2 -0
- package/dist/esm-node/build-config/build-environment.mjs +225 -0
- package/dist/esm-node/build-config/precompress/plugin.mjs +77 -0
- package/dist/esm-node/build-config/precompress/types.mjs +1 -0
- package/dist/esm-node/build-config/public.mjs +2 -0
- package/dist/esm-node/cloudflare/constants.mjs +1 -1
- package/dist/esm-node/cloudflare/delivery-unit.mjs +1 -10
- package/dist/esm-node/cloudflare-output-contract.mjs +35 -2
- package/dist/esm-node/cloudflare-output-verifier/identity.mjs +1 -1
- package/dist/esm-node/cloudflare-output-verifier/worker-bundles.mjs +5 -2
- package/dist/esm-node/config.mjs +1 -0
- package/dist/esm-node/deploy-output/npmAliases.mjs +175 -0
- package/dist/esm-node/deploy-output/plugin.mjs +31 -0
- package/dist/esm-node/deploy-output/target.mjs +11 -0
- package/dist/esm-node/native-type-checker.mjs +143 -0
- package/dist/esm-node/policy-defaults.mjs +46 -0
- package/dist/esm-node/release-envelope/framework-output.mjs +51 -6
- package/dist/esm-node/release-envelope/plugin.mjs +5 -3
- package/dist/esm-node/release-identity.mjs +3 -6
- package/dist/esm-node/runtime-package-resolution.mjs +61 -0
- package/dist/types/backend-federation-build/codegen.d.ts +86 -0
- package/dist/types/backend-federation-build/config.d.ts +102 -0
- package/dist/types/backend-federation-build/emit.d.ts +21 -0
- package/dist/types/backend-federation-build/index.d.ts +2 -0
- package/dist/types/build-config/build-environment.d.ts +22 -0
- package/dist/types/build-config/precompress/plugin.d.ts +4 -0
- package/dist/types/build-config/precompress/types.d.ts +19 -0
- package/dist/types/build-config/public.d.ts +2 -0
- package/dist/types/cloudflare/artifacts.d.ts +1 -1
- package/dist/types/cloudflare/constants.d.ts +2 -2
- package/dist/types/cloudflare/delivery-unit.d.ts +2 -2
- package/dist/types/cloudflare/i18n-worker.d.ts +1 -1
- package/dist/types/cloudflare/index.d.ts +1 -1
- package/dist/types/cloudflare/security-policies.d.ts +1 -1
- package/dist/types/cloudflare/types.d.ts +1 -1
- package/dist/types/cloudflare/utils.d.ts +1 -1
- package/dist/types/cloudflare/worker-manifest.d.ts +4 -4
- package/dist/types/cloudflare/wrangler-config.d.ts +2 -2
- package/dist/types/cloudflare-output-contract.d.ts +2 -1
- package/dist/types/cloudflare-output-plan.d.ts +1 -1
- package/dist/types/cloudflare-output-verifier/identity.d.ts +2 -2
- package/dist/types/cloudflare-output-verifier/index.d.ts +5 -5
- package/dist/types/cloudflare-output-verifier/manifest.d.ts +1 -1
- package/dist/types/cloudflare-output-verifier/mutation-policy.d.ts +1 -1
- package/dist/types/cloudflare-output-verifier/worker-bundles.d.ts +1 -1
- package/dist/types/config.d.ts +2 -0
- package/dist/types/deploy-output/npmAliases.d.ts +14 -0
- package/dist/types/deploy-output/plugin.d.ts +15 -0
- package/dist/types/deploy-output/target.d.ts +6 -0
- package/dist/types/index.d.ts +10 -10
- package/dist/types/native-type-checker.d.ts +15 -0
- package/dist/types/policy-defaults.d.ts +46 -0
- package/dist/types/precompress/LICENSE +20 -0
- package/dist/types/precompress/index.d.ts +76 -0
- package/dist/types/release-envelope/canonical.d.ts +1 -1
- package/dist/types/release-envelope/framework-output.d.ts +3 -2
- package/dist/types/release-envelope/index.d.ts +3 -3
- package/dist/types/runtime-package-resolution.d.ts +28 -0
- package/package.json +72 -14
- package/src/backend-federation-build/codegen.ts +245 -0
- package/src/backend-federation-build/config.ts +356 -0
- package/src/backend-federation-build/emit.ts +202 -0
- package/src/backend-federation-build/index.ts +2 -0
- package/src/build-config/build-environment.ts +404 -0
- package/src/build-config/precompress/plugin.ts +140 -0
- package/src/build-config/precompress/types.ts +26 -0
- package/src/build-config/public.ts +6 -0
- package/src/cloudflare/constants.ts +1 -1
- package/src/cloudflare/delivery-unit.ts +3 -20
- package/src/cloudflare/worker-manifest.ts +1 -0
- package/src/cloudflare-builder.ts +1 -0
- package/src/cloudflare-output-contract.ts +37 -0
- package/src/cloudflare-output-verifier/identity.ts +1 -1
- package/src/cloudflare-output-verifier/worker-bundles.ts +5 -3
- package/src/config.ts +7 -0
- package/src/deploy-output/npmAliases.ts +333 -0
- package/src/deploy-output/plugin.ts +49 -0
- package/src/deploy-output/target.ts +23 -0
- package/src/native-type-checker.ts +183 -0
- package/src/policy-defaults.ts +127 -0
- package/src/release-envelope/framework-output.ts +135 -4
- package/src/release-envelope/plugin.ts +15 -3
- package/src/release-identity.ts +3 -2
- package/src/runtime-package-resolution.ts +115 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { resolveUltramodernReleaseIdentity } from '@modern-js/app-tools-extensions/release-identity';
|
|
5
|
+
import {
|
|
6
|
+
BACKEND_FEDERATION_MANIFEST_FILE as BACKEND_MANIFEST_FILE,
|
|
7
|
+
BACKEND_FEDERATION_REMOTE_ENTRY_FILE as BACKEND_REMOTE_ENTRY_FILE,
|
|
8
|
+
stampUltramodernBuildArtifactIdentity,
|
|
9
|
+
ULTRAMODERN_BUILD_ARTIFACT_FILE,
|
|
10
|
+
} from '@modern-js/backend-federation-contracts';
|
|
11
|
+
import { createBackendFederationEntryIntegrity } from '@modern-js/server-runtime-extensions/backend-federation-security/node';
|
|
12
|
+
import {
|
|
13
|
+
createBackendManifest,
|
|
14
|
+
createBackendRemoteEntrySource,
|
|
15
|
+
} from './codegen';
|
|
16
|
+
import {
|
|
17
|
+
type BackendFederationApp,
|
|
18
|
+
buildArtifactPathFor,
|
|
19
|
+
COMPACT_CONFIG_PATH,
|
|
20
|
+
createStampedDeliveryUnit,
|
|
21
|
+
findBackendFederationApp,
|
|
22
|
+
findWorkspaceRoot,
|
|
23
|
+
readBuildIdentity,
|
|
24
|
+
resolveWorkspaceSourceRevision,
|
|
25
|
+
} from './config';
|
|
26
|
+
|
|
27
|
+
export type { BackendFederationBuildIdentity } from './config';
|
|
28
|
+
|
|
29
|
+
export type BackendFederationArtifactResult = {
|
|
30
|
+
appId: string;
|
|
31
|
+
manifestPath: string;
|
|
32
|
+
containerPath: string;
|
|
33
|
+
deliveryUnitArtifactPath?: string;
|
|
34
|
+
remoteName: string;
|
|
35
|
+
remoteType: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const emitBackendFederationArtifacts = async (
|
|
39
|
+
appDirectory: string,
|
|
40
|
+
distDirectory: string,
|
|
41
|
+
): Promise<BackendFederationArtifactResult | undefined> => {
|
|
42
|
+
const workspaceRoot = findWorkspaceRoot(appDirectory);
|
|
43
|
+
if (!workspaceRoot) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const effectApiPath = path.join(appDirectory, 'api/effect-api.ts');
|
|
48
|
+
const backendFederationConfigPath = path.join(
|
|
49
|
+
appDirectory,
|
|
50
|
+
'backend-federation.config.ts',
|
|
51
|
+
);
|
|
52
|
+
if (!existsSync(effectApiPath) || !existsSync(backendFederationConfigPath)) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const app = await findBackendFederationApp(workspaceRoot, appDirectory);
|
|
57
|
+
if (!app) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
const buildIdentity = await readBuildIdentity(appDirectory);
|
|
61
|
+
const compactDeliveryUnit = app.compactDeliveryUnit;
|
|
62
|
+
const hasCompactDeliveryUnit =
|
|
63
|
+
compactDeliveryUnit !== undefined &&
|
|
64
|
+
(compactDeliveryUnit.unitId !== undefined ||
|
|
65
|
+
compactDeliveryUnit.buildMarker !== undefined ||
|
|
66
|
+
compactDeliveryUnit.packageName !== undefined ||
|
|
67
|
+
compactDeliveryUnit.version !== undefined);
|
|
68
|
+
const hasBuildIdentity =
|
|
69
|
+
buildIdentity.unitId !== undefined ||
|
|
70
|
+
buildIdentity.buildVersion !== undefined ||
|
|
71
|
+
buildIdentity.packageName !== undefined ||
|
|
72
|
+
buildIdentity.version !== undefined;
|
|
73
|
+
|
|
74
|
+
if (hasBuildIdentity) {
|
|
75
|
+
const compactConfigPath = path.join(workspaceRoot, COMPACT_CONFIG_PATH);
|
|
76
|
+
const buildIdentityPath = buildArtifactPathFor(appDirectory);
|
|
77
|
+
const mismatches: string[] = [];
|
|
78
|
+
const compare = (
|
|
79
|
+
label: string,
|
|
80
|
+
a?: string,
|
|
81
|
+
b?: string,
|
|
82
|
+
leftLabel = 'deliveryUnit',
|
|
83
|
+
) => {
|
|
84
|
+
if (a !== undefined && b !== undefined && a !== b) {
|
|
85
|
+
mismatches.push(
|
|
86
|
+
`${label}: ${leftLabel}=${a} vs ultramodern-build=${b}`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
compare('appId', app.id, buildIdentity.appId, 'topology');
|
|
91
|
+
|
|
92
|
+
if (hasCompactDeliveryUnit) {
|
|
93
|
+
compare('unitId', compactDeliveryUnit?.unitId, buildIdentity.unitId);
|
|
94
|
+
compare(
|
|
95
|
+
'buildMarker/build',
|
|
96
|
+
compactDeliveryUnit?.buildMarker,
|
|
97
|
+
buildIdentity.buildVersion,
|
|
98
|
+
);
|
|
99
|
+
compare(
|
|
100
|
+
'packageName',
|
|
101
|
+
compactDeliveryUnit?.packageName,
|
|
102
|
+
buildIdentity.packageName,
|
|
103
|
+
);
|
|
104
|
+
compare('version', compactDeliveryUnit?.version, buildIdentity.version);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (mismatches.length > 0) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`[backend-federation-build] Delivery-unit identity drift between ${compactConfigPath} (topology) and ${buildIdentityPath}: ${mismatches.join('; ')}`,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const unitId = compactDeliveryUnit?.unitId ?? buildIdentity.unitId;
|
|
115
|
+
const generationBuildMarker =
|
|
116
|
+
compactDeliveryUnit?.buildMarker ?? buildIdentity.buildVersion;
|
|
117
|
+
const sourceRevision = await resolveWorkspaceSourceRevision(workspaceRoot);
|
|
118
|
+
const buildVersion =
|
|
119
|
+
generationBuildMarker && unitId
|
|
120
|
+
? resolveUltramodernReleaseIdentity({
|
|
121
|
+
generationBuildMarker,
|
|
122
|
+
unitId,
|
|
123
|
+
workspaceRoot,
|
|
124
|
+
}).buildMarker
|
|
125
|
+
: undefined;
|
|
126
|
+
const packageName =
|
|
127
|
+
compactDeliveryUnit?.packageName ??
|
|
128
|
+
buildIdentity.packageName ??
|
|
129
|
+
app.packageName;
|
|
130
|
+
const version = compactDeliveryUnit?.version ?? buildIdentity.version;
|
|
131
|
+
const deliveryUnit = createStampedDeliveryUnit({
|
|
132
|
+
appId: app.id,
|
|
133
|
+
unitId,
|
|
134
|
+
buildMarker: buildVersion,
|
|
135
|
+
packageName,
|
|
136
|
+
version,
|
|
137
|
+
sourceRevision,
|
|
138
|
+
});
|
|
139
|
+
const stampedBuildArtifact = buildIdentity.artifact
|
|
140
|
+
? stampUltramodernBuildArtifactIdentity(buildIdentity.artifact, {
|
|
141
|
+
buildMarker:
|
|
142
|
+
buildVersion ?? buildIdentity.artifact.deliveryUnit.buildMarker,
|
|
143
|
+
sourceRevision,
|
|
144
|
+
})
|
|
145
|
+
: undefined;
|
|
146
|
+
|
|
147
|
+
const resolvedApp: BackendFederationApp = {
|
|
148
|
+
...app,
|
|
149
|
+
packageName,
|
|
150
|
+
version,
|
|
151
|
+
buildVersion,
|
|
152
|
+
unitId,
|
|
153
|
+
sourceRevision,
|
|
154
|
+
deliveryUnit,
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const manifestPath = path.join(distDirectory, BACKEND_MANIFEST_FILE);
|
|
158
|
+
const entryPath = path.join(distDirectory, BACKEND_REMOTE_ENTRY_FILE);
|
|
159
|
+
const deliveryUnitArtifactPath = stampedBuildArtifact
|
|
160
|
+
? path.join(distDirectory, ULTRAMODERN_BUILD_ARTIFACT_FILE)
|
|
161
|
+
: undefined;
|
|
162
|
+
const entrySource = await createBackendRemoteEntrySource(
|
|
163
|
+
workspaceRoot,
|
|
164
|
+
resolvedApp,
|
|
165
|
+
effectApiPath,
|
|
166
|
+
entryPath,
|
|
167
|
+
);
|
|
168
|
+
await fs.mkdir(distDirectory, { recursive: true });
|
|
169
|
+
await fs.writeFile(entryPath, entrySource);
|
|
170
|
+
await fs.writeFile(
|
|
171
|
+
manifestPath,
|
|
172
|
+
`${JSON.stringify(createBackendManifest(workspaceRoot, distDirectory, resolvedApp, createBackendFederationEntryIntegrity(entrySource)), null, 2)}\n`,
|
|
173
|
+
);
|
|
174
|
+
if (stampedBuildArtifact && deliveryUnitArtifactPath) {
|
|
175
|
+
await fs.writeFile(
|
|
176
|
+
deliveryUnitArtifactPath,
|
|
177
|
+
`${JSON.stringify(stampedBuildArtifact, null, 2)}\n`,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
appId: resolvedApp.id,
|
|
183
|
+
manifestPath,
|
|
184
|
+
containerPath: entryPath,
|
|
185
|
+
...(deliveryUnitArtifactPath ? { deliveryUnitArtifactPath } : {}),
|
|
186
|
+
remoteName: resolvedApp.backendName,
|
|
187
|
+
remoteType: resolvedApp.remoteType,
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export default () => ({
|
|
192
|
+
name: '@modern-js/backend-federation-build',
|
|
193
|
+
setup(api: {
|
|
194
|
+
getAppContext(): { appDirectory: string; distDirectory: string };
|
|
195
|
+
onAfterBuild(handler: () => Promise<void>): void;
|
|
196
|
+
}) {
|
|
197
|
+
api.onAfterBuild(async () => {
|
|
198
|
+
const { appDirectory, distDirectory } = api.getAppContext();
|
|
199
|
+
await emitBackendFederationArtifacts(appDirectory, distDirectory);
|
|
200
|
+
});
|
|
201
|
+
},
|
|
202
|
+
});
|
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import {
|
|
4
|
+
accessSync,
|
|
5
|
+
chmodSync,
|
|
6
|
+
constants,
|
|
7
|
+
lstatSync,
|
|
8
|
+
mkdirSync,
|
|
9
|
+
readFileSync,
|
|
10
|
+
realpathSync,
|
|
11
|
+
renameSync,
|
|
12
|
+
rmSync,
|
|
13
|
+
writeFileSync,
|
|
14
|
+
} from 'node:fs';
|
|
15
|
+
import { createRequire } from 'node:module';
|
|
16
|
+
import { tmpdir } from 'node:os';
|
|
17
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
18
|
+
import type { URL } from 'node:url';
|
|
19
|
+
import type { Rspack } from '@rsbuild/core';
|
|
20
|
+
|
|
21
|
+
const EFFECT_TSGO_PACKAGE = '@effect/tsgo';
|
|
22
|
+
const EFFECT_TSGO_BIN = 'effect-tsgo';
|
|
23
|
+
const EFFECT_TSGO_RESOLUTION_ERROR =
|
|
24
|
+
'Unable to resolve the Effect TS-Go compiler. Install "@effect/tsgo" and a native TypeScript backend for this build config, or set EFFECT_TSGO_BIN.';
|
|
25
|
+
const executableEffectTsgoCompilers = new Map<string, string>();
|
|
26
|
+
const BUILD_CONFIG_ENVIRONMENT_PLUGIN =
|
|
27
|
+
'ModernJsBuildConfigEnvironmentLeasePlugin';
|
|
28
|
+
const ENVIRONMENT_LEASE_REGISTRY_KEY = Symbol.for(
|
|
29
|
+
'@modern-js/app-tools/build-config-environment-lease-registry/v1',
|
|
30
|
+
);
|
|
31
|
+
const ENVIRONMENT_LEASE_REGISTRY_BRAND = Symbol.for(
|
|
32
|
+
'@modern-js/app-tools/build-config-environment-lease-registry-brand/v1',
|
|
33
|
+
);
|
|
34
|
+
const LIFECYCLE_HOOK_NAMES = [
|
|
35
|
+
'run',
|
|
36
|
+
'watchRun',
|
|
37
|
+
'afterDone',
|
|
38
|
+
'failed',
|
|
39
|
+
'shutdown',
|
|
40
|
+
'watchClose',
|
|
41
|
+
] as const;
|
|
42
|
+
|
|
43
|
+
type PackageJson = {
|
|
44
|
+
bin?: Record<string, string> | string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type EnvironmentLease = {
|
|
48
|
+
originalValue: string | undefined;
|
|
49
|
+
owners: Set<symbol>;
|
|
50
|
+
value: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
type EnvironmentLeaseRegistry = Readonly<{
|
|
54
|
+
acquire: (name: string, value: string) => () => void;
|
|
55
|
+
brand: symbol;
|
|
56
|
+
}>;
|
|
57
|
+
|
|
58
|
+
type LifecycleHookName = (typeof LIFECYCLE_HOOK_NAMES)[number];
|
|
59
|
+
|
|
60
|
+
type LifecycleHook = {
|
|
61
|
+
tap: (options: { name: string; stage: number }, handler: () => void) => void;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function restoreEnvironment(name: string, value: string | undefined): void {
|
|
65
|
+
if (value === undefined) {
|
|
66
|
+
delete process.env[name];
|
|
67
|
+
} else {
|
|
68
|
+
process.env[name] = value;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function createEnvironmentLeaseRegistry(): EnvironmentLeaseRegistry {
|
|
73
|
+
const environmentLeases = new Map<string, EnvironmentLease>();
|
|
74
|
+
|
|
75
|
+
return Object.freeze({
|
|
76
|
+
acquire(name: string, value: string): () => void {
|
|
77
|
+
let lease = environmentLeases.get(name);
|
|
78
|
+
|
|
79
|
+
if (lease) {
|
|
80
|
+
if (lease.value !== value) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Build config environment "${name}" already has an active lease for a different value.`,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
if (process.env[name] !== lease.value) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`Build config environment lease for "${name}" lost ownership before another lease was acquired.`,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
lease = {
|
|
92
|
+
originalValue: process.env[name],
|
|
93
|
+
owners: new Set(),
|
|
94
|
+
value,
|
|
95
|
+
};
|
|
96
|
+
process.env[name] = value;
|
|
97
|
+
environmentLeases.set(name, lease);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const owner = Symbol(name);
|
|
101
|
+
lease.owners.add(owner);
|
|
102
|
+
let released = false;
|
|
103
|
+
|
|
104
|
+
return () => {
|
|
105
|
+
if (released) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (environmentLeases.get(name) !== lease || !lease.owners.has(owner)) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`Build config environment lease for "${name}" cannot be restored because its ownership record was lost.`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
released = true;
|
|
116
|
+
const lostOwnership = process.env[name] !== lease.value;
|
|
117
|
+
lease.owners.delete(owner);
|
|
118
|
+
|
|
119
|
+
if (lease.owners.size === 0) {
|
|
120
|
+
environmentLeases.delete(name);
|
|
121
|
+
restoreEnvironment(name, lease.originalValue);
|
|
122
|
+
} else if (lostOwnership) {
|
|
123
|
+
process.env[name] = lease.value;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (lostOwnership) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
`Build config environment lease for "${name}" lost ownership before restoration.`,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
brand: ENVIRONMENT_LEASE_REGISTRY_BRAND,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function getEnvironmentLeaseRegistry(): EnvironmentLeaseRegistry {
|
|
138
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
139
|
+
process,
|
|
140
|
+
ENVIRONMENT_LEASE_REGISTRY_KEY,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
if (descriptor) {
|
|
144
|
+
const registry = descriptor.value as
|
|
145
|
+
| Partial<EnvironmentLeaseRegistry>
|
|
146
|
+
| undefined;
|
|
147
|
+
if (
|
|
148
|
+
descriptor.configurable ||
|
|
149
|
+
descriptor.enumerable ||
|
|
150
|
+
descriptor.writable ||
|
|
151
|
+
registry?.brand !== ENVIRONMENT_LEASE_REGISTRY_BRAND ||
|
|
152
|
+
typeof registry.acquire !== 'function'
|
|
153
|
+
) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
'The process-global build config environment lease registry is occupied by an incompatible value.',
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
return registry as EnvironmentLeaseRegistry;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const registry = createEnvironmentLeaseRegistry();
|
|
162
|
+
Object.defineProperty(process, ENVIRONMENT_LEASE_REGISTRY_KEY, {
|
|
163
|
+
configurable: false,
|
|
164
|
+
enumerable: false,
|
|
165
|
+
value: registry,
|
|
166
|
+
writable: false,
|
|
167
|
+
});
|
|
168
|
+
return registry;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function acquireEnvironmentLease(name: string, value: string): () => void {
|
|
172
|
+
return getEnvironmentLeaseRegistry().acquire(name, value);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function releaseAfterFailure(
|
|
176
|
+
release: () => void,
|
|
177
|
+
originalError: unknown,
|
|
178
|
+
): never {
|
|
179
|
+
try {
|
|
180
|
+
release();
|
|
181
|
+
} catch (restorationError) {
|
|
182
|
+
throw new AggregateError(
|
|
183
|
+
[originalError, restorationError],
|
|
184
|
+
'Build config environment setup and restoration both failed.',
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
throw originalError;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
class BuildConfigEnvironmentLeasePlugin {
|
|
192
|
+
constructor(private readonly release: () => void) {}
|
|
193
|
+
|
|
194
|
+
apply(compiler: Rspack.Compiler): void {
|
|
195
|
+
const hooks = compiler.hooks as unknown as Record<
|
|
196
|
+
string,
|
|
197
|
+
LifecycleHook | undefined
|
|
198
|
+
>;
|
|
199
|
+
|
|
200
|
+
try {
|
|
201
|
+
const lifecycleHooks = {} as Record<LifecycleHookName, LifecycleHook>;
|
|
202
|
+
for (const hookName of LIFECYCLE_HOOK_NAMES) {
|
|
203
|
+
const hook = hooks[hookName];
|
|
204
|
+
if (!hook || typeof hook.tap !== 'function') {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`Rspack does not expose the "${hookName}" lifecycle hook required to restore build config environment leases.`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
lifecycleHooks[hookName] = hook;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
let mode: 'pending' | 'run' | 'watch' = 'pending';
|
|
213
|
+
const modeTap = {
|
|
214
|
+
name: BUILD_CONFIG_ENVIRONMENT_PLUGIN,
|
|
215
|
+
stage: Number.MIN_SAFE_INTEGER,
|
|
216
|
+
};
|
|
217
|
+
const releaseTap = {
|
|
218
|
+
name: BUILD_CONFIG_ENVIRONMENT_PLUGIN,
|
|
219
|
+
stage: Number.MAX_SAFE_INTEGER,
|
|
220
|
+
};
|
|
221
|
+
const releaseAfterOneShot = () => {
|
|
222
|
+
if (mode !== 'watch' && compiler.watchMode !== true) {
|
|
223
|
+
this.release();
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
lifecycleHooks.run.tap(modeTap, () => {
|
|
228
|
+
mode = 'run';
|
|
229
|
+
});
|
|
230
|
+
lifecycleHooks.watchRun.tap(modeTap, () => {
|
|
231
|
+
mode = 'watch';
|
|
232
|
+
});
|
|
233
|
+
lifecycleHooks.afterDone.tap(releaseTap, releaseAfterOneShot);
|
|
234
|
+
lifecycleHooks.failed.tap(releaseTap, releaseAfterOneShot);
|
|
235
|
+
lifecycleHooks.shutdown.tap(releaseTap, this.release);
|
|
236
|
+
lifecycleHooks.watchClose.tap(releaseTap, this.release);
|
|
237
|
+
} catch (error) {
|
|
238
|
+
releaseAfterFailure(this.release, error);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function resolveEffectTsgoCli(from: string | URL): string {
|
|
244
|
+
const projectRequire = createRequire(from);
|
|
245
|
+
const packageJsonPath = projectRequire.resolve(
|
|
246
|
+
`${EFFECT_TSGO_PACKAGE}/package.json`,
|
|
247
|
+
);
|
|
248
|
+
const packageJson = JSON.parse(
|
|
249
|
+
readFileSync(packageJsonPath, 'utf-8'),
|
|
250
|
+
) as PackageJson;
|
|
251
|
+
const bin =
|
|
252
|
+
typeof packageJson.bin === 'string'
|
|
253
|
+
? packageJson.bin
|
|
254
|
+
: packageJson.bin?.[EFFECT_TSGO_BIN];
|
|
255
|
+
|
|
256
|
+
if (!bin) {
|
|
257
|
+
throw new Error(EFFECT_TSGO_RESOLUTION_ERROR);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return resolve(dirname(packageJsonPath), bin);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function resolveExecutableEffectTsgoCompiler(compilerPath: string): string {
|
|
264
|
+
if (process.platform === 'win32') {
|
|
265
|
+
return compilerPath;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const sourcePath = realpathSync(compilerPath);
|
|
269
|
+
const source = lstatSync(sourcePath);
|
|
270
|
+
if (!source.isFile()) {
|
|
271
|
+
throw new Error(EFFECT_TSGO_RESOLUTION_ERROR);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
try {
|
|
275
|
+
accessSync(sourcePath, constants.X_OK);
|
|
276
|
+
return compilerPath;
|
|
277
|
+
} catch {
|
|
278
|
+
// npm currently publishes Effect TS-Go's native Unix files without an
|
|
279
|
+
// execute bit. Keep node_modules immutable and materialize a private copy.
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const cachedCompiler = executableEffectTsgoCompilers.get(sourcePath);
|
|
283
|
+
if (cachedCompiler) {
|
|
284
|
+
try {
|
|
285
|
+
accessSync(cachedCompiler, constants.X_OK);
|
|
286
|
+
return cachedCompiler;
|
|
287
|
+
} catch {
|
|
288
|
+
executableEffectTsgoCompilers.delete(sourcePath);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const compilerBytes = readFileSync(sourcePath);
|
|
293
|
+
const compilerDigest = createHash('sha256')
|
|
294
|
+
.update(compilerBytes)
|
|
295
|
+
.digest('hex');
|
|
296
|
+
const compilerDirectory = join(
|
|
297
|
+
tmpdir(),
|
|
298
|
+
'modern-js',
|
|
299
|
+
'effect-tsgo',
|
|
300
|
+
compilerDigest,
|
|
301
|
+
);
|
|
302
|
+
mkdirSync(compilerDirectory, { recursive: true, mode: 0o700 });
|
|
303
|
+
const executableCompiler = join(compilerDirectory, basename(sourcePath));
|
|
304
|
+
try {
|
|
305
|
+
accessSync(executableCompiler, constants.X_OK);
|
|
306
|
+
} catch {
|
|
307
|
+
const temporaryCompiler = join(
|
|
308
|
+
compilerDirectory,
|
|
309
|
+
`.${basename(sourcePath)}.${process.pid}.tmp`,
|
|
310
|
+
);
|
|
311
|
+
rmSync(temporaryCompiler, { force: true });
|
|
312
|
+
try {
|
|
313
|
+
writeFileSync(temporaryCompiler, compilerBytes, {
|
|
314
|
+
flag: 'wx',
|
|
315
|
+
mode: 0o700,
|
|
316
|
+
});
|
|
317
|
+
chmodSync(temporaryCompiler, 0o700);
|
|
318
|
+
renameSync(temporaryCompiler, executableCompiler);
|
|
319
|
+
} finally {
|
|
320
|
+
rmSync(temporaryCompiler, { force: true });
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
accessSync(executableCompiler, constants.X_OK);
|
|
324
|
+
executableEffectTsgoCompilers.set(sourcePath, executableCompiler);
|
|
325
|
+
return executableCompiler;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Reads an environment variable while keeping generated build configs free of
|
|
330
|
+
* direct process access.
|
|
331
|
+
*/
|
|
332
|
+
export function getBuildConfigEnvironment(name: string): string | undefined {
|
|
333
|
+
return process.env[name];
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Runs a Rspack config setup under a leased environment value. The lease is
|
|
338
|
+
* restored when that compiler reaches any supported terminal hook.
|
|
339
|
+
*/
|
|
340
|
+
export function withBuildConfigEnvironment<
|
|
341
|
+
Config extends { plugins?: Rspack.Plugin[] },
|
|
342
|
+
SetupArguments extends unknown[],
|
|
343
|
+
>(
|
|
344
|
+
name: string,
|
|
345
|
+
value: string,
|
|
346
|
+
setup: (
|
|
347
|
+
config: Config,
|
|
348
|
+
...args: SetupArguments
|
|
349
|
+
) => Config | void | Promise<Config | void>,
|
|
350
|
+
): (config: Config, ...args: SetupArguments) => Promise<Config> {
|
|
351
|
+
return async (config, ...args) => {
|
|
352
|
+
const release = acquireEnvironmentLease(name, value);
|
|
353
|
+
|
|
354
|
+
try {
|
|
355
|
+
const configured = (await setup(config, ...args)) ?? config;
|
|
356
|
+
configured.plugins = [
|
|
357
|
+
...(configured.plugins ?? []),
|
|
358
|
+
new BuildConfigEnvironmentLeasePlugin(release),
|
|
359
|
+
];
|
|
360
|
+
return configured;
|
|
361
|
+
} catch (error) {
|
|
362
|
+
releaseAfterFailure(release, error);
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export type ResolveEffectTsgoCompilerOptions = {
|
|
368
|
+
/** Module URL or absolute filename used to resolve the consumer's compiler. */
|
|
369
|
+
from: string | URL;
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Resolves the Effect TS-Go executable used by Module Federation DTS builds.
|
|
374
|
+
*/
|
|
375
|
+
export function resolveEffectTsgoCompiler(
|
|
376
|
+
options: ResolveEffectTsgoCompilerOptions,
|
|
377
|
+
): string {
|
|
378
|
+
const configuredCompiler =
|
|
379
|
+
getBuildConfigEnvironment('EFFECT_TSGO_BIN')?.trim();
|
|
380
|
+
|
|
381
|
+
if (configuredCompiler) {
|
|
382
|
+
try {
|
|
383
|
+
return resolveExecutableEffectTsgoCompiler(configuredCompiler);
|
|
384
|
+
} catch {
|
|
385
|
+
throw new Error(EFFECT_TSGO_RESOLUTION_ERROR);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
try {
|
|
390
|
+
const compiler = execFileSync(
|
|
391
|
+
process.execPath,
|
|
392
|
+
[resolveEffectTsgoCli(options.from), 'get-exe-path'],
|
|
393
|
+
{ encoding: 'utf-8' },
|
|
394
|
+
).trim();
|
|
395
|
+
|
|
396
|
+
if (compiler) {
|
|
397
|
+
return resolveExecutableEffectTsgoCompiler(compiler);
|
|
398
|
+
}
|
|
399
|
+
} catch {
|
|
400
|
+
// Use one stable error for package, platform-binary, and CLI failures.
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
throw new Error(EFFECT_TSGO_RESOLUTION_ERROR);
|
|
404
|
+
}
|