@modern-js/module-tools 1.5.5 → 1.6.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +78 -0
  2. package/dist/js/modern/cli/build.js +5 -4
  3. package/dist/js/modern/commands/build.js +40 -27
  4. package/dist/js/modern/features/build/bundle/index.js +6 -0
  5. package/dist/js/modern/features/build/bundle/runRollup.js +149 -0
  6. package/dist/js/modern/features/build/bundle/runSpeedy.js +185 -0
  7. package/dist/js/modern/{tasks → features/build/bundleless}/copy-assets.js +31 -55
  8. package/dist/js/modern/{tasks → features/build/bundleless}/generator-dts/index.js +46 -70
  9. package/dist/js/modern/{tasks → features/build/bundleless}/generator-dts/utils.js +6 -6
  10. package/dist/js/modern/features/build/bundleless/index.js +13 -0
  11. package/dist/js/modern/features/build/bundleless/runBabel.js +269 -0
  12. package/dist/js/modern/features/build/bundleless/style.js +266 -0
  13. package/dist/js/modern/features/build/constants.js +94 -59
  14. package/dist/js/modern/features/build/error.js +150 -0
  15. package/dist/js/modern/features/build/index.js +84 -19
  16. package/dist/js/modern/features/build/legacy-constants.js +99 -0
  17. package/dist/js/modern/features/build/normalize.js +293 -0
  18. package/dist/js/modern/features/build/types.js +0 -0
  19. package/dist/js/modern/features/build/utils.js +29 -137
  20. package/dist/js/modern/index.js +3 -1
  21. package/dist/js/modern/locale/en.js +5 -3
  22. package/dist/js/modern/locale/zh.js +5 -3
  23. package/dist/js/modern/schema/build-config.js +104 -0
  24. package/dist/js/modern/schema/index.js +2 -1
  25. package/dist/js/modern/schema/output.js +18 -9
  26. package/dist/js/modern/schema/source.js +2 -10
  27. package/dist/js/modern/schema/tools.js +6 -0
  28. package/dist/js/modern/schema/types.js +0 -0
  29. package/dist/js/modern/utils/babel.js +6 -3
  30. package/dist/js/modern/utils/constants.js +3 -0
  31. package/dist/js/node/cli/build.js +5 -4
  32. package/dist/js/node/commands/build.js +50 -29
  33. package/dist/js/node/features/build/bundle/index.js +17 -0
  34. package/dist/js/node/features/build/bundle/runRollup.js +174 -0
  35. package/dist/js/node/features/build/bundle/runSpeedy.js +209 -0
  36. package/dist/js/node/{tasks → features/build/bundleless}/copy-assets.js +37 -55
  37. package/dist/js/node/{tasks → features/build/bundleless}/generator-dts/index.js +59 -73
  38. package/dist/js/node/{tasks → features/build/bundleless}/generator-dts/utils.js +6 -6
  39. package/dist/js/node/features/build/bundleless/index.js +26 -0
  40. package/dist/js/node/features/build/bundleless/runBabel.js +309 -0
  41. package/dist/js/node/features/build/bundleless/style.js +299 -0
  42. package/dist/js/node/features/build/constants.js +115 -67
  43. package/dist/js/node/features/build/error.js +175 -0
  44. package/dist/js/node/features/build/index.js +92 -19
  45. package/dist/js/node/features/build/legacy-constants.js +107 -0
  46. package/dist/js/node/features/build/normalize.js +322 -0
  47. package/dist/js/node/features/build/types.js +0 -0
  48. package/dist/js/node/features/build/utils.js +31 -145
  49. package/dist/js/node/index.js +5 -1
  50. package/dist/js/node/locale/en.js +5 -3
  51. package/dist/js/node/locale/zh.js +5 -3
  52. package/dist/js/node/schema/build-config.js +113 -0
  53. package/dist/js/node/schema/index.js +3 -1
  54. package/dist/js/node/schema/output.js +20 -9
  55. package/dist/js/node/schema/source.js +2 -10
  56. package/dist/js/node/schema/tools.js +13 -0
  57. package/dist/js/node/schema/types.js +0 -0
  58. package/dist/js/node/utils/babel.js +5 -2
  59. package/dist/js/node/utils/constants.js +11 -0
  60. package/dist/types/cli/build.d.ts +1 -1
  61. package/dist/types/cli/new.d.ts +1 -1
  62. package/dist/types/commands/build.d.ts +12 -12
  63. package/dist/types/features/build/bundle/index.d.ts +3 -0
  64. package/dist/types/features/build/bundle/runRollup.d.ts +3 -0
  65. package/dist/types/features/build/bundle/runSpeedy.d.ts +10 -0
  66. package/dist/types/features/build/bundleless/copy-assets.d.ts +3 -0
  67. package/dist/types/features/build/bundleless/generator-dts/index.d.ts +3 -0
  68. package/dist/types/{tasks → features/build/bundleless}/generator-dts/utils.d.ts +3 -6
  69. package/dist/types/features/build/bundleless/index.d.ts +4 -0
  70. package/dist/types/features/build/bundleless/runBabel.d.ts +32 -0
  71. package/dist/types/features/build/bundleless/style.d.ts +25 -0
  72. package/dist/types/features/build/constants.d.ts +17 -13
  73. package/dist/types/features/build/error.d.ts +33 -0
  74. package/dist/types/features/build/index.d.ts +4 -3
  75. package/dist/types/features/build/legacy-constants.d.ts +8 -0
  76. package/dist/types/features/build/logger/loggerManager.d.ts +2 -0
  77. package/dist/types/features/build/normalize.d.ts +18 -0
  78. package/dist/types/features/build/types.d.ts +15 -0
  79. package/dist/types/features/build/utils.d.ts +9 -31
  80. package/dist/types/locale/en.d.ts +2 -0
  81. package/dist/types/locale/index.d.ts +4 -0
  82. package/dist/types/locale/zh.d.ts +2 -0
  83. package/dist/types/schema/build-config.d.ts +179 -0
  84. package/dist/types/schema/index.d.ts +166 -18
  85. package/dist/types/schema/output.d.ts +170 -10
  86. package/dist/types/schema/source.d.ts +2 -11
  87. package/dist/types/schema/tools.d.ts +6 -0
  88. package/dist/types/schema/types.d.ts +45 -0
  89. package/dist/types/types.d.ts +29 -28
  90. package/dist/types/utils/babel.d.ts +3 -2
  91. package/dist/types/utils/constants.d.ts +2 -0
  92. package/dist/types/utils/logger.d.ts +2 -0
  93. package/package.json +29 -16
  94. package/dist/js/modern/features/build/build-watch.js +0 -101
  95. package/dist/js/modern/features/build/build.js +0 -123
  96. package/dist/js/modern/tasks/build-source-code.js +0 -207
  97. package/dist/js/modern/tasks/build-style.js +0 -168
  98. package/dist/js/modern/tasks/build-watch-source-code.js +0 -161
  99. package/dist/js/modern/tasks/build-watch-style.js +0 -215
  100. package/dist/js/modern/tasks/constants.js +0 -1
  101. package/dist/js/node/features/build/build-watch.js +0 -121
  102. package/dist/js/node/features/build/build.js +0 -143
  103. package/dist/js/node/tasks/build-source-code.js +0 -233
  104. package/dist/js/node/tasks/build-style.js +0 -183
  105. package/dist/js/node/tasks/build-watch-source-code.js +0 -173
  106. package/dist/js/node/tasks/build-watch-style.js +0 -232
  107. package/dist/js/node/tasks/constants.js +0 -8
  108. package/dist/types/features/build/build-watch.d.ts +0 -3
  109. package/dist/types/features/build/build.d.ts +0 -3
  110. package/dist/types/tasks/build-source-code.d.ts +0 -25
  111. package/dist/types/tasks/build-style.d.ts +0 -1
  112. package/dist/types/tasks/build-watch-source-code.d.ts +0 -1
  113. package/dist/types/tasks/build-watch-style.d.ts +0 -1
  114. package/dist/types/tasks/constants.d.ts +0 -1
  115. package/dist/types/tasks/copy-assets.d.ts +0 -1
  116. package/dist/types/tasks/generator-dts/index.d.ts +0 -1
