@modern-js/app-tools 1.21.3 → 2.0.0-beta.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 (146) hide show
  1. package/CHANGELOG.md +138 -16
  2. package/dist/js/modern/analyze/constants.js +1 -0
  3. package/dist/js/modern/analyze/generateCode.js +118 -16
  4. package/dist/js/modern/analyze/getBundleEntry.js +3 -2
  5. package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +198 -0
  6. package/dist/js/modern/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +8 -28
  7. package/dist/js/modern/analyze/getClientRoutes/index.js +2 -0
  8. package/dist/js/modern/analyze/getClientRoutes/utils.js +21 -0
  9. package/dist/js/modern/analyze/getFileSystemEntry.js +31 -12
  10. package/dist/js/modern/analyze/index.js +2 -1
  11. package/dist/js/modern/analyze/nestedRoutes.js +127 -0
  12. package/dist/js/modern/analyze/templates.js +164 -10
  13. package/dist/js/modern/analyze/utils.js +2 -1
  14. package/dist/js/modern/builder/builderPlugins/compatModern.js +226 -0
  15. package/dist/js/modern/builder/createHtmlConfig.js +58 -0
  16. package/dist/js/modern/builder/createOutputConfig.js +67 -0
  17. package/dist/js/modern/builder/createSourceConfig.js +85 -0
  18. package/dist/js/modern/builder/createToolsConfig.js +94 -0
  19. package/dist/js/modern/builder/index.js +106 -0
  20. package/dist/js/modern/builder/share.js +48 -0
  21. package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +37 -0
  22. package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +44 -0
  23. package/dist/js/modern/commands/build.js +41 -95
  24. package/dist/js/modern/commands/dev.js +15 -19
  25. package/dist/js/modern/commands/inspect.js +17 -58
  26. package/dist/js/modern/commands/start.js +2 -1
  27. package/dist/js/modern/index.js +14 -4
  28. package/dist/js/modern/locale/en.js +0 -1
  29. package/dist/js/modern/locale/zh.js +0 -1
  30. package/dist/js/modern/utils/commands.js +5 -0
  31. package/dist/js/modern/utils/createCompiler.js +37 -40
  32. package/dist/js/modern/utils/createServer.js +16 -0
  33. package/dist/js/modern/utils/printInstructions.js +1 -1
  34. package/dist/js/node/analyze/constants.js +3 -1
  35. package/dist/js/node/analyze/generateCode.js +121 -14
  36. package/dist/js/node/analyze/getBundleEntry.js +3 -2
  37. package/dist/js/node/analyze/getClientRoutes/getRoutes.js +221 -0
  38. package/dist/js/node/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +16 -35
  39. package/dist/js/node/analyze/getClientRoutes/index.js +21 -0
  40. package/dist/js/node/analyze/getClientRoutes/utils.js +42 -0
  41. package/dist/js/node/analyze/getFileSystemEntry.js +30 -11
  42. package/dist/js/node/analyze/index.js +2 -1
  43. package/dist/js/node/analyze/nestedRoutes.js +144 -0
  44. package/dist/js/node/analyze/templates.js +167 -10
  45. package/dist/js/node/analyze/utils.js +6 -2
  46. package/dist/js/node/builder/builderPlugins/compatModern.js +245 -0
  47. package/dist/js/node/builder/createHtmlConfig.js +76 -0
  48. package/dist/js/node/builder/createOutputConfig.js +75 -0
  49. package/dist/js/node/builder/createSourceConfig.js +99 -0
  50. package/dist/js/node/builder/createToolsConfig.js +103 -0
  51. package/dist/js/node/builder/index.js +129 -0
  52. package/dist/js/node/builder/share.js +63 -0
  53. package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +46 -0
  54. package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +53 -0
  55. package/dist/js/node/commands/build.js +44 -98
  56. package/dist/js/node/commands/dev.js +13 -23
  57. package/dist/js/node/commands/inspect.js +18 -74
  58. package/dist/js/node/commands/start.js +3 -1
  59. package/dist/js/node/index.js +19 -2
  60. package/dist/js/node/locale/en.js +0 -1
  61. package/dist/js/node/locale/zh.js +0 -1
  62. package/dist/js/node/utils/commands.js +14 -0
  63. package/dist/js/node/utils/createCompiler.js +43 -49
  64. package/dist/js/node/utils/createServer.js +21 -2
  65. package/dist/js/node/utils/printInstructions.js +1 -1
  66. package/dist/js/treeshaking/analyze/constants.js +16 -0
  67. package/dist/js/treeshaking/analyze/generateCode.js +412 -0
  68. package/dist/js/treeshaking/analyze/getBundleEntry.js +77 -0
  69. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +210 -0
  70. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +211 -0
  71. package/dist/js/treeshaking/analyze/getClientRoutes/index.js +2 -0
  72. package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +27 -0
  73. package/dist/js/treeshaking/analyze/getFileSystemEntry.js +109 -0
  74. package/dist/js/treeshaking/analyze/getHtmlTemplate.js +153 -0
  75. package/dist/js/treeshaking/analyze/getServerRoutes.js +170 -0
  76. package/dist/js/treeshaking/analyze/index.js +214 -0
  77. package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +32 -0
  78. package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +18 -0
  79. package/dist/js/treeshaking/analyze/nestedRoutes.js +207 -0
  80. package/dist/js/treeshaking/analyze/templates.js +189 -0
  81. package/dist/js/treeshaking/analyze/utils.js +95 -0
  82. package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +231 -0
  83. package/dist/js/treeshaking/builder/createHtmlConfig.js +59 -0
  84. package/dist/js/treeshaking/builder/createOutputConfig.js +67 -0
  85. package/dist/js/treeshaking/builder/createSourceConfig.js +100 -0
  86. package/dist/js/treeshaking/builder/createToolsConfig.js +90 -0
  87. package/dist/js/treeshaking/builder/index.js +172 -0
  88. package/dist/js/treeshaking/builder/share.js +50 -0
  89. package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +58 -0
  90. package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +53 -0
  91. package/dist/js/treeshaking/commands/build.js +168 -0
  92. package/dist/js/treeshaking/commands/deploy.js +29 -0
  93. package/dist/js/treeshaking/commands/dev.js +146 -0
  94. package/dist/js/treeshaking/commands/index.js +3 -0
  95. package/dist/js/treeshaking/commands/inspect.js +52 -0
  96. package/dist/js/treeshaking/commands/start.js +78 -0
  97. package/dist/js/treeshaking/exports/server.js +1 -0
  98. package/dist/js/treeshaking/hooks.js +21 -0
  99. package/dist/js/treeshaking/index.js +304 -0
  100. package/dist/js/treeshaking/locale/en.js +34 -0
  101. package/dist/js/treeshaking/locale/index.js +9 -0
  102. package/dist/js/treeshaking/locale/zh.js +34 -0
  103. package/dist/js/treeshaking/utils/commands.js +5 -0
  104. package/dist/js/treeshaking/utils/config.js +140 -0
  105. package/dist/js/treeshaking/utils/createCompiler.js +117 -0
  106. package/dist/js/treeshaking/utils/createServer.js +83 -0
  107. package/dist/js/treeshaking/utils/getSpecifiedEntries.js +68 -0
  108. package/dist/js/treeshaking/utils/language.js +5 -0
  109. package/dist/js/treeshaking/utils/printInstructions.js +35 -0
  110. package/dist/js/treeshaking/utils/routes.js +33 -0
  111. package/dist/js/treeshaking/utils/types.js +0 -0
  112. package/dist/types/analyze/constants.d.ts +1 -0
  113. package/dist/types/analyze/generateCode.d.ts +1 -1
  114. package/dist/types/analyze/{getClientRoutes.d.ts → getClientRoutes/getRoutes.d.ts} +2 -7
  115. package/dist/types/analyze/getClientRoutes/getRoutesLegacy.d.ts +15 -0
  116. package/dist/types/analyze/getClientRoutes/index.d.ts +2 -0
  117. package/dist/types/analyze/getClientRoutes/utils.d.ts +5 -0
  118. package/dist/types/analyze/index.d.ts +2 -2
  119. package/dist/types/analyze/nestedRoutes.d.ts +5 -0
  120. package/dist/types/analyze/templates.d.ts +17 -3
  121. package/dist/types/analyze/utils.d.ts +2 -1
  122. package/dist/types/builder/builderPlugins/compatModern.d.ts +14 -0
  123. package/dist/types/builder/createHtmlConfig.d.ts +6 -0
  124. package/dist/types/builder/createOutputConfig.d.ts +3 -0
  125. package/dist/types/builder/createSourceConfig.d.ts +5 -0
  126. package/dist/types/builder/createToolsConfig.d.ts +13 -0
  127. package/dist/types/builder/index.d.ts +19 -0
  128. package/dist/types/builder/share.d.ts +26 -0
  129. package/dist/types/builder/webpackPlugins/htmlAsyncChunkPlugin.d.ts +8 -0
  130. package/dist/types/builder/webpackPlugins/htmlBottomTemplate.d.ts +10 -0
  131. package/dist/types/commands/build.d.ts +2 -1
  132. package/dist/types/commands/deploy.d.ts +2 -1
  133. package/dist/types/commands/dev.d.ts +2 -1
  134. package/dist/types/commands/inspect.d.ts +5 -6
  135. package/dist/types/commands/start.d.ts +2 -1
  136. package/dist/types/hooks.d.ts +15 -10
  137. package/dist/types/index.d.ts +4 -2
  138. package/dist/types/locale/en.d.ts +0 -1
  139. package/dist/types/locale/index.d.ts +0 -2
  140. package/dist/types/locale/zh.d.ts +0 -1
  141. package/dist/types/utils/commands.d.ts +1 -0
  142. package/dist/types/utils/createCompiler.d.ts +9 -10
  143. package/dist/types/utils/createServer.d.ts +8 -1
  144. package/dist/types/utils/printInstructions.d.ts +3 -2
  145. package/dist/types/utils/types.d.ts +2 -3
  146. package/package.json +25 -43
