@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
@@ -0,0 +1,99 @@
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
+
10
+ var _path = require("path");
11
+
12
+ var _utils = require("@modern-js/utils");
13
+
14
+ function createSourceConfig(normalizedConfig, appContext) {
15
+ const {
16
+ alias,
17
+ envVars,
18
+ globalVars,
19
+ include,
20
+ moduleScopes,
21
+ preEntry
22
+ } = normalizedConfig.source;
23
+ const builderGlobalVars = globalVars || {};
24
+
25
+ for (const envVar of envVars || []) {
26
+ const envVarValue = process.env[envVar];
27
+ envVarValue && (builderGlobalVars[`process.env.${envVar}`] = envVarValue);
28
+ }
29
+
30
+ const builderModuleScope = createBuilderModuleScope(moduleScopes);
31
+ const builderInclude = createBuilderInclude(include, appContext);
32
+ return {
33
+ alias: (0, _utils.mergeAlias)(alias),
34
+ moduleScopes: builderModuleScope,
35
+ globalVars: builderGlobalVars,
36
+ include: builderInclude,
37
+ preEntry,
38
+ // ensure resolve.extensions same as before
39
+ resolveExtensionPrefix: '.web'
40
+ };
41
+ }
42
+
43
+ function createBuilderInclude(include, appContext) {
44
+ const defaultInclude = [appContext.internalDirectory];
45
+ const transformInclude = (include || []).map(include => {
46
+ if (typeof include === 'string') {
47
+ if ((0, _path.isAbsolute)(include)) {
48
+ return include;
49
+ }
50
+
51
+ return new RegExp(include);
52
+ }
53
+
54
+ return include;
55
+ }).concat(defaultInclude); // concat default Include
56
+
57
+ const root = (0, _utils.findMonorepoRoot)(appContext.appDirectory);
58
+
59
+ if (!root) {
60
+ return transformInclude;
61
+ }
62
+
63
+ const modernjsMonorepo = (0, _utils.isModernjsMonorepo)(root);
64
+
65
+ if (modernjsMonorepo) {
66
+ const paths = _utils.globby.sync(_path.posix.join(root, 'features', '**', 'package.json'), {
67
+ ignore: ['**/node_modules/**/*']
68
+ }).map(pathname => (0, _path.dirname)(pathname) + _path.sep);
69
+
70
+ return [...paths, ...transformInclude];
71
+ }
72
+
73
+ return transformInclude;
74
+ }
75
+
76
+ function createBuilderModuleScope(moduleScopes) {
77
+ if (moduleScopes) {
78
+ let builderModuleScope = [];
79
+ const DEFAULT_SCOPES = ['./src', './shared', /node_modules/];
80
+
81
+ if (Array.isArray(moduleScopes)) {
82
+ if (isPrimitiveScope(moduleScopes)) {
83
+ builderModuleScope = DEFAULT_SCOPES.concat(moduleScopes);
84
+ } else {
85
+ builderModuleScope = [DEFAULT_SCOPES, ...moduleScopes];
86
+ }
87
+ } else {
88
+ builderModuleScope = [DEFAULT_SCOPES, moduleScopes];
89
+ }
90
+
91
+ return builderModuleScope;
92
+ } else {
93
+ return undefined;
94
+ }
95
+
96
+ function isPrimitiveScope(items) {
97
+ return items.every(item => typeof item === 'string' || Object.prototype.toString.call(item) === '[object RegExp]');
98
+ }
99
+ }
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createBuilderTsChecker = createBuilderTsChecker;
7
+ exports.createToolsConfig = createToolsConfig;
8
+
9
+ var _utils = require("@modern-js/utils");
10
+
11
+ 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; }
12
+
13
+ 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; }
14
+
15
+ 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; }
16
+
17
+ function createToolsConfig(normalizedConfig) {
18
+ const {
19
+ disableCssExtract,
20
+ enableTsLoader
21
+ } = normalizedConfig.output;
22
+ const {
23
+ autoprefixer,
24
+ babel,
25
+ minifyCss,
26
+ terser,
27
+ webpack,
28
+ webpackChain,
29
+ tsLoader,
30
+ styledComponents,
31
+ sass,
32
+ postcss,
33
+ less,
34
+ htmlPlugin,
35
+ // TODO: remove modernjs tools.lodash config
36
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
37
+ lodash
38
+ } = normalizedConfig.tools;
39
+ const builderTsLoader = createBuilderTsLoader(tsLoader, enableTsLoader);
40
+ const builderTsChecker = createBuilderTsChecker(normalizedConfig.output);
41
+ return {
42
+ tsChecker: builderTsChecker,
43
+ styleLoader: disableCssExtract ? {} : undefined,
44
+ cssExtract: disableCssExtract ? false : undefined,
45
+ autoprefixer,
46
+ babel,
47
+ minifyCss,
48
+ terser,
49
+ webpack,
50
+ webpackChain: webpackChain,
51
+ tsLoader: builderTsLoader,
52
+ styledComponents: styledComponents,
53
+ sass: sass,
54
+ postcss: postcss,
55
+ less: less,
56
+ // can't remove comment in html minify.
57
+ // some html template insert by using those comments.
58
+ htmlPlugin: [config => _objectSpread(_objectSpread({}, config), {}, {
59
+ minify: typeof config.minify === 'object' ? _objectSpread(_objectSpread({}, config.minify), {}, {
60
+ removeComments: false
61
+ }) : config.minify
62
+ }), // eslint-disable-next-line no-nested-ternary
63
+ ...(Array.isArray(htmlPlugin) ? htmlPlugin : htmlPlugin ? [htmlPlugin] : [])]
64
+ };
65
+ }
66
+
67
+ function createBuilderTsLoader(tsLoader, enableTsLoader) {
68
+ const useTsLoader = Boolean(enableTsLoader);
69
+
70
+ if (!useTsLoader) {
71
+ return undefined;
72
+ }
73
+
74
+ const defaultTsLoader = {
75
+ compilerOptions: {
76
+ target: 'es5',
77
+ module: 'ESNext'
78
+ },
79
+ transpileOnly: false,
80
+ allowTsInNodeModules: true
81
+ };
82
+ return (_, utils) => (0, _utils.applyOptionsChain)(defaultTsLoader, tsLoader, utils);
83
+ }
84
+
85
+ function createBuilderTsChecker(output) {
86
+ if (output.enableTsLoader || output.disableTsChecker) {
87
+ return false;
88
+ }
89
+
90
+ const defaultTsChecker = {
91
+ issue: {
92
+ include: [{
93
+ file: '**/src/**/*'
94
+ }],
95
+ exclude: [{
96
+ file: '**/*.(spec|test).ts'
97
+ }, {
98
+ file: '**/node_modules/**/*'
99
+ }]
100
+ }
101
+ };
102
+ return defaultTsChecker;
103
+ }
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createBuilderOptions = createBuilderOptions;
7
+ exports.default = void 0;
8
+
9
+ var _builder = require("@modern-js/builder");
10
+
11
+ var _builderWebpackProvider = require("@modern-js/builder-webpack-provider");
12
+
13
+ var _utils = require("@modern-js/utils");
14
+
15
+ var _compatModern = require("./builderPlugins/compatModern");
16
+
17
+ var _createHtmlConfig = require("./createHtmlConfig");
18
+
19
+ var _createOutputConfig = require("./createOutputConfig");
20
+
21
+ var _createSourceConfig = require("./createSourceConfig");
22
+
23
+ var _createToolsConfig = require("./createToolsConfig");
24
+
25
+ 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); }
26
+
27
+ 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; }
28
+
29
+ var _default = async ({
30
+ target: _target = 'web',
31
+ normalizedConfig,
32
+ appContext,
33
+ compatPluginConfig
34
+ }) => {
35
+ const targets = Array.isArray(_target) ? _target : [_target];
36
+
37
+ if (normalizedConfig.output.enableModernMode && !targets.includes('modern-web')) {
38
+ targets.push('modern-web');
39
+ }
40
+
41
+ const builderConfig = createBuilderProviderConfig(normalizedConfig, appContext); // create webpack provider
42
+
43
+ const webpackProvider = (0, _builderWebpackProvider.builderWebpackProvider)({
44
+ builderConfig
45
+ });
46
+ const builderOptions = createBuilderOptions(_target, appContext);
47
+ const builder = await (0, _builder.createBuilder)(webpackProvider, builderOptions);
48
+ await applyBuilderPlugins(builder, normalizedConfig, appContext, compatPluginConfig);
49
+ return builder;
50
+ };
51
+
52
+ exports.default = _default;
53
+
54
+ function createBuilderProviderConfig(normalizedConfig, appContext) {
55
+ const source = (0, _createSourceConfig.createSourceConfig)(normalizedConfig, appContext);
56
+ const html = (0, _createHtmlConfig.createHtmlConfig)(normalizedConfig, appContext);
57
+ const output = (0, _createOutputConfig.createOutputConfig)(normalizedConfig, appContext);
58
+ const tools = (0, _createToolsConfig.createToolsConfig)(normalizedConfig);
59
+ return {
60
+ source,
61
+ html,
62
+ output,
63
+ tools,
64
+ performance: {
65
+ // `@modern-js/webpack` used to remove moment locale by default
66
+ removeMomentLocale: true
67
+ }
68
+ };
69
+ }
70
+
71
+ function createBuilderOptions(target, appContext) {
72
+ // create entries
73
+ const entries = {};
74
+ const {
75
+ entrypoints = [],
76
+ checkedEntries
77
+ } = appContext;
78
+
79
+ for (const {
80
+ entryName,
81
+ entry
82
+ } of entrypoints) {
83
+ if (checkedEntries && !checkedEntries.includes(entryName)) {
84
+ continue;
85
+ }
86
+
87
+ if (entryName in entries) {
88
+ entries[entryName].push(entry);
89
+ } else {
90
+ entries[entryName] = [entry];
91
+ }
92
+ }
93
+
94
+ return {
95
+ cwd: appContext.appDirectory,
96
+ target,
97
+ configPath: appContext.configFile || undefined,
98
+ entry: entries,
99
+ framework: appContext.metaName
100
+ };
101
+ }
102
+ /**
103
+ * register builder Plugin by condition
104
+ */
105
+
106
+
107
+ async function applyBuilderPlugins(builder, normalizedConfig, appContext, compatPluginConfig) {
108
+ if (!normalizedConfig.output.disableNodePolyfill) {
109
+ const {
110
+ PluginNodePolyfill
111
+ } = await Promise.resolve().then(() => _interopRequireWildcard(require('@modern-js/builder-plugin-node-polyfill')));
112
+ builder.addPlugins([PluginNodePolyfill()]);
113
+ }
114
+
115
+ if (normalizedConfig.tools.esbuild) {
116
+ const {
117
+ esbuild: esbuildOptions
118
+ } = normalizedConfig.tools;
119
+ const {
120
+ PluginEsbuild
121
+ } = await Promise.resolve().then(() => _interopRequireWildcard(require('@modern-js/builder-plugin-esbuild')));
122
+ builder.addPlugins([PluginEsbuild({
123
+ loader: false,
124
+ minimize: (0, _utils.applyOptionsChain)({}, esbuildOptions)
125
+ })]);
126
+ }
127
+
128
+ builder.addPlugins([(0, _compatModern.PluginCompatModern)(appContext, normalizedConfig, compatPluginConfig)]);
129
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createCopyPattern = createCopyPattern;
7
+
8
+ var _path = _interopRequireDefault(require("path"));
9
+
10
+ var _lodash = require("@modern-js/utils/lodash");
11
+
12
+ var _utils = require("@modern-js/utils");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ function createCopyPattern(appContext, config, patternsType, chain) {
17
+ const configDir = _path.default.resolve(appContext.appDirectory, config.source.configDir);
18
+
19
+ const uploadDir = _path.default.posix.join(configDir.replace(/\\/g, '/'), 'upload');
20
+
21
+ const publicDir = _path.default.posix.join(configDir.replace(/\\/g, '/'), 'public');
22
+
23
+ const minifiedJsRexExp = /\.min\.js/;
24
+
25
+ const info = file => ({
26
+ // If the file name ends with `.min.js`, we assume it's a compressed file.
27
+ // So we don't want copy-webpack-plugin to minify it.
28
+ // ref: https://github.com/webpack-contrib/copy-webpack-plugin#info
29
+ minimized: minifiedJsRexExp.test(file.sourceFilename)
30
+ });
31
+
32
+ if (patternsType === 'public') {
33
+ if (!chain) {
34
+ throw new Error("expect get a webpackChain, but receive 'undefined'");
35
+ }
36
+
37
+ return {
38
+ info,
39
+ from: '**/*',
40
+ to: 'public',
41
+ context: publicDir,
42
+ noErrorOnMissing: true,
43
+ // eslint-disable-next-line node/prefer-global/buffer
44
+ transform: (content, absoluteFrom) => {
45
+ if (!/\.html?$/.test(absoluteFrom)) {
46
+ return content;
47
+ }
48
+
49
+ return (0, _lodash.template)(content.toString('utf8'))({
50
+ assetPrefix: (0, _utils.removeTailSlash)(chain.output.get('publicPath'))
51
+ });
52
+ }
53
+ };
54
+ } else {
55
+ return {
56
+ info,
57
+ from: '**/*',
58
+ to: 'upload',
59
+ context: uploadDir,
60
+ noErrorOnMissing: true
61
+ };
62
+ }
63
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.HtmlAsyncChunkPlugin = void 0;
7
+
8
+ 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; }
9
+
10
+ class HtmlAsyncChunkPlugin {
11
+ constructor(htmlWebpackPlugin) {
12
+ _defineProperty(this, "name", void 0);
13
+
14
+ _defineProperty(this, "htmlWebpackPlugin", void 0);
15
+
16
+ this.name = 'HtmlAsyncChunkPlugin';
17
+ this.htmlWebpackPlugin = htmlWebpackPlugin;
18
+ }
19
+
20
+ apply(compiler) {
21
+ compiler.hooks.compilation.tap(this.name, compilation => {
22
+ const hooks = this.htmlWebpackPlugin.getHooks(compilation);
23
+ hooks.alterAssetTagGroups.tap(this.name, assets => {
24
+ const tags = [...assets.headTags, ...assets.bodyTags];
25
+
26
+ for (const tag of tags) {
27
+ if (tag.tagName === 'script') {
28
+ const {
29
+ attributes
30
+ } = tag;
31
+
32
+ if (attributes && attributes.defer === true) {
33
+ attributes.async = true;
34
+ delete attributes.defer;
35
+ }
36
+ }
37
+ }
38
+
39
+ return assets;
40
+ });
41
+ });
42
+ }
43
+
44
+ }
45
+
46
+ exports.HtmlAsyncChunkPlugin = HtmlAsyncChunkPlugin;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.BottomTemplatePlugin = void 0;
7
+
8
+ 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; }
9
+
10
+ class BottomTemplatePlugin {
11
+ constructor(htmlWebpackPlugin) {
12
+ _defineProperty(this, "htmlWebpackPlugin", void 0);
13
+
14
+ _defineProperty(this, "bottomTemplateReg", /<!--<\?-\s*bottomTemplate\s*\?>-->/);
15
+
16
+ _defineProperty(this, "bodyRegExp", /(<\/\s*body\s*>)/i);
17
+
18
+ _defineProperty(this, "name", void 0);
19
+
20
+ this.htmlWebpackPlugin = htmlWebpackPlugin;
21
+ this.name = 'bottom-template';
22
+ }
23
+
24
+ apply(compiler) {
25
+ compiler.hooks.compilation.tap(this.name, compilation => {
26
+ this.htmlWebpackPlugin.getHooks(compilation).beforeEmit.tap(this.name, data => {
27
+ var _data$plugin$options;
28
+
29
+ if (!((_data$plugin$options = data.plugin.options) !== null && _data$plugin$options !== void 0 && _data$plugin$options.__internal__)) {
30
+ return data;
31
+ } // 含有 <!--<?- bottomTemplate ?>--> 占位符时才需要注入 bottom.html
32
+
33
+
34
+ if (this.bottomTemplateReg.test(data.html)) {
35
+ // 清空占位符
36
+ data.html = data.html.replace(this.bottomTemplateReg, '');
37
+ const {
38
+ bottomTemplate
39
+ } = data.plugin.options;
40
+
41
+ if (bottomTemplate) {
42
+ data.html = data.html.replace(this.bodyRegExp, match => `\n${bottomTemplate}\n${match}`);
43
+ }
44
+ }
45
+
46
+ return data;
47
+ });
48
+ });
49
+ }
50
+
51
+ }
52
+
53
+ exports.BottomTemplatePlugin = BottomTemplatePlugin;
@@ -5,8 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.build = void 0;
7
7
 