@@ -1,21 +1,18 @@
1
1
  import path from 'path';
2
- import { Import, fs } from '@modern-js/utils';
3
- const buildFeature = Import.lazy('./build', require);
4
- const buildWatchFeature = Import.lazy('./build-watch', require);
2
+ import { Import, fs, chalk } from '@modern-js/utils';
3
+ import { ReadlineUtils } from "../../utils/readline";
4
+ import { normalizeModuleConfig } from "./normalize";
5
+ import { buildingText, buildSuccessText } from "./constants";
6
+ import { ModuleBuildError, isInternalError } from "./error";
7
+ const bundle = Import.lazy('./bundle', require);
8
+ const bundleless = Import.lazy('./bundleless', require);
5
9
  const bp = Import.lazy('./build-platform', require);
6
- export const build = async (api, config, modernConfig) => {
10
+
11
+ const checkPlatformAndRunBuild = async (platform, options) => {
7
12
  const {
8
- appDirectory,
9
- enableWatchMode,
10
- platform,
11
- clear = true,
13
+ api,
12
14
  isTsProject
13
- } = config;
14
- const {
15
- output: {
16
- path: outputPath = 'dist'
17
- }
18
- } = modernConfig; // TODO: maybe need watch mode in build platform
15
+ } = options;
19
16
 
20
17
  if (typeof platform === 'boolean' && platform) {
21
18
  if (process.env.RUN_PLATFORM) {
@@ -25,7 +22,9 @@ export const build = async (api, config, modernConfig) => {
25
22
  });
26
23
  }
27
24
 
28
- return;
25
+ return {
26
+ exit: true
27
+ };
29
28
  }
