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