@modern-js/app-tools 2.54.5 → 2.55.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. package/dist/cjs/builder/generator/createBuilderOptions.js +18 -1
  2. package/dist/cjs/builder/shared/builderPlugins/adapterHtml.js +2 -2
  3. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +2 -2
  4. package/dist/cjs/commands/index.js +2 -2
  5. package/dist/cjs/commands/serve.js +1 -0
  6. package/dist/cjs/config/default.js +2 -0
  7. package/dist/cjs/config/legacy/createSourceConfig.js +2 -1
  8. package/dist/cjs/hooks.js +2 -5
  9. package/dist/cjs/plugins/analyze/constants.js +3 -18
  10. package/dist/cjs/plugins/analyze/getFileSystemEntry.js +36 -11
  11. package/dist/cjs/plugins/analyze/index.js +7 -64
  12. package/dist/cjs/plugins/analyze/templates.js +2 -55
  13. package/dist/cjs/plugins/analyze/utils.js +0 -86
  14. package/dist/esm/builder/generator/createBuilderOptions.js +21 -1
  15. package/dist/esm/builder/shared/builderPlugins/adapterHtml.js +1 -1
  16. package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +1 -1
  17. package/dist/esm/commands/index.js +1 -1
  18. package/dist/esm/commands/serve.js +1 -0
  19. package/dist/esm/config/default.js +2 -0
  20. package/dist/esm/config/legacy/createSourceConfig.js +2 -1
  21. package/dist/esm/hooks.js +2 -5
  22. package/dist/esm/plugins/analyze/constants.js +2 -12
  23. package/dist/esm/plugins/analyze/getFileSystemEntry.js +57 -14
  24. package/dist/esm/plugins/analyze/index.js +25 -128
  25. package/dist/esm/plugins/analyze/templates.js +1 -17
  26. package/dist/esm/plugins/analyze/utils.js +1 -87
  27. package/dist/esm-node/builder/generator/createBuilderOptions.js +18 -1
  28. package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.js +1 -1
  29. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +1 -1
  30. package/dist/esm-node/commands/index.js +1 -1
  31. package/dist/esm-node/commands/serve.js +1 -0
  32. package/dist/esm-node/config/default.js +2 -0
  33. package/dist/esm-node/config/legacy/createSourceConfig.js +2 -1
  34. package/dist/esm-node/hooks.js +2 -5
  35. package/dist/esm-node/plugins/analyze/constants.js +2 -12
  36. package/dist/esm-node/plugins/analyze/getFileSystemEntry.js +37 -12
  37. package/dist/esm-node/plugins/analyze/index.js +10 -67
  38. package/dist/esm-node/plugins/analyze/templates.js +1 -52
  39. package/dist/esm-node/plugins/analyze/utils.js +1 -86
  40. package/dist/types/builder/builder-webpack/createCopyPattern.d.ts +2 -2
  41. package/dist/types/builder/generator/createBuilderOptions.d.ts +1 -2
  42. package/dist/types/builder/generator/getBuilderTargets.d.ts +1 -1
  43. package/dist/types/builder/generator/index.d.ts +1 -2
  44. package/dist/types/builder/shared/builderPlugins/adapterBasic.d.ts +1 -1
  45. package/dist/types/builder/shared/builderPlugins/adapterHtml.d.ts +1 -1
  46. package/dist/types/builder/shared/builderPlugins/adapterSSR.d.ts +1 -1
  47. package/dist/types/builder/shared/builderPlugins/adapterWorker.d.ts +1 -1
  48. package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +1 -1
  49. package/dist/types/commands/inspect.d.ts +1 -1
  50. package/dist/types/plugins/analyze/constants.d.ts +1 -6
  51. package/dist/types/plugins/analyze/templates.d.ts +0 -14
  52. package/dist/types/plugins/analyze/utils.d.ts +0 -11
  53. package/dist/types/types/config/source.d.ts +5 -0
  54. package/dist/types/types/hooks.d.ts +7 -27
  55. package/dist/types/types/index.d.ts +1 -1
  56. package/dist/types/types/legacyConfig/output.d.ts +1 -1
  57. package/dist/types/types/legacyConfig/source.d.ts +4 -0
  58. package/dist/types/utils/register.d.ts +1 -1
  59. package/package.json +17 -18
  60. package/dist/cjs/plugins/analyze/generateCode.js +0 -185
  61. package/dist/esm/plugins/analyze/generateCode.js +0 -296
  62. package/dist/esm-node/plugins/analyze/generateCode.js +0 -149
  63. package/dist/types/plugins/analyze/generateCode.d.ts +0 -16
