@modern-js/app-tools 2.0.0-beta.0 → 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 (126) hide show
  1. package/CHANGELOG.md +181 -0
  2. package/dist/js/modern/analyze/generateCode.js +29 -37
  3. package/dist/js/modern/analyze/getBundleEntry.js +4 -12
  4. package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +5 -30
  5. package/dist/js/modern/analyze/getClientRoutes/getRoutesLegacy.js +5 -30
  6. package/dist/js/modern/analyze/getClientRoutes/utils.js +0 -3
  7. package/dist/js/modern/analyze/getFileSystemEntry.js +4 -17
  8. package/dist/js/modern/analyze/getHtmlTemplate.js +2 -9
  9. package/dist/js/modern/analyze/getServerRoutes.js +15 -32
  10. package/dist/js/modern/analyze/index.js +79 -22
  11. package/dist/js/modern/analyze/isDefaultExportFunction.js +0 -4
  12. package/dist/js/modern/analyze/makeLegalIdentifier.js +0 -2
  13. package/dist/js/modern/analyze/nestedRoutes.js +8 -33
  14. package/dist/js/modern/analyze/templates.js +22 -37
  15. package/dist/js/modern/analyze/utils.js +0 -7
  16. package/dist/js/modern/builder/builderPlugins/compatModern.js +30 -48
  17. package/dist/js/modern/builder/createHtmlConfig.js +2 -2
  18. package/dist/js/modern/builder/createOutputConfig.js +7 -4
  19. package/dist/js/modern/builder/createSourceConfig.js +0 -11
  20. package/dist/js/modern/builder/createToolsConfig.js +3 -10
  21. package/dist/js/modern/builder/index.js +24 -22
  22. package/dist/js/modern/builder/share.js +0 -4
  23. package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +0 -8
  24. package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +2 -13
  25. package/dist/js/modern/builder/webpackPlugins/routerPlugin.js +97 -0
  26. package/dist/js/modern/commands/build.js +6 -46
  27. package/dist/js/modern/commands/dev.js +4 -16
  28. package/dist/js/modern/commands/inspect.js +3 -20
  29. package/dist/js/modern/commands/start.js +0 -2
  30. package/dist/js/modern/index.js +6 -15
  31. package/dist/js/modern/utils/config.js +1 -12
  32. package/dist/js/modern/utils/createServer.js +0 -5
  33. package/dist/js/modern/utils/getSpecifiedEntries.js +0 -6
  34. package/dist/js/modern/utils/printInstructions.js +2 -1
  35. package/dist/js/modern/utils/routes.js +0 -2
  36. package/dist/js/node/analyze/generateCode.js +29 -60
  37. package/dist/js/node/analyze/getBundleEntry.js +4 -18
  38. package/dist/js/node/analyze/getClientRoutes/getRoutes.js +5 -45
  39. package/dist/js/node/analyze/getClientRoutes/getRoutesLegacy.js +5 -45
  40. package/dist/js/node/analyze/getClientRoutes/index.js +0 -2
  41. package/dist/js/node/analyze/getClientRoutes/utils.js +0 -14
  42. package/dist/js/node/analyze/getFileSystemEntry.js +4 -25
  43. package/dist/js/node/analyze/getHtmlTemplate.js +2 -23
  44. package/dist/js/node/analyze/getServerRoutes.js +14 -40
  45. package/dist/js/node/analyze/index.js +79 -32
  46. package/dist/js/node/analyze/isDefaultExportFunction.js +0 -12
  47. package/dist/js/node/analyze/makeLegalIdentifier.js +0 -4
  48. package/dist/js/node/analyze/nestedRoutes.js +8 -41
  49. package/dist/js/node/analyze/templates.js +22 -47
  50. package/dist/js/node/analyze/utils.js +0 -20
  51. package/dist/js/node/builder/builderPlugins/compatModern.js +30 -59
  52. package/dist/js/node/builder/createHtmlConfig.js +2 -10
  53. package/dist/js/node/builder/createOutputConfig.js +7 -6
  54. package/dist/js/node/builder/createSourceConfig.js +0 -17
  55. package/dist/js/node/builder/createToolsConfig.js +3 -12
  56. package/dist/js/node/builder/index.js +24 -36
  57. package/dist/js/node/builder/share.js +0 -12
  58. package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +0 -10
  59. package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +2 -15
  60. package/dist/js/node/builder/webpackPlugins/routerPlugin.js +105 -0
  61. package/dist/js/node/commands/build.js +4 -57
  62. package/dist/js/node/commands/deploy.js +0 -2
  63. package/dist/js/node/commands/dev.js +3 -27
  64. package/dist/js/node/commands/index.js +0 -6
  65. package/dist/js/node/commands/inspect.js +3 -27
  66. package/dist/js/node/commands/start.js +0 -11
  67. package/dist/js/node/exports/server.js +0 -1
  68. package/dist/js/node/hooks.js +0 -2
  69. package/dist/js/node/index.js +7 -34
  70. package/dist/js/node/locale/index.js +0 -4
  71. package/dist/js/node/utils/commands.js +0 -2
  72. package/dist/js/node/utils/config.js +1 -25
  73. package/dist/js/node/utils/createServer.js +0 -15
  74. package/dist/js/node/utils/getSpecifiedEntries.js +0 -9
  75. package/dist/js/node/utils/language.js +0 -2
  76. package/dist/js/node/utils/printInstructions.js +2 -5
  77. package/dist/js/node/utils/routes.js +0 -5
  78. package/dist/js/treeshaking/analyze/generateCode.js +57 -100
  79. package/dist/js/treeshaking/analyze/getBundleEntry.js +11 -19
  80. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +16 -42
  81. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +16 -42
  82. package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +0 -3
  83. package/dist/js/treeshaking/analyze/getFileSystemEntry.js +4 -17
  84. package/dist/js/treeshaking/analyze/getHtmlTemplate.js +2 -26
  85. package/dist/js/treeshaking/analyze/getServerRoutes.js +24 -37
  86. package/dist/js/treeshaking/analyze/index.js +185 -65
  87. package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +0 -4
  88. package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +0 -2
  89. package/dist/js/treeshaking/analyze/nestedRoutes.js +9 -51
  90. package/dist/js/treeshaking/analyze/templates.js +31 -50
  91. package/dist/js/treeshaking/analyze/utils.js +7 -14
  92. package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +40 -59
  93. package/dist/js/treeshaking/builder/createHtmlConfig.js +15 -15
  94. package/dist/js/treeshaking/builder/createOutputConfig.js +26 -23
  95. package/dist/js/treeshaking/builder/createSourceConfig.js +7 -19
  96. package/dist/js/treeshaking/builder/createToolsConfig.js +16 -21
  97. package/dist/js/treeshaking/builder/index.js +38 -49
  98. package/dist/js/treeshaking/builder/share.js +0 -4
  99. package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +1 -13
  100. package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +2 -14
  101. package/dist/js/treeshaking/builder/webpackPlugins/routerPlugin.js +121 -0
  102. package/dist/js/treeshaking/commands/build.js +26 -111
  103. package/dist/js/treeshaking/commands/deploy.js +0 -3
  104. package/dist/js/treeshaking/commands/dev.js +13 -35
  105. package/dist/js/treeshaking/commands/inspect.js +5 -28
  106. package/dist/js/treeshaking/commands/start.js +0 -9
  107. package/dist/js/treeshaking/index.js +2 -39
  108. package/dist/js/treeshaking/utils/config.js +1 -17
  109. package/dist/js/treeshaking/utils/createServer.js +0 -10
  110. package/dist/js/treeshaking/utils/getSpecifiedEntries.js +0 -10
  111. package/dist/js/treeshaking/utils/printInstructions.js +0 -5
  112. package/dist/js/treeshaking/utils/routes.js +0 -4
  113. package/dist/types/analyze/index.d.ts +2 -3
  114. package/dist/types/analyze/nestedRoutes.d.ts +1 -1
  115. package/dist/types/analyze/templates.d.ts +3 -1
  116. package/dist/types/builder/builderPlugins/compatModern.d.ts +0 -1
  117. package/dist/types/builder/index.d.ts +2 -6
  118. package/dist/types/builder/webpackPlugins/routerPlugin.d.ts +10 -0
  119. package/dist/types/commands/inspect.d.ts +1 -4
  120. package/dist/types/index.d.ts +0 -2
  121. package/dist/types/utils/config.d.ts +0 -1
  122. package/package.json +23 -22
  123. package/dist/js/modern/utils/createCompiler.js +0 -58
  124. package/dist/js/node/utils/createCompiler.js +0 -75
  125. package/dist/js/treeshaking/utils/createCompiler.js +0 -117
  126. package/dist/types/utils/createCompiler.d.ts +0 -12