@@ -3,7 +3,13 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.renderFunction = exports.index = exports.html = exports.fileSystemRoutes = void 0;
6
+ exports.routesForServer = exports.renderFunction = exports.index = exports.html = exports.fileSystemRoutes = void 0;
7
+
8
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
9
+
10
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
11
+
12
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
7
13
 
8
14
  const index = ({
9
15
  mountId,
@@ -89,18 +95,169 @@ const html = partials => `
89
95
 
90
96
  exports.html = html;
91
97
 
98
+ const routesForServer = ({
99
+ routes,
100
+ alias
101
+ }) => {
102
+ const {
103
+ name,
104
+ basename
105
+ } = alias;
106
+ const loaders = [];
107
+
108
+ const traverseRouteTree = route => {
109
+ let children;
110
+
111
+ if ('children' in route && route.children) {
112
+ var _route$children;
113
+
114
+ children = route === null || route === void 0 ? void 0 : (_route$children = route.children) === null || _route$children === void 0 ? void 0 : _route$children.map(traverseRouteTree);
115
+ }
116
+
117
+ let loader;
118
+
119
+ if (route.type === 'nested') {
120
+ if (route.loader) {
121
+ loaders.push(route.loader);
122
+ loader = `loader_${loaders.length - 1}`;
123
+ }
124
+ }
125
+
126
+ const finalRoute = _objectSpread(_objectSpread({}, route), {}, {
127
+ loader,
128
+ children
129
+ });
130
+
131
+ return finalRoute;
132
+ };
133
+
134
+ let routesCode = `
135
+ export const routes = [
136
+ `;
137
+
138
+ for (const route of routes) {
139
+ if ('type' in route) {
140
+ const newRoute = traverseRouteTree(route);
141
+ routesCode += `${JSON.stringify(newRoute, null, 2).replace(/"(loader_[^"])"/g, '$1')},`;
142
+ } else {
143
+ routesCode += `${JSON.stringify(route, null, 2)}`;
144
+ }
145
+ }
146
+
147
+ routesCode += `\n];`;
148
+ const importLoadersCode = loaders.map((loader, index) => {
149
+ const realLoaderPath = loader.replace(name, basename);
150
+ return `import loader_${index} from '${realLoaderPath}';\n`;
151
+ }).join('');
152
+ return `
153
+ ${importLoadersCode}
154
+ ${routesCode}
155
+ `;
156
+ };
157
+
158
+ exports.routesForServer = routesForServer;
159
+
92
160
  const fileSystemRoutes = ({
93
- routes
94
- }) => `
95
- import loadable from '@modern-js/runtime/loadable';
161
+ routes,
162
+ ssrMode,
163
+ nestedRoutesEntry
164
+ }) => {
165
+ const importLazyCode = ssrMode === 'stream' ? 'import { lazy } from "react";' : `import loadable from '@modern-js/runtime/loadable'`;
166
+ let dataLoaderPath = '';
96
167
 
97
- ${routes.map(({
98
- component,
99
- _component
100
- }) => `const ${component} = loadable(() => import('${_component}'));`).join('\n\n')}
168
+ if (ssrMode) {
169
+ dataLoaderPath = require.resolve(`@modern-js/plugin-data-loader/loader`);
101
170
 
171
+ if (nestedRoutesEntry) {
172
+ dataLoaderPath = `${dataLoaderPath}?routesDir=${nestedRoutesEntry}!`;
173
+ }
174
+ }
102
175
 
103
- export const routes = ${JSON.stringify(routes, null, 2).replace(/"component"\s*:\s*"(\S+)"/g, '"component": $1')}
104
- `;
176
+ const loadings = [];
177
+ const errors = [];
178
+ const loaders = [];
179
+
180
+ const traverseRouteTree = route => {
181
+ let children;
182
+
183
+ if ('children' in route && route.children) {
184
+ var _route$children2;
185
+
186
+ children = route === null || route === void 0 ? void 0 : (_route$children2 = route.children) === null || _route$children2 === void 0 ? void 0 : _route$children2.map(traverseRouteTree);
187
+ }
188
+
189
+ let loading;
190
+ let error;
191
+ let loader;
192
+
193
+ if (route.type === 'nested') {
194
+ if (route.loading) {
195
+ loadings.push(route.loading);
196
+ loading = `loading_${loadings.length - 1}`;
197
+ }
198
+
199
+ if (route.error) {
200
+ errors.push(route.error);
201
+ error = `error_${errors.length - 1}`;
202
+ }
203
+
204
+ if (route.loader) {
205
+ loaders.push(route.loader);
206
+ loader = `loader_${loaders.length - 1}`;
207
+ }
208
+ }
209
+
210
+ const finalRoute = _objectSpread(_objectSpread({}, route), {}, {
211
+ loading,
212
+ loader,
213
+ error,
214
+ children
215
+ });
216
+
217
+ if (route._component) {
218
+ const component = ssrMode === 'stream' ? `lazy(() => import('${route._component}'))` : `loadable(() => import('${route._component}'))`;
219
+ finalRoute.component = component;
220
+ }
221
+
222
+ return finalRoute;
223
+ };
224
+
225
+ let routeComponentsCode = `
226
+ export const routes = [
227
+ `;
228
+
229
+ for (const route of routes) {
230
+ if ('type' in route) {
231
+ const newRoute = traverseRouteTree(route);
232
+ routeComponentsCode += `${JSON.stringify(newRoute, null, 2).replace(/"(loadable[^"]*)"/g, '$1').replace(/"(lazy[^"]*)"/g, '$1').replace(/"(loading_[^"])"/g, '$1').replace(/"(loader_[^"])"/g, '$1').replace(/"(error_[^"])"/g, '$1')},`;
233
+ } else {
234
+ const component = ssrMode === 'stream' ? `lazy(() => import('${route._component}'))` : `loadable(() => import('${route._component}'))`;
235
+
236
+ const finalRoute = _objectSpread(_objectSpread({}, route), {}, {
237
+ component
238
+ });
239
+
240
+ routeComponentsCode += `${JSON.stringify(finalRoute, null, 2).replace(/"(loadable[^"]*)"/g, '$1').replace(/"(lazy[^"]*)"/g, '$1')},`;
241
+ }
242
+ }
243
+
244
+ routeComponentsCode += `\n];`;
245
+ const importLoadingCode = loadings.map((loading, index) => {
246
+ return `import loading_${index} from '${loading}';\n`;
247
+ }).join('');
248
+ const importErrorComponentsCode = errors.map((error, index) => {
249
+ return `import error_${index} from '${error}';\n`;
250
+ }).join('');
251
+ const importLoaderComponentsCode = loaders.map((loader, index) => {
252
+ return `import loader_${index} from '${dataLoaderPath}${loader}';\n`;
253
+ }).join('');
254
+ return `
255
+ ${importLazyCode}
256
+ ${importLoadingCode}
257
+ ${importErrorComponentsCode}
258
+ ${importLoaderComponentsCode}
259
+ ${routeComponentsCode}
260
+ `;
261
+ };
105
262
 
