Skip to content

Commit ade0661

Browse files
committed
refactor: change middleware interface
1 parent 11b69c0 commit ade0661

File tree

13 files changed

+594
-75
lines changed

13 files changed

+594
-75
lines changed

packages/rspack-test-tools/src/plugin/lazy-compilation-test-plugin.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createServer } from "node:http";
22
import type { Socket } from "node:net";
33
import type { AddressInfo } from "node:net";
4-
import type { Compiler, MultiCompiler } from "@rspack/core";
4+
import { type Compiler, MultiCompiler } from "@rspack/core";
55
import { experiments } from "@rspack/core";
66

77
export class LazyCompilationTestPlugin {
@@ -22,11 +22,16 @@ export class LazyCompilationTestPlugin {
2222
: addr.family === "IPv6"
2323
? `${protocol}://[${addr.address}]:${addr.port}`
2424
: `${protocol}://${addr.address}:${addr.port}`;
25-
middleware = experiments.lazyCompilationMiddleware(compiler, {
26-
// @ts-expect-error cacheable is hidden config only for tests
27-
cacheable: false,
28-
serverUrl: urlBase
29-
});
25+
if (compiler instanceof MultiCompiler) {
26+
for (const c of compiler.compilers) {
27+
if (c.options.experiments.lazyCompilation) {
28+
c.options.experiments.lazyCompilation.serverUrl = urlBase;
29+
}
30+
}
31+
} else if (compiler.options.experiments.lazyCompilation) {
32+
compiler.options.experiments.lazyCompilation.serverUrl = urlBase;
33+
}
34+
middleware = experiments.lazyCompilationMiddleware(compiler);
3035

3136
resolve(null);
3237
});

packages/rspack/src/builtin-plugin/lazy-compilation/middleware.ts

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,8 @@ const getFullServerUrl = ({ serverUrl, prefix }: LazyCompilationOptions) => {
3737
};
3838

3939
export const lazyCompilationMiddleware = (
40-
compiler: Compiler | MultiCompiler,
41-
rawOptions?: LazyCompilationOptions | boolean
40+
compiler: Compiler | MultiCompiler
4241
): Middleware => {
43-
let userOptions = rawOptions;
44-
if (userOptions === false) {
45-
return noop;
46-
}
47-
48-
if (userOptions === true) {
49-
userOptions = {};
50-
}
51-
5242
if (compiler instanceof MultiCompiler) {
5343
const middlewareByCompiler: Map<string, Middleware> = new Map();
5444

@@ -59,8 +49,7 @@ export const lazyCompilationMiddleware = (
5949
}
6050

6151
const options = {
62-
...c.options.experiments.lazyCompilation,
63-
...userOptions
52+
...c.options.experiments.lazyCompilation
6453
};
6554

6655
const prefix = options.prefix || LAZY_COMPILATION_PREFIX;
@@ -78,36 +67,7 @@ export const lazyCompilationMiddleware = (
7867
)
7968
);
8069

81-
const compilerConfig = c.options.experiments.lazyCompilation
82-
? c.options.experiments.lazyCompilation
83-
: {};
84-
85-
const testConfig = compilerConfig.test ?? options.test;
86-
87-
const plugin = new BuiltinLazyCompilationPlugin(
88-
({ module, path }) => {
89-
const key = encodeURIComponent(
90-
module.replace(/\\/g, "/").replace(/@/g, "_")
91-
)
92-
// module identifier may contain query, bang(!) or split(|),
93-
// should do our best to ensure it's the same with which comes
94-
// from server url
95-
.replace(/%(2F|3A|24|26|2B|2C|3B|3D)/g, decodeURIComponent);
96-
filesByKey.set(key, path);
97-
const active = activeModules.get(key) === true;
98-
return {
99-
client: `${options.client || getDefaultClient(c)}?${encodeURIComponent(getFullServerUrl(options))}`,
100-
data: key,
101-
active
102-
};
103-
},
104-
// @ts-expect-error internal option
105-
compilerConfig.cacheable ?? options.cacheable ?? true,
106-
compilerConfig.entries ?? options.entries ?? true,
107-
compilerConfig.imports ?? options.imports ?? true,
108-
testConfig
109-
);
110-
plugin.apply(c);
70+
applyPlugin(c, options, activeModules, filesByKey);
11171
}
11272

11373
const keys = [...middlewareByCompiler.keys()];
@@ -123,17 +83,33 @@ export const lazyCompilationMiddleware = (
12383
};
12484
}
12585