30
29
 
31
30
  if (typeof platform === 'string') {
@@ -36,16 +35,82 @@ export const build = async (api, config, modernConfig) => {
36
35
  });
37
36
  }
38
37
 
38
+ return {
39
+ exit: true
40
+ };
41
+ }
42
+
43
+ return {
44
+ exit: false
45
+ };
46
+ };
47
+
48
+ export const buildInNormalMode = async buildTasks => {
49
+ console.info(chalk.blue.bold(buildingText));
50
+
51
+ try {
52
+ // eslint-disable-next-line no-console
53
+ console.time(buildSuccessText);
54
+ await Promise.all(buildTasks);
55
+ ReadlineUtils.clearPrevLine(process.stdout); // eslint-disable-next-line no-console
56
+
57
+ console.timeEnd(buildSuccessText);
58
+ } catch (e) {
59
+ ReadlineUtils.clearPrevLine(process.stdout);
60
+
61
+ if (isInternalError(e)) {
62
+ throw new ModuleBuildError(e);
63
+ } else {
64
+ throw e;
65
+ }
66
+ }
67
+ };
68
+ export const build = async (api, config) => {
69
+ const {
70
+ platform,
71
+ clear = true,
72
+ isTsProject
73
+ } = config;
74
+ const {
75
+ appDirectory
76
+ } = api.useAppContext();
77
+ const modernConfig = api.useResolvedConfigContext();
78
+ const {
79
+ output: {
80
+ path: outputPath = 'dist'
81
+ }
82
+ } = modernConfig; // build platform
83
+
84
+ const platformBuildRet = await checkPlatformAndRunBuild(platform, {
85
+ api,
86
+ isTsProject
87
+ });
88
+
89
+ if (platformBuildRet.exit) {
39
90
  return;
40
91
  }
41
92
 
42
93
  if (clear) {
43
94
  fs.removeSync(path.join(appDirectory, outputPath));
44
- }
95
+ } // should normalize module tool config here, ensure the same config for build
96
+
97
+
98
+ const normalizedModuleConfig = normalizeModuleConfig({
99
+ buildFeatOption: config,
100
+ api
101
+ });
102
+ const buildTasks = normalizedModuleConfig.map(moduleConfig => {
103
+ if (moduleConfig.buildType === 'bundle') {
104
+ return bundle.build(api, moduleConfig);
105
+ } else {
106
+ return bundleless.build(api, moduleConfig, config);
107
+ }
108
+ });
45
109
 
46
- if (enableWatchMode) {
47
- await buildWatchFeature.buildInWatchMode(api, config, modernConfig);
110
+ if (config.enableWatchMode) {
111
+ console.info(chalk.blue.underline('start build in watch mode...\n'));
112
+ await Promise.all(buildTasks);
48
113
  } else {
49
- await buildFeature.buildSourceCode(api, config, modernConfig);
114
+ await buildInNormalMode(buildTasks);
50
115
  }
51
116
  };