8
- var _webpack = require("@modern-js/webpack");
9
-
10
8
  var _core = require("@modern-js/core");
11
9
 
12
10
  var _utils = require("@modern-js/utils");
@@ -15,16 +13,16 @@ var _routes = require("../utils/routes");
15
13
 
16
14
  var _config = require("../utils/config");
17
15
 
16
+ var _builder = _interopRequireDefault(require("../builder"));
17
+
18
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
+
18
20
  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; }
19
21
 
20
22
  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; }
21
23
 
22
24
  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; }
23
25
 
24
- // These sizes are pretty large. We'll warn for bundles exceeding them.
25
- const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
26
- const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
27
-
28
26
  const build = async (api, options) => {
29
27
  let resolvedConfig = api.useResolvedConfigContext();
30
28
  const appContext = api.useAppContext();
@@ -39,9 +37,9 @@ const build = async (api, options) => {
39
37
  distDirectory,
40
38
  serverConfigFile
41
39
  } = appContext;
42
- await (0, _utils.emptyDir)(distDirectory);
43
40
  await hookRunners.beforeBuild({
44
- webpackConfigs: []
41
+ // "null" bundlerConfigs
42
+ bundlerConfigs: undefined
45
43
  });
46
44
  await (0, _config.buildServerConfig)({
47
45
  appDirectory,
@@ -49,64 +47,13 @@ const build = async (api, options) => {
49
47
  configFile: serverConfigFile
50
48
  });
51
49
  await (0, _routes.generateRoutes)(appContext);
52
- await hookRunners.afterBuild();
50
+ await hookRunners.afterBuild({
51
+ // "null" stats
52
+ stats: undefined
53
+ });
53
54
  return;
54
55
  }
55
56
 
56
- const webpackBuild = async (webpackConfig, type) => {
57
- const compiler = (0, _webpack.webpack)(webpackConfig);
58
- return new Promise((resolve, reject) => {
59
- let label = process.env.NODE_ENV || '';
60
-
61
- if (type && type !== 'legacy') {
62
- label += ` ${type}`;
63
- }
64
-
65
- _utils.logger.info(`Creating a ${label} build...`);
66
-
67
- compiler.run((err, stats) => {
68
- let messages;
69
-
70
- if (!err) {
71
- messages = (0, _utils.formatWebpackMessages)(stats.toJson({
72
- all: false,
73
- warnings: true,
74
- errors: true
75
- }));
76
-
77
- if (messages.errors.length === 0) {
78
- _utils.logger.info(`File sizes after ${label} build:\n`);
79
-
80
- (0, _utils.printFileSizesAfterBuild)(stats, previousFileSizes, distDirectory, WARN_AFTER_BUNDLE_GZIP_SIZE, WARN_AFTER_CHUNK_GZIP_SIZE);
81
-
82
- _utils.logger.log();
83
- }
84
- } // When using run or watch, call close and wait for it to finish before calling run or watch again.
85
- // Concurrent compilations will corrupt the output files.
86
-
87
-
88
- compiler.close(closeErr => {
89
- if (closeErr) {
90
- _utils.logger.error(closeErr);
91
- }
92
-
93
- if (err) {
94
- reject(err);
95
- } else {
96
- if (messages.errors.length) {
97
- reject(new Error(messages.errors.join('\n\n')));
98
- return;
99
- }
100
-
101
- resolve({
102
- warnings: messages.warnings
103
- });
104
- }
105
- });
106
- });
107
- });
108
- };
109
-
110
57
  resolvedConfig = _objectSpread(_objectSpread({}, resolvedConfig), {}, {
111
58
  cliOptions: options
112
59
  });
@@ -118,57 +65,56 @@ const build = async (api, options) => {
118
65
  appDirectory,
119
66
  serverConfigFile
120
67
  } = appContext;
121
- const previousFileSizes = await (0, _utils.measureFileSizesBeforeBuild)(distDirectory);
122
- await (0, _utils.emptyDir)(distDirectory);
123
68
  await (0, _config.buildServerConfig)({
124
69
  appDirectory,
125
70
  distDirectory,
126
71
  configFile: serverConfigFile
127
72
  });
128
- const buildConfigs = [];
129
- buildConfigs.push({
130
- type: 'legacy',
131
- config: (0, _webpack.getWebpackConfig)(_webpack.WebpackConfigTarget.CLIENT, appContext, resolvedConfig)
132
- });
73
+ const targets = ['web'];
133
74
 
134
75
  if (resolvedConfig.output.enableModernMode) {
135
- buildConfigs.push({
136
- type: 'modern',
137
- config: (0, _webpack.getWebpackConfig)(_webpack.WebpackConfigTarget.MODERN, appContext, resolvedConfig)
138
- });
76
+ targets.push('modern-web');
139
77
  }
140
78
 
141
79
  if ((0, _utils.isUseSSRBundle)(resolvedConfig)) {
142
- buildConfigs.push({
143
- type: 'ssr',
144
- config: (0, _webpack.getWebpackConfig)(_webpack.WebpackConfigTarget.NODE, appContext, resolvedConfig)
145
- });
80
+ targets.push('node');
146
81
  }
147
82
 
148
- await hookRunners.beforeBuild({
149
- webpackConfigs: buildConfigs.map(({
150
- config
151
- }) => config)
152
- });
83
+ try {
84
+ const builder = await (0, _builder.default)({
85
+ target: targets,
86
+ appContext,
87
+ normalizedConfig: resolvedConfig,
88
+ compatPluginConfig: {
89
+ async onBeforeBuild({
90
+ bundlerConfigs
91
+ }) {
92
+ await (0, _routes.generateRoutes)(appContext);
93
+ await hookRunners.beforeBuild({
94
+ bundlerConfigs
95
+ });
96
+ },
97
+
98
+ async onAfterBuild({
99
+ stats
100
+ }) {
101
+ await hookRunners.afterBuild({
102
+ stats
103
+ });
104
+ await (0, _config.emitResolvedConfig)(appDirectory, resolvedConfig);
105
+ }
153
106
 
154
- for (const buildConfig of buildConfigs) {
155
- const {
156
- type: buildType,
157
- config
158
- } = buildConfig;
107
+ }
108
+ });
159
109
 
160
- try {
161
- await webpackBuild(config, buildType);
162
- } catch (error) {
163
- (0, _utils.printBuildError)(error); // eslint-disable-next-line no-process-exit
110
+ _utils.logger.info('Create a production build...\n');
164
111
 
165
- process.exit(1);
166
- }
167
- }
112
+ await builder.build();
113
+ } catch (error) {
114
+ (0, _utils.printBuildError)(error); // eslint-disable-next-line no-process-exit
168
115
 
169
- await (0, _routes.generateRoutes)(appContext);
170
- await hookRunners.afterBuild();
171
- await (0, _config.emitResolvedConfig)(appDirectory, resolvedConfig);
116
+ process.exit(1);
117
+ }
172
118
  };
173
119
 
174
120
  exports.build = build;