@modern-js/app-tools 2.27.1-alpha.0 → 2.28.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/dist/cjs/analyze/generateCode.js +2 -2
  3. package/dist/cjs/analyze/getFileSystemEntry.js +2 -1
  4. package/dist/cjs/analyze/getServerRoutes.js +5 -4
  5. package/dist/cjs/analyze/index.js +209 -211
  6. package/dist/cjs/analyze/nestedRoutes.js +2 -2
  7. package/dist/cjs/analyze/templates.js +4 -4
  8. package/dist/cjs/builder/builder-rspack/adapterCopy.js +41 -43
  9. package/dist/cjs/builder/builder-webpack/adapterModern.js +23 -25
  10. package/dist/cjs/builder/generator/createBuilderProviderConfig.js +2 -1
  11. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +12 -15
  12. package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +33 -75
  13. package/dist/cjs/commands/build.js +2 -1
  14. package/dist/cjs/commands/inspect.js +2 -1
  15. package/dist/cjs/commands/serve.js +2 -2
  16. package/dist/cjs/index.js +3 -2
  17. package/dist/cjs/initialize/index.js +70 -72
  18. package/dist/esm/analyze/generateCode.js +2 -2
  19. package/dist/esm/analyze/getFileSystemEntry.js +2 -1
  20. package/dist/esm/analyze/getServerRoutes.js +5 -4
  21. package/dist/esm/analyze/nestedRoutes.js +2 -2
  22. package/dist/esm/analyze/templates.js +4 -4
  23. package/dist/esm/builder/builder-rspack/adapterCopy.js +1 -1
  24. package/dist/esm/builder/generator/createBuilderProviderConfig.js +2 -1
  25. package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +13 -16
  26. package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +41 -101
  27. package/dist/esm/commands/build.js +2 -2
  28. package/dist/esm/commands/inspect.js +2 -2
  29. package/dist/esm/commands/serve.js +2 -2
  30. package/dist/esm/index.js +3 -2
  31. package/dist/esm-node/analyze/generateCode.js +2 -2
  32. package/dist/esm-node/analyze/getFileSystemEntry.js +2 -1
  33. package/dist/esm-node/analyze/getServerRoutes.js +5 -4
  34. package/dist/esm-node/analyze/index.js +209 -211
  35. package/dist/esm-node/analyze/nestedRoutes.js +2 -2
  36. package/dist/esm-node/analyze/templates.js +4 -4
  37. package/dist/esm-node/builder/builder-rspack/adapterCopy.js +41 -43
  38. package/dist/esm-node/builder/builder-webpack/adapterModern.js +23 -25
  39. package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +2 -1
  40. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +12 -15
  41. package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +33 -75
  42. package/dist/esm-node/commands/build.js +2 -1
  43. package/dist/esm-node/commands/inspect.js +2 -1
  44. package/dist/esm-node/commands/serve.js +2 -2
  45. package/dist/esm-node/index.js +3 -2
  46. package/dist/esm-node/initialize/index.js +70 -72
  47. package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +0 -8
  48. package/package.json +24 -24
