@modern-js/app-tools 1.21.5 → 2.0.0-beta.1

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 (170) hide show
  1. package/CHANGELOG.md +319 -51
  2. package/dist/js/modern/analyze/constants.js +1 -0
  3. package/dist/js/modern/analyze/generateCode.js +126 -32
  4. package/dist/js/modern/analyze/getBundleEntry.js +7 -14
  5. package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +173 -0
  6. package/dist/js/modern/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +13 -58
  7. package/dist/js/modern/analyze/getClientRoutes/index.js +2 -0
  8. package/dist/js/modern/analyze/getClientRoutes/utils.js +18 -0
  9. package/dist/js/modern/analyze/getFileSystemEntry.js +27 -21
  10. package/dist/js/modern/analyze/getHtmlTemplate.js +2 -9
  11. package/dist/js/modern/analyze/getServerRoutes.js +15 -32
  12. package/dist/js/modern/analyze/index.js +81 -23
  13. package/dist/js/modern/analyze/isDefaultExportFunction.js +0 -4
  14. package/dist/js/modern/analyze/makeLegalIdentifier.js +0 -2
  15. package/dist/js/modern/analyze/nestedRoutes.js +102 -0
  16. package/dist/js/modern/analyze/templates.js +151 -12
  17. package/dist/js/modern/analyze/utils.js +2 -8
  18. package/dist/js/modern/builder/builderPlugins/compatModern.js +208 -0
  19. package/dist/js/modern/builder/createHtmlConfig.js +58 -0
  20. package/dist/js/modern/builder/createOutputConfig.js +70 -0
  21. package/dist/js/modern/builder/createSourceConfig.js +74 -0
  22. package/dist/js/modern/builder/createToolsConfig.js +87 -0
  23. package/dist/js/modern/builder/index.js +108 -0
  24. package/dist/js/modern/builder/share.js +44 -0
  25. package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +29 -0
  26. package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +33 -0
  27. package/dist/js/modern/builder/webpackPlugins/routerPlugin.js +97 -0
  28. package/dist/js/modern/commands/build.js +16 -110
  29. package/dist/js/modern/commands/dev.js +12 -28
  30. package/dist/js/modern/commands/inspect.js +8 -66
  31. package/dist/js/modern/commands/start.js +2 -3
  32. package/dist/js/modern/index.js +17 -16
  33. package/dist/js/modern/locale/en.js +0 -1
  34. package/dist/js/modern/locale/zh.js +0 -1
  35. package/dist/js/modern/utils/commands.js +5 -0
  36. package/dist/js/modern/utils/config.js +1 -12
  37. package/dist/js/modern/utils/createServer.js +12 -1
  38. package/dist/js/modern/utils/getSpecifiedEntries.js +0 -6
  39. package/dist/js/modern/utils/printInstructions.js +3 -2
  40. package/dist/js/modern/utils/routes.js +0 -2
  41. package/dist/js/node/analyze/constants.js +3 -1
  42. package/dist/js/node/analyze/generateCode.js +123 -47
  43. package/dist/js/node/analyze/getBundleEntry.js +7 -20
  44. package/dist/js/node/analyze/getClientRoutes/getRoutes.js +181 -0
  45. package/dist/js/node/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +19 -78
  46. package/dist/js/node/analyze/getClientRoutes/index.js +19 -0
  47. package/dist/js/node/analyze/getClientRoutes/utils.js +28 -0
  48. package/dist/js/node/analyze/getFileSystemEntry.js +26 -28
  49. package/dist/js/node/analyze/getHtmlTemplate.js +2 -23
  50. package/dist/js/node/analyze/getServerRoutes.js +14 -40
  51. package/dist/js/node/analyze/index.js +81 -33
  52. package/dist/js/node/analyze/isDefaultExportFunction.js +0 -12
  53. package/dist/js/node/analyze/makeLegalIdentifier.js +0 -4
  54. package/dist/js/node/analyze/nestedRoutes.js +111 -0
  55. package/dist/js/node/analyze/templates.js +153 -21
  56. package/dist/js/node/analyze/utils.js +4 -20
  57. package/dist/js/node/builder/builderPlugins/compatModern.js +216 -0
  58. package/dist/js/node/builder/createHtmlConfig.js +68 -0
  59. package/dist/js/node/builder/createOutputConfig.js +76 -0
  60. package/dist/js/node/builder/createSourceConfig.js +82 -0
  61. package/dist/js/node/builder/createToolsConfig.js +94 -0
  62. package/dist/js/node/builder/index.js +117 -0
  63. package/dist/js/node/builder/share.js +51 -0
  64. package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +36 -0
  65. package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +40 -0
  66. package/dist/js/node/builder/webpackPlugins/routerPlugin.js +105 -0
  67. package/dist/js/node/commands/build.js +14 -121
  68. package/dist/js/node/commands/deploy.js +0 -2
  69. package/dist/js/node/commands/dev.js +10 -44
  70. package/dist/js/node/commands/index.js +0 -6
  71. package/dist/js/node/commands/inspect.js +10 -90
  72. package/dist/js/node/commands/start.js +2 -11
  73. package/dist/js/node/exports/server.js +0 -1
  74. package/dist/js/node/hooks.js +0 -2
  75. package/dist/js/node/index.js +22 -32
  76. package/dist/js/node/locale/en.js +0 -1
  77. package/dist/js/node/locale/index.js +0 -4
  78. package/dist/js/node/locale/zh.js +0 -1
  79. package/dist/js/node/utils/commands.js +12 -0
  80. package/dist/js/node/utils/config.js +1 -25
  81. package/dist/js/node/utils/createServer.js +15 -11
  82. package/dist/js/node/utils/getSpecifiedEntries.js +0 -9
  83. package/dist/js/node/utils/language.js +0 -2
  84. package/dist/js/node/utils/printInstructions.js +3 -6
  85. package/dist/js/node/utils/routes.js +0 -5
  86. package/dist/js/treeshaking/analyze/constants.js +16 -0
  87. package/dist/js/treeshaking/analyze/generateCode.js +369 -0
  88. package/dist/js/treeshaking/analyze/getBundleEntry.js +69 -0
  89. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +184 -0
  90. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +185 -0
  91. package/dist/js/treeshaking/analyze/getClientRoutes/index.js +2 -0
  92. package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +24 -0
  93. package/dist/js/treeshaking/analyze/getFileSystemEntry.js +96 -0
  94. package/dist/js/treeshaking/analyze/getHtmlTemplate.js +129 -0
  95. package/dist/js/treeshaking/analyze/getServerRoutes.js +157 -0
  96. package/dist/js/treeshaking/analyze/index.js +334 -0
  97. package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +28 -0
  98. package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +16 -0
  99. package/dist/js/treeshaking/analyze/nestedRoutes.js +165 -0
  100. package/dist/js/treeshaking/analyze/templates.js +170 -0
  101. package/dist/js/treeshaking/analyze/utils.js +88 -0
  102. package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +212 -0
  103. package/dist/js/treeshaking/builder/createHtmlConfig.js +59 -0
  104. package/dist/js/treeshaking/builder/createOutputConfig.js +70 -0
  105. package/dist/js/treeshaking/builder/createSourceConfig.js +88 -0
  106. package/dist/js/treeshaking/builder/createToolsConfig.js +85 -0
  107. package/dist/js/treeshaking/builder/index.js +161 -0
  108. package/dist/js/treeshaking/builder/share.js +46 -0
  109. package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +46 -0
  110. package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +41 -0
  111. package/dist/js/treeshaking/builder/webpackPlugins/routerPlugin.js +121 -0
  112. package/dist/js/treeshaking/commands/build.js +83 -0
  113. package/dist/js/treeshaking/commands/deploy.js +26 -0
  114. package/dist/js/treeshaking/commands/dev.js +124 -0
  115. package/dist/js/treeshaking/commands/index.js +3 -0
  116. package/dist/js/treeshaking/commands/inspect.js +29 -0
  117. package/dist/js/treeshaking/commands/start.js +69 -0
  118. package/dist/js/treeshaking/exports/server.js +1 -0
  119. package/dist/js/treeshaking/hooks.js +21 -0
  120. package/dist/js/treeshaking/index.js +267 -0
  121. package/dist/js/treeshaking/locale/en.js +34 -0
  122. package/dist/js/treeshaking/locale/index.js +9 -0
  123. package/dist/js/treeshaking/locale/zh.js +34 -0
  124. package/dist/js/treeshaking/utils/commands.js +5 -0
  125. package/dist/js/treeshaking/utils/config.js +124 -0
  126. package/dist/js/treeshaking/utils/createServer.js +73 -0
  127. package/dist/js/treeshaking/utils/getSpecifiedEntries.js +58 -0
  128. package/dist/js/treeshaking/utils/language.js +5 -0
  129. package/dist/js/treeshaking/utils/printInstructions.js +30 -0
  130. package/dist/js/treeshaking/utils/routes.js +29 -0
  131. package/dist/js/treeshaking/utils/types.js +0 -0
  132. package/dist/types/analyze/constants.d.ts +1 -0
  133. package/dist/types/analyze/generateCode.d.ts +1 -1
  134. package/dist/types/analyze/{getClientRoutes.d.ts → getClientRoutes/getRoutes.d.ts} +2 -7
  135. package/dist/types/analyze/getClientRoutes/getRoutesLegacy.d.ts +15 -0
  136. package/dist/types/analyze/getClientRoutes/index.d.ts +2 -0
  137. package/dist/types/analyze/getClientRoutes/utils.d.ts +5 -0
  138. package/dist/types/analyze/index.d.ts +4 -5
  139. package/dist/types/analyze/nestedRoutes.d.ts +5 -0
  140. package/dist/types/analyze/templates.d.ts +19 -3
  141. package/dist/types/analyze/utils.d.ts +2 -1
  142. package/dist/types/builder/builderPlugins/compatModern.d.ts +13 -0
  143. package/dist/types/builder/createHtmlConfig.d.ts +6 -0
  144. package/dist/types/builder/createOutputConfig.d.ts +3 -0
  145. package/dist/types/builder/createSourceConfig.d.ts +5 -0
  146. package/dist/types/builder/createToolsConfig.d.ts +13 -0
  147. package/dist/types/builder/index.d.ts +15 -0
  148. package/dist/types/builder/share.d.ts +26 -0
  149. package/dist/types/builder/webpackPlugins/htmlAsyncChunkPlugin.d.ts +8 -0
  150. package/dist/types/builder/webpackPlugins/htmlBottomTemplate.d.ts +10 -0
  151. package/dist/types/builder/webpackPlugins/routerPlugin.d.ts +10 -0
  152. package/dist/types/commands/build.d.ts +2 -1
  153. package/dist/types/commands/deploy.d.ts +2 -1
  154. package/dist/types/commands/dev.d.ts +2 -1
  155. package/dist/types/commands/inspect.d.ts +2 -6
  156. package/dist/types/commands/start.d.ts +2 -1
  157. package/dist/types/hooks.d.ts +15 -10
  158. package/dist/types/index.d.ts +4 -4
  159. package/dist/types/locale/en.d.ts +0 -1
  160. package/dist/types/locale/index.d.ts +0 -2
  161. package/dist/types/locale/zh.d.ts +0 -1
  162. package/dist/types/utils/commands.d.ts +1 -0
  163. package/dist/types/utils/config.d.ts +0 -1
  164. package/dist/types/utils/createServer.d.ts +8 -1
  165. package/dist/types/utils/printInstructions.d.ts +3 -2
  166. package/dist/types/utils/types.d.ts +2 -3
  167. package/package.json +25 -42
  168. package/dist/js/modern/utils/createCompiler.js +0 -61
  169. package/dist/js/node/utils/createCompiler.js +0 -81
  170. package/dist/types/utils/createCompiler.d.ts +0 -13
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createBuilderInclude = createBuilderInclude;
7
+ exports.createBuilderModuleScope = createBuilderModuleScope;
8
+ exports.createSourceConfig = createSourceConfig;
9
+ var _path = require("path");
10
+ var _utils = require("@modern-js/utils");
11
+ function createSourceConfig(normalizedConfig, appContext) {
12
+ const {
13
+ alias,
14
+ envVars,
15
+ globalVars,
16
+ include,
17
+ moduleScopes,
18
+ preEntry
19
+ } = normalizedConfig.source;
20
+ const builderGlobalVars = globalVars || {};
21
+ for (const envVar of envVars || []) {
22
+ const envVarValue = process.env[envVar];
23
+ envVarValue && (builderGlobalVars[`process.env.${envVar}`] = envVarValue);
24
+ }
25
+ const builderModuleScope = createBuilderModuleScope(moduleScopes);
26
+ const builderInclude = createBuilderInclude(include, appContext);
27
+ return {
28
+ alias: (0, _utils.mergeAlias)(alias),
29
+ moduleScopes: builderModuleScope,
30
+ globalVars: builderGlobalVars,
31
+ include: builderInclude,
32
+ preEntry,
33
+ // ensure resolve.extensions same as before
34
+ resolveExtensionPrefix: '.web'
35
+ };
36
+ }
37
+ function createBuilderInclude(include, appContext) {
38
+ const defaultInclude = [appContext.internalDirectory];
39
+ const transformInclude = (include || []).map(include => {
40
+ if (typeof include === 'string') {
41
+ if ((0, _path.isAbsolute)(include)) {
42
+ return include;
43
+ }
44
+ return new RegExp(include);
45
+ }
46
+ return include;
47
+ }).concat(defaultInclude); // concat default Include
48
+
49
+ const root = (0, _utils.findMonorepoRoot)(appContext.appDirectory);
50
+ if (!root) {
51
+ return transformInclude;
52
+ }
53
+ const modernjsMonorepo = (0, _utils.isModernjsMonorepo)(root);
54
+ if (modernjsMonorepo) {
55
+ const paths = _utils.globby.sync(_path.posix.join(root, 'features', '**', 'package.json'), {
56
+ ignore: ['**/node_modules/**/*']
57
+ }).map(pathname => (0, _path.dirname)(pathname) + _path.sep);
58
+ return [...paths, ...transformInclude];
59
+ }
60
+ return transformInclude;
61
+ }
62
+ function createBuilderModuleScope(moduleScopes) {
63
+ if (moduleScopes) {
64
+ let builderModuleScope = [];
65
+ const DEFAULT_SCOPES = ['./src', './shared', /node_modules/];
66
+ if (Array.isArray(moduleScopes)) {
67
+ if (isPrimitiveScope(moduleScopes)) {
68
+ builderModuleScope = DEFAULT_SCOPES.concat(moduleScopes);
69
+ } else {
70
+ builderModuleScope = [DEFAULT_SCOPES, ...moduleScopes];
71
+ }
72
+ } else {
73
+ builderModuleScope = [DEFAULT_SCOPES, moduleScopes];
74
+ }
75
+ return builderModuleScope;
76
+ } else {
77
+ return undefined;
78
+ }
79
+ function isPrimitiveScope(items) {
80
+ return items.every(item => typeof item === 'string' || Object.prototype.toString.call(item) === '[object RegExp]');
81
+ }
82
+ }
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createBuilderTsChecker = createBuilderTsChecker;
7
+ exports.createToolsConfig = createToolsConfig;
8
+ var _utils = require("@modern-js/utils");
9
+ 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; }
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
+ 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; }
12
+ function createToolsConfig(normalizedConfig) {
13
+ const {
14
+ disableCssExtract,
15
+ enableTsLoader
16
+ } = normalizedConfig.output;
17
+ const {
18
+ autoprefixer,
19
+ babel,
20
+ minifyCss,
21
+ terser,
22
+ webpack,
23
+ webpackChain,
24
+ tsLoader,
25
+ styledComponents,
26
+ sass,
27
+ postcss,
28
+ less,
29
+ htmlPlugin,
30
+ // TODO: remove modernjs tools.lodash config
31
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
32
+ lodash
33
+ } = normalizedConfig.tools;
34
+ const builderTsLoader = createBuilderTsLoader(tsLoader, enableTsLoader);
35
+ const builderTsChecker = createBuilderTsChecker(normalizedConfig.output);
36
+ return {
37
+ tsChecker: builderTsChecker,
38
+ styleLoader: disableCssExtract ? {} : undefined,
39
+ cssExtract: disableCssExtract ? false : undefined,
40
+ autoprefixer,
41
+ babel,
42
+ minifyCss,
43
+ terser,
44
+ webpack,
45
+ webpackChain: webpackChain,
46
+ tsLoader: builderTsLoader,
47
+ styledComponents: styledComponents,
48
+ sass: sass,
49
+ postcss: postcss,
50
+ less: less,
51
+ // can't remove comment in html minify.
52
+ // some html template insert by using those comments.
53
+ htmlPlugin: [config => _objectSpread(_objectSpread({}, config), {}, {
54
+ minify: typeof config.minify === 'object' ? _objectSpread(_objectSpread({}, config.minify), {}, {
55
+ removeComments: false
56
+ }) : config.minify
57
+ }),
58
+ // eslint-disable-next-line no-nested-ternary
59
+ ...(Array.isArray(htmlPlugin) ? htmlPlugin : htmlPlugin ? [htmlPlugin] : [])]
60
+ };
61
+ }
62
+ function createBuilderTsLoader(tsLoader, enableTsLoader) {
63
+ const useTsLoader = Boolean(enableTsLoader);
64
+ if (!useTsLoader) {
65
+ return undefined;
66
+ }
67
+ const defaultTsLoader = {
68
+ compilerOptions: {
69
+ target: 'es5',
70
+ module: 'ESNext'
71
+ },
72
+ transpileOnly: false,
73
+ allowTsInNodeModules: true
74
+ };
75
+ return (_, utils) => (0, _utils.applyOptionsChain)(defaultTsLoader, tsLoader, utils);
76
+ }
77
+ function createBuilderTsChecker(output) {
78
+ if (output.enableTsLoader) {
79
+ return false;
80
+ }
81
+ const defaultTsChecker = {
82
+ issue: {
83
+ include: [{
84
+ file: '**/src/**/*'
85
+ }],
86
+ exclude: [{
87
+ file: '**/*.(spec|test).ts'
88
+ }, {
89
+ file: '**/node_modules/**/*'
90
+ }]
91
+ }
92
+ };
93
+ return defaultTsChecker;
94
+ }
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createBuilderForEdenX = createBuilderForEdenX;
7
+ exports.createBuilderOptions = createBuilderOptions;
8
+ var _builder = require("@modern-js/builder");
9
+ var _builderWebpackProvider = require("@modern-js/builder-webpack-provider");
10
+ var _utils = require("@modern-js/utils");
11
+ var _compatModern = require("./builderPlugins/compatModern");
12
+ var _createHtmlConfig = require("./createHtmlConfig");
13
+ var _createOutputConfig = require("./createOutputConfig");
14
+ var _createSourceConfig = require("./createSourceConfig");
15
+ var _createToolsConfig = require("./createToolsConfig");
16
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
17
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
18
+ function getBuilderTargets(normalizedConfig) {
19
+ const targets = ['web'];
20
+ if (normalizedConfig.output.enableModernMode && !targets.includes('modern-web')) {
21
+ targets.push('modern-web');
22
+ }
23
+ if ((0, _utils.isUseSSRBundle)(normalizedConfig)) {
24
+ targets.push('node');
25
+ }
26
+ return targets;
27
+ }
28
+ async function createBuilderForEdenX({
29
+ normalizedConfig,
30
+ appContext,
31
+ compatPluginConfig
32
+ }) {
33
+ const builderConfig = createBuilderProviderConfig(normalizedConfig, appContext);
34
+ // create webpack provider
35
+ const webpackProvider = (0, _builderWebpackProvider.builderWebpackProvider)({
36
+ builderConfig
37
+ });
38
+ const target = getBuilderTargets(normalizedConfig);
39
+ const builderOptions = createBuilderOptions(target, appContext);
40
+ const builder = await (0, _builder.createBuilder)(webpackProvider, builderOptions);
41
+ await applyBuilderPlugins(builder, normalizedConfig, appContext, compatPluginConfig);
42
+ return builder;
43
+ }
44
+ function createBuilderProviderConfig(normalizedConfig, appContext) {
45
+ const source = (0, _createSourceConfig.createSourceConfig)(normalizedConfig, appContext);
46
+ const html = (0, _createHtmlConfig.createHtmlConfig)(normalizedConfig, appContext);
47
+ const output = (0, _createOutputConfig.createOutputConfig)(normalizedConfig, appContext);
48
+ const tools = (0, _createToolsConfig.createToolsConfig)(normalizedConfig);
49
+ return {
50
+ source,
51
+ html,
52
+ output,
53
+ tools,
54
+ dev: {
55
+ https: normalizedConfig.dev.https,
56
+ assetPrefix: normalizedConfig.dev.assetPrefix
57
+ },
58
+ performance: {
59
+ // `@modern-js/webpack` used to remove moment locale by default
60
+ removeMomentLocale: true
61
+ }
62
+ };
63
+ }
64
+ function createBuilderOptions(target, appContext) {
65
+ // create entries
66
+
67
+ const entries = {};
68
+ const {
69
+ entrypoints = [],
70
+ checkedEntries
71
+ } = appContext;
72
+ for (const {
73
+ entryName,
74
+ entry
75
+ } of entrypoints) {
76
+ if (checkedEntries && !checkedEntries.includes(entryName)) {
77
+ continue;
78
+ }
79
+ if (entryName in entries) {
80
+ entries[entryName].push(entry);
81
+ } else {
82
+ entries[entryName] = [entry];
83
+ }
84
+ }
85
+ return {
86
+ cwd: appContext.appDirectory,
87
+ target,
88
+ configPath: appContext.configFile || undefined,
89
+ entry: entries,
90
+ framework: appContext.metaName
91
+ };
92
+ }
93
+
94
+ /**
95
+ * register builder Plugin by condition
96
+ */
97
+ async function applyBuilderPlugins(builder, normalizedConfig, appContext, compatPluginConfig) {
98
+ if (!normalizedConfig.output.disableNodePolyfill) {
99
+ const {
100
+ PluginNodePolyfill
101
+ } = await Promise.resolve().then(() => _interopRequireWildcard(require('@modern-js/builder-plugin-node-polyfill')));
102
+ builder.addPlugins([PluginNodePolyfill()]);
103
+ }
104
+ if (normalizedConfig.tools.esbuild) {
105
+ const {
106
+ esbuild: esbuildOptions
107
+ } = normalizedConfig.tools;
108
+ const {
109
+ PluginEsbuild
110
+ } = await Promise.resolve().then(() => _interopRequireWildcard(require('@modern-js/builder-plugin-esbuild')));
111
+ builder.addPlugins([PluginEsbuild({
112
+ loader: false,
113
+ minimize: (0, _utils.applyOptionsChain)({}, esbuildOptions)
114
+ })]);
115
+ }
116
+ builder.addPlugins([(0, _compatModern.PluginCompatModern)(appContext, normalizedConfig, compatPluginConfig)]);
117
+ }
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createCopyPattern = createCopyPattern;
7
+ var _path = _interopRequireDefault(require("path"));
8
+ var _lodash = require("@modern-js/utils/lodash");
9
+ var _utils = require("@modern-js/utils");
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ function createCopyPattern(appContext, config, patternsType, chain) {
12
+ const configDir = _path.default.resolve(appContext.appDirectory, config.source.configDir);
13
+ const uploadDir = _path.default.posix.join(configDir.replace(/\\/g, '/'), 'upload');
14
+ const publicDir = _path.default.posix.join(configDir.replace(/\\/g, '/'), 'public');
15
+ const minifiedJsRexExp = /\.min\.js/;
16
+ const info = file => ({
17
+ // If the file name ends with `.min.js`, we assume it's a compressed file.
18
+ // So we don't want copy-webpack-plugin to minify it.
19
+ // ref: https://github.com/webpack-contrib/copy-webpack-plugin#info
20
+ minimized: minifiedJsRexExp.test(file.sourceFilename)
21
+ });
22
+ if (patternsType === 'public') {
23
+ if (!chain) {
24
+ throw new Error("expect get a webpackChain, but receive 'undefined'");
25
+ }
26
+ return {
27
+ info,
28
+ from: '**/*',
29
+ to: 'public',
30
+ context: publicDir,
31
+ noErrorOnMissing: true,
32
+ // eslint-disable-next-line node/prefer-global/buffer
33
+ transform: (content, absoluteFrom) => {
34
+ if (!/\.html?$/.test(absoluteFrom)) {
35
+ return content;
36
+ }
37
+ return (0, _lodash.template)(content.toString('utf8'))({
38
+ assetPrefix: (0, _utils.removeTailSlash)(chain.output.get('publicPath'))
39
+ });
40
+ }
41
+ };
42
+ } else {
43
+ return {
44
+ info,
45
+ from: '**/*',
46
+ to: 'upload',
47
+ context: uploadDir,
48
+ noErrorOnMissing: true
49
+ };
50
+ }
51
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.HtmlAsyncChunkPlugin = void 0;
7
+ 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; }
8
+ class HtmlAsyncChunkPlugin {
9
+ constructor(htmlWebpackPlugin) {
10
+ _defineProperty(this, "name", void 0);
11
+ _defineProperty(this, "htmlWebpackPlugin", void 0);
12
+ this.name = 'HtmlAsyncChunkPlugin';
13
+ this.htmlWebpackPlugin = htmlWebpackPlugin;
14
+ }
15
+ apply(compiler) {
16
+ compiler.hooks.compilation.tap(this.name, compilation => {
17
+ const hooks = this.htmlWebpackPlugin.getHooks(compilation);
18
+ hooks.alterAssetTagGroups.tap(this.name, assets => {
19
+ const tags = [...assets.headTags, ...assets.bodyTags];
20
+ for (const tag of tags) {
21
+ if (tag.tagName === 'script') {
22
+ const {
23
+ attributes
24
+ } = tag;
25
+ if (attributes && attributes.defer === true) {
26
+ attributes.async = true;
27
+ delete attributes.defer;
28
+ }
29
+ }
30
+ }
31
+ return assets;
32
+ });
33
+ });
34
+ }
35
+ }
36
+ exports.HtmlAsyncChunkPlugin = HtmlAsyncChunkPlugin;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.BottomTemplatePlugin = void 0;
7
+ 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; }
8
+ class BottomTemplatePlugin {
9
+ constructor(htmlWebpackPlugin) {
10
+ _defineProperty(this, "htmlWebpackPlugin", void 0);
11
+ _defineProperty(this, "bottomTemplateReg", /<!--<\?-\s*bottomTemplate\s*\?>-->/);
12
+ _defineProperty(this, "bodyRegExp", /(<\/\s*body\s*>)/i);
13
+ _defineProperty(this, "name", void 0);
14
+ this.htmlWebpackPlugin = htmlWebpackPlugin;
15
+ this.name = 'bottom-template';
16
+ }
17
+ apply(compiler) {
18
+ compiler.hooks.compilation.tap(this.name, compilation => {
19
+ this.htmlWebpackPlugin.getHooks(compilation).beforeEmit.tap(this.name, data => {
20
+ var _data$plugin$options;
21
+ if (!((_data$plugin$options = data.plugin.options) !== null && _data$plugin$options !== void 0 && _data$plugin$options.__internal__)) {
22
+ return data;
23
+ }
24
+ // 含有 <!--<?- bottomTemplate ?>--> 占位符时才需要注入 bottom.html
25
+ if (this.bottomTemplateReg.test(data.html)) {
26
+ // 清空占位符
27
+ data.html = data.html.replace(this.bottomTemplateReg, '');
28
+ const {
29
+ bottomTemplate
30
+ } = data.plugin.options;
31
+ if (bottomTemplate) {
32
+ data.html = data.html.replace(this.bodyRegExp, match => `\n${bottomTemplate}\n${match}`);
33
+ }
34
+ }
35
+ return data;
36
+ });
37
+ });
38
+ }
39
+ }
40
+ exports.BottomTemplatePlugin = BottomTemplatePlugin;
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _path = _interopRequireDefault(require("path"));
8
+ var _utils = require("@modern-js/utils");
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ 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; }
11
+ const PLUGIN_NAME = 'ModernjsRoutePlugin';
12
+ class RouterPlugin {
13
+ constructor(options) {
14
+ _defineProperty(this, "existNestedRoutes", void 0);
15
+ this.existNestedRoutes = options.existNestedRoutes;
16
+ }
17
+ apply(compiler) {
18
+ const {
19
+ existNestedRoutes
20
+ } = this;
21
+ const {
22
+ target
23
+ } = compiler.options;
24
+ if (target === 'node' || Array.isArray(target) && target.includes('node')) {
25
+ return;
26
+ }
27
+ if (!existNestedRoutes) {
28
+ return;
29
+ }
30
+ const {
31
+ webpack
32
+ } = compiler;
33
+ const {
34
+ Compilation,
35
+ sources
36
+ } = webpack;
37
+ const {
38
+ RawSource
39
+ } = sources;
40
+ const {
41
+ PROCESS_ASSETS_STAGE_REPORT
42
+ } = Compilation;
43
+ const outputPath = compiler.options.output.path;
44
+ const newAssetsMap = new Map();
45
+ const normalizePath = path => {
46
+ if (!path.endsWith('/')) {
47
+ return `${path}/`;
48
+ }
49
+ return path;
50
+ };
51
+ compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
52
+ compilation.hooks.processAssets.tapPromise({
53
+ name: PLUGIN_NAME,
54
+ stage: PROCESS_ASSETS_STAGE_REPORT
55
+ }, async () => {
56
+ const stats = compilation.getStats().toJson({
57
+ chunkGroups: true,
58
+ chunks: true
59
+ });
60
+ const {
61
+ publicPath
62
+ } = stats;
63
+ const routeAssets = {};
64
+ const {
65
+ namedChunkGroups,
66
+ assetsByChunkName
67
+ } = stats;
68
+ if (!namedChunkGroups || !assetsByChunkName) {
69
+ _utils.logger.warn('Route manifest does not exist, performance will be affected');
70
+ return;
71
+ }
72
+ for (const [name, chunkGroup] of Object.entries(namedChunkGroups)) {
73
+ routeAssets[name] = {
74
+ chunkIds: chunkGroup.chunks,
75
+ assets: assetsByChunkName[name].map(item => publicPath ? normalizePath(publicPath) + item : item)
76
+ };
77
+ }
78
+ const manifest = {
79
+ routeAssets
80
+ };
81
+ const injectedContent = `
82
+ ;(function(){
83
+ window.${_utils.ROUTE_MANIFEST} = ${JSON.stringify(manifest)};
84
+ })();
85
+ `;
86
+ const entrypointsArray = Array.from(compilation.entrypoints.entries());
87
+ const entryChunkIds = entrypointsArray.map(entrypoint => entrypoint[0]);
88
+ const entryChunks = [...compilation.chunks].filter(chunk => {
89
+ return entryChunkIds.includes(chunk.name);
90
+ });
91
+ const entryChunkFiles = entryChunks.map(chunk => [...chunk.files][0]);
92
+ for (const file of entryChunkFiles) {
93
+ const asset = compilation.assets[file];
94
+ const newContent = `${injectedContent}${asset.source().toString()}`;
95
+ newAssetsMap.set(_path.default.join(outputPath, file), newContent);
96
+ compilation.updateAsset(file, new RawSource(newContent));
97
+ }
98
+ const filename = _path.default.join(outputPath, _utils.ROUTE_MINIFEST_FILE);
99
+ await _utils.fs.ensureFile(filename);
100
+ await _utils.fs.writeFile(filename, JSON.stringify(manifest, null, 2));
101
+ });
102
+ });
103
+ }
104
+ }
105
+ exports.default = RouterPlugin;