@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,213 @@
1
+ /* eslint-disable max-statements */
2
+ import * as path from 'path';
3
+ import { fs, watch, WatchChangeType, Import } from '@modern-js/utils';
4
+ const logger = Import.lazy('../features/build/logger', require);
5
+ const cssConfig = Import.lazy('@modern-js/css-config', require);
6
+ const hooks = Import.lazy('@modern-js/module-tools-hooks', require);
7
+ const core = Import.lazy('@modern-js/core', require);
8
+ const pluginNode = Import.lazy('@modern-js/plugin/node', require);
9
+ const compiler = Import.lazy('@modern-js/style-compiler', require);
10
+ const glob = Import.lazy('glob', require);
11
+ const STYLE_DIRS = 'styles';
12
+ const SRC_STYLE_DIRS = 'src';
13
+
14
+ const checkStylesDirExist = option => {
15
+ const {
16
+ appDirectory
17
+ } = option;
18
+ return fs.existsSync(path.join(appDirectory, STYLE_DIRS));
19
+ };
20
+
21
+ const generatorFileAndReturnLog = (result, successMessage = '') => {
22
+ if (result.code === 0) {
23
+ for (const file of result.dists) {
24
+ fs.ensureFileSync(file.filename);
25
+ fs.writeFileSync(file.filename, file.content);
26
+ }
27
+
28
+ return successMessage;
29
+ } else {
30
+ return result.errors.join('\n');
31
+ }
32
+ };
33
+
34
+ const getPostcssOption = (appDirectory, modernConfig) => {
35
+ var _postcssOption$postcs;
36
+
37
+ const postcssOption = cssConfig.getPostcssConfig(appDirectory, modernConfig, false);
38
+ return {
39
+ plugins: (postcssOption === null || postcssOption === void 0 ? void 0 : (_postcssOption$postcs = postcssOption.postcssOptions) === null || _postcssOption$postcs === void 0 ? void 0 : _postcssOption$postcs.plugins) || [],
40
+ enableSourceMap: (postcssOption === null || postcssOption === void 0 ? void 0 : postcssOption.sourceMap) || false,
41
+ options: {}
42
+ };
43
+ };
44
+
45
+ const copyOriginStyleFiles = ({
46
+ targetDir,
47
+ outputDir
48
+ }) => {
49
+ const styleFiles = glob.sync(`${targetDir}/**/*.{css,sass,scss,less}`);
50
+
51
+ if (styleFiles.length > 0) {
52
+ fs.ensureDirSync(outputDir);
53
+ }
54
+
55
+ for (const styleFile of styleFiles) {
56
+ const file = path.relative(targetDir, styleFile);
57
+ fs.copyFileSync(styleFile, path.join(outputDir, file));
58
+ }
59
+ };
60
+
61
+ const logCompilerMessage = compilerMessage => {
62
+ console.info(logger.clearFlag);
63
+ console.info(compilerMessage.src);
64
+ console.info(compilerMessage.styles);
65
+ };
66
+
67
+ const taskMain = async ({
68
+ modernConfig,
69
+ appContext
70
+ }) => {
71
+ const {
72
+ assetsPath = 'styles',
73
+ path: outputPath = 'dist',
74
+ jsPath = 'js',
75
+ importStyle
76
+ } = modernConfig.output;
77
+ const {
78
+ appDirectory
79
+ } = appContext;
80
+ const lessOption = await core.mountHook().moduleLessConfig({
81
+ modernConfig
82
+ }, // eslint-disable-next-line @typescript-eslint/require-await
83
+ {
84
+ onLast: async _ => null
85
+ });
86
+ const sassOption = await core.mountHook().moduleSassConfig({
87
+ modernConfig
88
+ }, // eslint-disable-next-line @typescript-eslint/require-await
89
+ {
90
+ onLast: async _ => null
91
+ });
92
+ const postcssOption = getPostcssOption(appDirectory, modernConfig);
93
+ const existStylesDir = checkStylesDirExist({
94
+ appDirectory
95
+ });
96
+ const compilerMessage = {
97
+ src: '',
98
+ styles: ''
99
+ }; // 编译 styles 目录样式
100
+
101
+ let styleEmitter = null;
102
+
103
+ if (existStylesDir) {
104
+ styleEmitter = compiler.styleCompiler({
105
+ watch: true,
106
+ projectDir: appDirectory,
107
+ stylesDir: path.resolve(appDirectory, STYLE_DIRS),
108
+ outDir: path.join(appDirectory, outputPath, assetsPath),
109
+ enableVirtualDist: true,
110
+ compilerOption: {
111
+ less: lessOption,
112
+ sass: sassOption,
113
+ postcss: postcssOption
114
+ }
115
+ });
116
+ styleEmitter.on(compiler.BuildWatchEvent.firstCompiler, styleResult => {
117
+ compilerMessage.styles = generatorFileAndReturnLog(styleResult, `[Style Compiler] Successfully for 'styles' dir`);
118
+ logCompilerMessage(compilerMessage);
119
+ });
120
+ styleEmitter.on(compiler.BuildWatchEvent.compilering, () => {
121
+ compilerMessage.styles = `[${assetsPath}] Compiling...`;
122
+ logCompilerMessage(compilerMessage);
123
+ });
124
+ styleEmitter.on(compiler.BuildWatchEvent.watchingCompiler, styleResult => {
125
+ compilerMessage.styles = generatorFileAndReturnLog(styleResult, `[Style Compiler] Successfully for 'styles' dir`);
126
+ logCompilerMessage(compilerMessage);
127
+ }); // await styleEmitter.watch();
128
+ } // 编译 src 内的样式代码
129
+
130
+
131
+ const srcDir = path.resolve(appDirectory, SRC_STYLE_DIRS);
132
+ const outputDirToSrc = path.join(appDirectory, outputPath, jsPath, assetsPath);
133
+
134
+ if (importStyle === 'compiled-code') {
135
+ compilerMessage.src = `[src] Compiling`;
136
+ const srcStyleEmitter = compiler.styleCompiler({
137
+ watch: true,
138
+ projectDir: appDirectory,
139
+ stylesDir: srcDir,
140
+ outDir: outputDirToSrc,
141
+ enableVirtualDist: true,
142
+ compilerOption: {
143
+ less: lessOption,
144
+ sass: sassOption,
145
+ postcss: postcssOption
146
+ }
147
+ });
148
+ srcStyleEmitter.on(compiler.BuildWatchEvent.firstCompiler, srcStyleResult => {
149
+ compilerMessage.src = generatorFileAndReturnLog(srcStyleResult, `[Style Compiler] Successfully for 'src' dir`);
150
+ logCompilerMessage(compilerMessage);
151
+ });
152
+ srcStyleEmitter.on(compiler.BuildWatchEvent.compilering, () => {
153
+ compilerMessage.src = `[src] Compiling`;
154
+ logCompilerMessage(compilerMessage);
155
+ });
156
+ srcStyleEmitter.on(compiler.BuildWatchEvent.watchingCompiler, srcStyleResult => {
157
+ compilerMessage.src = generatorFileAndReturnLog(srcStyleResult, `[Style Compiler] Successfully for 'src' dir`);
158
+ logCompilerMessage(compilerMessage);
159
+ });
160
+ styleEmitter && (await styleEmitter.watch());
161
+ await srcStyleEmitter.watch();
162
+ } else {
163
+ compilerMessage.src = `['src' dir] Copying in progress`;
164
+ styleEmitter && (await styleEmitter.watch());
165
+ logCompilerMessage(compilerMessage);
166
+ copyOriginStyleFiles({
167
+ targetDir: srcDir,
168
+ outputDir: outputDirToSrc
169
+ });
170
+ compilerMessage.src = `[Style Compiler] Successfully for 'src' dir`;
171
+ logCompilerMessage(compilerMessage);
172
+ watch(`${srcDir}/**/*.{css,less,sass,scss}`, ({
173
+ changeType,
174
+ changedFilePath
175
+ }) => {
176
+ compilerMessage.src = `['src' dir] Copying in progress`;
177
+ logCompilerMessage(compilerMessage);
178
+
179
+ if (changeType === WatchChangeType.UNLINK) {
180
+ const removeFile = path.normalize(`${outputDirToSrc}/${path.relative(srcDir, changedFilePath)}`);
181
+ fs.removeSync(removeFile);
182
+ } else {
183
+ copyOriginStyleFiles({
184
+ targetDir: srcDir,
185
+ outputDir: outputDirToSrc
186
+ });
187
+ }
188
+
189
+ compilerMessage.src = `[Style Compiler] Successfully for 'src' dir`;
190
+ logCompilerMessage(compilerMessage);
191
+ });
192
+ }
193
+ };
194
+
195
+ (async () => {
196
+ pluginNode.enable();
197
+ hooks.buildLifeCycle();
198
+ const {
199
+ resolved: modernConfig,
200
+ appContext
201
+ } = await core.cli.init();
202
+ await core.manager.run(async () => {
203
+ try {
204
+ await taskMain({
205
+ modernConfig,
206
+ appContext
207
+ });
208
+ } catch (e) {
209
+ console.error(e.message);
210
+ }
211
+ });
212
+ })();
213
+ /* eslint-enable max-statements */
@@ -0,0 +1 @@
1
+ export const tempTsconfigName = './tsconfig.temp.json';
@@ -0,0 +1,108 @@
1
+ import * as path from 'path';
2
+ import { fs, watch, WatchChangeType, Import } from '@modern-js/utils';
3
+ const argv = Import.lazy('process.argv', require);
4
+ const glob = Import.lazy('glob', require);
5
+ const core = Import.lazy('@modern-js/core', require);
6
+ const pluginNode = Import.lazy('@modern-js/plugin/node', require);
7
+ const copyUtils = Import.lazy('../utils/copy', require);
8
+ const STYLE_DIRS = 'styles';
9
+ const SRC_DIRS = 'src';
10
+
11
+ const copyAssets = ({
12
+ targetDir,
13
+ outputDir
14
+ }) => {
15
+ const assetsFiles = glob.sync(`${targetDir}/**/*.*`, {
16
+ ignore: ['**/*.{js,jsx,ts,tsx,d.ts,scss,less,css,sass}']
17
+ });
18
+
19
+ if (assetsFiles.length > 0) {
20
+ fs.ensureDirSync(outputDir);
21
+ }
22
+
23
+ for (const resource of assetsFiles) {
24
+ const file = path.relative(targetDir, resource);
25
+ fs.ensureDirSync(path.dirname(path.resolve(outputDir, file)));
26
+ fs.copyFileSync(resource, path.resolve(outputDir, file));
27
+ }
28
+ };
29
+
30
+ const watchAssets = ({
31
+ targetDir,
32
+ outputDir
33
+ }) => {
34
+ watch(`${targetDir}/**/*.*`, ({
35
+ changeType,
36
+ changedFilePath
37
+ }) => {
38
+ if (changeType === WatchChangeType.UNLINK) {
39
+ const removeFile = path.normalize(`${outputDir}/${path.relative(targetDir, changedFilePath)}`);
40
+ fs.removeSync(removeFile);
41
+ return;
42
+ }
43
+
44
+ const file = path.relative(targetDir, changedFilePath);
45
+ fs.copyFileSync(changedFilePath, path.resolve(outputDir, file));
46
+ }, ['**/*.{js,jsx,ts,tsx,d.ts,scss,less,css,sass}']);
47
+ };
48
+
49
+ const taskMain = ({
50
+ modernConfig,
51
+ appContext
52
+ }) => {
53
+ const processArgv = argv(process.argv.slice(2));
54
+ const config = processArgv({});
55
+ const {
56
+ appDirectory
57
+ } = appContext;
58
+ const {
59
+ jsPath = 'js',
60
+ assetsPath = 'styles',
61
+ path: outputPath = 'dist'
62
+ } = modernConfig.output;
63
+ const srcDir = path.join(appDirectory, SRC_DIRS);
64
+ const outputDirToSrc = path.join(appDirectory, outputPath, jsPath, assetsPath);
65
+ const styleDir = path.join(appDirectory, STYLE_DIRS);
66
+ const outputDirToStyle = path.join(appDirectory, outputPath, assetsPath);
67
+ copyAssets({
68
+ targetDir: srcDir,
69
+ outputDir: outputDirToSrc
70
+ });
71
+ copyAssets({
72
+ targetDir: styleDir,
73
+ outputDir: outputDirToStyle
74
+ });
75
+ copyUtils.copyTask({
76
+ modernConfig,
77
+ appContext
78
+ });
79
+
80
+ if (config.watch) {
81
+ watchAssets({
82
+ targetDir: srcDir,
83
+ outputDir: outputDirToSrc
84
+ });
85
+ watchAssets({
86
+ targetDir: styleDir,
87
+ outputDir: outputDirToStyle
88
+ });
89
+ }
90
+ };
91
+
92
+ (async () => {
93
+ pluginNode.enable();
94
+ const {
95
+ resolved: modernConfig,
96
+ appContext
97
+ } = await core.cli.init();
98
+ core.manager.run(() => {
99
+ try {
100
+ taskMain({
101
+ modernConfig,
102
+ appContext
103
+ });
104
+ } catch (e) {
105
+ console.error(e.message);
106
+ }
107
+ });
108
+ })();
@@ -0,0 +1,220 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 * as path from 'path';
8
+ import * as os from 'os';
9
+ import { Import, fs } from '@modern-js/utils';
10
+ const tsPathsTransform = Import.lazy('../utils/tspaths-transform', require);
11
+ const babel = Import.lazy('../utils/babel', require);
12
+ const constants = Import.lazy('./constants', require);
13
+ const core = Import.lazy('@modern-js/core', require);
14
+ const pluginNode = Import.lazy('@modern-js/plugin/node', require);
15
+ const execa = Import.lazy('execa', require);
16
+ const JSON5 = Import.lazy('json5', require);
17
+ const argv = Import.lazy('process.argv', require);
18
+ const deepMerge = Import.lazy('lodash.merge', require);
19
+ const glob = Import.lazy('glob', require);
20
+ let removeTsconfigPath = '';
21
+
22
+ const generatorTsConfig = (projectTsconfig, {
23
+ appDirectory,
24
+ distDir,
25
+ sourceDir: _sourceDir = 'src'
26
+ }) => {
27
+ var _projectTsconfig$comp, _projectTsconfig$comp2, _projectTsconfig$incl, _projectTsconfig$excl;
28
+
29
+ const tempPath = path.resolve(appDirectory, './node_modules');
30
+ const resolvePath = path.relative(tempPath, appDirectory); // const rootDir = projectTsconfig.compilerOptions?.rootDir
31
+ // ? path.join(resolvePath, projectTsconfig.compilerOptions?.rootDir)
32
+ // : resolvePath;
33
+ // 目前限制 rootDir 的路径为 sourceDir
34
+
35
+ const rootDir = path.join(resolvePath, _sourceDir);
36
+ const baseUrl = (_projectTsconfig$comp = projectTsconfig.compilerOptions) !== null && _projectTsconfig$comp !== void 0 && _projectTsconfig$comp.baseUrl ? path.join(appDirectory, (_projectTsconfig$comp2 = projectTsconfig.compilerOptions) === null || _projectTsconfig$comp2 === void 0 ? void 0 : _projectTsconfig$comp2.baseUrl) : appDirectory; // if include = ['src'], final include should be ['../src']
37
+
38
+ const include = (_projectTsconfig$incl = projectTsconfig.include) === null || _projectTsconfig$incl === void 0 ? void 0 : _projectTsconfig$incl.map(includePath => path.join(resolvePath, includePath));
39
+ const exclude = (_projectTsconfig$excl = projectTsconfig.exclude) === null || _projectTsconfig$excl === void 0 ? void 0 : _projectTsconfig$excl.map(excludePath => path.join(resolvePath, excludePath));
40
+ const resetConfig = {
41
+ compilerOptions: {
42
+ rootDir,
43
+ baseUrl,
44
+ // Ensure that .d.ts files are created by tsc, but not .js files
45
+ declaration: true,
46
+ emitDeclarationOnly: true,
47
+ outDir: distDir
48
+ },
49
+ include,
50
+ exclude
51
+ };
52
+ const recommendOption = {
53
+ // Ensure that Babel can safely transpile files in the TypeScript project
54
+ compilerOptions: {
55
+ isolatedModules: true
56
+ }
57
+ };
58
+ const tempTsconfigPath = path.join(tempPath, constants.tempTsconfigName);
59
+ fs.ensureFileSync(tempTsconfigPath);
60
+ fs.writeJSONSync(tempTsconfigPath, deepMerge(recommendOption, projectTsconfig, // 此处是必须要覆盖用户默认配置
61
+ resetConfig));
62
+ return tempTsconfigPath;
63
+ };
64
+
65
+ const getProjectTsconfig = tsconfigPath => {
66
+ if (!tsconfigPath || !fs.existsSync(tsconfigPath)) {
67
+ return {};
68
+ }
69
+
70
+ return JSON5.parse(fs.readFileSync(tsconfigPath, 'utf-8'));
71
+ };
72
+
73
+ const resolveLog = (childProgress, {
74
+ tsCheck: _tsCheck = false,
75
+ watch: _watch = false
76
+ } = {}) => {
77
+ var _childProgress$stdout, _childProgress$stdout2, _childProgress$stderr;
78
+
79
+ /**
80
+ * tsc 所有的log信息都是从stdout data 事件中获取
81
+ * 正常模式下,如果有报错信息,交给 resolveLog 后面的逻辑来处理
82
+ * watch 模式下,则使用这里的信息
83
+ */
84
+ (_childProgress$stdout = childProgress.stdout) === null || _childProgress$stdout === void 0 ? void 0 : _childProgress$stdout.on('data', data => {
85
+ if (!_tsCheck) {
86
+ return;
87
+ }
88
+
89
+ if (_watch) {
90
+ console.info(data.toString());
91
+ }
92
+ }); // 正常以下内容都不会触发,因为tsc 不会产生以下类型的log信息,不过防止意外情况
93
+
94
+ (_childProgress$stdout2 = childProgress.stdout) === null || _childProgress$stdout2 === void 0 ? void 0 : _childProgress$stdout2.on('error', error => {
95
+ console.error(error.message);
96
+ });
97
+ (_childProgress$stderr = childProgress.stderr) === null || _childProgress$stderr === void 0 ? void 0 : _childProgress$stderr.on('data', chunk => {
98
+ console.error(chunk.toString());
99
+ });
100
+ };
101
+
102
+ const generatorDts = async (_, config) => {
103
+ const {
104
+ tsconfigPath,
105
+ distDir,
106
+ sourceDirName = 'src',
107
+ projectData: {
108
+ appDirectory
109
+ },
110
+ tsCheck = false,
111
+ watch = false
112
+ } = config;
113
+ const userTsconfig = getProjectTsconfig(tsconfigPath);
114
+ const willDeleteTsconfigPath = generatorTsConfig(userTsconfig, {
115
+ appDirectory,
116
+ distDir,
117
+ sourceDir: sourceDirName
118
+ });
119
+ removeTsconfigPath = willDeleteTsconfigPath;
120
+ const tscBinFile = path.join(appDirectory, './node_modules/.bin/tsc');
121
+ const watchParams = watch ? ['-w'] : [];
122
+ const childProgress = execa(tscBinFile, ['-p', willDeleteTsconfigPath, ...watchParams], {
123
+ stdio: 'pipe',
124
+ cwd: appDirectory
125
+ });
126
+ resolveLog(childProgress, {
127
+ tsCheck,
128
+ watch
129
+ });
130
+
131
+ try {
132
+ await childProgress;
133
+ console.info('[Tsc Compiler]: Successfully');
134
+ } catch (e) {
135
+ if (!tsCheck) {
136
+ console.warn(`There are some type issues, which can be checked by configuring 'source.enableTsChecker = true' ${os.EOL} in modern.config.js`);
137
+ } else {
138
+ const isRecord = input => typeof input === 'object'; // 通过使用 execa,可以将tsc 的 data 类型的报错信息变为异常错误信息
139
+
140
+
141
+ if (isRecord(e)) {
142
+ console.error(e.stdout);
143
+ }
144
+ }
145
+ }
146
+
147
+ fs.removeSync(willDeleteTsconfigPath);
148
+ };
149
+
150
+ const resolveAlias = (modernConfig, config, watchFilenames = []) => {
151
+ const {
152
+ output
153
+ } = modernConfig;
154
+ const defaultPaths = {
155
+ '@': ['./src']
156
+ };
157
+ const dtsDistPath = `${config.distDir}/**/*.d.ts`;
158
+ const dtsFilenames = watchFilenames.length > 0 ? watchFilenames : glob.sync(dtsDistPath, {
159
+ absolute: true
160
+ });
161
+ const alias = babel.getFinalAlias(modernConfig, {
162
+ appDirectory: config.projectData.appDirectory,
163
+ tsconfigPath: config.tsconfigPath || path.join(config.projectData.appDirectory, './tsconfig.json'),
164
+ sourceAbsDir: config.distDir
165
+ });
166
+ const mergedPaths = alias.isTsPath ? alias.paths || {} : _objectSpread(_objectSpread({}, defaultPaths), alias.paths || {});
167
+ const result = tsPathsTransform.transformDtsAlias({
168
+ filenames: dtsFilenames,
169
+ baseUrl: path.join(config.projectData.appDirectory, output.path || 'dist'),
170
+ paths: mergedPaths
171
+ });
172
+
173
+ for (const r of result) {
174
+ fs.writeFileSync(r.path, r.content);
175
+ }
176
+ };
177
+
178
+ const taskMain = async ({
179
+ modernConfig
180
+ }) => {
181
+ const processArgv = argv(process.argv.slice(2));
182
+ const config = processArgv({
183
+ appDirectory: process.cwd(),
184
+ srcDir: 'src',
185
+ distDir: 'dist/types',
186
+ tsconfigPath: './tsconfig.json',
187
+ sourceDirName: 'src'
188
+ });
189
+ const option = {
190
+ srcDir: config.srcDir,
191
+ distDir: config.distDir,
192
+ projectData: {
193
+ appDirectory: config.appDirectory
194
+ },
195
+ tsconfigPath: config.tsconfigPath,
196
+ watch: config.watch,
197
+ tsCheck: config.tsCheck,
198
+ sourceDirName: config.sourceDirName
199
+ };
200
+ await generatorDts(modernConfig, option); // TODO: watch 模式下无法转换
201
+
202
+ resolveAlias(modernConfig, option);
203
+ };
204
+
205
+ (async () => {
206
+ pluginNode.enable();
207
+ const {
208
+ resolved
209
+ } = await core.cli.init();
210
+ await core.manager.run(async () => {
211
+ try {
212
+ await taskMain({
213
+ modernConfig: resolved
214
+ });
215
+ } catch (e) {
216
+ console.error(e.message);
217
+ fs.removeSync(removeTsconfigPath);
218
+ }
219
+ });
220
+ })();
File without changes
@@ -0,0 +1,85 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 { getBabelConfig, getModuleBabelChain } from '@modern-js/babel-preset-module';
8
+ import { applyOptionsChain, getAlias } from '@modern-js/utils';
9
+ export const getFinalAlias = (modernConfig, option) => {
10
+ const aliasConfig = getAlias(modernConfig.source.alias, option); // 排除内部别名,因为不需要处理
11
+
12
+ const finalPaths = {};
13
+ const internalAliasPrefix = '@modern-js/runtime';
14
+
15
+ if (aliasConfig.paths && typeof aliasConfig.paths === 'object') {
16
+ const pathsKey = Object.keys(aliasConfig.paths);
17
+
18
+ for (const key of pathsKey) {
19
+ if (!key.includes(internalAliasPrefix)) {
20
+ finalPaths[key] = aliasConfig.paths[key];
21
+ }
22
+ }
23
+ }
24
+
25
+ aliasConfig.paths = finalPaths;
26
+ return aliasConfig;
27
+ };
28
+ export const resolveBabelConfig = (appDirectory, modernConfig, option) => {
29
+ const {
30
+ source: {
31
+ envVars,
32
+ globalVars,
33
+ jsxTransformRuntime = 'automatic'
34
+ },
35
+ output: {
36
+ importStyle
37
+ },
38
+ tools: {
39
+ lodash: userLodashOption
40
+ }
41
+ } = modernConfig; // alias config
42
+
43
+ const aliasConfig = getFinalAlias(modernConfig, _objectSpread({
44
+ appDirectory
45
+ }, option)); // lodash config
46
+
47
+ const lodashOptions = applyOptionsChain({
48
+ id: ['lodash', 'ramda']
49
+ }, // TODO: 需要处理类型问题
50
+ userLodashOption); // babel config
51
+
52
+ const internalBabelConfig = getBabelConfig({
53
+ appDirectory,
54
+ enableReactPreset: true,
55
+ enableTypescriptPreset: true,
56
+ alias: aliasConfig,
57
+ envVars,
58
+ globalVars,
59
+ lodashOptions,
60
+ jsxTransformRuntime,
61
+ importStyle
62
+ }, {
63
+ type: option.type,
64
+ syntax: option.syntax
65
+ });
66
+ const babelChain = getModuleBabelChain({
67
+ appDirectory,
68
+ enableReactPreset: true,
69
+ enableTypescriptPreset: true,
70
+ alias: aliasConfig,
71
+ envVars,
72
+ globalVars,
73
+ lodashOptions,
74
+ jsxTransformRuntime,
75
+ importStyle
76
+ }, {
77
+ type: option.type,
78
+ syntax: option.syntax
79
+ });
80
+ const userBabelConfig = modernConfig.tools.babel;
81
+ return applyOptionsChain(internalBabelConfig, // TODO: 感觉 userBabelConfig 的类型应该是TransformOptions
82
+ userBabelConfig, {
83
+ chain: babelChain
84
+ });
85
+ };
@@ -0,0 +1,2 @@
1
+ import { chalk } from '@modern-js/utils';
2
+ export const devMenuTitle = chalk.rgb(255, 153, 0);