@@ -11,234 +11,232 @@ import { checkIsBuildCommands, isPageComponentFile, parseModule, replaceWithAlia
11
11
  import { APP_CONFIG_NAME, APP_INIT_EXPORTED, APP_INIT_IMPORTED } from "./constants";
12
12
  import { generateIndexCode } from "./generateCode";
13
13
  const debug = createDebugger("plugin-analyze");
14
- export default ({ bundler }) => {
15
- return {
16
- name: "@modern-js/plugin-analyze",
17
- setup: (api) => {
18
- let pagesDir = [];
19
- let nestedRouteEntries = [];
20
- let originEntrypoints = [];
21
- return {
22
- async prepare() {
23
- var _resolvedConfig_source;
24
- let appContext = api.useAppContext();
25
- const resolvedConfig = api.useResolvedConfigContext();
26
- const hookRunners = api.useHookRunners();
27
- try {
28
- if (checkIsBuildCommands()) {
29
- fs.emptydirSync(appContext.internalDirectory);
30
- }
31
- } catch {
32
- }
33
- const apiOnly = await isApiOnly(appContext.appDirectory, (_resolvedConfig_source = resolvedConfig.source) === null || _resolvedConfig_source === void 0 ? void 0 : _resolvedConfig_source.entriesDir, appContext.apiDirectory);
34
- await hookRunners.addRuntimeExports();
35
- if (apiOnly) {
36
- const { routes: routes2 } = await hookRunners.modifyServerRoutes({
37
- routes: []
38
- });
39
- debug(`server routes: %o`, routes2);
40
- appContext = {
41
- ...api.useAppContext(),
42
- apiOnly,
43
- serverRoutes: routes2
44
- };
45
- api.setAppContext(appContext);
46
- return;
14
+ export default ({ bundler }) => ({
15
+ name: "@modern-js/plugin-analyze",
16
+ setup: (api) => {
17
+ let pagesDir = [];
18
+ let nestedRouteEntries = [];
19
+ let originEntrypoints = [];
20
+ return {
21
+ async prepare() {
22
+ var _resolvedConfig_source;
23
+ let appContext = api.useAppContext();
24
+ const resolvedConfig = api.useResolvedConfigContext();
25
+ const hookRunners = api.useHookRunners();
26
+ try {
27
+ if (checkIsBuildCommands()) {
28
+ fs.emptydirSync(appContext.internalDirectory);
47
29
  }
48
- const [{ getBundleEntry }, { getServerRoutes }, { generateCode }, { getHtmlTemplate }] = await Promise.all([
49
- import("./getBundleEntry"),
50
- import("./getServerRoutes"),
51
- import("./generateCode"),
52
- import("./getHtmlTemplate")
53
- ]);
54
- const entrypoints = getBundleEntry(appContext, resolvedConfig);
55
- debug(`entrypoints: %o`, entrypoints);
56
- const initialRoutes = getServerRoutes(entrypoints, {
57
- appContext,
58
- config: resolvedConfig
59
- });
60
- const { routes } = await hookRunners.modifyServerRoutes({
61
- routes: initialRoutes
62
- });
63
- debug(`server routes: %o`, routes);
64
- appContext = {
65
- ...api.useAppContext(),
66
- entrypoints,
67
- serverRoutes: routes
68
- };
69
- api.setAppContext(appContext);
70
- nestedRouteEntries = entrypoints.map((point) => point.nestedRoutesEntry).filter(Boolean);
71
- pagesDir = entrypoints.map((point) => point.entry).filter((entry) => entry && !path.extname(entry)).concat(nestedRouteEntries);
72
- originEntrypoints = cloneDeep(entrypoints);
73
- const { importsStatemets } = await generateCode(appContext, resolvedConfig, entrypoints, api);
74
- const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
75
- appContext,
76
- config: resolvedConfig
30
+ } catch {
31
+ }
32
+ const apiOnly = await isApiOnly(appContext.appDirectory, (_resolvedConfig_source = resolvedConfig.source) === null || _resolvedConfig_source === void 0 ? void 0 : _resolvedConfig_source.entriesDir, appContext.apiDirectory);
33
+ await hookRunners.addRuntimeExports();
34
+ if (apiOnly) {
35
+ const { routes: routes2 } = await hookRunners.modifyServerRoutes({
36
+ routes: []
77
37
  });
78
- debug(`html templates: %o`, htmlTemplates);
79
- await hookRunners.addDefineTypes();
80
- debug(`add Define Types`);
81
- let checkedEntries = entrypoints.map((point) => point.entryName);
82
- if (isDevCommand()) {
83
- const { entry } = minimist(getArgv());
84
- checkedEntries = await getSelectedEntries(typeof entry === "string" ? entry.split(",") : entry, entrypoints);
85
- }
38
+ debug(`server routes: %o`, routes2);
86
39
  appContext = {
87
40
  ...api.useAppContext(),
88
- entrypoints,
89
- checkedEntries,
90
41
  apiOnly,
91
- serverRoutes: routes,
92
- htmlTemplates
42
+ serverRoutes: routes2
93
43
  };
94
44
  api.setAppContext(appContext);
95
- if (checkIsBuildCommands()) {
96
- const normalizedConfig = api.useResolvedConfigContext();
97
- const createBuilderForModern = await createBuilderGenerator(bundler);
98
- const builder = await createBuilderForModern({
99
- normalizedConfig,
100
- appContext
45
+ return;
46
+ }
47
+ const [{ getBundleEntry }, { getServerRoutes }, { generateCode }, { getHtmlTemplate }] = await Promise.all([
48
+ import("./getBundleEntry"),
49
+ import("./getServerRoutes"),
50
+ import("./generateCode"),
51
+ import("./getHtmlTemplate")
52
+ ]);
53
+ const entrypoints = getBundleEntry(appContext, resolvedConfig);
54
+ debug(`entrypoints: %o`, entrypoints);
55
+ const initialRoutes = getServerRoutes(entrypoints, {
56
+ appContext,
57
+ config: resolvedConfig
58
+ });
59
+ const { routes } = await hookRunners.modifyServerRoutes({
60
+ routes: initialRoutes
61
+ });
62
+ debug(`server routes: %o`, routes);
63
+ appContext = {
64
+ ...api.useAppContext(),
65
+ entrypoints,
66
+ serverRoutes: routes
67
+ };
68
+ api.setAppContext(appContext);
69
+ nestedRouteEntries = entrypoints.map((point) => point.nestedRoutesEntry).filter(Boolean);
70
+ pagesDir = entrypoints.map((point) => point.entry).filter((entry) => entry && !path.extname(entry)).concat(nestedRouteEntries);
71
+ originEntrypoints = cloneDeep(entrypoints);
72
+ const { importsStatemets } = await generateCode(appContext, resolvedConfig, entrypoints, api);
73
+ const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
74
+ appContext,
75
+ config: resolvedConfig
76
+ });
77
+ debug(`html templates: %o`, htmlTemplates);
78
+ await hookRunners.addDefineTypes();
79
+ debug(`add Define Types`);
80
+ let checkedEntries = entrypoints.map((point) => point.entryName);
81
+ if (isDevCommand()) {
82
+ const { entry } = minimist(getArgv());
83
+ checkedEntries = await getSelectedEntries(typeof entry === "string" ? entry.split(",") : entry, entrypoints);
84
+ }
85
+ appContext = {
86
+ ...api.useAppContext(),
87
+ entrypoints,
88
+ checkedEntries,
89
+ apiOnly,
90
+ serverRoutes: routes,
91
+ htmlTemplates
92
+ };
93
+ api.setAppContext(appContext);
94
+ if (checkIsBuildCommands()) {
95
+ const normalizedConfig = api.useResolvedConfigContext();
96
+ const createBuilderForModern = await createBuilderGenerator(bundler);
97
+ const builder = await createBuilderForModern({
98
+ normalizedConfig,
99
+ appContext
100
+ });
101
+ builder.onBeforeBuild(async ({ bundlerConfigs }) => {
102
+ const hookRunners2 = api.useHookRunners();
103
+ await generateRoutes(appContext);
104
+ await hookRunners2.beforeBuild({
105
+ bundlerConfigs
101
106
  });
102
- builder.onBeforeBuild(async ({ bundlerConfigs }) => {
103
- const hookRunners2 = api.useHookRunners();
104
- await generateRoutes(appContext);
105
- await hookRunners2.beforeBuild({
106
- bundlerConfigs
107
- });
107
+ });
108
+ builder.onAfterBuild(async ({ stats }) => {
109
+ const hookRunners2 = api.useHookRunners();
110
+ await hookRunners2.afterBuild({
111
+ stats
108
112
  });
109
- builder.onAfterBuild(async ({ stats }) => {
110
- const hookRunners2 = api.useHookRunners();
111
- await hookRunners2.afterBuild({
112
- stats
113
+ await emitResolvedConfig(appContext.appDirectory, normalizedConfig);
114
+ });
115
+ builder.onDevCompileDone(async ({ isFirstCompile }) => {
116
+ const hookRunners2 = api.useHookRunners();
117
+ if (process.stdout.isTTY || isFirstCompile) {
118
+ hookRunners2.afterDev({
119
+ isFirstCompile
113
120
  });
114
- await emitResolvedConfig(appContext.appDirectory, normalizedConfig);
115
- });
116
- builder.onDevCompileDone(async ({ isFirstCompile }) => {
117
- const hookRunners2 = api.useHookRunners();
118
- if (process.stdout.isTTY || isFirstCompile) {
119
- hookRunners2.afterDev({
120
- isFirstCompile
121
- });
122
- if (isFirstCompile) {
123
- printInstructions(hookRunners2, appContext, normalizedConfig);
124
- }
121
+ if (isFirstCompile) {
122
+ printInstructions(hookRunners2, appContext, normalizedConfig);
125
123
  }
124
+ }
125
+ });
126
+ builder.onBeforeCreateCompiler(async ({ bundlerConfigs }) => {
127
+ const hookRunners2 = api.useHookRunners();
128
+ await generateIndexCode({
129
+ appContext,
130
+ config: resolvedConfig,
131
+ entrypoints,
132
+ api,
133
+ importsStatemets,
134
+ bundlerConfigs
126
135
  });
127
- builder.onBeforeCreateCompiler(async ({ bundlerConfigs }) => {
128
- const hookRunners2 = api.useHookRunners();
129
- await generateIndexCode({
130
- appContext,
131
- config: resolvedConfig,
132
- entrypoints,
133
- api,
134
- importsStatemets,
135
- bundlerConfigs
136
- });
137
- await hookRunners2.beforeCreateCompiler({
138
- bundlerConfigs
139
- });
136
+ await hookRunners2.beforeCreateCompiler({
137
+ bundlerConfigs
140
138
  });
141
- builder.onAfterCreateCompiler(async ({ compiler }) => {
142
- const hookRunners2 = api.useHookRunners();
143
- await hookRunners2.afterCreateCompiler({
144
- compiler
145
- });
139
+ });
140
+ builder.onAfterCreateCompiler(async ({ compiler }) => {
141
+ const hookRunners2 = api.useHookRunners();
142
+ await hookRunners2.afterCreateCompiler({
143
+ compiler
146
144
  });
147
- builder.addPlugins(resolvedConfig.builderPlugins);
148
- appContext = {
149
- ...api.useAppContext(),
150
- builder
151
- };
152
- api.setAppContext(appContext);
153
- }
154
- },
155
- watchFiles() {
156
- return {
157
- files: pagesDir,
158
- isPrivate: true
159
- };
160
- },
161
- resolvedConfig({ resolved }) {
162
- const appContext = api.useAppContext();
163
- const config = initialNormalizedConfig(resolved, appContext, bundler);
164
- return {
165
- resolved: config
145
+ });
146
+ builder.addPlugins(resolvedConfig.builderPlugins);
147
+ appContext = {
148
+ ...api.useAppContext(),
149
+ builder
166
150
  };
167
- },
168
- // This logic is not in the router plugin to avoid having to include some dependencies in the utils package
169
- async modifyEntryImports({ entrypoint, imports }) {
170
- const appContext = api.useAppContext();
171
- const { srcDirectory, internalSrcAlias } = appContext;
172
- const { fileSystemRoutes, nestedRoutesEntry } = entrypoint;
173
- if (fileSystemRoutes && nestedRoutesEntry) {
174
- const rootLayoutPath = path.join(nestedRoutesEntry, "layout");
175
- const rootLayoutFile = findExists([
176
- ".js",
177
- ".ts",
178
- ".jsx",
179
- ".tsx"
180
- ].map((ext) => `${rootLayoutPath}${ext}`));
181
- if (rootLayoutFile) {
182
- const rootLayoutBuffer = await fs.readFile(rootLayoutFile);
183
- const rootLayout = rootLayoutBuffer.toString();
184
- const [, moduleExports] = await parseModule({
185
- source: rootLayout.toString(),
186
- filename: rootLayoutFile
151
+ api.setAppContext(appContext);
152
+ }
153
+ },
154
+ watchFiles() {
155
+ return {
156
+ files: pagesDir,
157
+ isPrivate: true
158
+ };
159
+ },
160
+ resolvedConfig({ resolved }) {
161
+ const appContext = api.useAppContext();
162
+ const config = initialNormalizedConfig(resolved, appContext, bundler);
163
+ return {
164
+ resolved: config
165
+ };
166
+ },
167
+ // This logic is not in the router plugin to avoid having to include some dependencies in the utils package
168
+ async modifyEntryImports({ entrypoint, imports }) {
169
+ const appContext = api.useAppContext();
170
+ const { srcDirectory, internalSrcAlias } = appContext;
171
+ const { fileSystemRoutes, nestedRoutesEntry } = entrypoint;
172
+ if (fileSystemRoutes && nestedRoutesEntry) {
173
+ const rootLayoutPath = path.join(nestedRoutesEntry, "layout");
174
+ const rootLayoutFile = findExists([
175
+ ".js",
176
+ ".ts",
177
+ ".jsx",
178
+ ".tsx"
179
+ ].map((ext) => `${rootLayoutPath}${ext}`));
180
+ if (rootLayoutFile) {
181
+ const rootLayoutBuffer = await fs.readFile(rootLayoutFile);
182
+ const rootLayout = rootLayoutBuffer.toString();
183
+ const [, moduleExports] = await parseModule({
184
+ source: rootLayout.toString(),
185
+ filename: rootLayoutFile
186
+ });
187
+ const hasAppConfig = moduleExports.some((e) => e.n === APP_CONFIG_NAME);
188
+ const generateLayoutPath = replaceWithAlias(srcDirectory, rootLayoutFile, internalSrcAlias);
189
+ if (hasAppConfig) {
190
+ imports.push({
191
+ value: generateLayoutPath,
192
+ specifiers: [
193
+ {
194
+ imported: APP_CONFIG_NAME
195
+ }
196
+ ]
187
197
  });
188
- const hasAppConfig = moduleExports.some((e) => e.n === APP_CONFIG_NAME);
189
- const generateLayoutPath = replaceWithAlias(srcDirectory, rootLayoutFile, internalSrcAlias);
190
- if (hasAppConfig) {
191
- imports.push({
192
- value: generateLayoutPath,
193
- specifiers: [
194
- {
195
- imported: APP_CONFIG_NAME
196
- }
197
- ]
198
- });
199
- }
200
- const hasAppInit = moduleExports.some((e) => e.n === APP_INIT_EXPORTED);
201
- if (hasAppInit) {
202
- imports.push({
203
- value: generateLayoutPath,
204
- specifiers: [
205
- {
206
- imported: APP_INIT_EXPORTED,
207
- local: APP_INIT_IMPORTED
208
- }
209
- ]
210
- });
211
- }
212
198
  }
213
- }
214
- return {
215
- entrypoint,
216
- imports
217
- };
218
- },
219
- validateSchema() {
220
- return {
221
- target: "output.splitRouteChunks",
222
- schema: {
223
- type: "boolean"
199
+ const hasAppInit = moduleExports.some((e) => e.n === APP_INIT_EXPORTED);
200
+ if (hasAppInit) {
201
+ imports.push({
202
+ value: generateLayoutPath,
203
+ specifiers: [
204
+ {
205
+ imported: APP_INIT_EXPORTED,
206
+ local: APP_INIT_IMPORTED
207
+ }
208
+ ]
209
+ });
224
210
  }
225
- };
226
- },
227
- async fileChange(e) {
228
- const appContext = api.useAppContext();
229
- const { appDirectory } = appContext;
230
- const { filename, eventType } = e;
231
- const isPageFile = (name) => pagesDir.some((pageDir) => name.includes(pageDir));
232
- const absoluteFilePath = path.resolve(appDirectory, filename);
233
- const isRouteComponent = isPageFile(absoluteFilePath) && isPageComponentFile(absoluteFilePath);
234
- if (isRouteComponent && (eventType === "add" || eventType === "unlink")) {
235
- const resolvedConfig = api.useResolvedConfigContext();
236
- const { generateCode } = await import("./generateCode");
237
- const entrypoints = cloneDeep(originEntrypoints);
238
- generateCode(appContext, resolvedConfig, entrypoints, api);
239
211
  }
240
212
  }
241
- };
242
- }
243
- };
244
- };
213
+ return {
214
+ entrypoint,
215
+ imports
216
+ };
217
+ },
218
+ validateSchema() {
219
+ return {
220
+ target: "output.splitRouteChunks",
221
+ schema: {
222
+ type: "boolean"
223
+ }
224
+ };
225
+ },
226
+ async fileChange(e) {
227
+ const appContext = api.useAppContext();
228
+ const { appDirectory } = appContext;
229
+ const { filename, eventType } = e;
230
+ const isPageFile = (name) => pagesDir.some((pageDir) => name.includes(pageDir));
231
+ const absoluteFilePath = path.resolve(appDirectory, filename);
232
+ const isRouteComponent = isPageFile(absoluteFilePath) && isPageComponentFile(absoluteFilePath);
233
+ if (isRouteComponent && (eventType === "add" || eventType === "unlink")) {
234
+ const resolvedConfig = api.useResolvedConfigContext();
235
+ const { generateCode } = await import("./generateCode");
236
+ const entrypoints = cloneDeep(originEntrypoints);
237
+ generateCode(appContext, resolvedConfig, entrypoints, api);
238
+ }
239
+ }
240
+ };
241
+ }
242
+ });
@@ -37,7 +37,7 @@ const createRoute = (routeInfo, rootDir, filename, entryName, isMainEntry) => {
37
37
  };
38
38
  };
39
39
  export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
40
- var _route_children;
40
+ var _routePath, _route_children;
41
41
  if (!await fs.pathExists(dirname)) {
42
42
  return null;
43
43
  }
@@ -57,7 +57,7 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
57
57
  }
58
58
  routePath = replaceDynamicPath(routePath);
59
59
  const route = {
60
- path: routePath === null || routePath === void 0 ? void 0 : routePath.replace(/\$$/, "?"),
60
+ path: (_routePath = routePath) === null || _routePath === void 0 ? void 0 : _routePath.replace(/\$$/, "?"),
61
61
  children: [],
62
62
  isRoot
63
63
  };
@@ -79,8 +79,8 @@ export const routesForServer = ({ routes }) => {
79
79
  const traverseRouteTree = (route) => {
80
80
  let children;
81
81
  if ("children" in route && route.children) {
82
- var _route_children;
83
- children = route === null || route === void 0 ? void 0 : (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
82
+ var _route_children, _route;
83
+ children = (_route = route) === null || _route === void 0 ? void 0 : (_route_children = _route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
84
84
  }
85
85
  let loader;
86
86
  if (route.type === "nested") {
@@ -147,8 +147,8 @@ export const fileSystemRoutes = async ({ routes, ssrMode, nestedRoutesEntry, ent
147
147
  const traverseRouteTree = (route) => {
148
148
  let children;
149
149
  if ("children" in route && route.children) {
150
- var _route_children;
151
- children = route === null || route === void 0 ? void 0 : (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
150
+ var _route_children, _route;
151
+ children = (_route = route) === null || _route === void 0 ? void 0 : (_route_children = _route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
152
152
  }
153
153
  let loading;
154
154
  let error;
@@ -2,52 +2,50 @@ import path from "path";
2
2
  import fs from "@modern-js/utils/fs-extra";
3
3
  import { logger, removeTailSlash } from "@modern-js/utils";
4
4
  import { createCopyInfo } from "../shared";
5
- export const builderPluginAdpaterCopy = (options) => {
6
- return {
7
- name: "builder-plugin-adapter-rspack-copy",
8
- setup(api) {
9
- let publicPath;
10
- api.modifyRspackConfig((config) => {
11
- var _config_builtins, _config_builtins_copy, _config_output;
12
- config.builtins = {
13
- ...config.builtins || {},
14
- copy: {
15
- patterns: [
16
- ...((_config_builtins = config.builtins) === null || _config_builtins === void 0 ? void 0 : (_config_builtins_copy = _config_builtins.copy) === null || _config_builtins_copy === void 0 ? void 0 : _config_builtins_copy.patterns) || [],
17
- ...createConfigBuiltinCopy(options)
18
- ]
19
- }
20
- };
21
- publicPath = (_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath;
22
- });
23
- api.onDevCompileDone(async () => {
24
- await transformHtmlFiles();
25
- });
26
- api.onAfterBuild(async () => {
27
- await transformHtmlFiles();
28
- });
29
- async function transformHtmlFiles() {
30
- var _normalizedConfig_output_distPath;
31
- const { normalizedConfig } = options;
32
- const publicDir = path.resolve(((_normalizedConfig_output_distPath = normalizedConfig.output.distPath) === null || _normalizedConfig_output_distPath === void 0 ? void 0 : _normalizedConfig_output_distPath.root) || "./dist", "./public");
33
- if (!fs.existsSync(publicDir) || !fs.statSync(publicDir).isDirectory()) {
34
- return;
5
+ export const builderPluginAdpaterCopy = (options) => ({
6
+ name: "builder-plugin-adapter-rspack-copy",
7
+ setup(api) {
8
+ let publicPath;
9
+ api.modifyRspackConfig((config) => {
10
+ var _config_builtins_copy, _config_builtins, _config_output;
11
+ config.builtins = {
12
+ ...config.builtins || {},
13
+ copy: {
14
+ patterns: [
15
+ ...((_config_builtins = config.builtins) === null || _config_builtins === void 0 ? void 0 : (_config_builtins_copy = _config_builtins.copy) === null || _config_builtins_copy === void 0 ? void 0 : _config_builtins_copy.patterns) || [],
16
+ ...createConfigBuiltinCopy(options)
17
+ ]
35
18
  }
36
- const HTML_REGEXP = /\.html?$/;
37
- const filepaths = (await fs.readdir(publicDir)).map((file) => path.resolve(publicDir, file));
38
- await Promise.all(filepaths.filter((file) => HTML_REGEXP.test(file)).map(async (file) => {
39
- const content = await fs.readFile(file, "utf-8");
40
- if (publicPath) {
41
- await fs.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, removeTailSlash(publicPath)));
42
- } else {
43
- logger.warn("Expect get a string from `publicPath`, but receive `undefined`.");
44
- await fs.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, removeTailSlash("/")));
45
- }
46
- }));
19
+ };
20
+ publicPath = (_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath;
21
+ });
22
+ api.onDevCompileDone(async () => {
23
+ await transformHtmlFiles();
24
+ });
25
+ api.onAfterBuild(async () => {
26
+ await transformHtmlFiles();
27
+ });
28
+ async function transformHtmlFiles() {
29
+ var _normalizedConfig_output_distPath;
30
+ const { normalizedConfig } = options;
31
+ const publicDir = path.resolve(((_normalizedConfig_output_distPath = normalizedConfig.output.distPath) === null || _normalizedConfig_output_distPath === void 0 ? void 0 : _normalizedConfig_output_distPath.root) || "./dist", "./public");
32
+ if (!fs.existsSync(publicDir) || !fs.statSync(publicDir).isDirectory()) {
33
+ return;
47
34
  }
35
+ const HTML_REGEXP = /\.html?$/;
36
+ const filepaths = (await fs.readdir(publicDir)).map((file) => path.resolve(publicDir, file));
37
+ await Promise.all(filepaths.filter((file) => HTML_REGEXP.test(file)).map(async (file) => {
38
+ const content = await fs.readFile(file, "utf-8");
39
+ if (publicPath) {
40
+ await fs.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, removeTailSlash(publicPath)));
41
+ } else {
42
+ logger.warn("Expect get a string from `publicPath`, but receive `undefined`.");
43
+ await fs.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, removeTailSlash("/")));
44
+ }
45
+ }));
48
46
  }
49
- };
50
- };
47
+ }
48
+ });
51
49
  function createConfigBuiltinCopy(options) {
52
50
  const { normalizedConfig, appContext } = options;
53
51
  const { publicDir } = createCopyInfo(appContext, normalizedConfig);
@@ -1,27 +1,25 @@
1
1
  import { join } from "path";
2
2
  import { createPublicPattern } from "./createCopyPattern";
3
- export const builderPluginAdapterModern = (options) => {
4
- return {
5
- name: "builder-plugin-adapter-modern",
6
- setup(api) {
7
- const { normalizedConfig: modernConfig, appContext } = options;
8
- api.modifyWebpackChain((chain, { CHAIN_ID }) => {
9
- chain.resolve.modules.add("node_modules").add(join(api.context.rootPath, "node_modules"));
10
- if (chain.plugins.has(CHAIN_ID.PLUGIN.COPY)) {
11
- const defaultCopyPattern = createPublicPattern(appContext, modernConfig, chain);
12
- chain.plugin(CHAIN_ID.PLUGIN.COPY).tap((args) => {
13
- var _args_;
14
- return [
15
- {
16
- patterns: [
17
- ...((_args_ = args[0]) === null || _args_ === void 0 ? void 0 : _args_.patterns) || [],
18
- defaultCopyPattern
19
- ]
20
- }
21
- ];
22
- });
23
- }
24
- });
25
- }
26
- };
27
- };
3
+ export const builderPluginAdapterModern = (options) => ({
4
+ name: "builder-plugin-adapter-modern",
5
+ setup(api) {
6
+ const { normalizedConfig: modernConfig, appContext } = options;
7
+ api.modifyWebpackChain((chain, { CHAIN_ID }) => {
8
+ chain.resolve.modules.add("node_modules").add(join(api.context.rootPath, "node_modules"));
9
+ if (chain.plugins.has(CHAIN_ID.PLUGIN.COPY)) {
10
+ const defaultCopyPattern = createPublicPattern(appContext, modernConfig, chain);
11
+ chain.plugin(CHAIN_ID.PLUGIN.COPY).tap((args) => {
12
+ var _args_;
13
+ return [
14
+ {
15
+ patterns: [
16
+ ...((_args_ = args[0]) === null || _args_ === void 0 ? void 0 : _args_.patterns) || [],
17
+ defaultCopyPattern
18
+ ]
19
+ }
20
+ ];
21
+ });
22
+ }
23
+ });
24
+ }
25
+ });
@@ -1,8 +1,9 @@
1
1
  import { createUploadPattern } from "../builder-webpack/createCopyPattern";
2
2
  function modifyOutputConfig(config, appContext) {
3
+ var _copy;
3
4
  const defaultCopyPattern = createUploadPattern(appContext, config);
4
5
  const { copy } = config.output;
5
- const copyOptions = Array.isArray(copy) ? copy : copy === null || copy === void 0 ? void 0 : copy.patterns;
6
+ const copyOptions = Array.isArray(copy) ? copy : (_copy = copy) === null || _copy === void 0 ? void 0 : _copy.patterns;
6
7
  const builderCopy = [
7
8
  ...copyOptions || [],
8
9
  defaultCopyPattern