@modern-js/module-tools 1.0.0-alpha.3

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 (187) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/LICENSE +21 -0
  3. package/README.md +32 -0
  4. package/bin/modern.js +3 -0
  5. package/dist/js/modern/cli/build.js +9 -0
  6. package/dist/js/modern/cli/dev.js +8 -0
  7. package/dist/js/modern/cli/index.js +3 -0
  8. package/dist/js/modern/cli/new.js +16 -0
  9. package/dist/js/modern/commands/build.js +56 -0
  10. package/dist/js/modern/commands/dev.js +29 -0
  11. package/dist/js/modern/commands/index.js +2 -0
  12. package/dist/js/modern/features/build/build-platform.js +70 -0
  13. package/dist/js/modern/features/build/build-watch.js +96 -0
  14. package/dist/js/modern/features/build/build.js +110 -0
  15. package/dist/js/modern/features/build/constants.js +66 -0
  16. package/dist/js/modern/features/build/index.js +29 -0
  17. package/dist/js/modern/features/build/logger/index.js +2 -0
  18. package/dist/js/modern/features/build/logger/logText.js +63 -0
  19. package/dist/js/modern/features/build/logger/loggerManager.js +101 -0
  20. package/dist/js/modern/features/build/utils.js +198 -0
  21. package/dist/js/modern/features/dev/index.js +41 -0
  22. package/dist/js/modern/index.js +46 -0
  23. package/dist/js/modern/locale/en.js +23 -0
  24. package/dist/js/modern/locale/index.js +10 -0
  25. package/dist/js/modern/locale/zh.js +23 -0
  26. package/dist/js/modern/schema/index.js +3 -0
  27. package/dist/js/modern/schema/output.js +29 -0
  28. package/dist/js/modern/schema/source.js +15 -0
  29. package/dist/js/modern/tasks/build-source-code.js +213 -0
  30. package/dist/js/modern/tasks/build-style.js +152 -0
  31. package/dist/js/modern/tasks/build-watch-source-code.js +144 -0
  32. package/dist/js/modern/tasks/build-watch-style.js +213 -0
  33. package/dist/js/modern/tasks/constants.js +1 -0
  34. package/dist/js/modern/tasks/copy-assets.js +108 -0
  35. package/dist/js/modern/tasks/generator-dts.js +220 -0
  36. package/dist/js/modern/types.js +0 -0
  37. package/dist/js/modern/utils/babel.js +85 -0
  38. package/dist/js/modern/utils/color.js +2 -0
  39. package/dist/js/modern/utils/copy.js +80 -0
  40. package/dist/js/modern/utils/json.js +13 -0
  41. package/dist/js/modern/utils/language.js +6 -0
  42. package/dist/js/modern/utils/logger.js +110 -0
  43. package/dist/js/modern/utils/readline.js +19 -0
  44. package/dist/js/modern/utils/tsconfig.js +15 -0
  45. package/dist/js/modern/utils/tspaths-transform/constants.js +3 -0
  46. package/dist/js/modern/utils/tspaths-transform/index.js +130 -0
  47. package/dist/js/modern/utils/tspaths-transform/utils.js +21 -0
  48. package/dist/js/modern/utils/valide.js +28 -0
  49. package/dist/js/node/cli/build.js +21 -0
  50. package/dist/js/node/cli/dev.js +20 -0
  51. package/dist/js/node/cli/index.js +44 -0
  52. package/dist/js/node/cli/new.js +28 -0
  53. package/dist/js/node/commands/build.js +76 -0
  54. package/dist/js/node/commands/dev.js +49 -0
  55. package/dist/js/node/commands/index.js +21 -0
  56. package/dist/js/node/features/build/build-platform.js +84 -0
  57. package/dist/js/node/features/build/build-watch.js +118 -0
  58. package/dist/js/node/features/build/build.js +133 -0
  59. package/dist/js/node/features/build/constants.js +79 -0
  60. package/dist/js/node/features/build/index.js +41 -0
  61. package/dist/js/node/features/build/logger/index.js +31 -0
  62. package/dist/js/node/features/build/logger/logText.js +79 -0
  63. package/dist/js/node/features/build/logger/loggerManager.js +111 -0
  64. package/dist/js/node/features/build/utils.js +231 -0
  65. package/dist/js/node/features/dev/index.js +60 -0
  66. package/dist/js/node/index.js +64 -0
  67. package/dist/js/node/locale/en.js +30 -0
  68. package/dist/js/node/locale/index.js +22 -0
  69. package/dist/js/node/locale/zh.js +30 -0
  70. package/dist/js/node/schema/index.js +14 -0
  71. package/dist/js/node/schema/output.js +36 -0
  72. package/dist/js/node/schema/source.js +22 -0
  73. package/dist/js/node/tasks/build-source-code.js +240 -0
  74. package/dist/js/node/tasks/build-style.js +168 -0
  75. package/dist/js/node/tasks/build-watch-source-code.js +156 -0
  76. package/dist/js/node/tasks/build-watch-style.js +231 -0
  77. package/dist/js/node/tasks/constants.js +8 -0
  78. package/dist/js/node/tasks/copy-assets.js +126 -0
  79. package/dist/js/node/tasks/generator-dts.js +243 -0
  80. package/dist/js/node/types.js +0 -0
  81. package/dist/js/node/utils/babel.js +99 -0
  82. package/dist/js/node/utils/color.js +12 -0
  83. package/dist/js/node/utils/copy.js +99 -0
  84. package/dist/js/node/utils/json.js +22 -0
  85. package/dist/js/node/utils/language.js +15 -0
  86. package/dist/js/node/utils/logger.js +133 -0
  87. package/dist/js/node/utils/readline.js +33 -0
  88. package/dist/js/node/utils/tsconfig.js +30 -0
  89. package/dist/js/node/utils/tspaths-transform/constants.js +10 -0
  90. package/dist/js/node/utils/tspaths-transform/index.js +155 -0
  91. package/dist/js/node/utils/tspaths-transform/utils.js +32 -0
  92. package/dist/js/node/utils/valide.js +41 -0
  93. package/dist/types/cli/build.d.ts +2 -0
  94. package/dist/types/cli/dev.d.ts +2 -0
  95. package/dist/types/cli/index.d.ts +3 -0
  96. package/dist/types/cli/new.d.ts +2 -0
  97. package/dist/types/commands/build.d.ts +16 -0
  98. package/dist/types/commands/dev.d.ts +4 -0
  99. package/dist/types/commands/index.d.ts +2 -0
  100. package/dist/types/features/build/build-platform.d.ts +6 -0
  101. package/dist/types/features/build/build-watch.d.ts +3 -0
  102. package/dist/types/features/build/build.d.ts +3 -0
  103. package/dist/types/features/build/constants.d.ts +14 -0
  104. package/dist/types/features/build/index.d.ts +3 -0
  105. package/dist/types/features/build/logger/index.d.ts +2 -0
  106. package/dist/types/features/build/logger/logText.d.ts +39 -0
  107. package/dist/types/features/build/logger/loggerManager.d.ts +39 -0
  108. package/dist/types/features/build/utils.d.ts +58 -0
  109. package/dist/types/features/dev/index.d.ts +7 -0
  110. package/dist/types/index.d.ts +17 -0
  111. package/dist/types/locale/en.d.ts +23 -0
  112. package/dist/types/locale/index.d.ts +47 -0
  113. package/dist/types/locale/zh.d.ts +23 -0
  114. package/dist/types/schema/index.d.ts +36 -0
  115. package/dist/types/schema/output.d.ts +22 -0
  116. package/dist/types/schema/source.d.ts +15 -0
  117. package/dist/types/tasks/build-source-code.d.ts +42 -0
  118. package/dist/types/tasks/build-style.d.ts +1 -0
  119. package/dist/types/tasks/build-watch-source-code.d.ts +1 -0
  120. package/dist/types/tasks/build-watch-style.d.ts +1 -0
  121. package/dist/types/tasks/constants.d.ts +1 -0
  122. package/dist/types/tasks/copy-assets.d.ts +1 -0
  123. package/dist/types/tasks/generator-dts.d.ts +1 -0
  124. package/dist/types/types.d.ts +48 -0
  125. package/dist/types/utils/babel.d.ts +8 -0
  126. package/dist/types/utils/color.d.ts +2 -0
  127. package/dist/types/utils/copy.d.ts +5 -0
  128. package/dist/types/utils/json.d.ts +4 -0
  129. package/dist/types/utils/language.d.ts +1 -0
  130. package/dist/types/utils/logger.d.ts +56 -0
  131. package/dist/types/utils/readline.d.ts +9 -0
  132. package/dist/types/utils/tsconfig.d.ts +16 -0
  133. package/dist/types/utils/tspaths-transform/constants.d.ts +1 -0
  134. package/dist/types/utils/tspaths-transform/index.d.ts +15 -0
  135. package/dist/types/utils/tspaths-transform/utils.d.ts +4 -0
  136. package/dist/types/utils/valide.d.ts +12 -0
  137. package/modern.config.js +6 -0
  138. package/package.json +95 -0
  139. package/src/.eslintrc.json +5 -0
  140. package/src/cli/build.ts +39 -0
  141. package/src/cli/dev.ts +24 -0
  142. package/src/cli/index.ts +3 -0
  143. package/src/cli/new.ts +32 -0
  144. package/src/commands/build.ts +81 -0
  145. package/src/commands/dev.ts +41 -0
  146. package/src/commands/index.ts +2 -0
  147. package/src/features/build/build-platform.ts +76 -0
  148. package/src/features/build/build-watch.ts +93 -0
  149. package/src/features/build/build.ts +111 -0
  150. package/src/features/build/constants.ts +52 -0
  151. package/src/features/build/index.ts +36 -0
  152. package/src/features/build/logger/index.ts +2 -0
  153. package/src/features/build/logger/logText.ts +80 -0
  154. package/src/features/build/logger/loggerManager.ts +132 -0
  155. package/src/features/build/utils.ts +250 -0
  156. package/src/features/dev/index.ts +62 -0
  157. package/src/index.ts +55 -0
  158. package/src/locale/en.ts +21 -0
  159. package/src/locale/index.ts +15 -0
  160. package/src/locale/zh.ts +21 -0
  161. package/src/schema/index.ts +4 -0
  162. package/src/schema/output.ts +34 -0
  163. package/src/schema/schema.d.ts +13 -0
  164. package/src/schema/source.ts +16 -0
  165. package/src/tasks/build-source-code.ts +245 -0
  166. package/src/tasks/build-style.ts +175 -0
  167. package/src/tasks/build-watch-source-code.ts +185 -0
  168. package/src/tasks/build-watch-style.ts +260 -0
  169. package/src/tasks/constants.ts +1 -0
  170. package/src/tasks/copy-assets.ts +117 -0
  171. package/src/tasks/generator-dts.ts +277 -0
  172. package/src/type.d.ts +1 -0
  173. package/src/types.ts +63 -0
  174. package/src/utils/babel.ts +100 -0
  175. package/src/utils/color.ts +3 -0
  176. package/src/utils/copy.ts +70 -0
  177. package/src/utils/json.ts +13 -0
  178. package/src/utils/language.ts +9 -0
  179. package/src/utils/logger.ts +144 -0
  180. package/src/utils/readline.ts +28 -0
  181. package/src/utils/tsconfig.ts +37 -0
  182. package/src/utils/tspaths-transform/constants.ts +19 -0
  183. package/src/utils/tspaths-transform/index.ts +139 -0
  184. package/src/utils/tspaths-transform/utils.ts +25 -0
  185. package/src/utils/valide.ts +37 -0
  186. package/tsconfig.json +13 -0
  187. package/types.d.ts +1 -0
