@modern-js/uni-builder 2.68.0 → 2.68.2

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.
@@ -128,12 +128,13 @@ async function parseConfig(uniBuilderConfig, options) {
128
128
  var _uniBuilderConfig_server_rsc;
129
129
  const enableRsc = (_uniBuilderConfig_server_rsc = (_uniBuilderConfig_server = uniBuilderConfig.server) === null || _uniBuilderConfig_server === void 0 ? void 0 : _uniBuilderConfig_server.rsc) !== null && _uniBuilderConfig_server_rsc !== void 0 ? _uniBuilderConfig_server_rsc : false;
130
130
  if (enableRsc) {
131
- const { rscClientRuntimePath, rscServerRuntimePath } = options;
131
+ const { rscClientRuntimePath, rscServerRuntimePath, internalDirectory } = options;
132
132
  rsbuildPlugins.push((0, import_rsbuild_rsc_plugin.rsbuildRscPlugin)({
133
133
  appDir: options.cwd,
134
134
  isRspack: true,
135
135
  rscClientRuntimePath,
136
- rscServerRuntimePath
136
+ rscServerRuntimePath,
137
+ internalDirectory
137
138
  }));
138
139
  }
139
140
  return {
@@ -142,7 +143,7 @@ async function parseConfig(uniBuilderConfig, options) {
142
143
  };
143
144
  }
144
145
  async function createRspackBuilder(options) {
145
- const { cwd = process.cwd(), config, rscClientRuntimePath, rscServerRuntimePath, ...rest } = options;
146
+ const { cwd = process.cwd(), config, ...rest } = options;
146
147
  const { rsbuildConfig, rsbuildPlugins } = await parseConfig(config, {
147
148
  ...rest,
148
149
  cwd
@@ -1,7 +1,8 @@
1
1
  import type { RsbuildPlugin } from '@rsbuild/core';
2
- export declare const rsbuildRscPlugin: ({ appDir, isRspack, rscClientRuntimePath, rscServerRuntimePath, }: {
2
+ export declare const rsbuildRscPlugin: ({ appDir, isRspack, rscClientRuntimePath, rscServerRuntimePath, internalDirectory, }: {
3
3
  appDir: string;
4
4
  isRspack?: boolean;
5
5
  rscClientRuntimePath?: string;
6
6
  rscServerRuntimePath?: string;
7
+ internalDirectory?: string;
7
8
  }) => RsbuildPlugin;
@@ -67,7 +67,7 @@ const checkReactVersionAtLeast19 = async (appDir) => {
67
67
  }
68
68
  return reactMajor >= 19 && reactDomMajor >= 19;
69
69
  };
70
- const rsbuildRscPlugin = ({ appDir, isRspack = true, rscClientRuntimePath, rscServerRuntimePath }) => ({
70
+ const rsbuildRscPlugin = ({ appDir, isRspack = true, rscClientRuntimePath, rscServerRuntimePath, internalDirectory }) => ({
71
71
  name: "uni-builder:rsc-rsbuild-plugin",
72
72
  setup(api) {
73
73
  api.modifyBundlerChain({
@@ -93,9 +93,11 @@ const rsbuildRscPlugin = ({ appDir, isRspack = true, rscClientRuntimePath, rscSe
93
93
  chain.module.rule(CHAIN_ID.RULE.JS).oneOf("rsc-server").issuerLayer(import_common.webpackRscLayerName).exclude.add(/universal[/\\]async_storage/).end().use("rsc-server-loader").loader(require.resolve("../rsc-server-loader")).options({
94
94
  entryPath2Name,
95
95
  appDir,
96
- runtimePath: rscServerRuntimePath
96
+ runtimePath: rscServerRuntimePath,
97
+ internalDirectory
97
98
  }).end().use(JSRule).loader(jsLoaderPath).options(jsLoaderOptions).end().end().oneOf("rsc-ssr").exclude.add(/universal[/\\]async_storage/).end().use("rsc-ssr-loader").loader(require.resolve("../rsc-ssr-loader")).options({
98
- entryPath2Name
99
+ entryPath2Name,
100
+ internalDirectory
99
101
  }).end().use(JSRule).loader(jsLoaderPath).options(jsLoaderOptions).end().end();
100
102
  }
101
103
  };
@@ -104,9 +106,11 @@ const rsbuildRscPlugin = ({ appDir, isRspack = true, rscClientRuntimePath, rscSe
104
106
  ...chain.experiments,
105
107
  layers: true
106
108
  });
109
+ const routesFileReg = new RegExp(`${internalDirectory.replace(/[/\\]/g, "[/\\\\]")}[/\\\\][^/\\\\]*[/\\\\]routes`);
107
110
  chain.module.rule("server-module").resource([
108
111
  /render[/\\].*[/\\]server[/\\]rsc/,
109
- /AppProxy/
112
+ /AppProxy/,
113
+ routesFileReg
110
114
  ]).layer(import_common.webpackRscLayerName).end();
111
115
  chain.module.rule(import_common.webpackRscLayerName).issuerLayer(import_common.webpackRscLayerName).resolve.conditionNames.add(import_common.webpackRscLayerName).add("...");
112
116
  chain.module.rule("rsc-common").resource([
@@ -139,11 +143,6 @@ const rsbuildRscPlugin = ({ appDir, isRspack = true, rscClientRuntimePath, rscSe
139
143
  const ClientPlugin = isRspack ? import_rspack_rsc_client_plugin.RspackRscClientPlugin : import_rsc_client_plugin.RscClientPlugin;
140
144
  chain.plugin("rsc-client-plugin").use(ClientPlugin);
141
145
  };
142
- const configureRuntimeChunk = () => {
143
- chain.optimization.runtimeChunk({
144
- name: (entrypoint) => `runtime-${entrypoint.name}`
145
- });
146
- };
147
146
  if (isServer) {
148
147
  chain.name("server");
149
148
  layerHandler();
@@ -157,7 +156,6 @@ const rsbuildRscPlugin = ({ appDir, isRspack = true, rscClientRuntimePath, rscSe
157
156
  ]);
158
157
  addRscClientLoader();
159
158
  addRscClientPlugin();
160
- configureRuntimeChunk();
161
159
  }
162
160
  },
163
161
  order: "post"
@@ -14,7 +14,15 @@ export declare class RscServerPlugin {
14
14
  private serverManifestFilename;
15
15
  private entryPath2Name;
16
16
  private styles;
17
+ private moduleToEntries;
17
18
  constructor(options: RscServerPluginOptions);
19
+ private isValidModule;
20
+ private hasValidEntries;
21
+ private traverseModulesFromEntry;
18
22
  private findModuleEntries;
23
+ private getEntryNameFromIssuer;
24
+ private createEntryFromIssuer;
25
+ private buildModuleToEntriesMapping;
26
+ private getEntryPathByName;
19
27
  apply(compiler: Webpack.Compiler): void;
20
28
  }
@@ -25,15 +25,50 @@ var import_webpack = require("webpack");
25
25
  var import_common = require("../common");
26
26
  const resourcePath2Entries = /* @__PURE__ */ new Map();
27
27
  class RscServerPlugin {
28
- findModuleEntries(module2, compilation, resourcePath2Entries2, visited = /* @__PURE__ */ new Set()) {
28
+ isValidModule(module2) {
29
+ return Boolean(module2 === null || module2 === void 0 ? void 0 : module2.resource);
30
+ }
31
+ hasValidEntries(entries) {
32
+ return Boolean(entries && entries.length > 0);
33
+ }
34
+ traverseModulesFromEntry(module2, entryName, moduleGraph, visited) {
29
35
  if (!(module2 === null || module2 === void 0 ? void 0 : module2.resource) || visited.has(module2.resource)) {
36
+ return;
37
+ }
38
+ visited.add(module2.resource);
39
+ if (!this.moduleToEntries.has(module2.resource)) {
40
+ this.moduleToEntries.set(module2.resource, /* @__PURE__ */ new Set());
41
+ }
42
+ this.moduleToEntries.get(module2.resource).add(entryName);
43
+ for (const connection of moduleGraph.getOutgoingConnections(module2)) {
44
+ if (connection.module && "resource" in connection.module) {
45
+ this.traverseModulesFromEntry(connection.module, entryName, moduleGraph, visited);
46
+ }
47
+ }
48
+ }
49
+ findModuleEntries(module2, compilation, resourcePath2Entries2, visited = /* @__PURE__ */ new Set()) {
50
+ if (!this.isValidModule(module2) || visited.has(module2.resource)) {
30
51
  return [];
31
52
  }
32
53
  visited.add(module2.resource);
33
54
  const currentEntries = resourcePath2Entries2.get(module2.resource);
34
- if (currentEntries && (currentEntries === null || currentEntries === void 0 ? void 0 : currentEntries.length) > 0) {
55
+ if (this.hasValidEntries(currentEntries)) {
35
56
  return currentEntries;
36
57
  }
58
+ const entryNames = this.moduleToEntries.get(module2.resource);
59
+ if (entryNames && entryNames.size > 0) {
60
+ const entries = [];
61
+ for (const entryName2 of entryNames) {
62
+ const entryPath = this.getEntryPathByName(entryName2, compilation);
63
+ if (entryPath) {
64
+ entries.push({
65
+ entryName: entryName2,
66
+ entryPath
67
+ });
68
+ }
69
+ }
70
+ return entries;
71
+ }
37
72
  const issuer = (0, import_common.findRootIssuer)(compilation.moduleGraph, module2);
38
73
  if (!issuer) {
39
74
  return [];
@@ -42,19 +77,42 @@ class RscServerPlugin {
42
77
  if (issuerEntries.length > 0) {
43
78
  return issuerEntries;
44
79
  }
45
- if (issuer.resource) {
46
- const entryName = this.entryPath2Name.get(issuer.resource);
47
- if (entryName) {
48
- return [
49
- {
50
- entryName,
51
- entryPath: issuer.resource
52
- }
53
- ];
54
- }
80
+ const entryName = this.getEntryNameFromIssuer(issuer);
81
+ if (entryName) {
82
+ return [
83
+ this.createEntryFromIssuer(issuer, entryName)
84
+ ];
55
85
  }
56
86
  return [];
57
87
  }
88
+ getEntryNameFromIssuer(issuer) {
89
+ return issuer.resource ? this.entryPath2Name.get(issuer.resource) : void 0;
90
+ }
91
+ createEntryFromIssuer(issuer, entryName) {
92
+ return {
93
+ entryName,
94
+ entryPath: issuer.resource
95
+ };
96
+ }
97
+ buildModuleToEntriesMapping(compilation) {
98
+ this.moduleToEntries.clear();
99
+ for (const [entryName, entryDependency] of compilation.entries.entries()) {
100
+ const entryModule = compilation.moduleGraph.getModule(entryDependency.dependencies[0]);
101
+ if (!entryModule)
102
+ continue;
103
+ this.traverseModulesFromEntry(entryModule, entryName, compilation.moduleGraph, /* @__PURE__ */ new Set());
104
+ }
105
+ }
106
+ getEntryPathByName(entryName, compilation) {
107
+ const entryDependency = compilation.entries.get(entryName);
108
+ if (entryDependency && entryDependency.dependencies.length > 0) {
109
+ const firstDep = entryDependency.dependencies[0];
110
+ if ("request" in firstDep && typeof firstDep.request === "string") {
111
+ return firstDep.request;
112
+ }
113
+ }
114
+ return void 0;
115
+ }
58
116
  apply(compiler) {
59
117
  const { EntryPlugin, WebpackError, dependencies: { NullDependency }, sources: { RawSource }, RuntimeGlobals } = compiler.webpack;
60
118
  class ServerReferenceDependency extends NullDependency {
@@ -107,6 +165,7 @@ class RscServerPlugin {
107
165
  };
108
166
  let needsAdditionalPass = false;
109
167
  compiler.hooks.finishMake.tapPromise(RscServerPlugin.name, async (compilation) => {
168
+ this.buildModuleToEntriesMapping(compilation);
110
169
  const processModules = (modules) => {
111
170
  let hasChangeReference2 = false;
112
171
  for (const module2 of modules) {
@@ -261,6 +320,7 @@ class RscServerPlugin {
261
320
  this.serverReferencesMap = /* @__PURE__ */ new Map();
262
321
  this.serverManifest = {};
263
322
  this.entryPath2Name = /* @__PURE__ */ new Map();
323
+ this.moduleToEntries = /* @__PURE__ */ new Map();
264
324
  this.styles = /* @__PURE__ */ new Set();
265
325
  this.serverManifestFilename = (options === null || options === void 0 ? void 0 : options.serverManifestFilename) || `react-server-manifest.json`;
266
326
  this.entryPath2Name = (options === null || options === void 0 ? void 0 : options.entryPath2Name) || /* @__PURE__ */ new Map();
@@ -18,11 +18,15 @@ export declare class RscServerPlugin {
18
18
  private serverManifestFilename;
19
19
  private entryPath2Name;
20
20
  private styles;
21
+ private moduleToEntries;
21
22
  constructor(options: RscServerPluginOptions);
22
23
  private isValidModule;
23
24
  private hasValidEntries;
24
25
  private getEntryNameFromIssuer;
25
26
  private createEntryFromIssuer;
27
+ private buildModuleToEntriesMapping;
28
+ private traverseModulesFromEntry;
26
29
  private findModuleEntries;
30
+ private getEntryPathByName;
27
31
  apply(compiler: Webpack.Compiler): void;
28
32
  }
@@ -39,6 +39,30 @@ class RscServerPlugin {
39
39
  entryPath: issuer.resource
40
40
  };
41
41
  }
42
+ buildModuleToEntriesMapping(compilation) {
43
+ this.moduleToEntries.clear();
44
+ for (const [entryName, entryDependency] of compilation.entries.entries()) {
45
+ const entryModule = compilation.moduleGraph.getModule(entryDependency.dependencies[0]);
46
+ if (!entryModule)
47
+ continue;
48
+ this.traverseModulesFromEntry(entryModule, entryName, compilation.moduleGraph, /* @__PURE__ */ new Set());
49
+ }
50
+ }
51
+ traverseModulesFromEntry(module2, entryName, moduleGraph, visited) {
52
+ if (!(module2 === null || module2 === void 0 ? void 0 : module2.resource) || visited.has(module2.resource)) {
53
+ return;
54
+ }
55
+ visited.add(module2.resource);
56
+ if (!this.moduleToEntries.has(module2.resource)) {
57
+ this.moduleToEntries.set(module2.resource, /* @__PURE__ */ new Set());
58
+ }
59
+ this.moduleToEntries.get(module2.resource).add(entryName);
60
+ for (const connection of moduleGraph.getOutgoingConnections(module2)) {
61
+ if (connection.module && "resource" in connection.module) {
62
+ this.traverseModulesFromEntry(connection.module, entryName, moduleGraph, visited);
63
+ }
64
+ }
65
+ }
42
66
  findModuleEntries(module2, compilation, resourcePath2Entries2, visited = /* @__PURE__ */ new Set()) {
43
67
  if (!this.isValidModule(module2) || visited.has(module2.resource)) {
44
68
  return [];
@@ -48,12 +72,26 @@ class RscServerPlugin {
48
72
  if (this.hasValidEntries(currentEntries)) {
49
73
  return currentEntries;
50
74
  }
75
+ const entryNames = this.moduleToEntries.get(module2.resource);
76
+ if (entryNames && entryNames.size > 0) {
77
+ const entries = [];
78
+ for (const entryName2 of entryNames) {
79
+ const entryPath = this.getEntryPathByName(entryName2, compilation);
80
+ if (entryPath) {
81
+ entries.push({
82
+ entryName: entryName2,
83
+ entryPath
84
+ });
85
+ }
86
+ }
87
+ return entries;
88
+ }
51
89
  const issuer = (0, import_common.findRootIssuer)(compilation.moduleGraph, module2);
52
90
  if (!issuer) {
53
91
  return [];
54
92
  }
55
93
  const issuerEntries = this.findModuleEntries(issuer, compilation, resourcePath2Entries2, visited);
56
- if (this.hasValidEntries(issuerEntries)) {
94
+ if (issuerEntries.length > 0) {
57
95
  return issuerEntries;
58
96
  }
59
97
  const entryName = this.getEntryNameFromIssuer(issuer);
@@ -64,6 +102,16 @@ class RscServerPlugin {
64
102
  }
65
103
  return [];
66
104
  }
105
+ getEntryPathByName(entryName, compilation) {
106
+ const entryDependency = compilation.entries.get(entryName);
107
+ if (entryDependency && entryDependency.dependencies.length > 0) {
108
+ const firstDep = entryDependency.dependencies[0];
109
+ if ("request" in firstDep && typeof firstDep.request === "string") {
110
+ return firstDep.request;
111
+ }
112
+ }
113
+ return void 0;
114
+ }
67
115
  apply(compiler) {
68
116
  const { EntryPlugin, WebpackError, sources: { RawSource } } = compiler.webpack;
69
117
  const includeModule = async (compilation, resource, resourceEntryNames, layer) => {
@@ -103,6 +151,7 @@ class RscServerPlugin {
103
151
  };
104
152
  let needsAdditionalPass = false;
105
153
  compiler.hooks.finishMake.tapPromise(RscServerPlugin.name, async (compilation) => {
154
+ this.buildModuleToEntriesMapping(compilation);
106
155
  const processModules = (modules) => {
107
156
  let hasChangeReference2 = false;
108
157
  for (const module2 of modules) {
@@ -231,6 +280,7 @@ class RscServerPlugin {
231
280
  this.serverReferencesMap = /* @__PURE__ */ new Map();
232
281
  this.serverManifest = {};
233
282
  this.entryPath2Name = /* @__PURE__ */ new Map();
283
+ this.moduleToEntries = /* @__PURE__ */ new Map();
234
284
  this.styles = /* @__PURE__ */ new Set();
235
285
  this.serverManifestFilename = (options === null || options === void 0 ? void 0 : options.serverManifestFilename) || `react-server-manifest.json`;
236
286
  this.entryPath2Name = (options === null || options === void 0 ? void 0 : options.entryPath2Name) || /* @__PURE__ */ new Map();
@@ -67,7 +67,8 @@ async function rscServerLoader(source) {
67
67
  ]
68
68
  ]
69
69
  }
70
- }
70
+ },
71
+ isModule: true
71
72
  });
72
73
  const { code, map } = result;
73
74
  const metadata = extractMetadata(code);
package/dist/types.d.ts CHANGED
@@ -41,6 +41,7 @@ export type CreateBuilderCommonOptions = {
41
41
  cwd: string;
42
42
  rscClientRuntimePath?: string;
43
43
  rscServerRuntimePath?: string;
44
+ internalDirectory?: string;
44
45
  };
45
46
  export type BundlerType = 'rspack' | 'webpack';
46
47
  export type CreateUniBuilderOptions = {
@@ -101,12 +101,13 @@ async function parseConfig(uniBuilderConfig, options) {
101
101
  var _uniBuilderConfig_server_rsc;
102
102
  const enableRsc = (_uniBuilderConfig_server_rsc = (_uniBuilderConfig_server = uniBuilderConfig.server) === null || _uniBuilderConfig_server === void 0 ? void 0 : _uniBuilderConfig_server.rsc) !== null && _uniBuilderConfig_server_rsc !== void 0 ? _uniBuilderConfig_server_rsc : false;
103
103
  if (enableRsc) {
104
- const { rscClientRuntimePath, rscServerRuntimePath } = options;
104
+ const { rscClientRuntimePath, rscServerRuntimePath, internalDirectory } = options;
105
105
  rsbuildPlugins.push((0, import_rsbuild_rsc_plugin.rsbuildRscPlugin)({
106
106
  appDir: options.cwd,
107
107
  isRspack: false,
108
108
  rscClientRuntimePath,
109
- rscServerRuntimePath
109
+ rscServerRuntimePath,
110
+ internalDirectory
110
111
  }));
111
112
  }
112
113
  if ((_uniBuilderConfig_tools1 = uniBuilderConfig.tools) === null || _uniBuilderConfig_tools1 === void 0 ? void 0 : _uniBuilderConfig_tools1.tsLoader) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/uni-builder",
3
- "version": "2.68.0",
3
+ "version": "2.68.2",
4
4
  "description": "Unified builder for Modern.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,19 +30,19 @@
30
30
  "@babel/preset-react": "^7.22.15",
31
31
  "@babel/types": "^7.26.0",
32
32
  "@pmmmwh/react-refresh-webpack-plugin": "0.5.16",
33
- "@rsbuild/core": "1.4.3",
34
- "@rsbuild/plugin-assets-retry": "1.3.0",
33
+ "@rsbuild/core": "1.4.4",
34
+ "@rsbuild/plugin-assets-retry": "1.4.0",
35
35
  "@rsbuild/plugin-babel": "1.0.5",
36
36
  "@rsbuild/plugin-check-syntax": "1.3.0",
37
37
  "@rsbuild/plugin-css-minimizer": "1.0.2",
38
- "@rsbuild/plugin-less": "1.2.4",
38
+ "@rsbuild/plugin-less": "1.2.5",
39
39
  "@rsbuild/plugin-pug": "1.3.1",
40
- "@rsbuild/plugin-react": "1.3.2",
40
+ "@rsbuild/plugin-react": "1.3.4",
41
41
  "@rsbuild/plugin-rem": "1.0.2",
42
- "@rsbuild/plugin-sass": "1.3.2",
42
+ "@rsbuild/plugin-sass": "1.3.3",
43
43
  "@rsbuild/plugin-source-build": "1.0.2",
44
44
  "@rsbuild/plugin-styled-components": "1.4.0",
45
- "@rsbuild/plugin-svgr": "1.2.0",
45
+ "@rsbuild/plugin-svgr": "1.2.1",
46
46
  "@rsbuild/plugin-toml": "1.1.0",
47
47
  "@rsbuild/plugin-type-check": "1.2.3",
48
48
  "@rsbuild/plugin-typed-css-modules": "1.0.2",
@@ -64,8 +64,8 @@
64
64
  "jiti": "1.21.7",
65
65
  "lodash": "^4.17.21",
66
66
  "magic-string": "0.30.17",
67
- "picocolors": "^1.0.1",
68
- "postcss": "^8.4.35",
67
+ "picocolors": "^1.1.1",
68
+ "postcss": "^8.5.6",
69
69
  "postcss-custom-properties": "13.3.12",
70
70
  "postcss-flexbugs-fixes": "5.0.2",
71
71
  "postcss-font-variant": "5.0.0",
@@ -80,9 +80,9 @@
80
80
  "ts-loader": "9.4.4",
81
81
  "webpack": "^5.99.8",
82
82
  "webpack-subresource-integrity": "5.1.0",
83
- "@modern-js/babel-preset": "2.68.0",
84
- "@modern-js/flight-server-transform-plugin": "2.68.0",
85
- "@modern-js/utils": "2.68.0"
83
+ "@modern-js/babel-preset": "2.68.2",
84
+ "@modern-js/flight-server-transform-plugin": "2.68.2",
85
+ "@modern-js/utils": "2.68.2"
86
86
  },
87
87
  "devDependencies": {
88
88
  "@rsbuild/plugin-webpack-swc": "1.1.1",
@@ -93,7 +93,7 @@
93
93
  "react-dom": "^18.2.0",
94
94
  "terser": "^5.31.1",
95
95
  "typescript": "^5.3.0",
96
- "@modern-js/types": "2.68.0",
96
+ "@modern-js/types": "2.68.2",
97
97
  "@scripts/build": "2.66.0",
98
98
  "@scripts/vitest-config": "2.66.0"
99
99
  },