@modern-js/app-tools 2.39.0 → 2.39.2-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.
Files changed (75) hide show
  1. package/dist/cjs/analyze/nestedRoutes.js +2 -1
  2. package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +14 -1
  3. package/dist/cjs/config/initialize/inits.js +39 -43
  4. package/dist/esm/analyze/nestedRoutes.js +2 -1
  5. package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +17 -2
  6. package/dist/esm/config/initialize/inits.js +44 -49
  7. package/dist/esm-node/analyze/nestedRoutes.js +2 -1
  8. package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +14 -1
  9. package/dist/esm-node/config/initialize/inits.js +38 -43
  10. package/dist/js/modern/analyze/constants.js +15 -0
  11. package/dist/js/modern/analyze/generateCode.js +179 -0
  12. package/dist/js/modern/analyze/getBundleEntry.js +75 -0
  13. package/dist/js/modern/analyze/getClientRoutes.js +219 -0
  14. package/dist/js/modern/analyze/getFileSystemEntry.js +74 -0
  15. package/dist/js/modern/analyze/getHtmlTemplate.js +82 -0
  16. package/dist/js/modern/analyze/getServerRoutes.js +192 -0
  17. package/dist/js/modern/analyze/index.js +148 -0
  18. package/dist/js/modern/analyze/isDefaultExportFunction.js +32 -0
  19. package/dist/js/modern/analyze/makeLegalIdentifier.js +16 -0
  20. package/dist/js/modern/analyze/templates.js +88 -0
  21. package/dist/js/modern/analyze/utils.js +92 -0
  22. package/dist/js/modern/commands/build.js +154 -0
  23. package/dist/js/modern/commands/deploy.js +5 -0
  24. package/dist/js/modern/commands/dev.js +95 -0
  25. package/dist/js/modern/commands/index.js +3 -0
  26. package/dist/js/modern/commands/inspect.js +69 -0
  27. package/dist/js/modern/commands/start.js +31 -0
  28. package/dist/js/modern/exports/server.js +1 -0
  29. package/dist/js/modern/hooks.js +21 -0
  30. package/dist/js/modern/index.js +109 -0
  31. package/dist/js/modern/locale/en.js +35 -0
  32. package/dist/js/modern/locale/index.js +9 -0
  33. package/dist/js/modern/locale/zh.js +35 -0
  34. package/dist/js/modern/utils/config.js +78 -0
  35. package/dist/js/modern/utils/createCompiler.js +61 -0
  36. package/dist/js/modern/utils/createServer.js +18 -0
  37. package/dist/js/modern/utils/getSpecifiedEntries.js +36 -0
  38. package/dist/js/modern/utils/language.js +5 -0
  39. package/dist/js/modern/utils/printInstructions.js +11 -0
  40. package/dist/js/modern/utils/routes.js +15 -0
  41. package/dist/js/modern/utils/types.js +0 -0
  42. package/dist/js/node/analyze/constants.js +36 -0
  43. package/dist/js/node/analyze/generateCode.js +208 -0
  44. package/dist/js/node/analyze/getBundleEntry.js +89 -0
  45. package/dist/js/node/analyze/getClientRoutes.js +241 -0
  46. package/dist/js/node/analyze/getFileSystemEntry.js +90 -0
  47. package/dist/js/node/analyze/getHtmlTemplate.js +106 -0
  48. package/dist/js/node/analyze/getServerRoutes.js +208 -0
  49. package/dist/js/node/analyze/index.js +178 -0
  50. package/dist/js/node/analyze/isDefaultExportFunction.js +50 -0
  51. package/dist/js/node/analyze/makeLegalIdentifier.js +24 -0
  52. package/dist/js/node/analyze/templates.js +106 -0
  53. package/dist/js/node/analyze/utils.js +113 -0
  54. package/dist/js/node/commands/build.js +174 -0
  55. package/dist/js/node/commands/deploy.js +14 -0
  56. package/dist/js/node/commands/dev.js +120 -0
  57. package/dist/js/node/commands/index.js +44 -0
  58. package/dist/js/node/commands/inspect.js +98 -0
  59. package/dist/js/node/commands/start.js +47 -0
  60. package/dist/js/node/exports/server.js +13 -0
  61. package/dist/js/node/hooks.js +39 -0
  62. package/dist/js/node/index.js +141 -0
  63. package/dist/js/node/locale/en.js +42 -0
  64. package/dist/js/node/locale/index.js +20 -0
  65. package/dist/js/node/locale/zh.js +42 -0
  66. package/dist/js/node/utils/config.js +103 -0
  67. package/dist/js/node/utils/createCompiler.js +81 -0
  68. package/dist/js/node/utils/createServer.js +35 -0
  69. package/dist/js/node/utils/getSpecifiedEntries.js +46 -0
  70. package/dist/js/node/utils/language.js +13 -0
  71. package/dist/js/node/utils/printInstructions.js +22 -0
  72. package/dist/js/node/utils/routes.js +25 -0
  73. package/dist/js/node/utils/types.js +0 -0
  74. package/dist/types/config/initialize/inits.d.ts +2 -1
  75. package/package.json +24 -24