@@ -10,12 +10,10 @@ export function createSourceConfig(normalizedConfig, appContext) {
10
10
  preEntry
11
11
  } = normalizedConfig.source;
12
12
  const builderGlobalVars = globalVars || {};
13
-
14
13
  for (const envVar of envVars || []) {
15
14
  const envVarValue = process.env[envVar];
16
15
  envVarValue && (builderGlobalVars[`process.env.${envVar}`] = envVarValue);
17
16
  }
18
-
19
17
  const builderModuleScope = createBuilderModuleScope(moduleScopes);
20
18
  const builderInclude = createBuilderInclude(include, appContext);
21
19
  return {
@@ -35,35 +33,28 @@ export function createBuilderInclude(include, appContext) {
35
33
  if (isAbsolute(include)) {
36
34
  return include;
37
35
  }
38
-
39
36
  return new RegExp(include);
40
37
  }
41
-
42
38
  return include;
43
39
  }).concat(defaultInclude); // concat default Include
44
40
 
45
41
  const root = findMonorepoRoot(appContext.appDirectory);
46
-
47
42
  if (!root) {
48
43
  return transformInclude;
49
44
  }
50
-
51
45
  const modernjsMonorepo = isModernjsMonorepo(root);
52
-
53
46
  if (modernjsMonorepo) {
54
47
  const paths = globby.sync(posix.join(root, 'features', '**', 'package.json'), {
55
48
  ignore: ['**/node_modules/**/*']
56
49
  }).map(pathname => dirname(pathname) + sep);
57
50
  return [...paths, ...transformInclude];
58
51
  }