@@ -0,0 +1,99 @@
1
+ // Universal JS 的默认选择,三份构建产物,支持 Node.js,对现代浏览器有优化
2
+ const universalJs = [{
3
+ format: 'esm',
4
+ target: 'es5',
5
+ outputPath: './js/treeshaking',
6
+ bundlelessOptions: {
7
+ style: {
8
+ compileMode: 'only-source-code'
9
+ }
10
+ }
11
+ }, {
12
+ format: 'cjs',
13
+ target: 'es6',
14
+ outputPath: './js/node'
15
+ }, {
16
+ format: 'esm',
17
+ target: 'es6',
18
+ outputPath: './js/modern'
19
+ }]; // Universal JS 的优化选择,两份构建产物,对现代浏览器无优化
20
+
21
+ const universalJsLite = [{
22
+ format: 'esm',
23
+ target: 'es5',
24
+ outputPath: './js/treeshaking',
25
+ bundlelessOptions: {
26
+ style: {
27
+ compileMode: 'only-source-code'
28
+ }
29
+ }
30
+ }, {
31
+ format: 'cjs',
32
+ target: 'es6',
33
+ outputPath: './js/node'
34
+ }, {
35
+ format: 'esm',
36
+ target: 'es5',
37
+ outputPath: './js/modern'
38
+ }]; // 纯前端代码的默认选择,两份构建产物
39
+
40
+ const browserJs = [{
41
+ format: 'esm',
42
+ target: 'es5',
43
+ outputPath: './js/treeshaking',
44
+ bundlelessOptions: {
45
+ style: {
46
+ compileMode: 'only-source-code'
47
+ }
48
+ }
49
+ }, {
50
+ format: 'esm',
51
+ target: 'es6',
52
+ outputPath: './js/node'
53
+ }, {
54
+ format: 'esm',
55
+ target: 'es6',
56
+ outputPath: './js/modern'
57
+ }]; // 纯前端代码的优化选择,单份构建产物,对现代浏览器无优化
58
+
59
+ const browserJsLite = [{
60
+ format: 'esm',
61
+ target: 'es5',
62
+ outputPath: './js/treeshaking',
63
+ bundlelessOptions: {
64
+ style: {
65
+ compileMode: 'only-source-code'
66
+ }
67
+ }
68
+ }, {
69
+ format: 'esm',
70
+ target: 'es5',
71
+ outputPath: './js/node'
72
+ }, {
73
+ format: 'esm',
74
+ target: 'es5',
75
+ outputPath: './js/modern'
76
+ }]; // 纯 Node.js 代码的默认选择,两份构建产物
77
+
78
+ const nodeJs = [{
79
+ format: 'cjs',
80
+ target: 'es6',
81
+ outputPath: './js/node',
82
+ bundlelessOptions: {
83
+ style: {
84
+ compileMode: 'only-source-code'
85
+ }
86
+ }
87
+ }, {
88
+ format: 'esm',
89
+ target: 'es6',
90
+ outputPath: './js/modern'
91
+ }];
92
+ export const DEFAULT_PACKAGE_MODE = 'universal-js';
93
+ export const PACKAGE_MODES = {
94
+ 'universal-js': universalJs,
95
+ 'universal-js-lite': universalJsLite,
96
+ 'browser-js': browserJs,
97
+ 'browser-js-lite': browserJsLite,
98
+ 'node-js': nodeJs
99
+ };
@@ -0,0 +1,293 @@
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
+ 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
+ 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 { Import, lodash } from '@modern-js/utils';
8
+ import { mergeWith } from '@modern-js/utils/lodash';
9
+ import { cliTsConfigDefaultValue } from "../../utils/constants";
10
+ const constants = Import.lazy('./constants', require);
11
+ const legacyConstants = Import.lazy('./legacy-constants', require);
12
+ export const getNormalizeModuleConfigByPackageModeAndFileds = (api, buildFeatOption) => {
13
+ const {
14
+ output: {
15
+ packageMode,
16
+ packageFields,
17
+ disableTsChecker,
18
+ importStyle
19
+ }
20
+ } = api.useResolvedConfigContext();
21
+ let configs = [];
22
+
23
+ if (buildFeatOption.styleOnly) {
24
+ configs.push({
25
+ buildType: 'bundleless',
26
+ outputPath: './js/styles',
27
+ bundlelessOptions: {
28
+ sourceDir: './src',
29
+ style: {
30
+ compileMode: importStyle === 'source-code' ? 'only-source-code' : 'all'
31
+ }
32
+ }
33
+ });
34
+ configs.push({
35
+ buildType: 'bundleless',
36
+ outputPath: './styles',
37
+ bundlelessOptions: {
38
+ sourceDir: './styles',
39
+ style: {
40
+ compileMode: 'only-compiled-code'
41
+ }
42
+ }
43
+ });
44
+ return configs;
45
+ }
46
+
47
+ const commonConfig = {
48
+ buildType: 'bundleless',
49
+ bundlelessOptions: {
50
+ sourceDir: './src',
51
+ style: {
52
+ path: '../styles',
53
+ compileMode: false
54
+ },
55
+ static: {
56
+ path: '../styles'
57
+ }
58
+ },
59
+ outputPath: './'
60
+ };
61
+ commonConfig.tsconfig = getFinalTsconfig(commonConfig, buildFeatOption);
62
+
63
+ if (!packageFields || typeof packageFields === 'object' && Object.keys(packageFields).length === 0) {
64
+ const buildConfigs = legacyConstants.PACKAGE_MODES[packageMode || legacyConstants.DEFAULT_PACKAGE_MODE];
65
+ configs = buildConfigs.map(config => lodash.mergeWith({}, commonConfig, config));
66
+ } else {
67
+ const getConfigsByJsSyntaxType = (js, outputPath) => {
68
+ if (js === 'CJS+ES6') {
69
+ return lodash.mergeWith({}, commonConfig, {
70
+ format: 'cjs',
71
+ target: 'es6',
72
+ outputPath
73
+ });
74
+ } else if (js === 'ESM+ES5') {
75
+ return lodash.mergeWith({}, commonConfig, {
76
+ format: 'esm',
77
+ target: 'es5',
78
+ outputPath
79
+ });
80
+ }
81
+
82
+ return lodash.mergeWith({}, commonConfig, {
83
+ format: 'esm',
84
+ target: 'es6',
85
+ outputPath
86
+ });
87
+ };
88
+
89
+ if (!packageFields['jsnext:modern'] && !packageFields.main && !packageFields.module) {
90
+ throw new Error(`Unrecognized ${JSON.stringify(packageFields)} configuration, please use keys: 'modern, main, jupiter:modern' and use values: 'CJS+ES6, ESM+ES5, ESM+ES6'`);
91
+ } // The fields configured in packageFields correspond to the main, module, and jsnext:modern fields on package.json,
92
+ // and can also be used on package.json exports field.
93
+
94
+
95
+ if (packageFields['jsnext:modern']) {
96
+ configs.push(getConfigsByJsSyntaxType(packageFields['jsnext:modern'], 'modern'));
97
+ }
98
+
99
+ if (packageFields.main) {
100
+ configs.push(getConfigsByJsSyntaxType(packageFields.main, 'node'));
101
+ }
102
+
103
+ if (packageFields.module) {
104
+ configs.push(getConfigsByJsSyntaxType(packageFields.module, 'treeshaking'));
105
+ }
106
+ }
107
+
108
+ if (configs.length > 0) {
109
+ const firstConfig = configs[0];
110
+ firstConfig.bundlelessOptions = lodash.mergeWith({}, firstConfig.bundlelessOptions, {
111
+ style: {
112
+ compileMode: importStyle === 'source-code' ? 'only-source-code' : 'all'
113
+ }
114
+ });
115
+ } // [compatibe mode]: dts gen
116
+
117
+
118
+ if (buildFeatOption.legacyTsc && !disableTsChecker) {
119
+ configs.push({
120
+ buildType: 'bundleless',
121
+ outputPath: './types',
122
+ enableDts: true,
123
+ dtsOnly: true
124
+ });
125
+ }
126
+
127
+ configs.push({
128
+ buildType: 'bundleless',
129
+ outputPath: './styles',
130
+ bundlelessOptions: {
131
+ sourceDir: './styles',
132
+ style: {
133
+ compileMode: 'only-compiled-code'
134
+ }
135
+ }
136
+ });
137
+ return configs;
138
+ };
139
+ export const getFinalTsconfig = (config, buildFeatOption) => {
140
+ var _config$tsconfig;
141
+
142
+ // cli tsconfig option > buildPreset tsconfig option
143
+ // Since tsconfig configuration has default values,
144
+ // compare the two to see if the user is configured with cli tsconfig
145
+ if (buildFeatOption.tsconfigName !== cliTsConfigDefaultValue) {
146
+ return buildFeatOption.tsconfigName;
147
+ }
148
+
149
+ return (_config$tsconfig = config.tsconfig) !== null && _config$tsconfig !== void 0 ? _config$tsconfig : './tsconfig.json';
150
+ };
151
+ export const getFinalDts = (config, buildFeatOption) => {
152
+ var _config$enableDts;
153
+
154
+ // is js project, should return false
155
+ if (!buildFeatOption.isTsProject) {
156
+ return false;
157
+ } // when `build --dts`, then all build tasks`s enableDts is true
158
+
159
+
160
+ if (buildFeatOption.enableDtsGen) {
161
+ return true;
162
+ }
163
+
164
+ return (_config$enableDts = config.enableDts) !== null && _config$enableDts !== void 0 ? _config$enableDts : false;
165
+ };
166
+ export const getSourceMap = (config, buildType, api) => {
167
+ // TODO: remove
168
+ const {
169
+ output: {
170
+ disableSourceMap
171
+ }
172
+ } = api.useResolvedConfigContext();
173
+
174
+ if (disableSourceMap) {
175
+ return false;
176
+ }
177
+
178
+ if (config.sourceMap !== undefined) {
179
+ return config.sourceMap;
180
+ }
181
+
182
+ return buildType === 'bundle';
183
+ };
184
+ export const normalizeBuildConfig = (context, buildConfig) => {
185
+ const {
186
+ buildFeatOption,
187
+ api
188
+ } = context;
189
+ const configArray = Array.isArray(buildConfig) ? buildConfig : [buildConfig];
190
+ const normalizedModule = configArray.map(config => {
191
+ var _config$format, _config$target, _bundleOptions$minify, _config$outputPath, _config$dtsOnly;
192
+
193
+ const format = (_config$format = config.format) !== null && _config$format !== void 0 ? _config$format : 'cjs';
194
+ const target = (_config$target = config.target) !== null && _config$target !== void 0 ? _config$target : 'esnext';
195
+ const {
196
+ bundleOptions
197
+ } = config;
198
+
199
+ const normalizedBundleOption = _objectSpread(_objectSpread({}, bundleOptions), {}, {
200
+ entry: (bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.entry) || {
201
+ index: `src/index.${buildFeatOption.isTsProject ? 'ts' : 'js'}`
202
+ },
203
+ platform: (bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.platform) || 'node',
204
+ minify: (_bundleOptions$minify = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.minify) !== null && _bundleOptions$minify !== void 0 ? _bundleOptions$minify : false
205
+ });
206
+
207
+ const normalizedBundlelessOptions = mergeWith({}, {
208
+ sourceDir: './src',
209
+ style: {
210
+ compileMode: 'all',
211
+ path: './'
212
+ },
213
+ static: {
214
+ path: './'
215
+ }
216
+ }, config.bundlelessOptions);
217
+ const watch = buildFeatOption.enableWatchMode || false;
218
+ const tsconfig = getFinalTsconfig(config, buildFeatOption);
219
+ const enableDts = getFinalDts(config, buildFeatOption);
220
+ const outputPath = (_config$outputPath = config.outputPath) !== null && _config$outputPath !== void 0 ? _config$outputPath : './';
221
+ const sourceMap = getSourceMap(config, config.buildType, api);
222
+ const dtsOnly = (_config$dtsOnly = config.dtsOnly) !== null && _config$dtsOnly !== void 0 ? _config$dtsOnly : false;
223
+ const commmonConfig = {
224
+ format,
225
+ target,
226
+ watch,
227
+ tsconfig,
228
+ enableDts,
229
+ outputPath,
230
+ dtsOnly,
231
+ sourceMap
232
+ };
233
+
234
+ if (!buildFeatOption.isTsProject && (dtsOnly || enableDts)) {
235
+ console.warn('[WARN] dtsOnly、enableDts 配置仅在 Ts 项目下生效');
236
+ } else if (buildFeatOption.isTsProject && dtsOnly && !enableDts) {
237
+ console.warn('[WARN] dtsOnly 配置仅在 enableDts 为 true 的时候生效. 请检查当前的 dtsOnly、enableDts 是否配置正确');
238
+ }
239
+
240
+ if (config.buildType === 'bundle') {
241
+ return _objectSpread(_objectSpread({}, commmonConfig), {}, {
242
+ buildType: 'bundle',
243
+ bundleOptions: normalizedBundleOption
244
+ });
245
+ } else {
246
+ return _objectSpread(_objectSpread({}, commmonConfig), {}, {
247
+ buildType: 'bundleless',
248
+ bundlelessOptions: normalizedBundlelessOptions
249
+ });
250
+ }
251
+ });
252
+ return normalizedModule;
253
+ };
254
+ export const normalizeModuleConfig = context => {
255
+ const {
256
+ buildFeatOption,
257
+ api
258
+ } = context;
259
+ const {
260
+ output: {
261
+ buildConfig,
262
+ buildPreset
263
+ }
264
+ } = api.useResolvedConfigContext(); // buildConfig is the most important.
265
+
266
+ if (buildConfig) {
267
+ return normalizeBuildConfig(context, buildConfig);
268
+ } // buildPreset is the second important. It can be used when buildConfig is not defined.
269
+ // buildPreset -> buildConfig
270
+
271
+
272
+ if (buildPreset) {
273
+ const {
274
+ unPresetConfigs,
275
+ unPresetWithTargetConfigs
276
+ } = constants;
277
+
278
+ if (unPresetConfigs[buildPreset]) {
279
+ return normalizeBuildConfig(context, unPresetConfigs[buildPreset]);
280
+ } else if (unPresetWithTargetConfigs[buildPreset]) {
281
+ return normalizeBuildConfig(context, unPresetWithTargetConfigs[buildPreset]);
282
+ } // If the buildPreset is not found, then it is used 'npm-library'
283
+ // TODO: Warning: The buildPreset 'npm-library' is not supported.
284
+
285
+
286
+ return normalizeBuildConfig(context, unPresetConfigs['npm-library']);
287
+ } // If the user does not configure buildConfig and buildPreset,
288
+ // the configuration is generated based on packageMode and packageField
289
+
290
+
291
+ const legacyBuildConfig = getNormalizeModuleConfigByPackageModeAndFileds(api, buildFeatOption);
292
+ return normalizeBuildConfig(context, legacyBuildConfig);
293
+ };
File without changes
@@ -1,129 +1,9 @@
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
2
 