@@ -79,7 +79,7 @@ const optimizeRoute = (routeTree) => {
79
79
  }
80
80
  const { children } = routeTree;
81
81
  const hasPage = children.some((child) => child.index);
82
- if (!routeTree._component && !routeTree.error && !routeTree.loading && !routeTree.config && !routeTree.clientData && !hasPage) {
82
+ if (!routeTree._component && !routeTree.error && !routeTree.loading && !routeTree.config && !routeTree.clientData) {
83
83
  const newRoutes = children.map((child) => {
84
84
  const routePath = `${routeTree.path ? routeTree.path : ""}${child.path ? `/${child.path}` : ""}`;
85
85
  const newRoute = {
@@ -285,6 +285,7 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry, oldVersion)
285
285
  throw new Error("The root layout component is required, make sure the routes/layout.tsx file exists.");
286
286
  }
287
287
  if (isRoot && !oldVersion) {
288
+ console.log("oooooooooooo");
288
289
  const optimizedRoutes = optimizeRoute(finalRoute);
289
290
  return optimizedRoutes;
290
291
  }
@@ -62,6 +62,7 @@ class RouterPlugin {
62
62
  return;
63
63
  }
64
64
  const { webpack } = compiler;
65
+ const isRspack = webpack.rspackVersion;
65
66
  const { Compilation, sources } = webpack;
66
67
  const { RawSource } = sources;
67
68
  const normalizePath = (path) => {
@@ -127,7 +128,19 @@ class RouterPlugin {
127
128
  routeAssets
128
129
  };
129
130
  const entryNames = Array.from(compilation.entrypoints.keys());
130
- const entryChunks = this.getEntryChunks(compilation, chunks);
131
+ let entryChunks = [];
132
+ if (isRspack) {
133
+ entryChunks = this.getEntryChunks(compilation, chunks);
134
+ } else {
135
+ const orignalEntryIds = Object.keys(compilation.options.entry).map((entryName) => {
136
+ const chunk = compilation.namedChunks.get(entryName);
137
+ if (chunk) {
138
+ return chunk.id;
139
+ }
140
+ return entryName;
141
+ });
142
+ entryChunks = this.getEntryChunks(compilation, chunks).filter((chunk) => orignalEntryIds.includes(chunk.id));
143
+ }
131
144
  const entryChunkFiles = this.getEntryChunkFiles(entryChunks);
132
145
  const entryChunkFileIds = entryChunks.map((chunk) => chunk.id);
133
146
  for (let i = 0; i < entryChunkFiles.length; i++) {
@@ -28,6 +28,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
  var inits_exports = {};
30
30
  __export(inits_exports, {
31
+ createBuilderModuleScope: () => createBuilderModuleScope,
31
32
  initHtmlConfig: () => initHtmlConfig,
32
33
  initSourceConfig: () => initSourceConfig
33
34
  });
@@ -62,57 +63,52 @@ function initSourceConfig(config, appContext, bundler) {
62
63
  if (bundler === "webpack") {
63
64
  config.source.moduleScopes = createBuilderModuleScope(config);
64
65
  }
65
- function createBuilderInclude(config2, appContext2) {
66
- const { include } = config2.source;
67
- const defaultInclude = [
68
- appContext2.internalDirectory
69
- ];
70
- const transformInclude = (include || []).map((include2) => {
71
- if (typeof include2 === "string") {
72
- if ((0, import_path.isAbsolute)(include2)) {
73
- return include2;
74
- }
75
- return new RegExp(include2);
66
+ }
67
+ function createBuilderInclude(config, appContext) {
68
+ const { include } = config.source;
69
+ const defaultInclude = [
70
+ appContext.internalDirectory
71
+ ];
72
+ const transformInclude = (include || []).map((include2) => {
73
+ if (typeof include2 === "string") {
74
+ if ((0, import_path.isAbsolute)(include2)) {
75
+ return include2;
76
76
  }
77
- return include2;
78
- }).concat(defaultInclude);
79
- return transformInclude;
77
+ return new RegExp(include2);
78
+ }
79
+ return include2;
80
+ }).concat(defaultInclude);
81
+ return transformInclude;
82
+ }
83
+ function createBuilderModuleScope(config) {
84
+ const { moduleScopes } = config.source;
85
+ if (moduleScopes) {
86
+ const DEFAULT_SCOPES = [
87
+ "./src",
88
+ "./shared",
89
+ /node_modules/
90
+ ];
91
+ const builderModuleScope = applyScopeOptions(DEFAULT_SCOPES, moduleScopes);
92
+ return builderModuleScope;
93
+ } else {
94
+ return void 0;
80
95
  }
81
- function createBuilderModuleScope(config2) {
82
- const { moduleScopes } = config2.source;
83
- if (moduleScopes) {
84
- let builderModuleScope = [];
85
- const DEFAULT_SCOPES = [
86
- "./src",
87
- "./shared",
88
- /node_modules/
89
- ];
90
- if (Array.isArray(moduleScopes)) {
91
- if (isPrimitiveScope(moduleScopes)) {
92
- builderModuleScope = DEFAULT_SCOPES.concat(moduleScopes);
93
- } else {
94
- builderModuleScope = [
95
- DEFAULT_SCOPES,
96
- ...moduleScopes
97
- ];
98
- }
99
- } else {
100
- builderModuleScope = [
101
- DEFAULT_SCOPES,
102
- moduleScopes
103
- ];
96
+ function isPrimitiveScope(items) {
97
+ return items.every((item) => typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]");
98
+ }
99
+ function applyScopeOptions(defaults, options) {
100
+ if (Array.isArray(options)) {
101
+ if (isPrimitiveScope(options)) {
102
+ return defaults.concat(options);
104
103
  }
105
- return builderModuleScope;
106
- } else {
107
- return void 0;
108
- }
109
- function isPrimitiveScope(items) {
110
- return items.every((item) => typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]");
104
+ return options.reduce(applyScopeOptions, defaults);
111
105
  }
106
+ return options(defaults) || defaults;
112
107
  }
113
108
  }
114
109
  // Annotate the CommonJS export names for ESM import in node:
115
110
  0 && (module.exports = {
111
+ createBuilderModuleScope,
116
112
  initHtmlConfig,
117
113
  initSourceConfig
118
114
  });
@@ -48,7 +48,7 @@ var optimizeRoute = function(routeTree) {
48
48
  var hasPage = children.some(function(child) {
49
49
  return child.index;
50
50
  });
51
- if (!routeTree._component && !routeTree.error && !routeTree.loading && !routeTree.config && !routeTree.clientData && !hasPage) {
51
+ if (!routeTree._component && !routeTree.error && !routeTree.loading && !routeTree.config && !routeTree.clientData) {
52
52
  var newRoutes = children.map(function(child) {
53
53
  var routePath = "".concat(routeTree.path ? routeTree.path : "").concat(child.path ? "/".concat(child.path) : "");
54
54
  var newRoute = _object_spread_props(_object_spread({}, child), {
@@ -379,6 +379,7 @@ var walk = function() {
379
379
  throw new Error("The root layout component is required, make sure the routes/layout.tsx file exists.");
380
380
  }
381
381
  if (isRoot && !oldVersion) {
382
+ console.log("oooooooooooo");
382
383
  optimizedRoutes = optimizeRoute(finalRoute);
383
384
  return [
384
385
  2,
@@ -80,6 +80,7 @@ var RouterPlugin = /* @__PURE__ */ function() {
80
80
  return;
81
81
  }
82
82
  var webpack = compiler.webpack;
83
+ var isRspack = webpack.rspackVersion;
83
84
  var Compilation = webpack.Compilation, sources = webpack.sources;
84
85
  var RawSource = sources.RawSource;
85
86
  var normalizePath = function(path) {
@@ -104,7 +105,7 @@ var RouterPlugin = /* @__PURE__ */ function() {
104
105
  name: PLUGIN_NAME,
105
106
  stage: Compilation.PROCESS_ASSETS_STAGE_REPORT
106
107
  }, /* @__PURE__ */ _async_to_generator(function() {
107
- var _loop, stats, publicPath, _stats_chunks, chunks, namedChunkGroups, routeAssets, prevManifestAsset, prevManifestStr, prevManifest, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, name, chunkGroup, assets, referenceCssAssets, manifest, entryNames, entryChunks, entryChunkFiles, entryChunkFileIds, i;
108
+ var _loop, stats, publicPath, _stats_chunks, chunks, namedChunkGroups, routeAssets, prevManifestAsset, prevManifestStr, prevManifest, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, name, chunkGroup, assets, referenceCssAssets, manifest, entryNames, entryChunks, orignalEntryIds, entryChunkFiles, entryChunkFileIds, i;
108
109
  return _ts_generator(this, function(_state) {
109
110
  _loop = function(i2) {
110
111
  var entryName = entryNames[i2];
@@ -261,7 +262,21 @@ var RouterPlugin = /* @__PURE__ */ function() {
261
262
  routeAssets
262
263
  };
263
264
  entryNames = Array.from(compilation.entrypoints.keys());
264
- entryChunks = _this1.getEntryChunks(compilation, chunks);
265
+ entryChunks = [];
266
+ if (isRspack) {
267
+ entryChunks = _this1.getEntryChunks(compilation, chunks);
268
+ } else {
269
+ orignalEntryIds = Object.keys(compilation.options.entry).map(function(entryName) {
270
+ var chunk = compilation.namedChunks.get(entryName);
271
+ if (chunk) {
272
+ return chunk.id;
273
+ }
274
+ return entryName;
275
+ });
276
+ entryChunks = _this1.getEntryChunks(compilation, chunks).filter(function(chunk) {
277
+ return orignalEntryIds.includes(chunk.id);
278
+ });
279
+ }
265
280
  entryChunkFiles = _this1.getEntryChunkFiles(entryChunks);
266
281
  entryChunkFileIds = entryChunks.map(function(chunk) {
267
282
  return chunk.id;
@@ -1,4 +1,3 @@
1
- import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
1
  import path, { isAbsolute } from "path";
3
2
  import { findExists } from "@modern-js/utils";
4
3
  function initHtmlConfig(config, appContext) {
@@ -29,61 +28,57 @@ function initHtmlConfig(config, appContext) {
29
28
  return config.html;
30
29
  }
31
30
  function initSourceConfig(config, appContext, bundler) {
32
- var createBuilderInclude = function createBuilderInclude2(config2, appContext2) {
33
- var include = config2.source.include;
34
- var defaultInclude = [
35
- appContext2.internalDirectory
36
- ];
37
- var transformInclude = (include || []).map(function(include2) {
38
- if (typeof include2 === "string") {
39
- if (isAbsolute(include2)) {
40
- return include2;
41
- }
42
- return new RegExp(include2);
31
+ config.source.include = createBuilderInclude(config, appContext);
32
+ if (bundler === "webpack") {
33
+ config.source.moduleScopes = createBuilderModuleScope(config);
34
+ }
35
+ }
36
+ function createBuilderInclude(config, appContext) {
37
+ var include = config.source.include;
38
+ var defaultInclude = [
39
+ appContext.internalDirectory
40
+ ];
41
+ var transformInclude = (include || []).map(function(include2) {
42
+ if (typeof include2 === "string") {
43
+ if (isAbsolute(include2)) {
44
+ return include2;
43
45
  }
44
- return include2;
45
- }).concat(defaultInclude);
46
- return transformInclude;
46
+ return new RegExp(include2);
47
+ }
48
+ return include2;
49
+ }).concat(defaultInclude);
50
+ return transformInclude;
51
+ }
52
+ function createBuilderModuleScope(config) {
53
+ var isPrimitiveScope = function isPrimitiveScope2(items) {
54
+ return items.every(function(item) {
55
+ return typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]";
56
+ });
47
57
  };
48
- var createBuilderModuleScope = function createBuilderModuleScope2(config2) {
49
- var moduleScopes = config2.source.moduleScopes;
50
- if (moduleScopes) {
51
- var builderModuleScope = [];
52
- var DEFAULT_SCOPES = [
53
- "./src",
54
- "./shared",
55
- /node_modules/
56
- ];
57
- if (Array.isArray(moduleScopes)) {
58
- if (isPrimitiveScope(moduleScopes)) {
59
- builderModuleScope = DEFAULT_SCOPES.concat(moduleScopes);
60
- } else {
61
- builderModuleScope = [
62
- DEFAULT_SCOPES
63
- ].concat(_to_consumable_array(moduleScopes));
64
- }
65
- } else {
66
- builderModuleScope = [
67
- DEFAULT_SCOPES,
68
- moduleScopes
69
- ];
58
+ var moduleScopes = config.source.moduleScopes;
59
+ if (moduleScopes) {
60
+ var DEFAULT_SCOPES = [
61
+ "./src",
62
+ "./shared",
63
+ /node_modules/
64
+ ];
65
+ var builderModuleScope = applyScopeOptions(DEFAULT_SCOPES, moduleScopes);
66
+ return builderModuleScope;
67
+ } else {
68
+ return void 0;
69
+ }
70
+ function applyScopeOptions(defaults, options) {
71
+ if (Array.isArray(options)) {
72
+ if (isPrimitiveScope(options)) {
73
+ return defaults.concat(options);
70
74
  }
71
- return builderModuleScope;
72
- } else {
73
- return void 0;
74
- }
75
- function isPrimitiveScope(items) {
76
- return items.every(function(item) {
77
- return typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]";
78
- });
75
+ return options.reduce(applyScopeOptions, defaults);
79
76
  }
80
- };
81
- config.source.include = createBuilderInclude(config, appContext);
82
- if (bundler === "webpack") {
83
- config.source.moduleScopes = createBuilderModuleScope(config);
77
+ return options(defaults) || defaults;
84
78
  }
85
79
  }
86
80
  export {
81
+ createBuilderModuleScope,
87
82
  initHtmlConfig,
88
83
  initSourceConfig
89
84
  };
@@ -44,7 +44,7 @@ const optimizeRoute = (routeTree) => {
44
44
  }
45
45
  const { children } = routeTree;
46
46
  const hasPage = children.some((child) => child.index);
47
- if (!routeTree._component && !routeTree.error && !routeTree.loading && !routeTree.config && !routeTree.clientData && !hasPage) {
47
+ if (!routeTree._component && !routeTree.error && !routeTree.loading && !routeTree.config && !routeTree.clientData) {
48
48
  const newRoutes = children.map((child) => {
49
49
  const routePath = `${routeTree.path ? routeTree.path : ""}${child.path ? `/${child.path}` : ""}`;
50
50
  const newRoute = {
@@ -250,6 +250,7 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry, oldVersion)
250
250
  throw new Error("The root layout component is required, make sure the routes/layout.tsx file exists.");
251
251
  }
252
252
  if (isRoot && !oldVersion) {
253
+ console.log("oooooooooooo");
253
254
  const optimizedRoutes = optimizeRoute(finalRoute);
254
255
  return optimizedRoutes;
255
256
  }
@@ -39,6 +39,7 @@ class RouterPlugin {
39
39
  return;
40
40
  }
41
41
  const { webpack } = compiler;
42
+ const isRspack = webpack.rspackVersion;
42
43
  const { Compilation, sources } = webpack;
43
44
  const { RawSource } = sources;
44
45
  const normalizePath = (path) => {
@@ -104,7 +105,19 @@ class RouterPlugin {
104
105
  routeAssets
105
106
  };
106
107
  const entryNames = Array.from(compilation.entrypoints.keys());
107
- const entryChunks = this.getEntryChunks(compilation, chunks);
108
+ let entryChunks = [];
109
+ if (isRspack) {
110
+ entryChunks = this.getEntryChunks(compilation, chunks);
111
+ } else {
112
+ const orignalEntryIds = Object.keys(compilation.options.entry).map((entryName) => {
113
+ const chunk = compilation.namedChunks.get(entryName);
114
+ if (chunk) {
115
+ return chunk.id;
116
+ }
117
+ return entryName;
118
+ });
119
+ entryChunks = this.getEntryChunks(compilation, chunks).filter((chunk) => orignalEntryIds.includes(chunk.id));
120
+ }
108
121
  const entryChunkFiles = this.getEntryChunkFiles(entryChunks);
109
122
  const entryChunkFileIds = entryChunks.map((chunk) => chunk.id);
110
123
  for (let i = 0; i < entryChunkFiles.length; i++) {
@@ -28,56 +28,51 @@ function initSourceConfig(config, appContext, bundler) {
28
28
  if (bundler === "webpack") {
29
29
  config.source.moduleScopes = createBuilderModuleScope(config);
30
30
  }
31
- function createBuilderInclude(config2, appContext2) {
32
- const { include } = config2.source;
33
- const defaultInclude = [
34
- appContext2.internalDirectory
35
- ];
36
- const transformInclude = (include || []).map((include2) => {
37
- if (typeof include2 === "string") {
38
- if (isAbsolute(include2)) {
39
- return include2;
40
- }
41
- return new RegExp(include2);
31
+ }
32
+ function createBuilderInclude(config, appContext) {
33
+ const { include } = config.source;
34
+ const defaultInclude = [
35
+ appContext.internalDirectory
36
+ ];
37
+ const transformInclude = (include || []).map((include2) => {
38
+ if (typeof include2 === "string") {
39
+ if (isAbsolute(include2)) {
40
+ return include2;
42
41
  }
43
- return include2;
44
- }).concat(defaultInclude);
45
- return transformInclude;
42
+ return new RegExp(include2);
43
+ }
44
+ return include2;
45
+ }).concat(defaultInclude);
46
+ return transformInclude;
47
+ }
48
+ function createBuilderModuleScope(config) {
49
+ const { moduleScopes } = config.source;
50
+ if (moduleScopes) {
51
+ const DEFAULT_SCOPES = [
52
+ "./src",
53
+ "./shared",
54
+ /node_modules/
55
+ ];
56
+ const builderModuleScope = applyScopeOptions(DEFAULT_SCOPES, moduleScopes);
57
+ return builderModuleScope;
58
+ } else {
59
+ return void 0;
46
60
  }
47
- function createBuilderModuleScope(config2) {
48
- const { moduleScopes } = config2.source;
49
- if (moduleScopes) {
50
- let builderModuleScope = [];
51
- const DEFAULT_SCOPES = [
52
- "./src",
53
- "./shared",
54
- /node_modules/
55
- ];
56
- if (Array.isArray(moduleScopes)) {
57
- if (isPrimitiveScope(moduleScopes)) {
58
- builderModuleScope = DEFAULT_SCOPES.concat(moduleScopes);
59
- } else {
60
- builderModuleScope = [
61
- DEFAULT_SCOPES,
62
- ...moduleScopes
63
- ];
64
- }
65
- } else {
66
- builderModuleScope = [
67
- DEFAULT_SCOPES,
68
- moduleScopes
69
- ];
61
+ function isPrimitiveScope(items) {
62
+ return items.every((item) => typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]");
63
+ }
64
+ function applyScopeOptions(defaults, options) {
65
+ if (Array.isArray(options)) {
66
+ if (isPrimitiveScope(options)) {
67
+ return defaults.concat(options);
70
68
  }
71
- return builderModuleScope;
72
- } else {
73
- return void 0;
74
- }
75
- function isPrimitiveScope(items) {
76
- return items.every((item) => typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]");
69
+ return options.reduce(applyScopeOptions, defaults);
77
70
  }
71
+ return options(defaults) || defaults;
78
72
  }
79
73
  }
80
74
  export {
75
+ createBuilderModuleScope,
81
76
  initHtmlConfig,
82
77
  initSourceConfig
83
78
  };
@@ -0,0 +1,15 @@
1
+ export const JS_EXTENSIONS = ['.js', '.ts', '.jsx', '.tsx'];
2
+ export const INDEX_FILE_NAME = 'index';
3
+ export const APP_FILE_NAME = 'App';
4
+ export const PAGES_DIR_NAME = 'pages';
5
+ export const FILE_SYSTEM_ROUTES_FILE_NAME = 'routes.js';
6
+ export const ENTRY_POINT_FILE_NAME = 'index.js';
7
+ export const ENTRY_BOOTSTRAP_FILE_NAME = 'bootstrap.js';
8
+ export const FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = /^\[(\S+)\]([*+?]?)$/;
9
+ export const FILE_SYSTEM_ROUTES_LAYOUT = '_layout';
10
+ export const FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = '_app';
11
+ export const FILE_SYSTEM_ROUTES_INDEX = 'index';
12
+ export const FILE_SYSTEM_ROUTES_IGNORED_REGEX = /\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/;
13
+ export const HTML_PARTIALS_FOLDER = 'html';
14
+ export const HTML_PARTIALS_EXTENSIONS = ['.htm', '.html', '.ejs'];
15
+ export const FILE_SYSTEM_ROUTES_COMPONENTS_DIR = 'internal_components';
@@ -0,0 +1,179 @@
1
+ import path from 'path';
2
+ import { fs } from '@modern-js/utils';
3
+ import * as templates from "./templates";
4
+ import { getClientRoutes } from "./getClientRoutes";
5
+ import { FILE_SYSTEM_ROUTES_FILE_NAME, ENTRY_POINT_FILE_NAME, ENTRY_BOOTSTRAP_FILE_NAME } from "./constants";
6
+ import { getDefaultImports } from "./utils";
7
+
8
+ const createImportSpecifier = specifiers => {
9
+ let defaults = '';
10
+ const named = [];
11
+
12
+ for (const {
13
+ local,
14
+ imported
15
+ } of specifiers) {
16
+ if (local && imported) {
17
+ named.push(`${imported} as ${local}`);
18
+ } else if (local) {
19
+ defaults = local;
20
+ } else {
21
+ named.push(imported);
22
+ }
23
+ }
24
+
25
+ if (defaults && named.length) {
26
+ return `${defaults}, { ${named.join(', ')} }`;
27
+ } else if (defaults) {
28
+ return defaults;
29
+ } else {
30
+ return `{ ${named.join(', ')} }`;
31
+ }
32
+ };
33
+
34
+ export const createImportStatements = statements => {
35
+ // merge import statements with the same value.
36
+ const deDuplicated = [];
37
+ const seen = new Map();
38
+
39
+ for (const {
40
+ value,
41
+ specifiers,
42
+ initialize
43
+ } of statements) {
44
+ if (!seen.has(value)) {
45
+ deDuplicated.push({
46
+ value,
47
+ specifiers,
48
+ initialize
49
+ });
50
+ seen.set(value, specifiers);
51
+ } else {
52
+ var _deDuplicated$modifyI, _deDuplicated$modifyI2;
53
+
54
+ seen.get(value).push(...specifiers); // make "initialize" param can be connected when multiple plugins were imported from same package
55
+
56
+ const modifyIndex = deDuplicated.findIndex(v => v.value === value);
57
+ const originInitialize = (_deDuplicated$modifyI = (_deDuplicated$modifyI2 = deDuplicated[modifyIndex]) === null || _deDuplicated$modifyI2 === void 0 ? void 0 : _deDuplicated$modifyI2.initialize) !== null && _deDuplicated$modifyI !== void 0 ? _deDuplicated$modifyI : '';
58
+ deDuplicated[modifyIndex].initialize = originInitialize.concat(`\n${initialize || ''}`);
59
+ }
60
+ }
61
+
62
+ return deDuplicated.map(({
63
+ value,
64
+ specifiers,
65
+ initialize
66
+ }) => `import ${createImportSpecifier(specifiers)} from '${value}';\n${initialize || ''}`).join('\n');
67
+ };
68
+ export const generateCode = async (appContext, config, entrypoints, api) => {
69
+ const {
70
+ internalDirectory,
71
+ srcDirectory,
72
+ internalDirAlias,
73
+ internalSrcAlias
74
+ } = appContext;
75
+ const hookRunners = api.useHookRunners();
76
+ const {
77
+ output: {
78
+ mountId
79
+ }
80
+ } = config;
81
+
82
+ for (const entrypoint of entrypoints) {
83
+ const {
84
+ entryName,
85
+ isAutoMount,
86
+ customBootstrap,
87
+ fileSystemRoutes
88
+ } = entrypoint;
89
+
90
+ if (isAutoMount) {
91
+ // generate routes file for file system routes entrypoint.
92
+ if (fileSystemRoutes) {
93
+ const initialRoutes = getClientRoutes({
94
+ entrypoint,
95
+ srcDirectory,
96
+ srcAlias: internalSrcAlias,
97
+ internalDirectory,
98
+ internalDirAlias
99
+ });
100
+ const {
101
+ routes
102
+ } = await hookRunners.modifyFileSystemRoutes({
103
+ entrypoint,
104
+ routes: initialRoutes
105
+ });
106
+ const {
107
+ code
108
+ } = await hookRunners.beforeGenerateRoutes({
109
+ entrypoint,
110
+ code: templates.fileSystemRoutes({
111
+ routes
112
+ })
113
+ });
114
+ fs.outputFileSync(path.resolve(internalDirectory, `./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`), code, 'utf8');
115
+ } // call modifyEntryImports hook
116
+
117
+
118
+ const {
119
+ imports: importStatements
120
+ } = await hookRunners.modifyEntryImports({
121
+ entrypoint,
122
+ imports: getDefaultImports({
123
+ entrypoint,
124
+ srcDirectory,
125
+ internalSrcAlias,
126
+ internalDirAlias,
127
+ internalDirectory
128
+ })
129
+ }); // call modifyEntryRuntimePlugins hook
130
+
131
+ const {
132
+ plugins
133
+ } = await hookRunners.modifyEntryRuntimePlugins({
134
+ entrypoint,
135
+ plugins: []
136
+ }); // call modifyEntryRenderFunction hook
137
+
138
+ const {
139
+ code: renderFunction
140
+ } = await hookRunners.modifyEntryRenderFunction({
141
+ entrypoint,
142
+ code: templates.renderFunction({
143
+ plugins,
144
+ customBootstrap,
145
+ fileSystemRoutes
146
+ })
147
+ }); // call modifyEntryExport hook
148
+
149
+ const {
150
+ exportStatement
151
+ } = await hookRunners.modifyEntryExport({
152
+ entrypoint,
153
+ exportStatement: 'export default AppWrapper;'
154
+ });
155
+ const code = templates.index({
156
+ mountId: mountId,
157
+ imports: createImportStatements(importStatements),
158
+ renderFunction,
159
+ exportStatement
160
+ });
161
+ const entryFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_POINT_FILE_NAME}`);
162
+ entrypoint.entry = entryFile; // generate entry file.
163
+
164
+ if (config.source.enableAsyncEntry) {
165
+ const {
166
+ code: asyncEntryCode
167
+ } = await hookRunners.modifyAsyncEntry({
168
+ entrypoint,
169
+ code: `import('./${ENTRY_BOOTSTRAP_FILE_NAME}');`
170
+ });
171
+ fs.outputFileSync(entryFile, asyncEntryCode, 'utf8');
172
+ const bootstrapFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_BOOTSTRAP_FILE_NAME}`);
173
+ fs.outputFileSync(bootstrapFile, code, 'utf8');
174
+ } else {
175
+ fs.outputFileSync(entryFile, code, 'utf8');
176
+ }
177
+ }
178
+ }
179
+ };