@@ -1,149 +0,0 @@
1
- import path from "path";
2
- import { findExists, fs, getEntryOptions, JS_EXTENSIONS, SERVER_RENDER_FUNCTION_NAME } from "@modern-js/utils";
3
- import * as templates from "./templates";
4
- import { ENTRY_POINT_FILE_NAME, ENTRY_BOOTSTRAP_FILE_NAME } from "./constants";
5
- import { getDefaultImports } from "./utils";
6
- const createImportSpecifier = (specifiers) => {
7
- let defaults = "";
8
- const named = [];
9
- for (const { local, imported } of specifiers) {
10
- if (local && imported) {
11
- named.push(`${imported} as ${local}`);
12
- } else if (local) {
13
- defaults = local;
14
- } else {
15
- named.push(imported);
16
- }
17
- }
18
- if (defaults && named.length) {
19
- return `${defaults}, { ${named.join(", ")} }`;
20
- } else if (defaults) {
21
- return defaults;
22
- } else {
23
- return `{ ${named.join(", ")} }`;
24
- }
25
- };
26
- const createImportStatements = (statements) => {
27
- const deDuplicated = [];
28
- const seen = /* @__PURE__ */ new Map();
29
- for (const { value, specifiers, initialize } of statements) {
30
- if (!seen.has(value)) {
31
- deDuplicated.push({
32
- value,
33
- specifiers,
34
- initialize
35
- });
36
- seen.set(value, specifiers);
37
- } else {
38
- var _deDuplicated_modifyIndex;
39
- seen.get(value).push(...specifiers);
40
- const modifyIndex = deDuplicated.findIndex((v) => v.value === value);
41
- var _deDuplicated_modifyIndex_initialize;
42
- const originInitialize = (_deDuplicated_modifyIndex_initialize = (_deDuplicated_modifyIndex = deDuplicated[modifyIndex]) === null || _deDuplicated_modifyIndex === void 0 ? void 0 : _deDuplicated_modifyIndex.initialize) !== null && _deDuplicated_modifyIndex_initialize !== void 0 ? _deDuplicated_modifyIndex_initialize : "";
43
- deDuplicated[modifyIndex].initialize = originInitialize.concat(`
44
- ${initialize || ""}`);
45
- }
46
- }
47
- return deDuplicated.map(({ value, specifiers, initialize }) => `import ${createImportSpecifier(specifiers)} from '${value}';
48
- ${initialize || ""}`).join("\n");
49
- };
50
- const generateCode = async (appContext, config, entrypoints, api) => {
51
- const { internalDirectory, srcDirectory, appDirectory, internalDirAlias, internalSrcAlias, runtimeConfigFile } = appContext;
52
- const hookRunners = api.useHookRunners();
53
- const customRuntimeConfig = findExists(JS_EXTENSIONS.map((ext) => path.resolve(srcDirectory, `${runtimeConfigFile}${ext}`)));
54
- const importsStatemets = /* @__PURE__ */ new Map();
55
- await Promise.all(entrypoints.map((entrypoint) => generateEntryCode(entrypoint, customRuntimeConfig)));
56
- return {
57
- importsStatemets
58
- };
59
- async function generateEntryCode(entrypoint, customRuntimeConfig2) {
60
- const { entryName, isAutoMount } = entrypoint;
61
- if (isAutoMount) {
62
- const { imports } = await hookRunners.modifyEntryImports({
63
- entrypoint,
64
- imports: getDefaultImports({
65
- entrypoint,
66
- srcDirectory,
67
- appDirectory,
68
- internalSrcAlias,
69
- internalDirAlias,
70
- runtimeConfigFile,
71
- customRuntimeConfig: customRuntimeConfig2
72
- })
73
- });
74
- importsStatemets.set(entryName, imports);
75
- const entryFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_POINT_FILE_NAME}`);
76
- entrypoint.internalEntry = entryFile;
77
- }
78
- }
79
- };
80
- const generateIndexCode = async ({ appContext, api, entrypoints, config, importsStatemets, bundlerConfigs }) => {
81
- const hookRunners = api.useHookRunners();
82
- const { mountId } = config.html;
83
- const { internalDirectory, packageName, srcDirectory, runtimeConfigFile } = appContext;
84
- const customRuntimeConfig = findExists(JS_EXTENSIONS.map((ext) => path.resolve(srcDirectory, `${runtimeConfigFile}${ext}`)));
85
- await Promise.all(entrypoints.map(async (entrypoint) => {
86
- const { entryName, isMainEntry, isAutoMount, customBootstrap, fileSystemRoutes } = entrypoint;
87
- if (isAutoMount) {
88
- const { plugins } = await hookRunners.modifyEntryRuntimePlugins({
89
- entrypoint,
90
- plugins: [],
91
- bundlerConfigs
92
- });
93
- const { code: renderFunction } = await hookRunners.modifyEntryRenderFunction({
94
- entrypoint,
95
- code: templates.renderFunction({
96
- plugins,
97
- customBootstrap,
98
- customRuntimeConfig,
99
- fileSystemRoutes
100
- })
101
- });
102
- const { exportStatement } = await hookRunners.modifyEntryExport({
103
- entrypoint,
104
- exportStatement: "export default AppWrapper;"
105
- });
106
- const imports = importsStatemets.get(entryName);
107
- const code = templates.index({
108
- mountId,
109
- imports: createImportStatements(imports),
110
- renderFunction,
111
- exportStatement
112
- });
113
- const entryFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_POINT_FILE_NAME}`);
114
- if (config.source.enableAsyncEntry) {
115
- let rawAsyncEntryCode = `import('./${ENTRY_BOOTSTRAP_FILE_NAME}');`;
116
- const ssr = getEntryOptions(entryName, isMainEntry, config.server.ssr, config.server.ssrByEntries, packageName);
117
- if (ssr) {
118
- rawAsyncEntryCode = `
119
- export const ${SERVER_RENDER_FUNCTION_NAME} = async (...args) => {
120
- let entry = await ${rawAsyncEntryCode};
121
- if (entry.default instanceof Promise){
122
- entry = await entry.default;
123
- return entry.default.${SERVER_RENDER_FUNCTION_NAME}.apply(null, args);
124
- }
125
- return entry.${SERVER_RENDER_FUNCTION_NAME}.apply(null, args);
126
- };
127
- if(typeof window!=='undefined'){
128
- ${rawAsyncEntryCode}
129
- }
130
- `;
131
- }
132
- const { code: asyncEntryCode } = await hookRunners.modifyAsyncEntry({
133
- entrypoint,
134
- code: rawAsyncEntryCode
135
- });
136
- fs.outputFileSync(entryFile, asyncEntryCode, "utf8");
137
- const bootstrapFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_BOOTSTRAP_FILE_NAME}`);
138
- fs.outputFileSync(bootstrapFile, code, "utf8");
139
- } else {
140
- fs.outputFileSync(entryFile, code, "utf8");
141
- }
142
- }
143
- }));
144
- };
145
- export {
146
- createImportStatements,
147
- generateCode,
148
- generateIndexCode
149
- };
@@ -1,16 +0,0 @@
1
- import { IAppContext, PluginAPI } from '@modern-js/core';
2
- import type { Entrypoint } from '@modern-js/types';
3
- import { RspackConfig, WebpackConfig } from '@rsbuild/shared';
4
- import { AppNormalizedConfig, AppTools, ImportStatement } from '../../types';
5
- export declare const createImportStatements: (statements: ImportStatement[]) => string;
6
- export declare const generateCode: (appContext: IAppContext, config: AppNormalizedConfig<'shared'>, entrypoints: Entrypoint[], api: PluginAPI<AppTools<'shared'>>) => Promise<{
7
- importsStatemets: Map<string, ImportStatement[]>;
8
- }>;
9
- export declare const generateIndexCode: ({ appContext, api, entrypoints, config, importsStatemets, bundlerConfigs, }: {
10
- appContext: IAppContext;
11
- api: PluginAPI<AppTools<'shared'>>;
12
- entrypoints: Entrypoint[];
13
- config: AppNormalizedConfig<'shared'>;
14
- importsStatemets: Map<string, ImportStatement[]>;
15
- bundlerConfigs?: RspackConfig[] | WebpackConfig[] | undefined;
16
- }) => Promise<void>;