3
- import * as path from 'path';
4
3
  import * as os from 'os';
5
- import { Import, chalk } from '@modern-js/utils';
6
- const constants = Import.lazy('./constants', require); // 硬解字符串返回相应格式的对象
7
-
8
- const updateMapper = (packageFieldValue, outDir, mapper) => {
9
- if (packageFieldValue === 'CJS+ES6') {
10
- return [...mapper, {
11
- type: 'commonjs',
12
- syntax: 'es6+',
13
- outDir
14
- }];
15
- } else if (packageFieldValue === 'ESM+ES5') {
16
- return [...mapper, {
17
- type: 'module',
18
- syntax: 'es5',
19
- outDir
20
- }];
21
- } else if (packageFieldValue === 'ESM+ES6') {
22
- return [...mapper, {
23
- type: 'module',
24
- syntax: 'es6+',
25
- outDir
26
- }];
27
- } else {
28
- return [...mapper];
29
- }
30
- };
31
-
32
- export const getCodeInitMapper = (api, _) => {
33
- const {
34
- output: {
35
- packageFields,
36
- packageMode
37
- }
38
- } = api.useResolvedConfigContext();
39
- let initMapper = []; // 如果不存在packageFields配置或者packageFields为空对象,则使用 packageMode
40
-
41
- if (!packageFields || typeof packageFields === 'object' && Object.keys(packageFields).length === 0) {
42
- initMapper = constants.PACKAGE_MODES[packageMode || constants.DEFAULT_PACKAGE_MODE];
43
- } else if (packageFields && Object.keys(packageFields).length > 0) {
44
- if (packageFields.modern) {
45
- initMapper = updateMapper(packageFields.modern, 'modern', initMapper);
46
- }
47
-
48
- if (packageFields.main) {
49
- initMapper = updateMapper(packageFields.main, 'node', initMapper);
50
- }
51
-
52
- if (packageFields.module) {
53
- initMapper = updateMapper(packageFields.module, 'treeshaking', initMapper);
54
- } // TODO: 如果存在其他配置,需要提示
55
-
56
-
57
- if (!packageFields.modern && !packageFields.main && !packageFields.module) {
58
- console.error(chalk.red(`Unrecognized ${JSON.stringify(packageFields)} configuration, please use keys: 'modern, main, jupiter:modern' and use values: 'CJS+ES6, ESM+ES5, ESM+ES6'`)); // eslint-disable-next-line no-process-exit
59
-
60
- process.exit(1);
61
- }
62
- }
63
-
64
- return initMapper;
65
- }; // 获取执行构建源码的参数
66
-
67
- export const getCodeMapper = (api, {
68
- logger,
69
- taskPath,
70
- config,
71
- initMapper,
72
- srcRootDir,
73
- willCompilerDirOrFile
74
- }) => {
75
- const {
76
- appDirectory
77
- } = api.useAppContext();
78
- const modernConfig = api.useResolvedConfigContext();
79
- const {
80
- output: {
81
- enableSourceMap,
82
- jsPath = 'js',
83
- path: distDir = 'dist'
84
- }
85
- } = modernConfig;
86
- const {
87
- tsconfigName = 'tsconfig.json'
88
- } = config;
89
- const tsconfigPath = path.join(appDirectory, tsconfigName);
90
- return initMapper.map(option => {
91
- var _option$copyDirs;
92
-
93
- // 不是output.copy配置,而是内部的js copy逻辑
94
- const copyDirs = (_option$copyDirs = option.copyDirs) === null || _option$copyDirs === void 0 ? void 0 : _option$copyDirs.map(copyDir => path.join(appDirectory, `./${distDir}/${jsPath}/${copyDir}`));
95
- return {
96
- logger,
97
- taskPath,
98
- params: [`--syntax=${option.syntax}`, `--type=${option.type}`, `--srcRootDir=${srcRootDir}`, `--willCompilerDirOrFile=${willCompilerDirOrFile}`, copyDirs ? `--copyDirs=${copyDirs.join(',')}` : '', `--distDir=${path.join(appDirectory, `./${distDir}/${jsPath}/${option.outDir}`)}`, `--appDirectory=${appDirectory}`, enableSourceMap ? '--sourceMaps' : '', `--tsconfigPath=${tsconfigPath}`]
99
- };
100
- });
101
- }; // 获取执行生成 d.ts 的参数
102
-
103
- export const getDtsMapper = (api, config, logger) => {
104
- const {
105
- appDirectory
106
- } = api.useAppContext();
107
- const modernConfig = api.useResolvedConfigContext();
108
- const {
109
- output: {
110
- disableTsChecker,
111
- path: outputPath = 'dist'
112
- }
113
- } = modernConfig;
114
- const {
115
- tsconfigName = 'tsconfig.json',
116
- enableWatchMode,
117
- sourceDir
118
- } = config;
119
- const srcDir = path.join(appDirectory, './src');
120
- const tsconfigPath = path.join(appDirectory, tsconfigName);
121
- return [{
122
- logger,
123
- taskPath: require.resolve("../../tasks/generator-dts"),
124
- params: [`--srcDir=${srcDir}`, `--distDir=${path.join(appDirectory, `./${outputPath}/types`)}`, `--appDirectory=${appDirectory}`, `--tsconfigPath=${tsconfigPath}`, `--sourceDirName=${sourceDir}`, enableWatchMode ? `--watch` : '', disableTsChecker ? '' : `--tsCheck`]
125
- }];
126
- };
4
+ import { chalk, Import } from '@modern-js/utils';
5
+ const constants = Import.lazy('./constants', require);
6
+ const cssConfig = Import.lazy('@modern-js/css-config', require);
127
7
  /**
128
8
  * 处理日志信息
129
9
  */