126-
if (!compiler.options.experiments.lazyCompilation && !userOptions) {
86+
if (!compiler.options.experiments.lazyCompilation) {
12787
return noop;
12888
}
12989

13090
const activeModules: Map<string, boolean> = new Map();
13191
const filesByKey: Map<string, string> = new Map();
13292

13393
const options = {
134-
...compiler.options.experiments.lazyCompilation,
135-
...userOptions
94+
...compiler.options.experiments.lazyCompilation
13695
};
96+
applyPlugin(compiler, options, activeModules, filesByKey);
97+
98+
const lazyCompilationPrefix = options.prefix || LAZY_COMPILATION_PREFIX;
99+
return lazyCompilationMiddlewareInternal(
100+
compiler,
101+
activeModules,
102+
filesByKey,
103+
lazyCompilationPrefix
104+
);
105+
};
106+
107+
function applyPlugin(
108+
compiler: Compiler,
109+
options: LazyCompilationOptions,
110+
activeModules: Map<string, boolean>,
111+
filesByKey: Map<string, string>
112+
) {
137113
const plugin = new BuiltinLazyCompilationPlugin(
138114
({ module, path }) => {
139115
const key = encodeURIComponent(
@@ -158,15 +134,7 @@ export const lazyCompilationMiddleware = (
158134
options.test
159135
);
160136
plugin.apply(compiler);
161-
162-
const lazyCompilationPrefix = options.prefix || LAZY_COMPILATION_PREFIX;
163-
return lazyCompilationMiddlewareInternal(
164-
compiler,
165-
activeModules,
166-
filesByKey,
167-
lazyCompilationPrefix
168-
);
169-
};
137+
}
170138

171139
// used for reuse code, do not export this
172140
const lazyCompilationMiddlewareInternal = (

tests/e2e/fixtures/rspack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Rspack {
3636
});
3737
const DevServerConstructor = RspackDevServer;
3838
if (compiler.options.experiments.lazyCompilation) {
39-
const middleware = experiments.lazyCompilationMiddleware(compiler, compiler.options.experiments.lazyCompilation)
39+
const middleware = experiments.lazyCompilationMiddleware(compiler)
4040
compiler.options.devServer ??= {};
4141
const setupMiddlewares = compiler.options.devServer.setupMiddlewares;
4242
compiler.options.devServer.setupMiddlewares = (middlewares, server) => {
@@ -84,7 +84,7 @@ class Rspack {
8484
const DevServerConstructor = RspackDevServer;
8585

8686
if (compiler.options.experiments.lazyCompilation) {
87-
const middleware = experiments.lazyCompilationMiddleware(compiler, compiler.options.experiments.lazyCompilation)
87+
const middleware = experiments.lazyCompilationMiddleware(compiler)
8888
compiler.options.devServer ??= {};
8989
const setupMiddleware = compiler.options.devServer.setupMiddlewares;
9090
compiler.options.devServer.setupMiddlewares = (middlewares, server) => {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"version": 1,
3+
"entries": {
4+
"https://cdn.esm.sh/[email protected]/es2022/p-map.mjs": {
5+
"resolved": "https://cdn.esm.sh/[email protected]/es2022/p-map.mjs",
6+
"integrity": "sha512-JZcUXZw5m11qMdvycDR0GkYCYxu8YAXHJW8qboAE4izGQUo0uFP3PeRDKVod3Nuk7BnX5LU1wRf2dXbRARZWpw==",
7+
"content_type": "application/javascript; charset=utf-8",
8+
"valid_until": 1779129816845,
9+
"etag": null
10+
},
11+
"https://cdn.skypack.dev/p-map": {
12+
"resolved": "https://cdn.skypack.dev/p-map",
13+
"integrity": "sha512-atjOl8e1pjhZTgxbda654yU0fB52OMxSgzpJ6oULQdSwzjOCWYElYD12vVJwvmAElZv4a4iTUV0KrwPLubFGQQ==",
14+
"content_type": "application/javascript; charset=utf-8",
15+
"valid_until": 1747594110959,
16+
"etag": "W/\"2f5-1pwG0XVgGJM8ZDuV+nzlayuwum8\""
17+
},
18+
"https://jspm.dev/p-map": {
19+
"resolved": "https://jspm.dev/p-map",
20+
"integrity": "sha512-oPi7OOl5e/mTsH7fRHbeoZmwJFlbGJmbVpN4Yo3iCoB6sbmg7+ifJGo4UV8ushlmRrrnDM9p6FMAhJ7YoeXUjw==",
21+
"content_type": "text/javascript; charset=utf-8",
22+
"valid_until": 1747594010978,
23+
"etag": "\"7d4e22f2cfb93098b6ae55ba300000ca\""
24+
},
25+
"https://cdn.skypack.dev/-/[email protected]/dist=es2019,mode=imports/optimized/p-map.js": {
26+
"resolved": "https://cdn.skypack.dev/-/[email protected]/dist=es2019,mode=imports/optimized/p-map.js",
27+
"integrity": "sha512-R6J00yIUh1ODEflzZ1k4cq7/Mv9p8h36UiRK+J7JQ0OkHE413AvEcAx/kkBaPj2WE2rk0tqSpyoBzyn5tPJbNw==",
28+
"content_type": "application/javascript; charset=utf-8",
29+
"valid_until": 1779129812543,
30+
"etag": "W/\"1857-442Jr2b8/nGzSJdnERCmZdhJN1Y\""
31+
},
32+
"https://jspm.dev/npm:[email protected]": {
33+
"resolved": "https://jspm.dev/npm:[email protected]",
34+
"integrity": "sha512-CBSr0VoXgJsHL7wTGi2dNUsttxdYbk1H723kltM4laDRH/Gr4ld6CLQs+lQzwgsXPm0kL+fnee9M3AtGwvvxSg==",
35+
"content_type": "text/javascript; charset=utf-8",
36+
"valid_until": 1779129812006,
37+
"etag": "\"1af45e3335faf08ab334464f962696ce\""
38+
},
39+
"https://unpkg.com/[email protected]/index.js?module": {
40+
"resolved": "https://unpkg.com/[email protected]/index.js?module",
41+
"integrity": "sha512-UnMpRjhEJJiuAj9ljbVZ70LztGexa1Blm3Paqmrn9mMkCjXdzeTs1QJ/H1yCQfwn24qFG06dMPyyrCCYAv1yGA==",
42+
"content_type": "text/javascript; charset=utf-8",
43+
"valid_until": 1779129811457,
44+
"etag": null
45+
},
46+
"https://cdn.esm.sh/p-map": {
47+
"resolved": "https://cdn.esm.sh/p-map",
48+
"integrity": "sha512-lXS3/nN/n3UwoR6dfb4hFDPw3WhK4EznvlOV+7Ru882YpQedz8/EhahEwgnyhpK6jupTe/ttCyNJ1rEr0RernQ==",
49+
"content_type": "application/javascript; charset=utf-8",
50+
"valid_until": 1747597410969,
51+
"etag": null
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* esm.sh - [email protected] */
2+
async function $(t,c,{concurrency:e=Number.POSITIVE_INFINITY,stopOnError:r=!0,signal:f}={}){return new Promise((n,w)=>{if(t[Symbol.iterator]===void 0&&t[Symbol.asyncIterator]===void 0)throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof t})`);if(typeof c!="function")throw new TypeError("Mapper function is required");if(!(Number.isSafeInteger(e)&&e>=1||e===Number.POSITIVE_INFINITY))throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${e}\` (${typeof e})`);let l=[],m=[],s=new Map,u=!1,o=!1,I=!1,i=0,b=0,g=t[Symbol.iterator]===void 0?t[Symbol.asyncIterator]():t[Symbol.iterator](),E=()=>{y(f.reason)},N=()=>{f?.removeEventListener("abort",E)},T=a=>{n(a),N()},y=a=>{u=!0,o=!0,w(a),N()};f&&(f.aborted&&y(f.reason),f.addEventListener("abort",E,{once:!0}));let x=async()=>{if(o)return;let a=await g.next(),h=b;if(b++,a.done){if(I=!0,i===0&&!o){if(!r&&m.length>0){y(new AggregateError(m));return}if(o=!0,s.size===0){T(l);return}let p=[];for(let[d,v]of l.entries())s.get(d)!==S&&p.push(v);T(p)}return}i++,(async()=>{try{let p=await a.value;if(o)return;let d=await c(p,h);d===S&&s.set(h,d),l[h]=d,i--,await x()}catch(p){if(r)y(p);else{m.push(p),i--;try{await x()}catch(d){y(d)}}}})()};(async()=>{for(let a=0;a<e;a++){try{await x()}catch(h){y(h);break}if(I||u)break}})()})}function M(t,c,{concurrency:e=Number.POSITIVE_INFINITY,backpressure:r=e}={}){if(t[Symbol.iterator]===void 0&&t[Symbol.asyncIterator]===void 0)throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof t})`);if(typeof c!="function")throw new TypeError("Mapper function is required");if(!(Number.isSafeInteger(e)&&e>=1||e===Number.POSITIVE_INFINITY))throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${e}\` (${typeof e})`);if(!(Number.isSafeInteger(r)&&r>=e||r===Number.POSITIVE_INFINITY))throw new TypeError(`Expected \`backpressure\` to be an integer from \`concurrency\` (${e}) and up or \`Infinity\`, got \`${r}\` (${typeof r})`);return{async*[Symbol.asyncIterator](){let f=t[Symbol.asyncIterator]===void 0?t[Symbol.iterator]():t[Symbol.asyncIterator](),n=[],w=0,l=!1,m=0;function s(){if(l||!(w<e&&n.length<r))return;let u=(async()=>{let{done:o,value:I}=await f.next();if(o)return{done:!0};w++,s();try{let i=await c(await I,m++);if(w--,i===S){let b=n.indexOf(u);b>0&&n.splice(b,1)}return s(),{done:!1,value:i}}catch(i){return l=!0,{error:i}}})();n.push(u)}for(s();n.length>0;){let{error:u,done:o,value:I}=await n[0];if(n.shift(),u)throw u;if(o)return;s(),I!==S&&(yield I)}}}}var S=Symbol("skip");export{$ as default,M as pMapIterable,S as pMapSkip};
3+
//# sourceMappingURL=p-map.mjs.map
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* esm.sh - [email protected] */
2+
export * from "/[email protected]/es2022/p-map.mjs";
3+
export { default } from "/[email protected]/es2022/p-map.mjs";

0 commit comments

Comments
 (0)