59
-
60
52
  return transformInclude;
61
53
  }
62
54
  export function createBuilderModuleScope(moduleScopes) {
63
55
  if (moduleScopes) {
64
56
  let builderModuleScope = [];
65
57
  const DEFAULT_SCOPES = ['./src', './shared', /node_modules/];
66
-
67
58
  if (Array.isArray(moduleScopes)) {
68
59
  if (isPrimitiveScope(moduleScopes)) {
69
60
  builderModuleScope = DEFAULT_SCOPES.concat(moduleScopes);
@@ -73,12 +64,10 @@ export function createBuilderModuleScope(moduleScopes) {
73
64
  } else {
74
65
  builderModuleScope = [DEFAULT_SCOPES, moduleScopes];
75
66
  }
76
-
77
67
  return builderModuleScope;
78
68
  } else {
79
69
  return undefined;
80
70
  }
81
-
82
71
  function isPrimitiveScope(items) {
83
72
  return items.every(item => typeof item === 'string' || Object.prototype.toString.call(item) === '[object RegExp]');
84
73
  }
@@ -1,9 +1,6 @@
1
1
  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; }
2
-
3
2
  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; }
4
-
5
3
  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; }
6
-
7
4
  import { applyOptionsChain } from '@modern-js/utils';
8
5
  export function createToolsConfig(normalizedConfig) {
9
6
  const {
@@ -50,18 +47,16 @@ export function createToolsConfig(normalizedConfig) {
50
47
  minify: typeof config.minify === 'object' ? _objectSpread(_objectSpread({}, config.minify), {}, {
51
48
  removeComments: false
52
49
  }) : config.minify
53
- }), // eslint-disable-next-line no-nested-ternary
50
+ }),
51
+ // eslint-disable-next-line no-nested-ternary
54
52
  ...(Array.isArray(htmlPlugin) ? htmlPlugin : htmlPlugin ? [htmlPlugin] : [])]
55
53
  };
56
54
  }