@@ -176,19 +56,31 @@ export const logTemplate = (title, messageStack, maxLength, {
176
56
  const template = `${title}:
177
57
  ${_contentColor(messageFragments.join(os.EOL))}${_noBottomBorder ? '' : `\n${_bottomBorderText}`}`;
178
58
  return template;
179
- }; // eslint-disable-next-line @typescript-eslint/no-extraneous-class
180
-
181
- export class TimeCounter {
182
- static time() {
183
- this._now = Date.now();
184
- }
185
-
186
- static timeEnd() {
187
- const span = Date.now() - this._now;
188
-
189
- return span < 1000 ? `${span}ms` : `${(span / 1000).toFixed(2)}s`;
59
+ };
60
+ export let SectionTitleStatus;
61
+
62
+ (function (SectionTitleStatus) {
63
+ SectionTitleStatus[SectionTitleStatus["Success"] = 0] = "Success";
64
+ SectionTitleStatus[SectionTitleStatus["Fail"] = 1] = "Fail";
65
+ SectionTitleStatus[SectionTitleStatus["Log"] = 2] = "Log";
66
+ })(SectionTitleStatus || (SectionTitleStatus = {}));
67
+
68
+ export const watchSectionTitle = (str, status) => {
69
+ if (status === SectionTitleStatus.Success) {
70
+ return `${chalk.bgWhite.gray.underline(str)} ${chalk.green.underline('Successful')}`;
71
+ } else if (status === SectionTitleStatus.Fail) {
72
+ return `${chalk.bgWhite.gray.underline(str)} ${chalk.red.underline('Build Failed')}`;
190
73
  }
191
74
 
192
- }
193
-
194
- _defineProperty(TimeCounter, "_now", void 0);
75
+ return `${chalk.bgWhite.gray.underline(str)} ${chalk.blue.underline('Log')}`;
76
+ };
77
+ export const getPostcssOption = (appDirectory, modernConfig) => {
78
+ var _postcssOption$postcs;
79
+
80
+ const postcssOption = cssConfig.getPostcssConfig(appDirectory, modernConfig, false);
81
+ return {
82
+ plugins: (postcssOption === null || postcssOption === void 0 ? void 0 : (_postcssOption$postcs = postcssOption.postcssOptions) === null || _postcssOption$postcs === void 0 ? void 0 : _postcssOption$postcs.plugins) || [],
83
+ enableSourceMap: (postcssOption === null || postcssOption === void 0 ? void 0 : postcssOption.sourceMap) || false,
84
+ options: {}
85
+ };
86
+ };