@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.
- package/dist/cjs/builder/generator/createBuilderOptions.js +18 -1
- package/dist/cjs/builder/shared/builderPlugins/adapterHtml.js +2 -2
- package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +2 -2
- package/dist/cjs/commands/index.js +2 -2
- package/dist/cjs/commands/serve.js +1 -0
- package/dist/cjs/config/default.js +2 -0
- package/dist/cjs/config/legacy/createSourceConfig.js +2 -1
- package/dist/cjs/hooks.js +2 -5
- package/dist/cjs/plugins/analyze/constants.js +3 -18
- package/dist/cjs/plugins/analyze/getFileSystemEntry.js +36 -11
- package/dist/cjs/plugins/analyze/index.js +7 -64
- package/dist/cjs/plugins/analyze/templates.js +2 -55
- package/dist/cjs/plugins/analyze/utils.js +0 -86
- package/dist/esm/builder/generator/createBuilderOptions.js +21 -1
- package/dist/esm/builder/shared/builderPlugins/adapterHtml.js +1 -1
- package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +1 -1
- package/dist/esm/commands/index.js +1 -1
- package/dist/esm/commands/serve.js +1 -0
- package/dist/esm/config/default.js +2 -0
- package/dist/esm/config/legacy/createSourceConfig.js +2 -1
- package/dist/esm/hooks.js +2 -5
- package/dist/esm/plugins/analyze/constants.js +2 -12
- package/dist/esm/plugins/analyze/getFileSystemEntry.js +57 -14
- package/dist/esm/plugins/analyze/index.js +25 -128
- package/dist/esm/plugins/analyze/templates.js +1 -17
- package/dist/esm/plugins/analyze/utils.js +1 -87
- package/dist/esm-node/builder/generator/createBuilderOptions.js +18 -1
- package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.js +1 -1
- package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +1 -1
- package/dist/esm-node/commands/index.js +1 -1
- package/dist/esm-node/commands/serve.js +1 -0
- package/dist/esm-node/config/default.js +2 -0
- package/dist/esm-node/config/legacy/createSourceConfig.js +2 -1
- package/dist/esm-node/hooks.js +2 -5
- package/dist/esm-node/plugins/analyze/constants.js +2 -12
- package/dist/esm-node/plugins/analyze/getFileSystemEntry.js +37 -12
- package/dist/esm-node/plugins/analyze/index.js +10 -67
- package/dist/esm-node/plugins/analyze/templates.js +1 -52
- package/dist/esm-node/plugins/analyze/utils.js +1 -86
- package/dist/types/builder/builder-webpack/createCopyPattern.d.ts +2 -2
- package/dist/types/builder/generator/createBuilderOptions.d.ts +1 -2
- package/dist/types/builder/generator/getBuilderTargets.d.ts +1 -1
- package/dist/types/builder/generator/index.d.ts +1 -2
- package/dist/types/builder/shared/builderPlugins/adapterBasic.d.ts +1 -1
- package/dist/types/builder/shared/builderPlugins/adapterHtml.d.ts +1 -1
- package/dist/types/builder/shared/builderPlugins/adapterSSR.d.ts +1 -1
- package/dist/types/builder/shared/builderPlugins/adapterWorker.d.ts +1 -1
- package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +1 -1
- package/dist/types/commands/inspect.d.ts +1 -1
- package/dist/types/plugins/analyze/constants.d.ts +1 -6
- package/dist/types/plugins/analyze/templates.d.ts +0 -14
- package/dist/types/plugins/analyze/utils.d.ts +0 -11
- package/dist/types/types/config/source.d.ts +5 -0
- package/dist/types/types/hooks.d.ts +7 -27
- package/dist/types/types/index.d.ts +1 -1
- package/dist/types/types/legacyConfig/output.d.ts +1 -1
- package/dist/types/types/legacyConfig/source.d.ts +4 -0
- package/dist/types/utils/register.d.ts +1 -1
- package/package.json +17 -18
- package/dist/cjs/plugins/analyze/generateCode.js +0 -185
- package/dist/esm/plugins/analyze/generateCode.js +0 -296
- package/dist/esm-node/plugins/analyze/generateCode.js +0 -149
- 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>;
|