106
263
  exports.fileSystemRoutes = fileSystemRoutes;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.walkDirectory = exports.isRouteComponentFile = exports.getDefaultImports = void 0;
6
+ exports.walkDirectory = exports.replaceWithAlias = exports.isRouteComponentFile = exports.getDefaultImports = void 0;
7
7
 
8
8
  var _fs = _interopRequireDefault(require("fs"));
9
9
 
@@ -110,4 +110,8 @@ const isRouteComponentFile = filePath => {
110
110
  return false;
111
111
  };
112
112
 
113
- exports.isRouteComponentFile = isRouteComponentFile;
113
+ exports.isRouteComponentFile = isRouteComponentFile;
114
+
115
+ const replaceWithAlias = (base, filePath, alias) => (0, _utils.normalizeToPosixPath)(_path.default.join(alias, _path.default.relative(base, filePath)));
116
+
117
+ exports.replaceWithAlias = replaceWithAlias;
@@ -0,0 +1,245 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.PluginCompatModern = void 0;
7
+
8
+ var _path = require("path");
9
+
10
+ var _builderShared = require("@modern-js/builder-shared");
11
+
12
+ var _lodash = require("@modern-js/utils/lodash");
13
+
14
+ var _htmlWebpackPlugin = _interopRequireDefault(require("@modern-js/builder-webpack-provider/html-webpack-plugin"));
15
+
16
+ var _utils = require("@modern-js/utils");
17
+
18
+ var _htmlBottomTemplate = require("../webpackPlugins/htmlBottomTemplate");
19
+
20
+ var _htmlAsyncChunkPlugin = require("../webpackPlugins/htmlAsyncChunkPlugin");
21
+
22
+ var _share = require("../share");
23
+
24
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
+
26
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
27
+
28
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
29
+
30
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
31
+
32
+ /**
33
+ * Provides default configuration consistent with `@modern-js/webpack`
34
+ */
35
+ const PluginCompatModern = (appContext, modernConfig, options) => ({
36
+ name: 'builder-plugin-compat-modern',
37
+
38
+ setup(api) {
39
+ api.modifyBuilderConfig(config => {
40
+ if (isStreamingSSR(modernConfig)) {
41
+ return (0, _builderShared.mergeBuilderConfig)(config, {
42
+ html: {
43
+ inject: 'body'
44
+ }
45
+ });
46
+ }
47
+
48
+ return config;
49
+ });
50
+ api.modifyWebpackChain((chain, {
51
+ target,
52
+ CHAIN_ID,
53
+ isProd
54
+ }) => {
55
+ const builderNormalizedConfig = api.getNormalizedConfig(); // set webpack config name
56
+
57
+ if (target === 'node') {
58
+ chain.name('server');
59
+ } else if (target === 'modern-web') {
60
+ chain.name('modern');
61
+ } else {
62
+ chain.name('client');
63
+ }
64
+
65
+ chain.resolve.modules.add('node_modules').add((0, _path.join)(api.context.rootPath, 'node_modules')); // apply node compat
66
+
67
+ if (target === 'node') {
68
+ applyNodeCompat(chain, modernConfig, isProd);
69
+ }
70
+
71
+ if (isHtmlEnabled(builderNormalizedConfig, target)) {
72
+ applyBottomHtmlWebpackPlugin({
73
+ api,
74
+ chain,
75
+ CHAIN_ID,
76
+ appContext,
77
+ modernConfig
78
+ });
79
+ applyAsyncChunkHtmlPlugin({
80
+ chain,
81
+ CHAIN_ID,
82
+ modernConfig
83
+ });
84
+ } // apply copy plugin
85
+ // const copyPatterns = createCopyPatterns(chain, appContext, modernConfig);
86
+
87
+
88
+ const defaultCopyPattern = (0, _share.createCopyPattern)(appContext, modernConfig, 'public', chain);
89
+ chain.plugin(CHAIN_ID.PLUGIN.COPY).tap(args => {
90
+ var _args$;
91
+
92
+ return [{
93
+ patterns: [...(((_args$ = args[0]) === null || _args$ === void 0 ? void 0 : _args$.patterns) || []), defaultCopyPattern]
94
+ }];
95
+ });
96
+
97
+ function isHtmlEnabled(config, target) {
98
+ var _config$tools;
99
+
100
+ return ((_config$tools = config.tools) === null || _config$tools === void 0 ? void 0 : _config$tools.htmlPlugin) !== false && target !== 'node' && target !== 'web-worker';
101
+ }
102
+ });
103
+
104
+ if (options) {
105
+ applyCallbacks(api, options);
106
+ }
107
+ }
108
+
109
+ });
110
+ /**
111
+ * register builder hooks callback
112
+ */
113
+
114
+
115
+ exports.PluginCompatModern = PluginCompatModern;
116
+
117
+ function applyCallbacks(api, options) {
118
+ options.onAfterBuild && api.onAfterBuild(options.onAfterBuild);
119
+ options.onAfterCreateCompiler && api.onAfterCreateCompiler(options.onAfterCreateCompiler);
120
+ options.onAfterStartDevServer && api.onAfterStartDevServer(options.onAfterStartDevServer);
121
+ options.onBeforeBuild && api.onBeforeBuild(options.onBeforeBuild);
122
+ options.onBeforeCreateCompiler && api.onBeforeCreateCompiler(options.onBeforeCreateCompiler);
123
+ options.onBeforeStartDevServer && api.onBeforeStartDevServer(options.onBeforeStartDevServer);
124
+ options.onDevCompileDone && api.onDevCompileDone(options.onDevCompileDone);
125
+ options.onExit && api.onExit(options.onExit);
126
+ }
127
+ /**
128
+ * compat some config, if target is `node`
129
+ */
130
+
131
+
132
+ function applyNodeCompat(chain, modernConfig, isProd) {
133
+ // apply node resolve extensions
134
+ for (const ext of ['.node.js', '.node.jsx', '.node.ts', '.node.tsx']) {
135
+ chain.resolve.extensions.prepend(ext);
136
+ } // apply filterEntriesBySSRConfig
137
+
138
+
139
+ filterEntriesBySSRConfig(isProd, chain, modernConfig.server, modernConfig.output);
140
+
141
+ function filterEntriesBySSRConfig(isProd, chain, serverConfig, outputConfig) {
142
+ var _outputConfig$ssg;
143
+
144
+ const entries = chain.entryPoints.entries(); // if prod and ssg config is true or function
145
+
146
+ if (isProd && ((outputConfig === null || outputConfig === void 0 ? void 0 : outputConfig.ssg) === true || typeof (outputConfig === null || outputConfig === void 0 ? void 0 : (_outputConfig$ssg = outputConfig.ssg) === null || _outputConfig$ssg === void 0 ? void 0 : _outputConfig$ssg[0]) === 'function')) {
147
+ return;
148
+ } // if single entry has ssg config
149
+ // `ssg: {}` is not allowed if multi entry
150
+
151
+
152
+ const entryNames = Object.keys(entries);
153
+
154
+ if (isProd && entryNames.length === 1 && outputConfig !== null && outputConfig !== void 0 && outputConfig.ssg) {
155
+ return;
156
+ } // collect all ssg entries
157
+
158
+
159
+ const ssgEntries = [];
160
+
161
+ if (isProd && outputConfig !== null && outputConfig !== void 0 && outputConfig.ssg) {
162
+ const {
163
+ ssg
164
+ } = outputConfig;
165
+ entryNames.forEach(name => {
166
+ if (ssg[name]) {
167
+ ssgEntries.push(name);
168
+ }
169
+ });
170
+ }
171
+
172
+ const {
173
+ ssr,
174
+ ssrByEntries
175
+ } = serverConfig || {};
176
+ entryNames.forEach(name => {
177
+ if (!ssgEntries.includes(name) && (ssr && (ssrByEntries === null || ssrByEntries === void 0 ? void 0 : ssrByEntries[name]) === false || !ssr && !(ssrByEntries !== null && ssrByEntries !== void 0 && ssrByEntries[name]))) {
178
+ chain.entryPoints.delete(name);
179
+ }
180
+ });
181
+ }
182
+ }
183
+ /**
184
+ * inject bottom template
185
+ */
186
+
187
+
188
+ function applyBottomHtmlWebpackPlugin({
189
+ api,
190
+ chain,
191
+ modernConfig,
192
+ appContext,
193
+ CHAIN_ID
194
+ }) {
195
+ // inject bottomTemplate into html-webpack-plugin
196
+ for (const entryName of Object.keys(api.context.entry)) {
197
+ // FIXME: the only need necessary
198
+ const baseTemplateParams = _objectSpread({
199
+ entryName,
200
+ title: (0, _utils.getEntryOptions)(entryName, modernConfig.output.title, modernConfig.output.titleByEntries, appContext.packageName),
201
+ mountId: modernConfig.output.mountId
202
+ }, (0, _utils.getEntryOptions)(entryName, modernConfig.output.templateParameters, modernConfig.output.templateParametersByEntries, appContext.packageName));
203
+
204
+ chain.plugin(`${CHAIN_ID.PLUGIN.HTML}-${entryName}`).tap(args => [_objectSpread(_objectSpread({}, args[0] || {}), {}, {
205
+ __internal__: true,
206
+ bottomTemplate: appContext.htmlTemplates[`__${entryName}-bottom__`] && (0, _lodash.template)(appContext.htmlTemplates[`__${entryName}-bottom__`])(baseTemplateParams)
207
+ })]);
208
+ }
209
+
210
+ chain.plugin(CHAIN_ID.PLUGIN.BOTTOM_TEMPLATE).use(_htmlBottomTemplate.BottomTemplatePlugin, [_htmlWebpackPlugin.default]);
211
+ }
212
+
213
+ const isStreamingSSR = userConfig => {
214
+ const isStreaming = ssr => ssr && typeof ssr === 'object' && ssr.mode === 'stream';
215
+
216
+ const {
217
+ server
218
+ } = userConfig;
219
+
220
+ if (isStreaming(server.ssr)) {
221
+ return true;
222
+ } // Since we cannot apply different plugins for different entries,
223
+ // we regard the whole app as streaming ssr only if one entry meets the requirement.
224
+
225
+
226
+ if (server !== null && server !== void 0 && server.ssrByEntries && typeof server.ssrByEntries === 'object') {
227
+ for (const name of Object.keys(server.ssrByEntries)) {
228
+ if (isStreaming(server.ssrByEntries[name])) {
229
+ return true;
230
+ }
231
+ }
232
+ }
233
+
234
+ return false;
235
+ };
236
+
237
+ function applyAsyncChunkHtmlPlugin({
238
+ chain,
239
+ modernConfig,
240
+ CHAIN_ID
241
+ }) {
242
+ if (isStreamingSSR(modernConfig)) {
243
+ chain.plugin(CHAIN_ID.PLUGIN.HTML_ASYNC_CHUNK).use(_htmlAsyncChunkPlugin.HtmlAsyncChunkPlugin, [_htmlWebpackPlugin.default]);
244
+ }
245
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createBuilderAppIcon = createBuilderAppIcon;
7
+ exports.createBuilderCrossorigin = createBuilderCrossorigin;
8
+ exports.createBuilderFavicon = createBuilderFavicon;
9
+ exports.createHtmlConfig = createHtmlConfig;
10
+
11
+ var _path = _interopRequireDefault(require("path"));
12
+
13
+ var _utils = require("@modern-js/utils");
14
+
15
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
+
17
+ function createHtmlConfig(normalizedConfig, appContext) {
18
+ const {
19
+ disableHtmlFolder,
20
+ favicon,
21
+ faviconByEntries,
22
+ inject,
23
+ injectByEntries,
24
+ meta,
25
+ metaByEntries,
26
+ mountId,
27
+ title,
28
+ titleByEntries,
29
+ scriptExt,
30
+ templateParameters,
31
+ templateParametersByEntries
32
+ } = normalizedConfig.output;
33
+ const {
34
+ configDir
35
+ } = normalizedConfig.source; // transform Modernjs `output.scriptExt` to Builder `html.crossorigin` configuration
36
+
37
+ const builderCrossorigin = createBuilderCrossorigin(scriptExt);
38
+ const builderAppIcon = createBuilderAppIcon(configDir, appContext);
39
+ const builderFavicon = createBuilderFavicon(favicon, configDir, appContext);
40
+ return {
41
+ appIcon: builderAppIcon,
42
+ disableHtmlFolder,
43
+ favicon: builderFavicon,
44
+ faviconByEntries,
45
+ inject,
46
+ injectByEntries,
47
+ meta,
48
+ metaByEntries,
49
+ mountId,
50
+ title,
51
+ titleByEntries,
52
+ crossorigin: builderCrossorigin,
53
+ templateByEntries: appContext.htmlTemplates,
54
+ templateParameters,
55
+ templateParametersByEntries: templateParametersByEntries
56
+ };
57
+ }
58
+
59
+ const ICON_EXTENSIONS = ['png', 'jpg', 'jpeg', 'svg', 'ico'];
60
+
61
+ function createBuilderAppIcon(configDir, appContext) {
62
+ const appIcon = (0, _utils.findExists)(ICON_EXTENSIONS.map(ext => _path.default.resolve(appContext.appDirectory, configDir, `icon.${ext}`)));
63
+ return typeof appIcon === 'string' ? appIcon : undefined;
64
+ }
65
+
66
+ function createBuilderCrossorigin(scriptExt) {
67
+ var _scriptExtCustomConfi;
68
+
69
+ const scriptExtCustomConfig = scriptExt === null || scriptExt === void 0 ? void 0 : scriptExt.custom;
70
+ return scriptExtCustomConfig !== null && scriptExtCustomConfig !== void 0 && (_scriptExtCustomConfi = scriptExtCustomConfig.test) !== null && _scriptExtCustomConfi !== void 0 && _scriptExtCustomConfi.test('.js') && (scriptExtCustomConfig === null || scriptExtCustomConfig === void 0 ? void 0 : scriptExtCustomConfig.attribute) === 'crossorigin' ? scriptExtCustomConfig.value : undefined;
71
+ }
72
+
73
+ function createBuilderFavicon(favicon, configDir, appContext) {
74
+ const defaultFavicon = (0, _utils.findExists)(ICON_EXTENSIONS.map(ext => _path.default.resolve(appContext.appDirectory, configDir, `favicon.${ext}`)));
75
+ return favicon || defaultFavicon || undefined;
76
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createOutputConfig = createOutputConfig;
7
+
8
+ var _share = require("./share");
9
+
10
+ function createOutputConfig(normalizedConfig, appContext) {
11
+ // TODO: add `externals` options in Modern.
12
+ const {
13
+ assetPrefix,
14
+ copy,
15
+ cssModuleLocalIdentName,
16
+ cssPath,
17
+ jsPath,
18
+ htmlPath,
19
+ mediaPath,
20
+ path,
21
+ disableInlineRuntimeChunk,
22
+ disableMinimize,
23
+ disableSourceMap,
24
+ enableCssModuleTSDeclaration,
25
+ enableInlineScripts,
26
+ enableInlineStyles,
27
+ polyfill,
28
+ dataUriLimit,
29
+ disableAssetsCache,
30
+ enableLatestDecorators,
31
+ disableCssModuleExtension
32
+ } = normalizedConfig.output;
33
+ const defaultCopyPattern = (0, _share.createCopyPattern)(appContext, normalizedConfig, 'upload');
34
+ const builderCopy = copy ? [...copy, defaultCopyPattern] : [defaultCopyPattern];
35
+ return {
36
+ assetPrefix,
37
+ copy: builderCopy,
38
+ distPath: {
39
+ root: path,
40
+ css: cssPath,
41
+ js: jsPath,
42
+ html: htmlPath,
43
+ // `@modern-js/webpack` output all media files to `dist/media` by default
44
+ svg: mediaPath || 'midia',
45
+ image: mediaPath || 'midia',
46
+ font: mediaPath || 'midia',
47
+ media: mediaPath || 'midia'
48
+ },
49
+ dataUriLimit: {
50
+ svg: dataUriLimit,
51
+ image: dataUriLimit,
52
+ font: dataUriLimit,
53
+ media: dataUriLimit
54
+ },
55
+ disableCssModuleExtension,
56
+ disableInlineRuntimeChunk,
57
+ disableMinimize,
58
+ disableSourceMap,
59
+ enableCssModuleTSDeclaration,
60
+ enableInlineScripts,
61
+ enableInlineStyles,
62
+ polyfill,
63
+ // We need to do this in the app-tools prepare hook because some files will be generated into the dist directory in the analyze process
64
+ cleanDistPath: false,
65
+ disableFilenameHash: disableAssetsCache,
66
+ enableLatestDecorators,
67
+ filename: {
68
+ css: cssModuleLocalIdentName
69
+ },
70
+ // `@modern-js/webpack` used to generate asset manifest by default
71
+ enableAssetManifest: true,
72
+ // compatible the modern-js with fallback behavior
73
+ enableAssetFallback: true
74
+ };
75
+ }