@@ -0,0 +1,52 @@
1
+ import type { IPackageModeValue } from '../../types';
2
+
3
+ // Universal JS 的默认选择,三份构建产物,支持 Node.js,对现代浏览器有优化
4
+ const universalJs: IPackageModeValue[] = [
5
+ { type: 'module', syntax: 'es5', outDir: 'treeshaking' },
6
+ { type: 'commonjs', syntax: 'es6+', outDir: 'node' },
7
+ { type: 'module', syntax: 'es6+', outDir: 'modern' },
8
+ ];
9
+
10
+ // Universal JS 的优化选择,两份构建产物,对现代浏览器无优化
11
+ const universalJsLite: IPackageModeValue[] = [
12
+ { type: 'module', syntax: 'es5', outDir: 'treeshaking' },
13
+ { type: 'commonjs', syntax: 'es6+', outDir: 'node', copyDirs: ['modern'] },
14
+ ];
15
+
16
+ // 纯前端代码的默认选择,两份构建产物
17
+ const browserJs: IPackageModeValue[] = [
18
+ { type: 'module', syntax: 'es5', outDir: 'treeshaking', copyDirs: ['node'] },
19
+ { type: 'module', syntax: 'es6+', outDir: 'modern' },
20
+ ];
21
+
22
+ // 纯前端代码的优化选择,单份构建产物,对现代浏览器无优化
23
+ const browserJsLite: IPackageModeValue[] = [
24
+ {
25
+ type: 'module',
26
+ syntax: 'es5',
27
+ outDir: 'treeshaking',
28
+ copyDirs: ['modern', 'node'],
29
+ },
30
+ ];
31
+
32
+ // 纯 Node.js 代码的默认选择,两份构建产物
33
+ const nodeJs: IPackageModeValue[] = [
34
+ { type: 'commonjs', syntax: 'es6+', outDir: 'node' },
35
+ { type: 'module', syntax: 'es6+', outDir: 'modern' },
36
+ ];
37
+
38
+ export const DEFAULT_PACKAGE_MODE = 'universal-js';
39
+
40
+ export const PACKAGE_MODES = {
41
+ 'universal-js': universalJs,
42
+ 'universal-js-lite': universalJsLite,
43
+ 'browser-js': browserJs,
44
+ 'browser-js-lite': browserJsLite,
45
+ 'node-js': nodeJs,
46
+ };
47
+
48
+ export const runBabelCompilerTitle = 'Run babel compiler code log';
49
+ export const runTscWatchTitle = 'Run `tsc -w` log';
50
+ export const runTscTitle = 'Run `tsc` log';
51
+ export const runStyleCompilerTitle = 'Run style compiler code log';
52
+ export const clearFlag = '\x1Bc';
@@ -0,0 +1,36 @@
1
+ import { Import } from '@modern-js/utils';
2
+ import type { NormalizedConfig } from '@modern-js/core';
3
+ import type { IBuildConfig } from '../../types';
4
+
5
+ const buildFeature: typeof import('./build') = Import.lazy('./build', require);
6
+ const buildWatchFeature: typeof import('./build-watch') = Import.lazy(
7
+ './build-watch',
8
+ require,
9
+ );
10
+ // const bp: typeof import('./build-platform') = Import.lazy(
11
+ // './build-platform',
12
+ // require,
13
+ // );
14
+
15
+ export const build = async (
16
+ config: IBuildConfig,
17
+ modernConfig: NormalizedConfig,
18
+ ) => {
19
+ const { enableWatchMode, platform } = config;
20
+ // TODO: maybe need watch mode in build platform
21
+ if (typeof platform === 'boolean' && platform) {
22
+ // await bp.buildPlatform({ platform: 'all', isTsProject });
23
+ return;
24
+ }
25
+
26
+ if (typeof platform === 'string') {
27
+ // await bp.buildPlatform({ platform, isTsProject });
28
+ return;
29
+ }
30
+
31
+ if (enableWatchMode) {
32
+ await buildWatchFeature.buildInWatchMode(config, modernConfig);
33
+ } else {
34
+ await buildFeature.buildSourceCode(config, modernConfig);
35
+ }
36
+ };
@@ -0,0 +1,2 @@
1
+ export * from './logText';
2
+ export * from './loggerManager';
@@ -0,0 +1,80 @@
1
+ import { chalk } from '@modern-js/utils';
2
+
3
+ export const colors = { title: chalk.rgb(218, 152, 92) };
4
+
5
+ export const clearFlag = '\x1Bc';
6
+
7
+ export const logTemplate = (
8
+ title: string,
9
+ messageStack: string[],
10
+ {
11
+ noBottomBorder = false,
12
+ bottomBorderText = '',
13
+ noLeftBorder = false,
14
+ leftBorder = '',
15
+ contentColor = (s: string) => s,
16
+ } = {},
17
+ ) => {
18
+ const maxLength = Infinity; // TODO: 考虑后面是否提供该参数
19
+ const leftBorderFlag = noLeftBorder ? '' : leftBorder;
20
+ const messageFragments = messageStack
21
+ .map(p => {
22
+ p.trim();
23
+
24
+ return `${leftBorderFlag}${p.replace(clearFlag, '')}`;
25
+ }) // 移除 clearFlag
26
+ .filter(s => s !== leftBorderFlag) // 过滤空字符串
27
+ .slice(0, maxLength); // 控制长度
28
+ const template = `${colors.title(title)}:
29
+ ${contentColor(messageFragments.join(''))}${
30
+ noBottomBorder ? '' : `\n${bottomBorderText}`
31
+ }`;
32
+ return template;
33
+ };
34
+
35
+ export interface LoggerTextOption {
36
+ title: string;
37
+ contentConfig?: {
38
+ noBottomBorder?: boolean;
39
+ bottomBorderText?: string;
40
+ noLeftBorder?: boolean;
41
+ leftBorder?: string;
42
+ contentColor?: (s: string) => string;
43
+ replace?: string[];
44
+ };
45
+ }
46
+ // 处理Log内容如何展示
47
+ export class LoggerText {
48
+ messages: string[];
49
+
50
+ hasErrorMessage: boolean;
51
+
52
+ option: LoggerTextOption;
53
+
54
+ constructor(option: LoggerTextOption) {
55
+ this.messages = [];
56
+ this.option = option;
57
+ this.hasErrorMessage = false;
58
+ }
59
+
60
+ append(message: string) {
61
+ if (message.includes(clearFlag)) {
62
+ this.messages = [];
63
+ }
64
+ this.messages.push(message);
65
+ }
66
+
67
+ errorHappen() {
68
+ this.hasErrorMessage = true;
69
+ }
70
+
71
+ hasMessages() {
72
+ return this.messages.length > 0;
73
+ }
74
+
75
+ get value() {
76
+ const { title, contentConfig } = this.option;
77
+ const messages = [...new Set(this.messages)];
78
+ return logTemplate(title, messages, contentConfig);
79
+ }
80
+ }
@@ -0,0 +1,132 @@
1
+ /**
2
+ * 1. 注册构建任务
3
+ * 2. 监听各个构建任务进程中的信息:process.stdout.on('data' | 'error')
4
+ * 3. 分别输出内容
5
+ */
6
+ import type { ChildProcess } from 'child_process';
7
+ import EventEmitter from 'events';
8
+ import { chalk, Import } from '@modern-js/utils';
9
+ import type { LoggerTextOption, LoggerText } from './logText';
10
+
11
+ const logText: typeof import('./logText') = Import.lazy('./logText', require);
12
+ const readline: typeof import('../../../utils/readline') = Import.lazy(
13
+ '../../../utils/readline',
14
+ require,
15
+ );
16
+
17
+ export type STDOUT =
18
+ | ChildProcess['stdout']
19
+ | (NodeJS.WriteStream & {
20
+ fd: 1;
21
+ });
22
+ export type STDERR =
23
+ | ChildProcess['stderr']
24
+ | (NodeJS.WriteStream & {
25
+ fd: 2;
26
+ });
27
+
28
+ interface IAddStdoutConfig {
29
+ event?: { data?: boolean; error?: boolean };
30
+ colors?: {
31
+ data?: (s: string) => string;
32
+ error?: (s: string) => string;
33
+ warning?: (s: string) => string;
34
+ };
35
+ }
36
+
37
+ export class LoggerManager extends EventEmitter {
38
+ private _compilering: boolean;
39
+
40
+ private readonly _listeners: STDOUT[];
41
+
42
+ constructor() {
43
+ super();
44
+ this._compilering = false;
45
+ this._listeners = [];
46
+ }
47
+
48
+ createLoggerText(option: LoggerTextOption) {
49
+ return new logText.LoggerText(option);
50
+ }
51
+
52
+ addStdout(
53
+ loggerText: LoggerText,
54
+ stdout: STDOUT,
55
+ config: IAddStdoutConfig = {},
56
+ ) {
57
+ const {
58
+ event = { data: true, error: true },
59
+ colors = {
60
+ data: chalk.green,
61
+ error: chalk.red,
62
+ warning: chalk.yellow,
63
+ },
64
+ } = config;
65
+ if (event.data) {
66
+ stdout?.on('data', chunk => {
67
+ const data = chunk.toString();
68
+ const content = colors.data ? colors.data(data) : chalk.green(data);
69
+ loggerText.append(content);
70
+ this.emit('data');
71
+ });
72
+ }
73
+
74
+ if (event.error) {
75
+ stdout?.on('error', error => {
76
+ console.info('error');
77
+ const data = error.message;
78
+ const content = colors.error ? colors.error(data) : chalk.red(data);
79
+ loggerText.append(content);
80
+ loggerText.errorHappen();
81
+ this.emit('data');
82
+ });
83
+ }
84
+
85
+ this._listeners.push(stdout);
86
+ }
87
+
88
+ addStderr(
89
+ loggerText: LoggerText,
90
+ stderr: STDERR,
91
+ color: (s: string) => string = chalk.red,
92
+ ) {
93
+ stderr?.on('data', chunk => {
94
+ const data = chunk.toString();
95
+ loggerText.append(color(data));
96
+ loggerText.errorHappen();
97
+ this.emit('data');
98
+ });
99
+ }
100
+
101
+ showCompiling() {
102
+ if (!this._compilering) {
103
+ this._compilering = true;
104
+ console.info(chalk.green`Compiling in progress...`);
105
+ }
106
+ }
107
+
108
+ disappearCompiling() {
109
+ if (this._compilering) {
110
+ readline.ReadlineUtils.clearLine(process.stdout);
111
+ this._compilering = false;
112
+ }
113
+ }
114
+
115
+ listenDateAndShow(
116
+ logTexts: LoggerText[],
117
+ // stdout: NodeJS.WriteStream & {
118
+ // fd: 1;
119
+ // } = process.stdout,
120
+ ) {
121
+ this.on('data', () => {
122
+ this.disappearCompiling();
123
+ const content = logTexts.map(logtext => logtext.value).join('');
124
+ // 每次更新,使用新的内容覆盖旧的内容,有几率出现内容错乱问题
125
+ console.info(content);
126
+ });
127
+ return () => {
128
+ // eslint-disable-next-line no-process-exit
129
+ process.exit(0);
130
+ };
131
+ }
132
+ }
@@ -0,0 +1,250 @@
1
+ /* eslint-disable max-classes-per-file */
2
+ import * as path from 'path';
3
+ import * as os from 'os';
4
+ import { Import, chalk } from '@modern-js/utils';
5
+ import type {
6
+ IBuildConfig,
7
+ IPackageModeValue,
8
+ JsSyntaxType,
9
+ ITaskMapper,
10
+ ModuleToolsConfig,
11
+ } from '../../types';
12
+ import type { LoggerText } from './logger';
13
+
14
+ const constants: typeof import('./constants') = Import.lazy(
15
+ './constants',
16
+ require,
17
+ );
18
+ const core: typeof import('@modern-js/core') = Import.lazy(
19
+ '@modern-js/core',
20
+ require,
21
+ );
22
+
23
+ // 硬解字符串返回相应格式的对象
24
+ const updateMapper = (
25
+ packageFieldValue: JsSyntaxType,
26
+ outDir: IPackageModeValue['outDir'],
27
+ mapper: IPackageModeValue[],
28
+ ): IPackageModeValue[] => {
29
+ if (packageFieldValue === 'CJS+ES6') {
30
+ return [...mapper, { type: 'commonjs', syntax: 'es6+', outDir }];
31
+ } else if (packageFieldValue === 'ESM+ES5') {
32
+ return [...mapper, { type: 'module', syntax: 'es5', outDir }];
33
+ } else if (packageFieldValue === 'ESM+ES6') {
34
+ return [...mapper, { type: 'module', syntax: 'es6+', outDir }];
35
+ } else {
36
+ return [...mapper];
37
+ }
38
+ };
39
+
40
+ export const getCodeInitMapper = (_: IBuildConfig) => {
41
+ const {
42
+ output: { packageFields, packageMode },
43
+ } = core.useResolvedConfigContext().value as ModuleToolsConfig;
44
+ let initMapper: IPackageModeValue[] = [];
45
+
46
+ // 如果不存在packageFields配置或者packageFields为空对象,则使用 packageMode
47
+ if (
48
+ !packageFields ||
49
+ (typeof packageFields === 'object' &&
50
+ Object.keys(packageFields).length === 0)
51
+ ) {
52
+ initMapper =
53
+ constants.PACKAGE_MODES[packageMode || constants.DEFAULT_PACKAGE_MODE];
54
+ } else if (packageFields && Object.keys(packageFields).length > 0) {
55
+ if (packageFields['modern']) {
56
+ initMapper = updateMapper(
57
+ packageFields['modern'],
58
+ 'modern',
59
+ initMapper,
60
+ );
61
+ }
62
+
63
+ if (packageFields.main) {
64
+ initMapper = updateMapper(packageFields.main, 'node', initMapper);
65
+ }
66
+
67
+ if (packageFields.module) {
68
+ initMapper = updateMapper(
69
+ packageFields.module,
70
+ 'treeshaking',
71
+ initMapper,
72
+ );
73
+ }
74
+
75
+ // TODO: 如果存在其他配置,需要提示
76
+ if (
77
+ !packageFields['modern'] &&
78
+ !packageFields.main &&
79
+ !packageFields.module
80
+ ) {
81
+ console.error(
82
+ chalk.red(
83
+ `Unrecognized ${JSON.stringify(
84
+ packageFields,
85
+ )} configuration, please use keys: 'modern, main, jupiter:modern' and use values: 'CJS+ES6, ESM+ES5, ESM+ES6'`,
86
+ ),
87
+ );
88
+
89
+ // eslint-disable-next-line no-process-exit
90
+ process.exit(1);
91
+ }
92
+ }
93
+
94
+ return initMapper;
95
+ };
96
+
97
+ // 获取执行构建源码的参数
98
+ export const getCodeMapper = ({
99
+ logger,
100
+ taskPath,
101
+ config,
102
+ initMapper,
103
+ srcRootDir,
104
+ willCompilerDirOrFile,
105
+ }: ITaskMapper & {
106
+ config: IBuildConfig;
107
+ initMapper: IPackageModeValue[];
108
+ srcRootDir: string;
109
+ willCompilerDirOrFile: string;
110
+ }) => {
111
+ const {
112
+ value: { appDirectory },
113
+ } = core.useAppContext();
114
+ const modernConfig = core.useResolvedConfigContext().value;
115
+ const {
116
+ output: { enableSourceMap, jsPath = 'js', path: distDir = 'dist' },
117
+ } = modernConfig as ModuleToolsConfig;
118
+
119
+ const { tsconfigName = 'tsconfig.json' } = config;
120
+ const tsconfigPath = path.join(appDirectory, tsconfigName);
121
+
122
+ return initMapper.map(option => {
123
+ // 不是output.copy配置,而是内部的js copy逻辑
124
+ const copyDirs = option.copyDirs?.map(copyDir =>
125
+ path.join(appDirectory, `./${distDir}/${jsPath}/${copyDir}`),
126
+ );
127
+ return {
128
+ logger,
129
+ taskPath,
130
+ params: [
131
+ `--syntax=${option.syntax}`,
132
+ `--type=${option.type}`,
133
+ `--srcRootDir=${srcRootDir}`,
134
+ `--willCompilerDirOrFile=${willCompilerDirOrFile}`,
135
+ copyDirs ? `--copyDirs=${copyDirs.join(',')}` : '',
136
+ `--distDir=${path.join(
137
+ appDirectory,
138
+ `./${distDir}/${jsPath}/${option.outDir}`,
139
+ )}`,
140
+ `--appDirectory=${appDirectory}`,
141
+ enableSourceMap ? '--sourceMaps' : '',
142
+ `--tsconfigPath=${tsconfigPath}`,
143
+ ],
144
+ };
145
+ });
146
+ };
147
+
148
+ // 获取执行生成 d.ts 的参数
149
+ export const getDtsMapper = (config: IBuildConfig, logger: LoggerText) => {
150
+ const {
151
+ value: { appDirectory },
152
+ } = core.useAppContext();
153
+ const modernConfig = core.useResolvedConfigContext().value;
154
+ const {
155
+ output: { disableTsChecker, path: outputPath = 'dist' },
156
+ } = modernConfig as ModuleToolsConfig;
157
+ const { tsconfigName = 'tsconfig.json', enableWatchMode, sourceDir } = config;
158
+ const srcDir = path.join(appDirectory, './src');
159
+ const tsconfigPath = path.join(appDirectory, tsconfigName);
160
+ return [
161
+ {
162
+ logger,
163
+ taskPath: require.resolve('../../tasks/generator-dts'),
164
+ params: [
165
+ `--srcDir=${srcDir}`,
166
+ `--distDir=${path.join(appDirectory, `./${outputPath}/types`)}`,
167
+ `--appDirectory=${appDirectory}`,
168
+ `--tsconfigPath=${tsconfigPath}`,
169
+ `--sourceDirName=${sourceDir}`,
170
+ enableWatchMode ? `--watch` : '',
171
+ disableTsChecker ? '' : `--tsCheck`,
172
+ ],
173
+ },
174
+ ];
175
+ };
176
+
177
+ /**
178
+ * 处理日志信息
179
+ */
180
+ export class LogStack {
181
+ private _codeLogStack: string[];
182
+
183
+ constructor() {
184
+ this._codeLogStack = [];
185
+ }
186
+
187
+ update(latestLog: string, { splitEOL = false } = {}) {
188
+ if (splitEOL) {
189
+ latestLog.split(os.EOL).forEach(log => {
190
+ this._codeLogStack.unshift(log.trim());
191
+ });
192
+ return;
193
+ }
194
+
195
+ this._codeLogStack.unshift(latestLog.trim());
196
+ }
197
+
198
+ clear() {
199
+ this._codeLogStack = [];
200
+ }
201
+
202
+ get value() {
203
+ return [...new Set(this._codeLogStack)];
204
+ }
205
+ }
206
+
207
+ export const logTemplate = (
208
+ title: string,
209
+ messageStack: string[],
210
+ maxLength: number,
211
+ {
212
+ noBottomBorder = false,
213
+ bottomBorderText = '',
214
+ noLeftBorder = false,
215
+ leftBorder = '│',
216
+ contentColor = (s: string) => s,
217
+ } = {},
218
+ ) => {
219
+ const leftBorderFlag = noLeftBorder ? '' : leftBorder;
220
+ const messageFragments = messageStack
221
+ .map(p => {
222
+ p.trim();
223
+
224
+ return `${leftBorderFlag}${p.replace(constants.clearFlag, '')}`;
225
+ }) // 移除 clearFlag
226
+ .slice(0, maxLength) // 控制长度
227
+ .filter(s => s !== leftBorderFlag) // 过滤空字符串
228
+ .reverse(); // 调换顺序,最新的消息在最后面
229
+ const template = `${title}:
230
+ ${contentColor(messageFragments.join(os.EOL))}${
231
+ noBottomBorder ? '' : `\n${bottomBorderText}`
232
+ }`;
233
+ return template;
234
+ };
235
+
236
+ // eslint-disable-next-line @typescript-eslint/no-extraneous-class
237
+ export class TimeCounter {
238
+ static _now: number;
239
+
240
+ static time() {
241
+ this._now = Date.now();
242
+ }
243
+
244
+ static timeEnd() {
245
+ const span = Date.now() - this._now;
246
+ return span < 1000 ? `${span}ms` : `${(span / 1000).toFixed(2)}s`;
247
+ }
248
+ }
249
+
250
+ /* eslint-enable max-classes-per-file */
@@ -0,0 +1,62 @@
1
+ import { Import } from '@modern-js/utils';
2
+ import chalk from 'chalk';
3
+
4
+ const core: typeof import('@modern-js/core') = Import.lazy(
5
+ '@modern-js/core',
6
+ require,
7
+ );
8
+ const inquirer: typeof import('inquirer') = Import.lazy('inquirer', require);
9
+ const color: typeof import('../../utils/color') = Import.lazy(
10
+ '../../utils/color',
11
+ require,
12
+ );
13
+
14
+ export interface IDevConfig {
15
+ appDirectory: string;
16
+ isTsProject: boolean;
17
+ }
18
+
19
+ export type DevTaskType = 'storybook' | 'docsite' | 'unknow';
20
+
21
+ export const showMenu = async (config: IDevConfig) => {
22
+ const metas = await (core.mountHook() as any).moduleToolsMenu(undefined);
23
+ if (metas.length <= 0) {
24
+ console.info(
25
+ chalk.yellow(
26
+ 'No runnable development features found.\nYou can use the `new` command to enable the development features',
27
+ ),
28
+ );
29
+ // eslint-disable-next-line no-process-exit
30
+ process.exit(0);
31
+ }
32
+ const menuMessage = color.devMenuTitle('Select the debug mode:');
33
+ const { type } = await inquirer.prompt([
34
+ {
35
+ name: 'type',
36
+ message: menuMessage,
37
+ type: 'list',
38
+ choices: metas,
39
+ },
40
+ ]);
41
+
42
+ const devMeta = metas.find((meta: any) => meta.value === type);
43
+ if (devMeta) {
44
+ await devMeta.runTask(config);
45
+ }
46
+ };
47
+
48
+ export const devStorybook = async (config: IDevConfig) => {
49
+ const metas = await (core.mountHook() as any).moduleToolsMenu(undefined);
50
+ const findStorybook = metas.find((meta: any) => meta.value === 'storybook');
51
+ if (findStorybook) {
52
+ await findStorybook.runTask(config);
53
+ } else {
54
+ console.info(
55
+ chalk.yellow(
56
+ 'You need to enable storybook functionality with the `new` script or `modern new` or install @modern-js/plugin-storybook',
57
+ ),
58
+ );
59
+ // eslint-disable-next-line no-process-exit
60
+ process.exit(0);
61
+ }
62
+ };
package/src/index.ts ADDED
@@ -0,0 +1,55 @@
1
+ import { Import } from '@modern-js/utils';
2
+
3
+ const core: typeof import('@modern-js/core') = Import.lazy(
4
+ '@modern-js/core',
5
+ require,
6
+ );
7
+ // const { createPlugin, usePlugins, defineConfig } = core;
8
+ const hooks: typeof import('@modern-js/module-tools-hooks') = Import.lazy(
9
+ '@modern-js/module-tools-hooks',
10
+ require,
11
+ );
12
+ const cli: typeof import('./cli') = Import.lazy('./cli', require);
13
+ const local: typeof import('./locale') = Import.lazy('./locale', require);
14
+ const schema: typeof import('./schema') = Import.lazy('./schema', require);
15
+ const lang: typeof import('./utils/language') = Import.lazy(
16
+ './utils/language',
17
+ require,
18
+ );
19
+
20
+ export const { defineConfig } = core;
21
+
22
+ core.usePlugins([
23
+ require.resolve('@modern-js/plugin-changeset/cli'),
24
+ require.resolve('@modern-js/plugin-analyze/cli'),
25
+ ]);
26
+
27
+ export default core.createPlugin(
28
+ (() => {
29
+ const locale = lang.getLocaleLanguage();
30
+ local.i18n.changeLanguage({ locale });
31
+ hooks.lifecycle();
32
+
33
+ return {
34
+ validateSchema() {
35
+ return schema.addSchema();
36
+ },
37
+ config() {
38
+ return {
39
+ output: {
40
+ enableSourceMap: false,
41
+ jsPath: 'js',
42
+ },
43
+ };
44
+ },
45
+ commands({ program }: any) {
46
+ cli.devCli(program);
47
+ cli.buildCli(program);
48
+ cli.newCli(program, locale);
49
+ // 便于其他插件辨别
50
+ program.$$libraryName = 'module-tools';
51
+ },
52
+ };
53
+ }) as any,
54
+ { post: ['@modern-js/plugin-analyze', '@modern-js/plugin-changeset'] },
55
+ );
@@ -0,0 +1,21 @@
1
+ export const EN_LOCALE = {
2
+ command: {
3
+ build: {
4
+ describe: 'command for building module',
5
+ watch: 'building module in watch mode',
6
+ tsconfig: 'Specify a path to the tsconfig.json file',
7
+ style_only: 'only build style',
8
+ platform: 'building for other platforms',
9
+ no_tsc: 'close tsc compiler to emit d.ts',
10
+ no_clear: 'disable auto clear dist dir',
11
+ },
12
+ dev: { describe: 'start dev server' },
13
+ new: {
14
+ describe: 'generator runner for modern project',
15
+ debug: 'using debug mode to log something',
16
+ config: 'set default generator config(json string)',
17
+ distTag: `use specified tag version for it's generator`,
18
+ registry: 'set npm registry url to run npm command',
19
+ },
20
+ },
21
+ };