57
-
58
55
  function createBuilderTsLoader(tsLoader, enableTsLoader) {
59
56
  const useTsLoader = Boolean(enableTsLoader);
60
-
61
57
  if (!useTsLoader) {
62
58
  return undefined;
63
59
  }
64
-
65
60
  const defaultTsLoader = {
66
61
  compilerOptions: {
67
62
  target: 'es5',
@@ -72,12 +67,10 @@ function createBuilderTsLoader(tsLoader, enableTsLoader) {
72
67
  };
73
68
  return (_, utils) => applyOptionsChain(defaultTsLoader, tsLoader, utils);
74
69
  }
75
-
76
70
  export function createBuilderTsChecker(output) {
77
- if (output.enableTsLoader || output.disableTsChecker) {
71
+ if (output.enableTsLoader) {
78
72
  return false;
79
73
  }
80
-
81
74
  const defaultTsChecker = {
82
75
  issue: {
83
76
  include: [{
@@ -1,34 +1,37 @@
1
1
  import { createBuilder } from '@modern-js/builder';
2
2
  import { builderWebpackProvider } from '@modern-js/builder-webpack-provider';
3
- import { applyOptionsChain } from '@modern-js/utils';
3
+ import { applyOptionsChain, isUseSSRBundle } from '@modern-js/utils';
4
4
  import { PluginCompatModern } from "./builderPlugins/compatModern";
5
5
  import { createHtmlConfig } from "./createHtmlConfig";
6
6
  import { createOutputConfig } from "./createOutputConfig";
7
7
  import { createSourceConfig } from "./createSourceConfig";
8
8
  import { createToolsConfig } from "./createToolsConfig";
9
- export default (async ({
10
- target: _target = 'web',
11
- normalizedConfig,
12
- appContext,
13
- compatPluginConfig
14
- }) => {
15
- const targets = Array.isArray(_target) ? _target : [_target];
16
-
9
+ function getBuilderTargets(normalizedConfig) {
10
+ const targets = ['web'];
17
11
  if (normalizedConfig.output.enableModernMode && !targets.includes('modern-web')) {
18
12
  targets.push('modern-web');
19
13
  }
20
-
21
- const builderConfig = createBuilderProviderConfig(normalizedConfig, appContext); // create webpack provider
22
-
14
+ if (isUseSSRBundle(normalizedConfig)) {
15
+ targets.push('node');
16
+ }
17
+ return targets;
18
+ }
19
+ export async function createBuilderForEdenX({
20
+ normalizedConfig,
21
+ appContext,
22
+ compatPluginConfig
23
+ }) {
24
+ const builderConfig = createBuilderProviderConfig(normalizedConfig, appContext);
25
+ // create webpack provider
23
26
  const webpackProvider = builderWebpackProvider({
24
27
  builderConfig
25
28
  });
26
- const builderOptions = createBuilderOptions(_target, appContext);
29
+ const target = getBuilderTargets(normalizedConfig);
30
+ const builderOptions = createBuilderOptions(target, appContext);
27
31
  const builder = await createBuilder(webpackProvider, builderOptions);
28
32
  await applyBuilderPlugins(builder, normalizedConfig, appContext, compatPluginConfig);
29
33
  return builder;
30
- });
31
-
34
+ }
32
35
  function createBuilderProviderConfig(normalizedConfig, appContext) {
33
36
  const source = createSourceConfig(normalizedConfig, appContext);
34
37
  const html = createHtmlConfig(normalizedConfig, appContext);
@@ -39,21 +42,24 @@ function createBuilderProviderConfig(normalizedConfig, appContext) {
39
42
  html,
40
43
  output,
41
44
  tools,
45
+ dev: {
46
+ https: normalizedConfig.dev.https,
47
+ assetPrefix: normalizedConfig.dev.assetPrefix
48
+ },
42
49
  performance: {
43
50
  // `@modern-js/webpack` used to remove moment locale by default
44
51
  removeMomentLocale: true
45
52
  }
46
53
  };
47
54
  }
48
-
49
55
  export function createBuilderOptions(target, appContext) {
50
56
  // create entries
57
+
51
58
  const entries = {};
52
59
  const {
53
60
  entrypoints = [],
54
61
  checkedEntries
55
62
  } = appContext;
56
-
57
63
  for (const {
58
64
  entryName,
59
65
  entry
@@ -61,14 +67,12 @@ export function createBuilderOptions(target, appContext) {
61
67
  if (checkedEntries && !checkedEntries.includes(entryName)) {
62
68
  continue;
63
69
  }
64
-
65
70
  if (entryName in entries) {
66
71
  entries[entryName].push(entry);
67
72
  } else {
68
73
  entries[entryName] = [entry];
69
74
  }
70
75
  }
71
-
72
76
  return {
73
77
  cwd: appContext.appDirectory,
74
78
  target,
@@ -77,10 +81,10 @@ export function createBuilderOptions(target, appContext) {
77
81
  framework: appContext.metaName
78
82
  };
79
83
  }
84
+
80
85
  /**
81
86
  * register builder Plugin by condition
82
87
  */
83
-
84
88
  async function applyBuilderPlugins(builder, normalizedConfig, appContext, compatPluginConfig) {
85
89
  if (!normalizedConfig.output.disableNodePolyfill) {
86
90
  const {
@@ -88,7 +92,6 @@ async function applyBuilderPlugins(builder, normalizedConfig, appContext, compat
88
92
  } = await import('@modern-js/builder-plugin-node-polyfill');
89
93
  builder.addPlugins([PluginNodePolyfill()]);
90
94
  }
91
-
92
95
  if (normalizedConfig.tools.esbuild) {
93
96
  const {
94
97
  esbuild: esbuildOptions
@@ -101,6 +104,5 @@ async function applyBuilderPlugins(builder, normalizedConfig, appContext, compat
101
104
  minimize: applyOptionsChain({}, esbuildOptions)
102
105
  })]);
103
106
  }
104
-
105
107
  builder.addPlugins([PluginCompatModern(appContext, normalizedConfig, compatPluginConfig)]);
106
108
  }
@@ -6,19 +6,16 @@ export function createCopyPattern(appContext, config, patternsType, chain) {
6
6
  const uploadDir = path.posix.join(configDir.replace(/\\/g, '/'), 'upload');
7
7
  const publicDir = path.posix.join(configDir.replace(/\\/g, '/'), 'public');
8
8
  const minifiedJsRexExp = /\.min\.js/;
9
-
10
9
  const info = file => ({
11
10
  // If the file name ends with `.min.js`, we assume it's a compressed file.
12
11
  // So we don't want copy-webpack-plugin to minify it.
13
12
  // ref: https://github.com/webpack-contrib/copy-webpack-plugin#info
14
13
  minimized: minifiedJsRexExp.test(file.sourceFilename)
15
14
  });
16
-
17
15
  if (patternsType === 'public') {
18
16
  if (!chain) {
19
17
  throw new Error("expect get a webpackChain, but receive 'undefined'");
20
18
  }
21
-
22
19
  return {
23
20
  info,
24
21
  from: '**/*',
@@ -30,7 +27,6 @@ export function createCopyPattern(appContext, config, patternsType, chain) {
30
27
  if (!/\.html?$/.test(absoluteFrom)) {
31
28
  return content;
32
29
  }
33
-
34
30
  return lodashTemplate(content.toString('utf8'))({
35
31
  assetPrefix: removeTailSlash(chain.output.get('publicPath'))
36
32
  });
@@ -1,37 +1,29 @@
1
1
  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; }
2
-
3
2
  export class HtmlAsyncChunkPlugin {
4
3
  constructor(htmlWebpackPlugin) {
5
4
  _defineProperty(this, "name", void 0);
6
-
7
5
  _defineProperty(this, "htmlWebpackPlugin", void 0);
8
-
9
6
  this.name = 'HtmlAsyncChunkPlugin';
10
7
  this.htmlWebpackPlugin = htmlWebpackPlugin;
11
8
  }
12
-
13
9
  apply(compiler) {
14
10
  compiler.hooks.compilation.tap(this.name, compilation => {
15
11
  const hooks = this.htmlWebpackPlugin.getHooks(compilation);
16
12
  hooks.alterAssetTagGroups.tap(this.name, assets => {
17
13
  const tags = [...assets.headTags, ...assets.bodyTags];
18
-
19
14
  for (const tag of tags) {
20
15
  if (tag.tagName === 'script') {
21
16
  const {
22
17
  attributes
23
18
  } = tag;
24
-
25
19
  if (attributes && attributes.defer === true) {
26
20
  attributes.async = true;
27
21
  delete attributes.defer;
28
22
  }
29
23
  }
30
24
  }
31
-
32
25
  return assets;
33
26
  });
34
27
  });
35
28
  }
36
-
37
29
  }
@@ -1,44 +1,33 @@
1
1
  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; }
2
-
3
2
  export class BottomTemplatePlugin {
4
3
  constructor(htmlWebpackPlugin) {
5
4
  _defineProperty(this, "htmlWebpackPlugin", void 0);
6
-
7
5
  _defineProperty(this, "bottomTemplateReg", /<!--<\?-\s*bottomTemplate\s*\?>-->/);
8
-
9
6
  _defineProperty(this, "bodyRegExp", /(<\/\s*body\s*>)/i);
10
-
11
7
  _defineProperty(this, "name", void 0);
12
-
13
8
  this.htmlWebpackPlugin = htmlWebpackPlugin;
14
9
  this.name = 'bottom-template';
15
10
  }
16
-
17
11
  apply(compiler) {
18
12
  compiler.hooks.compilation.tap(this.name, compilation => {
19
13
  this.htmlWebpackPlugin.getHooks(compilation).beforeEmit.tap(this.name, data => {
20
14
  var _data$plugin$options;
21
-
22
15
  if (!((_data$plugin$options = data.plugin.options) !== null && _data$plugin$options !== void 0 && _data$plugin$options.__internal__)) {
23
16
  return data;
24
- } // 含有 <!--<?- bottomTemplate ?>--> 占位符时才需要注入 bottom.html
25
-
26
-
17
+ }
18
+ // 含有 <!--<?- bottomTemplate ?>--> 占位符时才需要注入 bottom.html
27
19
  if (this.bottomTemplateReg.test(data.html)) {
28
20
  // 清空占位符
29
21
  data.html = data.html.replace(this.bottomTemplateReg, '');
30
22
  const {
31
23
  bottomTemplate
32
24
  } = data.plugin.options;
33
-
34
25
  if (bottomTemplate) {
35
26
  data.html = data.html.replace(this.bodyRegExp, match => `\n${bottomTemplate}\n${match}`);
36
27
  }
37
28
  }
38
-
39
29
  return data;
40
30
  });
41
31
  });
42
32
  }
43
-
44
33
  }
@@ -0,0 +1,97 @@
1
+ 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; }
2
+ import path from 'path';
3
+ import { fs, ROUTE_MANIFEST, logger, ROUTE_MINIFEST_FILE } from '@modern-js/utils';
4
+ const PLUGIN_NAME = 'ModernjsRoutePlugin';
5
+ export default class RouterPlugin {
6
+ constructor(options) {
7
+ _defineProperty(this, "existNestedRoutes", void 0);
8
+ this.existNestedRoutes = options.existNestedRoutes;
9
+ }
10
+ apply(compiler) {
11
+ const {
12
+ existNestedRoutes
13
+ } = this;
14
+ const {
15
+ target
16
+ } = compiler.options;
17
+ if (target === 'node' || Array.isArray(target) && target.includes('node')) {
18
+ return;
19
+ }
20
+ if (!existNestedRoutes) {
21
+ return;
22
+ }
23
+ const {
24
+ webpack
25
+ } = compiler;
26
+ const {
27
+ Compilation,
28
+ sources
29
+ } = webpack;
30
+ const {
31
+ RawSource
32
+ } = sources;
33
+ const {
34
+ PROCESS_ASSETS_STAGE_REPORT
35
+ } = Compilation;
36
+ const outputPath = compiler.options.output.path;
37
+ const newAssetsMap = new Map();
38
+ const normalizePath = path => {
39
+ if (!path.endsWith('/')) {
40
+ return `${path}/`;
41
+ }
42
+ return path;
43
+ };
44
+ compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
45
+ compilation.hooks.processAssets.tapPromise({
46
+ name: PLUGIN_NAME,
47
+ stage: PROCESS_ASSETS_STAGE_REPORT
48
+ }, async () => {
49
+ const stats = compilation.getStats().toJson({
50
+ chunkGroups: true,
51
+ chunks: true
52
+ });
53
+ const {
54
+ publicPath
55
+ } = stats;
56
+ const routeAssets = {};
57
+ const {
58
+ namedChunkGroups,
59
+ assetsByChunkName
60
+ } = stats;
61
+ if (!namedChunkGroups || !assetsByChunkName) {
62
+ logger.warn('Route manifest does not exist, performance will be affected');
63
+ return;
64
+ }
65
+ for (const [name, chunkGroup] of Object.entries(namedChunkGroups)) {
66
+ routeAssets[name] = {
67
+ chunkIds: chunkGroup.chunks,
68
+ assets: assetsByChunkName[name].map(item => publicPath ? normalizePath(publicPath) + item : item)
69
+ };
70
+ }
71
+ const manifest = {
72
+ routeAssets
73
+ };
74
+ const injectedContent = `
75
+ ;(function(){
76
+ window.${ROUTE_MANIFEST} = ${JSON.stringify(manifest)};
77
+ })();
78
+ `;
79
+ const entrypointsArray = Array.from(compilation.entrypoints.entries());
80
+ const entryChunkIds = entrypointsArray.map(entrypoint => entrypoint[0]);
81
+ const entryChunks = [...compilation.chunks].filter(chunk => {
82
+ return entryChunkIds.includes(chunk.name);
83
+ });
84
+ const entryChunkFiles = entryChunks.map(chunk => [...chunk.files][0]);
85
+ for (const file of entryChunkFiles) {
86
+ const asset = compilation.assets[file];
87
+ const newContent = `${injectedContent}${asset.source().toString()}`;
88
+ newAssetsMap.set(path.join(outputPath, file), newContent);
89
+ compilation.updateAsset(file, new RawSource(newContent));
90
+ }
91
+ const filename = path.join(outputPath, ROUTE_MINIFEST_FILE);
92
+ await fs.ensureFile(filename);
93
+ await fs.writeFile(filename, JSON.stringify(manifest, null, 2));
94
+ });
95
+ });
96
+ }
97
+ }
@@ -1,14 +1,10 @@
1
1
  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; }
2
-
3
2
  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; }
4
-
5
3
  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; }
6
-
7
4
  import { ResolvedConfigContext } from '@modern-js/core';
8
- import { logger, isUseSSRBundle, printBuildError } from '@modern-js/utils';
5
+ import { logger, printBuildError } from '@modern-js/utils';
9
6
  import { generateRoutes } from "../utils/routes";
10
- import { buildServerConfig, emitResolvedConfig } from "../utils/config";
11
- import createBuilder from "../builder";
7
+ import { buildServerConfig } from "../utils/config";
12
8
  export const build = async (api, options) => {
13
9
  let resolvedConfig = api.useResolvedConfigContext();
14
10
  const appContext = api.useAppContext();
@@ -16,7 +12,6 @@ export const build = async (api, options) => {
16
12
  const {
17
13
  apiOnly
18
14
  } = appContext;
19
-
20
15
  if (apiOnly) {
21
16
  const {
22
17
  appDirectory,
@@ -39,7 +34,6 @@ export const build = async (api, options) => {
39
34
  });
40
35
  return;
41
36
  }
42
-
43
37
  resolvedConfig = _objectSpread(_objectSpread({}, resolvedConfig), {}, {
44
38
  cliOptions: options
45
39
  });
@@ -54,47 +48,13 @@ export const build = async (api, options) => {
54
48
  distDirectory,
55
49
  configFile: serverConfigFile
56
50
  });
57
- const targets = ['web'];
58
-
59
- if (resolvedConfig.output.enableModernMode) {
60
- targets.push('modern-web');
61
- }
62
-
63
- if (isUseSSRBundle(resolvedConfig)) {
64
- targets.push('node');
65
- }
66
-
67
51
  try {
68
- const builder = await createBuilder({
69
- target: targets,
70
- appContext,
71
- normalizedConfig: resolvedConfig,
72
- compatPluginConfig: {
73
- async onBeforeBuild({
74
- bundlerConfigs
75
- }) {
76
- await generateRoutes(appContext);
77
- await hookRunners.beforeBuild({
78
- bundlerConfigs
79
- });
80
- },
81
-
82
- async onAfterBuild({
83
- stats
84
- }) {
85
- await hookRunners.afterBuild({
86
- stats
87
- });
88
- await emitResolvedConfig(appDirectory, resolvedConfig);
89
- }
90
-
91
- }
92
- });
52
+ var _appContext$builder;
93
53
  logger.info('Create a production build...\n');
94
- await builder.build();
54
+ await ((_appContext$builder = appContext.builder) === null || _appContext$builder === void 0 ? void 0 : _appContext$builder.build());
95
55
  } catch (error) {
96
- printBuildError(error); // eslint-disable-next-line no-process-exit
97
-
56
+ printBuildError(error);
57
+ // eslint-disable-next-line no-process-exit
98
58
  process.exit(1);
99
59
  }
100
60
  };
@@ -1,13 +1,9 @@
1
1
  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; }
2
-
3
2
  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; }
4
-
5
3
  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; }
6
-
7
- import { logger, isSSR } from '@modern-js/utils';
4
+ import { logger } from '@modern-js/utils';
8
5
  import { ResolvedConfigContext } from '@modern-js/core';
9
6
  import { printInstructions } from "../utils/printInstructions";
10
- import { createDevCompiler } from "../utils/createCompiler";
11
7
  import { createServer, injectDataLoaderPlugin } from "../utils/createServer";
12
8
  import { generateRoutes } from "../utils/routes";
13
9
  import { getSpecifiedEntries } from "../utils/getSpecifiedEntries";
@@ -46,17 +42,10 @@ export const dev = async (api, options) => {
46
42
  });
47
43
  await hookRunners.beforeDev();
48
44
  let compiler = null;
49
-
50
45
  if (!apiOnly) {
51
- const target = isSSR(userConfig) ? ['web', 'node'] : ['web'];
52
- compiler = await createDevCompiler({
53
- target,
54
- api,
55
- normalizedConfig: userConfig,
56
- appContext
57
- });
46
+ var _appContext$builder;
47
+ compiler = await ((_appContext$builder = appContext.builder) === null || _appContext$builder === void 0 ? void 0 : _appContext$builder.createCompiler());
58
48
  }
59
-
60
49
  await generateRoutes(appContext);
61
50
  const app = await createServer({
62
51
  dev: _objectSpread(_objectSpread({}, {
@@ -81,9 +70,8 @@ export const dev = async (api, options) => {
81
70
  if (err) {
82
71
  throw err;
83
72
  }
84
-
85
73
  if (!apiOnly) {
86
- logger.info(`Starting dev server...`);
74
+ logger.info(`Starting dev server...\n`);
87
75
  } else {
88
76
  printInstructions(hookRunners, appContext, userConfig);
89
77
  }
@@ -1,28 +1,11 @@
1
1
  import { join } from 'path';
2
- import { isUseSSRBundle } from '@modern-js/utils';
3
- import createBuilder from "../builder";
4
2
  export const inspect = async (api, options) => {
5
- const resolvedConfig = api.useResolvedConfigContext();
3
+ var _appContext$builder;
6
4
  const appContext = api.useAppContext();
7
- const targets = ['web'];
8
-
9
- if (resolvedConfig.output.enableModernMode) {
10
- targets.push('modern-web');
11
- }
12
-
13
- if (isUseSSRBundle(resolvedConfig)) {
14
- targets.push('node');
15
- }
16
-
17
- const builder = await createBuilder({
18
- target: targets,
19
- appContext,
20
- normalizedConfig: resolvedConfig
21
- });
22
- return builder.inspectConfig({
5
+ return (_appContext$builder = appContext.builder) === null || _appContext$builder === void 0 ? void 0 : _appContext$builder.inspectConfig({
23
6
  env: options.env,
24
7
  verbose: options.verbose,
25
- outputPath: join(builder.context.distPath, options.output),
8
+ outputPath: join(appContext === null || appContext === void 0 ? void 0 : appContext.builder.context.distPath, options.output),
26
9
  writeToDisk: true
27
10
  });
28
11
  };
@@ -4,7 +4,6 @@ import { printInstructions } from "../utils/printInstructions";
4
4
  import { injectDataLoaderPlugin } from "../utils/createServer";
5
5
  export const start = async api => {
6
6
  var _userConfig$source;
7
-
8
7
  const appContext = api.useAppContext();
9
8
  const userConfig = api.useResolvedConfigContext();
10
9
  const hookRunners = api.useHookRunners();
@@ -26,7 +25,6 @@ export const start = async api => {
26
25
  if (err) {
27
26
  throw err;
28
27
  }
29
-
30
28
  await printInstructions(hookRunners, appContext, userConfig);
31
29
  });
32
30
  };