@modern-js/app-tools 2.0.2 → 2.0.3-alpha.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.
- package/dist/js/modern/analyze/generateCode.js +4 -81
- package/dist/js/modern/analyze/index.js +24 -10
- package/dist/js/modern/analyze/nestedRoutes.js +2 -15
- package/dist/js/modern/analyze/templates.js +26 -56
- package/dist/js/modern/analyze/utils.js +8 -0
- package/dist/js/modern/config/initial/inits.js +1 -9
- package/dist/js/modern/index.js +6 -7
- package/dist/js/modern/locale/en.js +2 -1
- package/dist/js/modern/locale/zh.js +2 -1
- package/dist/js/node/analyze/generateCode.js +2 -77
- package/dist/js/node/analyze/index.js +22 -8
- package/dist/js/node/analyze/nestedRoutes.js +1 -14
- package/dist/js/node/analyze/templates.js +26 -56
- package/dist/js/node/analyze/utils.js +9 -0
- package/dist/js/node/config/initial/inits.js +1 -9
- package/dist/js/node/index.js +2 -3
- package/dist/js/node/locale/en.js +2 -1
- package/dist/js/node/locale/zh.js +2 -1
- package/dist/js/treeshaking/analyze/generateCode.js +21 -148
- package/dist/js/treeshaking/analyze/index.js +22 -15
- package/dist/js/treeshaking/analyze/nestedRoutes.js +26 -77
- package/dist/js/treeshaking/analyze/templates.js +88 -72
- package/dist/js/treeshaking/analyze/utils.js +4 -1
- package/dist/js/treeshaking/config/initial/inits.js +1 -12
- package/dist/js/treeshaking/index.js +8 -9
- package/dist/js/treeshaking/locale/en.js +2 -1
- package/dist/js/treeshaking/locale/zh.js +2 -1
- package/dist/types/analyze/utils.d.ts +2 -1
- package/dist/types/locale/en.d.ts +1 -0
- package/dist/types/locale/index.d.ts +2 -0
- package/dist/types/locale/zh.d.ts +1 -0
- package/package.json +10 -10
- package/dist/js/modern/analyze/Builder.js +0 -39
- package/dist/js/node/analyze/Builder.js +0 -64
- package/dist/js/treeshaking/analyze/Builder.js +0 -199
- package/dist/types/analyze/Builder.d.ts +0 -8
|
@@ -22,29 +22,20 @@ import path from "path";
|
|
|
22
22
|
import {
|
|
23
23
|
fs,
|
|
24
24
|
getEntryOptions,
|
|
25
|
-
LOADER_ROUTES_DIR,
|
|
26
25
|
logger
|
|
27
26
|
} from "@modern-js/utils";
|
|
28
27
|
import {
|
|
29
28
|
useResolvedConfigContext
|
|
30
29
|
} from "@modern-js/core";
|
|
31
|
-
import { isDevCommand } from "../utils/commands";
|
|
32
30
|
import * as templates from "./templates";
|
|
33
31
|
import { getClientRoutes, getClientRoutesLegacy } from "./getClientRoutes";
|
|
34
32
|
import {
|
|
35
33
|
FILE_SYSTEM_ROUTES_FILE_NAME,
|
|
36
34
|
ENTRY_POINT_FILE_NAME,
|
|
37
|
-
ENTRY_BOOTSTRAP_FILE_NAME
|
|
38
|
-
TEMP_LOADERS_DIR
|
|
35
|
+
ENTRY_BOOTSTRAP_FILE_NAME
|
|
39
36
|
} from "./constants";
|
|
40
|
-
import { getDefaultImports } from "./utils";
|
|
37
|
+
import { getDefaultImports, getServerLoadersFile } from "./utils";
|
|
41
38
|
import { walk } from "./nestedRoutes";
|
|
42
|
-
import { loaderBuilder, serverLoaderBuilder } from "./Builder";
|
|
43
|
-
const loader = {
|
|
44
|
-
".js": "jsx",
|
|
45
|
-
".ts": "tsx"
|
|
46
|
-
};
|
|
47
|
-
const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
48
39
|
const createImportSpecifier = (specifiers) => {
|
|
49
40
|
let defaults = "";
|
|
50
41
|
const named = [];
|
|
@@ -92,49 +83,6 @@ ${initialize || ""}`
|
|
|
92
83
|
${initialize || ""}`
|
|
93
84
|
).join("\n");
|
|
94
85
|
};
|
|
95
|
-
const buildLoader = (entry, outfile) => __async(void 0, null, function* () {
|
|
96
|
-
yield loaderBuilder.build({
|
|
97
|
-
format: "esm",
|
|
98
|
-
platform: "browser",
|
|
99
|
-
target: "esnext",
|
|
100
|
-
loader,
|
|
101
|
-
watch: isDevCommand() && {},
|
|
102
|
-
bundle: true,
|
|
103
|
-
logLevel: "error",
|
|
104
|
-
entryPoints: [entry],
|
|
105
|
-
outfile,
|
|
106
|
-
plugins: [
|
|
107
|
-
{
|
|
108
|
-
name: "make-all-packages-external",
|
|
109
|
-
setup(build) {
|
|
110
|
-
build.onResolve({ filter: EXTERNAL_REGEXP }, (args) => {
|
|
111
|
-
let external = true;
|
|
112
|
-
if (args.kind === "entry-point") {
|
|
113
|
-
external = false;
|
|
114
|
-
}
|
|
115
|
-
return {
|
|
116
|
-
path: args.path,
|
|
117
|
-
external
|
|
118
|
-
};
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
]
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
const buildServerLoader = (entry, outfile) => __async(void 0, null, function* () {
|
|
126
|
-
yield serverLoaderBuilder.build({
|
|
127
|
-
format: "cjs",
|
|
128
|
-
platform: "node",
|
|
129
|
-
target: "esnext",
|
|
130
|
-
loader,
|
|
131
|
-
watch: isDevCommand() && {},
|
|
132
|
-
bundle: true,
|
|
133
|
-
logLevel: "error",
|
|
134
|
-
entryPoints: [entry],
|
|
135
|
-
outfile
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
86
|
const generateCode = (appContext, config, entrypoints, api) => __async(void 0, null, function* () {
|
|
139
87
|
var _a, _b, _c;
|
|
140
88
|
const {
|
|
@@ -225,16 +173,9 @@ const generateCode = (appContext, config, entrypoints, api) => __async(void 0, n
|
|
|
225
173
|
})
|
|
226
174
|
});
|
|
227
175
|
if (entrypoint.nestedRoutesEntry) {
|
|
228
|
-
const routesServerFile =
|
|
176
|
+
const routesServerFile = getServerLoadersFile(
|
|
229
177
|
internalDirectory,
|
|
230
|
-
entryName
|
|
231
|
-
"route-server-loaders.js"
|
|
232
|
-
);
|
|
233
|
-
const outputRoutesServerFile = path.join(
|
|
234
|
-
distDirectory,
|
|
235
|
-
LOADER_ROUTES_DIR,
|
|
236
|
-
entryName,
|
|
237
|
-
"index.js"
|
|
178
|
+
entryName
|
|
238
179
|
);
|
|
239
180
|
const code3 = templates.routesForServer({
|
|
240
181
|
routes,
|
|
@@ -243,24 +184,6 @@ const generateCode = (appContext, config, entrypoints, api) => __async(void 0, n
|
|
|
243
184
|
});
|
|
244
185
|
yield fs.ensureFile(routesServerFile);
|
|
245
186
|
yield fs.writeFile(routesServerFile, code3);
|
|
246
|
-
const loaderEntryFile = path.join(
|
|
247
|
-
internalDirectory,
|
|
248
|
-
entryName,
|
|
249
|
-
TEMP_LOADERS_DIR,
|
|
250
|
-
"entry.js"
|
|
251
|
-
);
|
|
252
|
-
const loaderIndexFile = path.join(
|
|
253
|
-
internalDirectory,
|
|
254
|
-
entryName,
|
|
255
|
-
TEMP_LOADERS_DIR,
|
|
256
|
-
"index.js"
|
|
257
|
-
);
|
|
258
|
-
if (yield fs.pathExists(loaderEntryFile)) {
|
|
259
|
-
yield buildLoader(loaderEntryFile, loaderIndexFile);
|
|
260
|
-
}
|
|
261
|
-
if (yield fs.pathExists(routesServerFile)) {
|
|
262
|
-
yield buildServerLoader(routesServerFile, outputRoutesServerFile);
|
|
263
|
-
}
|
|
264
187
|
}
|
|
265
188
|
fs.outputFileSync(
|
|
266
189
|
path.resolve(
|
|
@@ -38,7 +38,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
38
38
|
});
|
|
39
39
|
};
|
|
40
40
|
import * as path from "path";
|
|
41
|
-
import { createDebugger, findExists, fs, isApiOnly } from "@modern-js/utils";
|
|
41
|
+
import { createDebugger, findExists, fs, isApiOnly, isUseSSRBundle } from "@modern-js/utils";
|
|
42
42
|
import { cloneDeep } from "@modern-js/utils/lodash";
|
|
43
43
|
import { createBuilderForModern } from "../builder";
|
|
44
44
|
import { printInstructions } from "../utils/printInstructions";
|
|
@@ -47,12 +47,11 @@ import { emitResolvedConfig } from "../utils/config";
|
|
|
47
47
|
import { getCommand } from "../utils/commands";
|
|
48
48
|
import { initialNormalizedConfig } from "../config";
|
|
49
49
|
import {
|
|
50
|
-
|
|
50
|
+
getServerLoadersFile,
|
|
51
51
|
isPageComponentFile,
|
|
52
52
|
parseModule,
|
|
53
53
|
replaceWithAlias
|
|
54
54
|
} from "./utils";
|
|
55
|
-
import { loaderBuilder, serverLoaderBuilder } from "./Builder";
|
|
56
55
|
import {
|
|
57
56
|
APP_CONFIG_NAME,
|
|
58
57
|
APP_INIT_EXPORTED,
|
|
@@ -207,6 +206,27 @@ var analyze_default = () => ({
|
|
|
207
206
|
watchFiles() {
|
|
208
207
|
return pagesDir;
|
|
209
208
|
},
|
|
209
|
+
config() {
|
|
210
|
+
return {
|
|
211
|
+
tools: {
|
|
212
|
+
webpackChain: (chain, { name, CHAIN_ID }) => {
|
|
213
|
+
const appContext = api.useAppContext();
|
|
214
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
|
215
|
+
const { entrypoints, internalDirectory } = appContext;
|
|
216
|
+
entrypoints.forEach((entrypoint) => {
|
|
217
|
+
const { entryName } = entrypoint;
|
|
218
|
+
if (entrypoint.nestedRoutesEntry && isUseSSRBundle(resolvedConfig)) {
|
|
219
|
+
const serverLoadersFile = getServerLoadersFile(
|
|
220
|
+
internalDirectory,
|
|
221
|
+
entryName
|
|
222
|
+
);
|
|
223
|
+
chain.entry(`${entryName}-server-loaders`).add(serverLoadersFile);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
},
|
|
210
230
|
resolvedConfig({ resolved }) {
|
|
211
231
|
const appContext = api.useAppContext();
|
|
212
232
|
const config = initialNormalizedConfig(resolved, appContext);
|
|
@@ -266,12 +286,6 @@ var analyze_default = () => ({
|
|
|
266
286
|
};
|
|
267
287
|
});
|
|
268
288
|
},
|
|
269
|
-
beforeRestart() {
|
|
270
|
-
return __async(this, null, function* () {
|
|
271
|
-
serverLoaderBuilder.stop();
|
|
272
|
-
loaderBuilder.stop();
|
|
273
|
-
});
|
|
274
|
-
},
|
|
275
289
|
fileChange(e) {
|
|
276
290
|
return __async(this, null, function* () {
|
|
277
291
|
const appContext = api.useAppContext();
|
|
@@ -280,7 +294,7 @@ var analyze_default = () => ({
|
|
|
280
294
|
const isPageFile = (name) => pagesDir.some((pageDir) => name.includes(pageDir));
|
|
281
295
|
const absoluteFilePath = path.resolve(appDirectory, filename);
|
|
282
296
|
const isRouteComponent = isPageFile(absoluteFilePath) && isPageComponentFile(absoluteFilePath);
|
|
283
|
-
if (isRouteComponent && (eventType === "add" || eventType === "unlink")
|
|
297
|
+
if (isRouteComponent && (eventType === "add" || eventType === "unlink")) {
|
|
284
298
|
const resolvedConfig = api.useResolvedConfigContext();
|
|
285
299
|
const { generateCode } = yield import("./generateCode");
|
|
286
300
|
const entrypoints = cloneDeep(originEntrypoints);
|
|
@@ -40,14 +40,8 @@ var __async = (__this, __arguments, generator) => {
|
|
|
40
40
|
import * as path from "path";
|
|
41
41
|
import { fs, getRouteId } from "@modern-js/utils";
|
|
42
42
|
import { JS_EXTENSIONS, NESTED_ROUTE } from "./constants";
|
|
43
|
-
import {
|
|
43
|
+
import { replaceWithAlias } from "./utils";
|
|
44
44
|
const conventionNames = Object.values(NESTED_ROUTE);
|
|
45
|
-
const getLoaderPath = (filename) => __async(void 0, null, function* () {
|
|
46
|
-
if (yield hasLoader(filename)) {
|
|
47
|
-
return filename;
|
|
48
|
-
}
|
|
49
|
-
return void 0;
|
|
50
|
-
});
|
|
51
45
|
const replaceDynamicPath = (routePath) => {
|
|
52
46
|
return routePath.replace(/\[(.*?)\]/g, ":$1");
|
|
53
47
|
};
|
|
@@ -118,10 +112,6 @@ const walk = (dirname, rootDir, alias, entryName) => __async(void 0, null, funct
|
|
|
118
112
|
}
|
|
119
113
|
if (itemWithoutExt === NESTED_ROUTE.LAYOUT_FILE) {
|
|
120
114
|
route._component = replaceWithAlias(alias.basename, itemPath, alias.name);
|
|
121
|
-
const loaderPath = yield getLoaderPath(itemPath);
|
|
122
|
-
if (loaderPath) {
|
|
123
|
-
route.loader = loaderPath;
|
|
124
|
-
}
|
|
125
115
|
}
|
|
126
116
|
if (itemWithoutExt === NESTED_ROUTE.PAGE_LOADER_FILE) {
|
|
127
117
|
pageLoaderFile = itemPath;
|
|
@@ -135,10 +125,7 @@ const walk = (dirname, rootDir, alias, entryName) => __async(void 0, null, funct
|
|
|
135
125
|
itemPath,
|
|
136
126
|
entryName
|
|
137
127
|
);
|
|
138
|
-
|
|
139
|
-
if (loaderPath) {
|
|
140
|
-
pageRoute.loader = loaderPath;
|
|
141
|
-
} else if (pageLoaderFile) {
|
|
128
|
+
if (pageLoaderFile) {
|
|
142
129
|
pageRoute.loader = pageLoaderFile;
|
|
143
130
|
}
|
|
144
131
|
(_b = route.children) == null ? void 0 : _b.push(pageRoute);
|
|
@@ -175,10 +175,9 @@ const routesForServer = ({
|
|
|
175
175
|
];`;
|
|
176
176
|
let importLoadersCode = "";
|
|
177
177
|
if (loaders.length > 0) {
|
|
178
|
-
importLoadersCode =
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
)} } from "${slash(loaderIndexFile)}"`;
|
|
178
|
+
importLoadersCode = loaders.map((loader, index2) => {
|
|
179
|
+
return `import loader_${index2} from "${loader}"`;
|
|
180
|
+
}).join("\n");
|
|
182
181
|
}
|
|
183
182
|
return `
|
|
184
183
|
${importLoadersCode}
|
|
@@ -197,12 +196,6 @@ const fileSystemRoutes = (_0) => __async(void 0, [_0], function* ({
|
|
|
197
196
|
const errors = [];
|
|
198
197
|
const loaders = [];
|
|
199
198
|
const loadersMap = {};
|
|
200
|
-
const loadersIndexFile = path.join(
|
|
201
|
-
internalDirAlias,
|
|
202
|
-
entryName,
|
|
203
|
-
TEMP_LOADERS_DIR,
|
|
204
|
-
"index.js"
|
|
205
|
-
);
|
|
206
199
|
const loadersMapFile = path.join(
|
|
207
200
|
internalDirectory,
|
|
208
201
|
entryName,
|
|
@@ -214,13 +207,17 @@ const fileSystemRoutes = (_0) => __async(void 0, [_0], function* ({
|
|
|
214
207
|
import loadable, { lazy as loadableLazy } from "@modern-js/runtime/loadable"
|
|
215
208
|
`;
|
|
216
209
|
let rootLayoutCode = ``;
|
|
217
|
-
let dataLoaderPath = "";
|
|
218
210
|
let componentLoaderPath = "";
|
|
219
|
-
|
|
220
|
-
|
|
211
|
+
const getDataLoaderPath = (loaderId) => {
|
|
212
|
+
if (!ssrMode)
|
|
213
|
+
return "";
|
|
214
|
+
let dataLoaderPath = require.resolve("@modern-js/plugin-data-loader/loader");
|
|
221
215
|
if (nestedRoutesEntry) {
|
|
222
|
-
dataLoaderPath = `${dataLoaderPath}?
|
|
216
|
+
dataLoaderPath = `${slash(dataLoaderPath)}?mapFile=${slash(loadersMapFile)}&loaderId=${loaderId}!`;
|
|
223
217
|
}
|
|
218
|
+
return dataLoaderPath;
|
|
219
|
+
};
|
|
220
|
+
if (ssrMode) {
|
|
224
221
|
componentLoaderPath = `${path.join(
|
|
225
222
|
__dirname,
|
|
226
223
|
"../builder/loaders/routerLoader"
|
|
@@ -250,7 +247,11 @@ const fileSystemRoutes = (_0) => __async(void 0, [_0], function* ({
|
|
|
250
247
|
loaders.push(route.loader);
|
|
251
248
|
const loaderId = loaders.length - 1;
|
|
252
249
|
loader = `loader_${loaderId}`;
|
|
253
|
-
loadersMap[loader] =
|
|
250
|
+
loadersMap[loader] = {
|
|
251
|
+
routeId: route.id,
|
|
252
|
+
filePath: route.loader,
|
|
253
|
+
inline: false
|
|
254
|
+
};
|
|
254
255
|
}
|
|
255
256
|
if (route._component) {
|
|
256
257
|
if (route.isRoot) {
|
|
@@ -306,48 +307,17 @@ const fileSystemRoutes = (_0) => __async(void 0, [_0], function* ({
|
|
|
306
307
|
`;
|
|
307
308
|
}).join("");
|
|
308
309
|
let importLoadersCode = "";
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
}).join("\n");
|
|
318
|
-
const loaderEntryFile = path.join(
|
|
319
|
-
internalDirectory,
|
|
320
|
-
entryName,
|
|
321
|
-
TEMP_LOADERS_DIR,
|
|
322
|
-
"entry.js"
|
|
323
|
-
);
|
|
324
|
-
yield fs.ensureFile(loaderEntryFile);
|
|
325
|
-
yield fs.writeFile(loaderEntryFile, loaderEntryCode);
|
|
326
|
-
yield fs.writeJSON(loadersMapFile, loadersMap);
|
|
327
|
-
yield Promise.all(
|
|
328
|
-
loaders.map((loader, index2) => __async(void 0, null, function* () {
|
|
329
|
-
const name = `loader_${index2}`;
|
|
330
|
-
const filename = path.join(
|
|
331
|
-
internalDirectory,
|
|
332
|
-
entryName,
|
|
333
|
-
TEMP_LOADERS_DIR,
|
|
334
|
-
`${name}.js`
|
|
335
|
-
);
|
|
336
|
-
let code = "";
|
|
337
|
-
if (loader.includes(".loader.")) {
|
|
338
|
-
code = `
|
|
339
|
-
export { default as ${name} } from '${slash(loader)}'
|
|
340
|
-
`;
|
|
341
|
-
} else {
|
|
342
|
-
code = `
|
|
343
|
-
export { loader as ${name} } from '${slash(loader)}'
|
|
344
|
-
`;
|
|
345
|
-
}
|
|
346
|
-
yield fs.ensureFile(filename);
|
|
347
|
-
yield fs.writeFile(filename, code);
|
|
348
|
-
}))
|
|
349
|
-
);
|
|
310
|
+
for (const [key, loaderInfo] of Object.entries(loadersMap)) {
|
|
311
|
+
if (loaderInfo.inline) {
|
|
312
|
+
importLoadersCode += `import { loader as ${key} } from "${getDataLoaderPath(key)}${loaderInfo.filePath}";
|
|
313
|
+
`;
|
|
314
|
+
} else {
|
|
315
|
+
importLoadersCode += `import ${key} from "${getDataLoaderPath(key)}${loaderInfo.filePath}";
|
|
316
|
+
`;
|
|
317
|
+
}
|
|
350
318
|
}
|
|
319
|
+
yield fs.ensureFile(loadersMapFile);
|
|
320
|
+
yield fs.writeJSON(loadersMapFile, loadersMap);
|
|
351
321
|
return `
|
|
352
322
|
${importLazyCode}
|
|
353
323
|
${rootLayoutCode}
|
|
@@ -139,8 +139,16 @@ const hasLoader = (filename) => __async(void 0, null, function* () {
|
|
|
139
139
|
});
|
|
140
140
|
return moduleExports.some((e) => e.n === LOADER_EXPORT_NAME);
|
|
141
141
|
});
|
|
142
|
+
const getServerLoadersFile = (internalDirectory, entryName) => {
|
|
143
|
+
return path.join(
|
|
144
|
+
internalDirectory,
|
|
145
|
+
entryName,
|
|
146
|
+
"route-server-loaders.js"
|
|
147
|
+
);
|
|
148
|
+
};
|
|
142
149
|
export {
|
|
143
150
|
getDefaultImports,
|
|
151
|
+
getServerLoadersFile,
|
|
144
152
|
hasLoader,
|
|
145
153
|
isNestedRouteComponent,
|
|
146
154
|
isPageComponentFile,
|
|
@@ -132,7 +132,7 @@ function initToolsConfig(config) {
|
|
|
132
132
|
]
|
|
133
133
|
}
|
|
134
134
|
};
|
|
135
|
-
const { tsChecker, tsLoader
|
|
135
|
+
const { tsChecker, tsLoader } = config.tools;
|
|
136
136
|
config.tools.tsChecker = applyOptionsChain(defaultTsChecker, tsChecker);
|
|
137
137
|
tsLoader && (config.tools.tsLoader = (tsLoaderConfig, utils) => {
|
|
138
138
|
applyOptionsChain(
|
|
@@ -144,14 +144,6 @@ function initToolsConfig(config) {
|
|
|
144
144
|
utils
|
|
145
145
|
);
|
|
146
146
|
});
|
|
147
|
-
config.tools.htmlPlugin = [
|
|
148
|
-
(config2) => __spreadProps(__spreadValues({}, config2), {
|
|
149
|
-
minify: typeof config2.minify === "object" ? __spreadProps(__spreadValues({}, config2.minify), {
|
|
150
|
-
removeComments: false
|
|
151
|
-
}) : config2.minify
|
|
152
|
-
}),
|
|
153
|
-
...Array.isArray(htmlPlugin) ? htmlPlugin : htmlPlugin ? [htmlPlugin] : []
|
|
154
|
-
];
|
|
155
147
|
}
|
|
156
148
|
export {
|
|
157
149
|
initHtmlConfig,
|
package/dist/js/modern/index.js
CHANGED
|
@@ -38,11 +38,11 @@ var __async = (__this, __arguments, generator) => {
|
|
|
38
38
|
});
|
|
39
39
|
};
|
|
40
40
|
import path from "path";
|
|
41
|
-
import
|
|
41
|
+
import lintPlugin from "@modern-js/plugin-lint";
|
|
42
42
|
import { cleanRequireCache, emptyDir, Import } from "@modern-js/utils";
|
|
43
43
|
import { castArray } from "@modern-js/utils/lodash";
|
|
44
|
-
import
|
|
45
|
-
import
|
|
44
|
+
import analyzePlugin from "./analyze";
|
|
45
|
+
import initializePlugin from "./initialize";
|
|
46
46
|
import { hooks } from "./hooks";
|
|
47
47
|
import { i18n, localeKeys } from "./locale";
|
|
48
48
|
import { getLocaleLanguage } from "./utils/language";
|
|
@@ -83,7 +83,6 @@ const buildCommand = (program, api) => __async(void 0, null, function* () {
|
|
|
83
83
|
const buildProgram = program.command("build").usage("[options]").description(i18n.t(localeKeys.command.build.describe)).option("-c --config <config>", i18n.t(localeKeys.command.shared.config)).option("--analyze", i18n.t(localeKeys.command.shared.analyze)).action((options) => __async(void 0, null, function* () {
|
|
84
84
|
const { build } = yield import("./commands/build");
|
|
85
85
|
yield build(api, options);
|
|
86
|
-
process.exit(0);
|
|
87
86
|
}));
|
|
88
87
|
for (const platformBuilder of platformBuilders) {
|
|
89
88
|
const platforms = castArray(platformBuilder.platform);
|
|
@@ -112,7 +111,7 @@ var src_default = () => ({
|
|
|
112
111
|
"@modern-js/plugin-polyfill"
|
|
113
112
|
],
|
|
114
113
|
registerHook: hooks,
|
|
115
|
-
usePlugins: [
|
|
114
|
+
usePlugins: [initializePlugin(), analyzePlugin(), lintPlugin()],
|
|
116
115
|
setup: (api) => {
|
|
117
116
|
const locale = getLocaleLanguage();
|
|
118
117
|
i18n.changeLanguage({ locale });
|
|
@@ -138,12 +137,12 @@ var src_default = () => ({
|
|
|
138
137
|
yield deploy(api, options);
|
|
139
138
|
process.exit(0);
|
|
140
139
|
}));
|
|
141
|
-
program.command("new").usage("[options]").description(i18n.t(localeKeys.command.new.describe)).option("-d, --debug", i18n.t(localeKeys.command.new.debug), false).option(
|
|
140
|
+
program.command("new").usage("[options]").description(i18n.t(localeKeys.command.new.describe)).option("--lang <lang>", i18n.t(localeKeys.command.new.lang)).option("-d, --debug", i18n.t(localeKeys.command.new.debug), false).option(
|
|
142
141
|
"-c, --config <config>",
|
|
143
142
|
i18n.t(localeKeys.command.new.config)
|
|
144
143
|
).option("--dist-tag <tag>", i18n.t(localeKeys.command.new.distTag)).option("--registry", i18n.t(localeKeys.command.new.registry)).action((options) => __async(this, null, function* () {
|
|
145
144
|
const { MWANewAction } = yield import("@modern-js/new-action");
|
|
146
|
-
yield MWANewAction(__spreadProps(__spreadValues({}, options), { locale }));
|
|
145
|
+
yield MWANewAction(__spreadProps(__spreadValues({}, options), { locale: options.lang || locale }));
|
|
147
146
|
}));
|
|
148
147
|
program.command("inspect").description("inspect internal webpack config").option(
|
|
149
148
|
`--env <env>`,
|
|
@@ -19,7 +19,8 @@ const EN_LOCALE = {
|
|
|
19
19
|
debug: "using debug mode to log something",
|
|
20
20
|
config: "set default generator config(json string)",
|
|
21
21
|
distTag: `use specified tag version for it's generator`,
|
|
22
|
-
registry: "set npm registry url to run npm command"
|
|
22
|
+
registry: "set npm registry url to run npm command",
|
|
23
|
+
lang: "set new command language(en or zh)"
|
|
23
24
|
},
|
|
24
25
|
inspect: {
|
|
25
26
|
env: "specify env mode",
|
|
@@ -19,7 +19,8 @@ const ZH_LOCALE = {
|
|
|
19
19
|
debug: "开启 Debug 模式,打印调试日志信息",
|
|
20
20
|
config: "生成器运行默认配置(JSON 字符串)",
|
|
21
21
|
distTag: "生成器使用特殊的 npm Tag 版本",
|
|
22
|
-
registry: "生成器运行过程中定制 npm Registry"
|
|
22
|
+
registry: "生成器运行过程中定制 npm Registry",
|
|
23
|
+
lang: "设置 new 命令执行语言(zh 或者 en)"
|
|
23
24
|
},
|
|
24
25
|
inspect: {
|
|
25
26
|
env: "查看指定环境下的配置",
|
|
@@ -50,18 +50,11 @@ module.exports = __toCommonJS(generateCode_exports);
|
|
|
50
50
|
var import_path = __toESM(require("path"));
|
|
51
51
|
var import_utils = require("@modern-js/utils");
|
|
52
52
|
var import_core = require("@modern-js/core");
|
|
53
|
-
var import_commands = require("../utils/commands");
|
|
54
53
|
var templates = __toESM(require("./templates"));
|
|
55
54
|
var import_getClientRoutes = require("./getClientRoutes");
|
|
56
55
|
var import_constants = require("./constants");
|
|
57
56
|
var import_utils2 = require("./utils");
|
|
58
57
|
var import_nestedRoutes = require("./nestedRoutes");
|
|
59
|
-
var import_Builder = require("./Builder");
|
|
60
|
-
const loader = {
|
|
61
|
-
".js": "jsx",
|
|
62
|
-
".ts": "tsx"
|
|
63
|
-
};
|
|
64
|
-
const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
65
58
|
const createImportSpecifier = (specifiers) => {
|
|
66
59
|
let defaults = "";
|
|
67
60
|
const named = [];
|
|
@@ -109,49 +102,6 @@ ${initialize || ""}`
|
|
|
109
102
|
${initialize || ""}`
|
|
110
103
|
).join("\n");
|
|
111
104
|
};
|
|
112
|
-
const buildLoader = (entry, outfile) => __async(void 0, null, function* () {
|
|
113
|
-
yield import_Builder.loaderBuilder.build({
|
|
114
|
-
format: "esm",
|
|
115
|
-
platform: "browser",
|
|
116
|
-
target: "esnext",
|
|
117
|
-
loader,
|
|
118
|
-
watch: (0, import_commands.isDevCommand)() && {},
|
|
119
|
-
bundle: true,
|
|
120
|
-
logLevel: "error",
|
|
121
|
-
entryPoints: [entry],
|
|
122
|
-
outfile,
|
|
123
|
-
plugins: [
|
|
124
|
-
{
|
|
125
|
-
name: "make-all-packages-external",
|
|
126
|
-
setup(build) {
|
|
127
|
-
build.onResolve({ filter: EXTERNAL_REGEXP }, (args) => {
|
|
128
|
-
let external = true;
|
|
129
|
-
if (args.kind === "entry-point") {
|
|
130
|
-
external = false;
|
|
131
|
-
}
|
|
132
|
-
return {
|
|
133
|
-
path: args.path,
|
|
134
|
-
external
|
|
135
|
-
};
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
]
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
const buildServerLoader = (entry, outfile) => __async(void 0, null, function* () {
|
|
143
|
-
yield import_Builder.serverLoaderBuilder.build({
|
|
144
|
-
format: "cjs",
|
|
145
|
-
platform: "node",
|
|
146
|
-
target: "esnext",
|
|
147
|
-
loader,
|
|
148
|
-
watch: (0, import_commands.isDevCommand)() && {},
|
|
149
|
-
bundle: true,
|
|
150
|
-
logLevel: "error",
|
|
151
|
-
entryPoints: [entry],
|
|
152
|
-
outfile
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
105
|
const generateCode = (appContext, config, entrypoints, api) => __async(void 0, null, function* () {
|
|
156
106
|
var _a, _b, _c;
|
|
157
107
|
const {
|
|
@@ -242,16 +192,9 @@ const generateCode = (appContext, config, entrypoints, api) => __async(void 0, n
|
|
|
242
192
|
})
|
|
243
193
|
});
|
|
244
194
|
if (entrypoint.nestedRoutesEntry) {
|
|
245
|
-
const routesServerFile =
|
|
195
|
+
const routesServerFile = (0, import_utils2.getServerLoadersFile)(
|
|
246
196
|
internalDirectory,
|
|
247
|
-
entryName
|
|
248
|
-
"route-server-loaders.js"
|
|
249
|
-
);
|
|
250
|
-
const outputRoutesServerFile = import_path.default.join(
|
|
251
|
-
distDirectory,
|
|
252
|
-
import_utils.LOADER_ROUTES_DIR,
|
|
253
|
-
entryName,
|
|
254
|
-
"index.js"
|
|
197
|
+
entryName
|
|
255
198
|
);
|
|
256
199
|
const code3 = templates.routesForServer({
|
|
257
200
|
routes,
|
|
@@ -260,24 +203,6 @@ const generateCode = (appContext, config, entrypoints, api) => __async(void 0, n
|
|
|
260
203
|
});
|
|
261
204
|
yield import_utils.fs.ensureFile(routesServerFile);
|
|
262
205
|
yield import_utils.fs.writeFile(routesServerFile, code3);
|
|
263
|
-
const loaderEntryFile = import_path.default.join(
|
|
264
|
-
internalDirectory,
|
|
265
|
-
entryName,
|
|
266
|
-
import_constants.TEMP_LOADERS_DIR,
|
|
267
|
-
"entry.js"
|
|
268
|
-
);
|
|
269
|
-
const loaderIndexFile = import_path.default.join(
|
|
270
|
-
internalDirectory,
|
|
271
|
-
entryName,
|
|
272
|
-
import_constants.TEMP_LOADERS_DIR,
|
|
273
|
-
"index.js"
|
|
274
|
-
);
|
|
275
|
-
if (yield import_utils.fs.pathExists(loaderEntryFile)) {
|
|
276
|
-
yield buildLoader(loaderEntryFile, loaderIndexFile);
|
|
277
|
-
}
|
|
278
|
-
if (yield import_utils.fs.pathExists(routesServerFile)) {
|
|
279
|
-
yield buildServerLoader(routesServerFile, outputRoutesServerFile);
|
|
280
|
-
}
|
|
281
206
|
}
|
|
282
207
|
import_utils.fs.outputFileSync(
|
|
283
208
|
import_path.default.resolve(
|
|
@@ -73,7 +73,6 @@ var import_config = require("../utils/config");
|
|
|
73
73
|
var import_commands = require("../utils/commands");
|
|
74
74
|
var import_config2 = require("../config");
|
|
75
75
|
var import_utils2 = require("./utils");
|
|
76
|
-
var import_Builder = require("./Builder");
|
|
77
76
|
var import_constants = require("./constants");
|
|
78
77
|
const debug = (0, import_utils.createDebugger)("plugin-analyze");
|
|
79
78
|
var analyze_default = () => ({
|
|
@@ -224,6 +223,27 @@ var analyze_default = () => ({
|
|
|
224
223
|
watchFiles() {
|
|
225
224
|
return pagesDir;
|
|
226
225
|
},
|
|
226
|
+
config() {
|
|
227
|
+
return {
|
|
228
|
+
tools: {
|
|
229
|
+
webpackChain: (chain, { name, CHAIN_ID }) => {
|
|
230
|
+
const appContext = api.useAppContext();
|
|
231
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
|
232
|
+
const { entrypoints, internalDirectory } = appContext;
|
|
233
|
+
entrypoints.forEach((entrypoint) => {
|
|
234
|
+
const { entryName } = entrypoint;
|
|
235
|
+
if (entrypoint.nestedRoutesEntry && (0, import_utils.isUseSSRBundle)(resolvedConfig)) {
|
|
236
|
+
const serverLoadersFile = (0, import_utils2.getServerLoadersFile)(
|
|
237
|
+
internalDirectory,
|
|
238
|
+
entryName
|
|
239
|
+
);
|
|
240
|
+
chain.entry(`${entryName}-server-loaders`).add(serverLoadersFile);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
},
|
|
227
247
|
resolvedConfig({ resolved }) {
|
|
228
248
|
const appContext = api.useAppContext();
|
|
229
249
|
const config = (0, import_config2.initialNormalizedConfig)(resolved, appContext);
|
|
@@ -283,12 +303,6 @@ var analyze_default = () => ({
|
|
|
283
303
|
};
|
|
284
304
|
});
|
|
285
305
|
},
|
|
286
|
-
beforeRestart() {
|
|
287
|
-
return __async(this, null, function* () {
|
|
288
|
-
import_Builder.serverLoaderBuilder.stop();
|
|
289
|
-
import_Builder.loaderBuilder.stop();
|
|
290
|
-
});
|
|
291
|
-
},
|
|
292
306
|
fileChange(e) {
|
|
293
307
|
return __async(this, null, function* () {
|
|
294
308
|
const appContext = api.useAppContext();
|
|
@@ -297,7 +311,7 @@ var analyze_default = () => ({
|
|
|
297
311
|
const isPageFile = (name) => pagesDir.some((pageDir) => name.includes(pageDir));
|
|
298
312
|
const absoluteFilePath = path.resolve(appDirectory, filename);
|
|
299
313
|
const isRouteComponent = isPageFile(absoluteFilePath) && (0, import_utils2.isPageComponentFile)(absoluteFilePath);
|
|
300
|
-
if (isRouteComponent && (eventType === "add" || eventType === "unlink")
|
|
314
|
+
if (isRouteComponent && (eventType === "add" || eventType === "unlink")) {
|
|
301
315
|
const resolvedConfig = api.useResolvedConfigContext();
|
|
302
316
|
const { generateCode } = yield Promise.resolve().then(() => __toESM(require("./generateCode")));
|
|
303
317
|
const entrypoints = (0, import_lodash.cloneDeep)(originEntrypoints);
|
|
@@ -68,12 +68,6 @@ var import_utils = require("@modern-js/utils");
|
|
|
68
68
|
var import_constants = require("./constants");
|
|
69
69
|
var import_utils2 = require("./utils");
|
|
70
70
|
const conventionNames = Object.values(import_constants.NESTED_ROUTE);
|
|
71
|
-
const getLoaderPath = (filename) => __async(void 0, null, function* () {
|
|
72
|
-
if (yield (0, import_utils2.hasLoader)(filename)) {
|
|
73
|
-
return filename;
|
|
74
|
-
}
|
|
75
|
-
return void 0;
|
|
76
|
-
});
|
|
77
71
|
const replaceDynamicPath = (routePath) => {
|
|
78
72
|
return routePath.replace(/\[(.*?)\]/g, ":$1");
|
|
79
73
|
};
|
|
@@ -144,10 +138,6 @@ const walk = (dirname, rootDir, alias, entryName) => __async(void 0, null, funct
|
|
|
144
138
|
}
|
|
145
139
|
if (itemWithoutExt === import_constants.NESTED_ROUTE.LAYOUT_FILE) {
|
|
146
140
|
route._component = (0, import_utils2.replaceWithAlias)(alias.basename, itemPath, alias.name);
|
|
147
|
-
const loaderPath = yield getLoaderPath(itemPath);
|
|
148
|
-
if (loaderPath) {
|
|
149
|
-
route.loader = loaderPath;
|
|
150
|
-
}
|
|
151
141
|
}
|
|
152
142
|
if (itemWithoutExt === import_constants.NESTED_ROUTE.PAGE_LOADER_FILE) {
|
|
153
143
|
pageLoaderFile = itemPath;
|
|
@@ -161,10 +151,7 @@ const walk = (dirname, rootDir, alias, entryName) => __async(void 0, null, funct
|
|
|
161
151
|
itemPath,
|
|
162
152
|
entryName
|
|
163
153
|
);
|
|
164
|
-
|
|
165
|
-
if (loaderPath) {
|
|
166
|
-
pageRoute.loader = loaderPath;
|
|
167
|
-
} else if (pageLoaderFile) {
|
|
154
|
+
if (pageLoaderFile) {
|
|
168
155
|
pageRoute.loader = pageLoaderFile;
|
|
169
156
|
}
|
|
170
157
|
(_b = route.children) == null ? void 0 : _b.push(pageRoute);
|