@bleedingdev/modern-js-server-runtime-extensions 3.11.3-ultramodern.10 → 3.11.3-ultramodern.12
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 +10 -0
- package/dist/cjs/env.js +5 -1
- package/dist/cjs/module-federation-css/plugin.js +7 -3
- package/dist/cjs/render-resources/channels.js +55 -0
- package/dist/cjs/render-resources/index.js +47 -0
- package/dist/cjs/render-resources/plugin.js +82 -0
- package/dist/cjs/serverPlugin.js +2 -0
- package/dist/cjs/static-serving/immutableAssetStat.js +64 -0
- package/dist/cjs/static-serving/plugin.js +2 -1
- package/dist/cjs/static-serving/responders.js +13 -8
- package/dist/cjs/static-serving/staticPrecompressed.js +6 -3
- package/dist/esm/env.mjs +5 -1
- package/dist/esm/module-federation-css/plugin.mjs +7 -3
- package/dist/esm/serverPlugin.mjs +2 -0
- package/dist/esm-node/env.mjs +5 -1
- package/dist/esm-node/module-federation-css/plugin.mjs +7 -3
- package/dist/esm-node/render-resources/channels.mjs +5 -0
- package/dist/esm-node/render-resources/index.mjs +3 -0
- package/dist/esm-node/render-resources/plugin.mjs +43 -0
- package/dist/esm-node/serverPlugin.mjs +2 -0
- package/dist/esm-node/static-serving/immutableAssetStat.mjs +24 -0
- package/dist/esm-node/static-serving/plugin.mjs +2 -1
- package/dist/esm-node/static-serving/responders.mjs +13 -8
- package/dist/esm-node/static-serving/staticPrecompressed.mjs +6 -3
- package/dist/types/env.d.ts +15 -0
- package/dist/types/module-federation-css/plugin.d.ts +3 -2
- package/dist/types/render-resources/channels.d.ts +27 -0
- package/dist/types/render-resources/index.d.ts +2 -0
- package/dist/types/render-resources/plugin.d.ts +3 -0
- package/dist/types/static-serving/immutableAssetStat.d.ts +8 -0
- package/dist/types/static-serving/responders.d.ts +7 -1
- package/dist/types/static-serving/staticPrecompressed.d.ts +1 -1
- package/dist/types-esm/env.d.mts +15 -0
- package/dist/types-esm/module-federation-css/plugin.d.mts +3 -2
- package/dist/types-esm/render-resources/channels.d.mts +27 -0
- package/dist/types-esm/render-resources/index.d.mts +2 -0
- package/dist/types-esm/render-resources/plugin.d.mts +3 -0
- package/dist/types-esm/static-serving/immutableAssetStat.d.mts +8 -0
- package/dist/types-esm/static-serving/responders.d.mts +7 -1
- package/dist/types-esm/static-serving/staticPrecompressed.d.mts +1 -1
- package/package.json +27 -10
- package/src/env.ts +25 -0
- package/src/module-federation-css/plugin.ts +21 -5
- package/src/render-resources/channels.ts +37 -0
- package/src/render-resources/index.ts +7 -0
- package/src/render-resources/plugin.ts +56 -0
- package/src/serverPlugin.ts +2 -0
- package/src/static-serving/immutableAssetStat.ts +41 -0
- package/src/static-serving/plugin.ts +4 -1
- package/src/static-serving/responders.ts +30 -6
- package/src/static-serving/staticPrecompressed.ts +5 -4
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ServerPlugin } from '@modern-js/server-core';
|
|
2
|
+
import { parseServerRuntimeExtensionsEnv } from '../env';
|
|
2
3
|
import { createStaticExtensionResponders } from './responders';
|
|
3
4
|
|
|
4
5
|
export function injectStaticServingPlugin(): ServerPlugin {
|
|
@@ -7,7 +8,9 @@ export function injectStaticServingPlugin(): ServerPlugin {
|
|
|
7
8
|
post: ['@modern-js/plugin-server-static'],
|
|
8
9
|
setup(api) {
|
|
9
10
|
api.updateServerContext({
|
|
10
|
-
staticAssetResponders: createStaticExtensionResponders(
|
|
11
|
+
staticAssetResponders: createStaticExtensionResponders(
|
|
12
|
+
parseServerRuntimeExtensionsEnv(),
|
|
13
|
+
),
|
|
11
14
|
});
|
|
12
15
|
},
|
|
13
16
|
};
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
type StaticAsset,
|
|
7
7
|
serveStaticAsset,
|
|
8
8
|
} from '@modern-js/server-core/node';
|
|
9
|
+
import { createImmutableAssetStat } from './immutableAssetStat';
|
|
9
10
|
import {
|
|
10
11
|
applyModuleFederationAssetHeaders,
|
|
11
12
|
getModuleFederationAssetList,
|
|
@@ -23,12 +24,24 @@ import {
|
|
|
23
24
|
|
|
24
25
|
type Context = Parameters<Middleware>[0];
|
|
25
26
|
|
|
27
|
+
export type StaticExtensionPolicy = {
|
|
28
|
+
/** Release files never change while serving: keep found files and the federation inventory. */
|
|
29
|
+
immutableAssets: boolean;
|
|
30
|
+
/** Probe and serve `.br`/`.gz` variants. */
|
|
31
|
+
precompressedAssets: boolean;
|
|
32
|
+
};
|
|
33
|
+
|
|
26
34
|
async function respondPrecompressedAsset(
|
|
27
35
|
context: Context,
|
|
28
36
|
asset: StaticAsset,
|
|
29
37
|
serve: ServeStaticAsset,
|
|
38
|
+
precompressedAssets: boolean,
|
|
30
39
|
) {
|
|
31
|
-
const selected = await resolvePreCompressedAsset(
|
|
40
|
+
const selected = await resolvePreCompressedAsset(
|
|
41
|
+
context,
|
|
42
|
+
asset.filename,
|
|
43
|
+
precompressedAssets,
|
|
44
|
+
);
|
|
32
45
|
const response = selected.acceptable
|
|
33
46
|
? await serve({
|
|
34
47
|
filename: selected.selected?.filepath,
|
|
@@ -48,7 +61,16 @@ async function respondPrecompressedAsset(
|
|
|
48
61
|
|
|
49
62
|
// Native core owns readers, containment and response conversion.
|
|
50
63
|
// This extension owns federation and compressed/generated asset selection.
|
|
51
|
-
export function createStaticExtensionResponders(
|
|
64
|
+
export function createStaticExtensionResponders({
|
|
65
|
+
immutableAssets,
|
|
66
|
+
precompressedAssets,
|
|
67
|
+
}: StaticExtensionPolicy): ServerStaticPluginOptions {
|
|
68
|
+
const stat = immutableAssets ? createImmutableAssetStat() : undefined;
|
|
69
|
+
const respondPrecompressed = (
|
|
70
|
+
context: Context,
|
|
71
|
+
asset: StaticAsset,
|
|
72
|
+
serve: ServeStaticAsset,
|
|
73
|
+
) => respondPrecompressedAsset(context, asset, serve, precompressedAssets);
|
|
52
74
|
const caches = new Map<
|
|
53
75
|
string,
|
|
54
76
|
{
|
|
@@ -73,7 +95,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
73
95
|
const pending = getModuleFederationAssetList(root)
|
|
74
96
|
.then(assets => {
|
|
75
97
|
current.assets = assets;
|
|
76
|
-
current.expires = Date.now() + 1_000;
|
|
98
|
+
current.expires = immutableAssets ? Infinity : Date.now() + 1_000;
|
|
77
99
|
return assets;
|
|
78
100
|
})
|
|
79
101
|
.finally(() => {
|
|
@@ -96,7 +118,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
96
118
|
isModuleFederationManifestRequest(requestPath) &&
|
|
97
119
|
!isBackendModuleFederationManifestRequest(requestPath);
|
|
98
120
|
if (!patchManifest && !remoteEntry) {
|
|
99
|
-
return
|
|
121
|
+
return respondPrecompressed(context, asset, serve);
|
|
100
122
|
}
|
|
101
123
|
const response = await serve({ contentLength: true, revalidate: false });
|
|
102
124
|
if (!response) {
|
|
@@ -141,7 +163,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
141
163
|
root: publicRoot,
|
|
142
164
|
realpath: true,
|
|
143
165
|
},
|
|
144
|
-
(asset, serve) =>
|
|
166
|
+
(asset, serve) => respondPrecompressed(context, asset, serve),
|
|
145
167
|
);
|
|
146
168
|
if (response && context.req.raw.method.toUpperCase() === 'HEAD') {
|
|
147
169
|
return new Response(null, {
|
|
@@ -153,6 +175,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
153
175
|
}
|
|
154
176
|
|
|
155
177
|
return {
|
|
178
|
+
stat,
|
|
156
179
|
async respondAsset(context, asset, serve, { root, pathPrefix }) {
|
|
157
180
|
const requestPath = getModuleFederationRequestPath(
|
|
158
181
|
context.req.path,
|
|
@@ -172,7 +195,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
172
195
|
assets.remoteEntries.has(requestPath),
|
|
173
196
|
);
|
|
174
197
|
}
|
|
175
|
-
return
|
|
198
|
+
return respondPrecompressed(context, asset, serve);
|
|
176
199
|
},
|
|
177
200
|
async respondPublicFallback(context, respondPublic, { root, pathPrefix }) {
|
|
178
201
|
const requestPath = getModuleFederationRequestPath(
|
|
@@ -190,6 +213,7 @@ export function createStaticExtensionResponders(): ServerStaticPluginOptions {
|
|
|
190
213
|
filename: path.join(root, requestPath),
|
|
191
214
|
kind: 'static',
|
|
192
215
|
root,
|
|
216
|
+
stat,
|
|
193
217
|
},
|
|
194
218
|
(asset, serve) =>
|
|
195
219
|
serveFederated(
|
|
@@ -126,14 +126,15 @@ const appendVaryHeader = (
|
|
|
126
126
|
export const resolvePreCompressedAsset = async (
|
|
127
127
|
c: Parameters<Middleware>[0],
|
|
128
128
|
filepath: string,
|
|
129
|
+
probeVariants: boolean,
|
|
129
130
|
): Promise<ResolvePreCompressedAssetResult> => {
|
|
130
131
|
const brPath = `${filepath}${PRE_COMPRESSED_ASSET_EXTENSIONS.br}`;
|
|
131
132
|
const gzipPath = `${filepath}${PRE_COMPRESSED_ASSET_EXTENSIONS.gzip}`;
|
|
132
133
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
fs.pathExists(gzipPath)
|
|
136
|
-
|
|
134
|
+
// Without variants only identity negotiation remains, including its 406.
|
|
135
|
+
const [hasBr, hasGzip] = probeVariants
|
|
136
|
+
? await Promise.all([fs.pathExists(brPath), fs.pathExists(gzipPath)])
|
|
137
|
+
: [false, false];
|
|
137
138
|
|
|
138
139
|
const hasVariant = hasBr || hasGzip;
|
|
139
140
|
const acceptedRepresentations = getAcceptedRepresentations(
|