@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,99 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.resolveBabelConfig = exports.getFinalAlias = void 0;
7
+
8
+ var _babelPresetModule = require("@modern-js/babel-preset-module");
9
+
10
+ var _utils = require("@modern-js/utils");
11
+
12
+ 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; }
13
+
14
+ 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; }
15
+
16
+ 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; }
17
+
18
+ const getFinalAlias = (modernConfig, option) => {
19
+ const aliasConfig = (0, _utils.getAlias)(modernConfig.source.alias, option); // 排除内部别名,因为不需要处理
20
+
21
+ const finalPaths = {};
22
+ const internalAliasPrefix = '@modern-js/runtime';
23
+
24
+ if (aliasConfig.paths && typeof aliasConfig.paths === 'object') {
25
+ const pathsKey = Object.keys(aliasConfig.paths);
26
+
27
+ for (const key of pathsKey) {
28
+ if (!key.includes(internalAliasPrefix)) {
29
+ finalPaths[key] = aliasConfig.paths[key];
30
+ }
31
+ }
32
+ }
33
+
34
+ aliasConfig.paths = finalPaths;
35
+ return aliasConfig;
36
+ };
37
+
38
+ exports.getFinalAlias = getFinalAlias;
39
+
40
+ const resolveBabelConfig = (appDirectory, modernConfig, option) => {
41
+ const {
42
+ source: {
43
+ envVars,
44
+ globalVars,
45
+ jsxTransformRuntime = 'automatic'
46
+ },
47
+ output: {
48
+ importStyle
49
+ },
50
+ tools: {
51
+ lodash: userLodashOption
52
+ }
53
+ } = modernConfig; // alias config
54
+
55
+ const aliasConfig = getFinalAlias(modernConfig, _objectSpread({
56
+ appDirectory
57
+ }, option)); // lodash config
58
+
59
+ const lodashOptions = (0, _utils.applyOptionsChain)({
60
+ id: ['lodash', 'ramda']
61
+ }, // TODO: 需要处理类型问题
62
+ userLodashOption); // babel config
63
+
64
+ const internalBabelConfig = (0, _babelPresetModule.getBabelConfig)({
65
+ appDirectory,
66
+ enableReactPreset: true,
67
+ enableTypescriptPreset: true,
68
+ alias: aliasConfig,
69
+ envVars,
70
+ globalVars,
71
+ lodashOptions,
72
+ jsxTransformRuntime,
73
+ importStyle
74
+ }, {
75
+ type: option.type,
76
+ syntax: option.syntax
77
+ });
78
+ const babelChain = (0, _babelPresetModule.getModuleBabelChain)({
79
+ appDirectory,
80
+ enableReactPreset: true,
81
+ enableTypescriptPreset: true,
82
+ alias: aliasConfig,
83
+ envVars,
84
+ globalVars,
85
+ lodashOptions,
86
+ jsxTransformRuntime,
87
+ importStyle
88
+ }, {
89
+ type: option.type,
90
+ syntax: option.syntax
91
+ });
92
+ const userBabelConfig = modernConfig.tools.babel;
93
+ return (0, _utils.applyOptionsChain)(internalBabelConfig, // TODO: 感觉 userBabelConfig 的类型应该是TransformOptions
94
+ userBabelConfig, {
95
+ chain: babelChain
96
+ });
97
+ };
98
+
99
+ exports.resolveBabelConfig = resolveBabelConfig;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.devMenuTitle = void 0;
7
+
8
+ var _utils = require("@modern-js/utils");
9
+
10
+ const devMenuTitle = _utils.chalk.rgb(255, 153, 0);
11
+
12
+ exports.devMenuTitle = devMenuTitle;
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.copyTask = void 0;
7
+
8
+ var path = _interopRequireWildcard(require("path"));
9
+
10
+ var _utils = require("@modern-js/utils");
11
+
12
+ const _excluded = ["glob"];
13
+
14
+ 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); }
15
+
16
+ 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; }
17
+
18
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
19
+
20
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
21
+
22
+ const globby = _utils.Import.lazy('globby', require);
23
+
24
+ const fastGlob = _utils.Import.lazy('fast-glob', require);
25
+
26
+ const normalizePath = _utils.Import.lazy('normalize-path', require); // eslint-disable-next-line max-statements
27
+
28
+
29
+ const copyTask = async option => {
30
+ const {
31
+ modernConfig,
32
+ appContext
33
+ } = option;
34
+ const {
35
+ appDirectory
36
+ } = appContext;
37
+ const {
38
+ output: {
39
+ copy,
40
+ path: outputPath = 'dist'
41
+ }
42
+ } = modernConfig;
43
+
44
+ if (!copy) {
45
+ return;
46
+ }
47
+
48
+ try {
49
+ // 类型暂时这样处理,待之后优化copy的逻辑
50
+ for (const copyOption of copy) {
51
+ // 在原来的基础上,引入了类似于 copy-webpck-plugin 的 context 属性,可以设置项目根路径
52
+ const {
53
+ context = appDirectory,
54
+ from,
55
+ globOptions,
56
+ to: toOrigin
57
+ } = copyOption;
58
+ let fromOrigin = from; // 获取 glob 规则
59
+
60
+ let glob = '';
61
+ let options = {};
62
+
63
+ if (fromOrigin !== null && typeof fromOrigin === 'object') {
64
+ var _fromOrigin = fromOrigin;
65
+ ({
66
+ glob
67
+ } = _fromOrigin);
68
+ options = _objectWithoutProperties(_fromOrigin, _excluded);
69
+ _fromOrigin;
70
+ fromOrigin = glob;
71
+ } else if (globOptions && typeof globOptions === 'object') {
72
+ options = globOptions;
73
+ }
74
+
75
+ glob = path.isAbsolute(fromOrigin) ? fromOrigin : path.posix.join(fastGlob.escapePath(normalizePath(path.resolve(context))), fromOrigin); // 计算 glob,获取目标文件
76
+
77
+ const paths = await globby(glob, options);
78
+
79
+ if (!paths.length) {
80
+ return;
81
+ }
82
+
83
+ const to = path.normalize(toOrigin ? toOrigin : '');
84
+ const isToDirectory = path.extname(to) === '';
85
+ paths.forEach(item => {
86
+ const relativeFrom = path.relative(context, item); // 如果 to 是目录,通过相对路径计算完整的产物路径;如果 to 是文件,直接作为最终产物路径
87
+
88
+ const fileName = isToDirectory ? path.join(to, relativeFrom) : to;
89
+ const finalToPath = path.resolve(outputPath, fileName);
90
+
91
+ _utils.fs.copySync(item, finalToPath);
92
+ });
93
+ }
94
+ } catch (e) {
95
+ console.error(`copy error: ${e.message}`);
96
+ }
97
+ };
98
+
99
+ exports.copyTask = copyTask;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.safeParse = void 0;
7
+
8
+ const safeParse = jsonString => {
9
+ try {
10
+ return {
11
+ code: 0,
12
+ value: JSON.parse(jsonString)
13
+ };
14
+ } catch (e) {
15
+ return {
16
+ code: 1,
17
+ value: jsonString
18
+ };
19
+ }
20
+ };
21
+
22
+ exports.safeParse = safeParse;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getLocaleLanguage = getLocaleLanguage;
7
+
8
+ var _utils = require("@modern-js/utils");
9
+
10
+ const i18n = _utils.Import.lazy('@modern-js/i18n-cli-language-detector', require);
11
+
12
+ function getLocaleLanguage() {
13
+ const detector = new i18n.I18CLILanguageDetector();
14
+ return detector.detect();
15
+ }
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.logTemplate = exports.clearFlag = exports.LoggerText = exports.LoggerManager = void 0;
7
+
8
+ var _events = _interopRequireDefault(require("events"));
9
+
10
+ var _utils = require("@modern-js/utils");
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ /* eslint-disable max-classes-per-file */
15
+ const chalk = _utils.Import.lazy('chalk', require);
16
+
17
+ const clearFlag = '\x1Bc';
18
+ exports.clearFlag = clearFlag;
19
+
20
+ const logTemplate = (title, messageStack, maxLength, {
21
+ noBottomBorder: _noBottomBorder = false,
22
+ bottomBorderText: _bottomBorderText = '',
23
+ noLeftBorder: _noLeftBorder = false,
24
+ leftBorder: _leftBorder = '│',
25
+ contentColor: _contentColor = s => s
26
+ } = {}) => {
27
+ const leftBorderFlag = _noLeftBorder ? '' : _leftBorder;
28
+ const messageFragments = messageStack.map(p => {
29
+ p.trim();
30
+ return `${leftBorderFlag}${p.replace(clearFlag, '')}`;
31
+ }) // 移除 clearFlag
32
+ .filter(s => s !== leftBorderFlag) // 过滤空字符串
33
+ .slice(0, maxLength) // 控制长度
34
+ .reverse(); // 调换顺序,最新的消息在最后面
35
+
36
+ messageFragments[messageFragments.length - 1] = messageFragments[messageFragments.length - 1].trim();
37
+ const template = `${title}:
38
+ ${_contentColor(messageFragments.join(''))}${_noBottomBorder ? '' : `\n${_bottomBorderText}`}`;
39
+ console.info('template', messageFragments);
40
+ return template;
41
+ };
42
+
43
+ exports.logTemplate = logTemplate;
44
+
45
+ class LoggerText {
46
+ constructor(option) {
47
+ this.messages = void 0;
48
+ this.option = void 0;
49
+ this.messages = [];
50
+ this.option = option;
51
+ }
52
+
53
+ append(message) {
54
+ var _this$option$contentC;
55
+
56
+ const replace = ((_this$option$contentC = this.option.contentConfig) === null || _this$option$contentC === void 0 ? void 0 : _this$option$contentC.replace) || [];
57
+ let content = message;
58
+
59
+ for (const r of replace) {
60
+ content = content.replace(new RegExp(r, 'g'), '');
61
+ }
62
+
63
+ this.messages.push(content);
64
+ }
65
+
66
+ get value() {
67
+ const {
68
+ title,
69
+ maxLength,
70
+ contentConfig
71
+ } = this.option;
72
+ const messages = [...new Set(this.messages)];
73
+ return logTemplate(title, messages, maxLength, contentConfig);
74
+ }
75
+
76
+ }
77
+
78
+ exports.LoggerText = LoggerText;
79
+
80
+ class LoggerManager extends _events.default {
81
+ // constructor() {}
82
+ createLoggerText(option) {
83
+ return new LoggerText(option);
84
+ }
85
+
86
+ addStdout(loggerText, stdout, config = {}) {
87
+ const {
88
+ event = {
89
+ data: true,
90
+ error: true
91
+ },
92
+ colors = {
93
+ data: chalk.green,
94
+ error: chalk.red,
95
+ warning: chalk.yellow
96
+ }
97
+ } = config;
98
+
99
+ if (event.data) {
100
+ stdout === null || stdout === void 0 ? void 0 : stdout.on('data', chunk => {
101
+ const data = chunk.toString();
102
+ const content = colors.data ? colors.data(data) : chalk.green(data);
103
+ loggerText.append(content);
104
+ this.emit('data');
105
+ });
106
+ }
107
+
108
+ if (event.error) {
109
+ stdout === null || stdout === void 0 ? void 0 : stdout.on('error', error => {
110
+ const data = error.message;
111
+ const content = colors.error ? colors.error(data) : chalk.red(data);
112
+ loggerText.append(content);
113
+ this.emit('data');
114
+ });
115
+ }
116
+ }
117
+
118
+ addStderr(loggerText, stderr) {
119
+ stderr === null || stderr === void 0 ? void 0 : stderr.on('data', chunk => {
120
+ const data = chunk.toString();
121
+ loggerText.append(data);
122
+ });
123
+ }
124
+
125
+ show(loggerText) {
126
+ console.info(loggerText.value);
127
+ }
128
+
129
+ }
130
+ /* eslint-enable max-classes-per-file */
131
+
132
+
133
+ exports.LoggerManager = LoggerManager;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ReadlineUtils = void 0;
7
+
8
+ var readline = _interopRequireWildcard(require("readline"));
9
+
10
+ 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); }
11
+
12
+ 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; }
13
+
14
+ // eslint-disable-next-line @typescript-eslint/no-extraneous-class
15
+ class ReadlineUtils {
16
+ static clearPrevLine(output) {
17
+ ReadlineUtils.clearLine(output, 1);
18
+ }
19
+
20
+ static clearLine(output, n = 1, dir = -1) {
21
+ // -1 向上,1 向下
22
+ const dx = dir === 1 ? 1 : -1;
23
+
24
+ for (let i = 0; i < n; i++) {
25
+ readline.moveCursor(output, 0, dx);
26
+ readline.clearLine(output, 0);
27
+ readline.cursorTo(output, 0);
28
+ }
29
+ }
30
+
31
+ }
32
+
33
+ exports.ReadlineUtils = ReadlineUtils;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.readTsConfig = exports.existTsConfigFile = void 0;
7
+
8
+ var _utils = require("@modern-js/utils");
9
+
10
+ const json5 = _utils.Import.lazy('json5', require);
11
+
12
+ const readTsConfig = (tsconfigPath, noExistReturn = null) => {
13
+ // 如果不存在,则返回 noExistReturn
14
+ if (!_utils.fs.existsSync(tsconfigPath)) {
15
+ return noExistReturn;
16
+ }
17
+
18
+ const content = _utils.fs.readFileSync(tsconfigPath, 'utf-8');
19
+
20
+ return json5.parse(content);
21
+ };
22
+
23
+ exports.readTsConfig = readTsConfig;
24
+
25
+ const existTsConfigFile = tsconfigAbsolutePath => {
26
+ const tsconfig = readTsConfig(tsconfigAbsolutePath);
27
+ return Boolean(tsconfig);
28
+ };
29
+
30
+ exports.existTsConfigFile = existTsConfigFile;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.defaultTransformedFunctions = void 0;
7
+ const defaultTransformedFunctions = ['require', 'require.resolve', 'System.import', // Jest methods
8
+ 'jest.genMockFromModule', 'jest.mock', 'jest.unmock', 'jest.doMock', 'jest.dontMock', 'jest.setMock', 'jest.requireActual', 'jest.requireMock', // Older Jest methods
9
+ 'require.requireActual', 'require.requireMock'];
10
+ exports.defaultTransformedFunctions = defaultTransformedFunctions;
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.transformDtsAlias = void 0;
7
+
8
+ var path = _interopRequireWildcard(require("path"));
9
+
10
+ var parser = _interopRequireWildcard(require("@babel/parser"));
11
+
12
+ var _traverse = _interopRequireDefault(require("@babel/traverse"));
13
+
14
+ var _generator = _interopRequireDefault(require("@babel/generator"));
15
+
16
+ var t = _interopRequireWildcard(require("@babel/types"));
17
+
18
+ var _tsconfigPaths = require("tsconfig-paths");
19
+
20
+ var _utils = require("@modern-js/utils");
21
+
22
+ var _constants = require("./constants");
23
+
24
+ var _utils2 = require("./utils");
25
+
26
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
+
28
+ 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); }
29
+
30
+ 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; }
31
+
32
+ const getPathsMap = (paths, sourceDirName = 'src') => {
33
+ const pathKeys = Object.keys(paths);
34
+ const pathsMap = {};
35
+
36
+ const replaceSrcToTypes = s => s.replace(sourceDirName, 'types');
37
+
38
+ for (const key of pathKeys) {
39
+ const p = paths[key];
40
+
41
+ if (typeof p === 'string') {
42
+ pathsMap[key] = [replaceSrcToTypes(p)];
43
+ } else {
44
+ pathsMap[key] = paths[key].map(sp => replaceSrcToTypes(sp));
45
+ }
46
+ }
47
+
48
+ return pathsMap;
49
+ };
50
+
51
+ function mapPathString(nodePath, {
52
+ filename,
53
+ baseUrl,
54
+ paths
55
+ }) {
56
+ if (!t.isStringLiteral(nodePath)) {
57
+ return;
58
+ }
59
+
60
+ const sourcePath = nodePath.node.value;
61
+ const currentFile = filename;
62
+ const pathsMap = getPathsMap(paths);
63
+ const matchPath = (0, _tsconfigPaths.createMatchPath)(baseUrl, pathsMap, ['index']);
64
+ const result = matchPath(sourcePath, packageJsonPath => {
65
+ if (!_utils.fs.existsSync(packageJsonPath)) {
66
+ return undefined;
67
+ }
68
+
69
+ return _utils.fs.readJSONSync(packageJsonPath);
70
+ }, filePath => _utils.fs.existsSync(filePath), ['.d.ts']);
71
+
72
+ if (result) {
73
+ const relativePath = path.relative(path.dirname(currentFile), path.dirname(result));
74
+ const fileName = path.basename(result); // 如果是同级文件,则返回的是 ''
75
+
76
+ const filePath = path.normalize(`${relativePath.length === 0 ? '.' : relativePath}/${fileName}`);
77
+ const replaceString = filePath.startsWith('.') ? filePath : `./${filePath}`;
78
+ nodePath.replaceWith(t.stringLiteral(replaceString));
79
+ }
80
+ }
81
+
82
+ const transformCall = option => nodePath => {
83
+ const calleePath = nodePath.get('callee');
84
+
85
+ const isNormalCall = _constants.defaultTransformedFunctions.some(pattern => (0, _utils2.matchesPattern)(calleePath, pattern));
86
+
87
+ if (isNormalCall || (0, _utils2.isImportCall)(nodePath)) {
88
+ mapPathString(nodePath.get('arguments.0'), option);
89
+ }
90
+ };
91
+
92
+ const transformImport = option => nodePath => {
93
+ mapPathString(nodePath.get('source'), option);
94
+ };
95
+
96
+ const transformExport = option => nodePath => {
97
+ mapPathString(nodePath.get('source'), option);
98
+ };
99
+
100
+ const transfromSingleFileAlias = ({
101
+ filename,
102
+ baseUrl,
103
+ paths
104
+ }) => {
105
+ const sourceCode = _utils.fs.readFileSync(filename, 'utf-8');
106
+
107
+ const ast = parser.parse(sourceCode, {
108
+ sourceType: 'module',
109
+ errorRecovery: true,
110
+ // 防止typescript不支持的语法出现而报错
111
+ plugins: ['typescript']
112
+ });
113
+ (0, _traverse.default)(ast, {
114
+ CallExpression: transformCall({
115
+ filename,
116
+ baseUrl,
117
+ paths
118
+ }),
119
+ ImportDeclaration: transformImport({
120
+ filename,
121
+ baseUrl,
122
+ paths
123
+ }),
124
+ ExportDeclaration: transformExport({
125
+ filename,
126
+ baseUrl,
127
+ paths
128
+ })
129
+ });
130
+ return (0, _generator.default)(ast).code;
131
+ };
132
+
133
+ const transformDtsAlias = option => {
134
+ const {
135
+ filenames = [],
136
+ baseUrl,
137
+ paths
138
+ } = option;
139
+ const transformResult = [];
140
+
141
+ for (const filename of filenames) {
142
+ transformResult.push({
143
+ path: filename,
144
+ content: transfromSingleFileAlias({
145
+ filename,
146
+ baseUrl,
147
+ paths
148
+ })
149
+ });
150
+ }
151
+
152
+ return transformResult;
153
+ };
154
+
155
+ exports.transformDtsAlias = transformDtsAlias;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isImportCall = isImportCall;
7
+ exports.matchesPattern = matchesPattern;
8
+
9
+ var _utils = require("@modern-js/utils");
10
+
11
+ const t = _utils.Import.lazy('@babel/types', require);
12
+
13
+ function matchesPattern(calleePath, pattern) {
14
+ const {
15
+ node
16
+ } = calleePath;
17
+
18
+ if (t.isMemberExpression(node)) {
19
+ return calleePath.matchesPattern(pattern);
20
+ }
21
+
22
+ if (!t.isIdentifier(node) || pattern.includes('.')) {
23
+ return false;
24
+ }
25
+
26
+ const name = pattern.split('.')[0];
27
+ return node.name === name;
28
+ }
29
+
30
+ function isImportCall(calleePath) {
31
+ return t.isImport(calleePath.node.callee);
32
+ }
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.valideBeforeTask = exports.modernConfigValid = void 0;
7
+
8
+ var _utils = require("@modern-js/utils");
9
+
10
+ const valideBeforeTask = ({
11
+ modernConfig,
12
+ tsconfigPath
13
+ }) => {
14
+ const modernConfigValidResult = modernConfigValid(modernConfig, {
15
+ tsconfigPath
16
+ });
17
+
18
+ if (modernConfigValidResult) {
19
+ console.error(modernConfigValidResult); // eslint-disable-next-line no-process-exit
20
+
21
+ process.exit(0);
22
+ }
23
+ };
24
+
25
+ exports.valideBeforeTask = valideBeforeTask;
26
+
27
+ const modernConfigValid = (modernConfig, option) => {
28
+ const valids = [_utils.validAlias];
29
+
30
+ for (const validFn of valids) {
31
+ const result = validFn(modernConfig, option);
32
+
33
+ if (result) {
34
+ return result;
35
+ }
36
+ }
37
+
38
+ return null;
39
+ };
40
+
41
+ exports.modernConfigValid = modernConfigValid;