@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,15 @@
1
+ export interface TransformOption {
2
+ filename: string;
3
+ baseUrl: string;
4
+ paths: Record<string, string[] | string>;
5
+ }
6
+ interface TransformDtsAlaisOption {
7
+ filenames?: string[];
8
+ baseUrl: string;
9
+ paths: Record<string, string[] | string>;
10
+ }
11
+ export declare const transformDtsAlias: (option: TransformDtsAlaisOption) => {
12
+ path: string;
13
+ content: string;
14
+ }[];
15
+ export {};
@@ -0,0 +1,4 @@
1
+ import type * as tt from '@babel/types';
2
+ import type { NodePath } from '@babel/traverse';
3
+ export declare function matchesPattern(calleePath: NodePath, pattern: string): boolean;
4
+ export declare function isImportCall(calleePath: NodePath<tt.CallExpression>): boolean;
@@ -0,0 +1,12 @@
1
+ import type { NormalizedConfig } from '@modern-js/core';
2
+ export interface IValideOption {
3
+ modernConfig: NormalizedConfig;
4
+ tsconfigPath: string;
5
+ }
6
+ export declare const valideBeforeTask: ({
7
+ modernConfig,
8
+ tsconfigPath
9
+ }: IValideOption) => void;
10
+ export declare const modernConfigValid: (modernConfig: NormalizedConfig, option: {
11
+ tsconfigPath: string;
12
+ }) => string | null;
@@ -0,0 +1,6 @@
1
+ /** @type {import('@modern-js/module-tools').UserConfig} */
2
+ module.exports = {
3
+ output: {
4
+ disableSourceMap: true,
5
+ },
6
+ };
package/package.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "name": "@modern-js/module-tools",
3
+ "version": "1.0.0-alpha.3",
4
+ "bin": {
5
+ "modern": "./bin/modern.js"
6
+ },
7
+ "jsnext:source": "./src/index.ts",
8
+ "types": "./dist/types/index.d.ts",
9
+ "main": "./dist/js/node/index.js",
10
+ "module": "./dist/js/treeshaking/index.js",
11
+ "jsnext:modern": "./dist/js/modern/index.js",
12
+ "exports": {
13
+ ".": {
14
+ "node": {
15
+ "import": "./dist/js/modern/index.js",
16
+ "require": "./dist/js/node/index.js"
17
+ },
18
+ "default": "./dist/js/treeshaking/index.js"
19
+ },
20
+ "./cli": "./dist/js/node/index.js",
21
+ "./build": "./dist/js/node/build.js"
22
+ },
23
+ "typesVersions": {
24
+ "*": {
25
+ "build": [
26
+ "./dist/types/build.d.ts"
27
+ ]
28
+ }
29
+ },
30
+ "dependencies": {
31
+ "@babel/generator": "^7.15.0",
32
+ "@babel/parser": "^7.15.2",
33
+ "@babel/runtime": "^7",
34
+ "@babel/traverse": "^7.15.0",
35
+ "@babel/types": "^7.15.0",
36
+ "@modern-js/babel-compiler": "^1.0.0-alpha.3",
37
+ "@modern-js/babel-preset-module": "^1.0.0-alpha.3",
38
+ "@modern-js/core": "^1.0.0-alpha.3",
39
+ "@modern-js/css-config": "^1.0.0-alpha.3",
40
+ "@modern-js/i18n-cli-language-detector": "^1.0.0-alpha.3",
41
+ "@modern-js/module-tools-hooks": "^1.0.0-alpha.3",
42
+ "@modern-js/new-action": "^1.0.0-alpha.3",
43
+ "@modern-js/plugin": "^1.0.0-alpha.3",
44
+ "@modern-js/plugin-analyze": "^1.0.0-alpha.3",
45
+ "@modern-js/plugin-changeset": "^1.0.0-alpha.3",
46
+ "@modern-js/plugin-fast-refresh": "^1.0.0-alpha.3",
47
+ "@modern-js/plugin-i18n": "^1.0.0-alpha.3",
48
+ "@modern-js/style-compiler": "^1.0.0-alpha.3",
49
+ "@modern-js/utils": "^1.0.0-alpha.3",
50
+ "chalk": "^4.1.2",
51
+ "chokidar": "^3.5.2",
52
+ "dotenv": "^10.0.0",
53
+ "execa": "^5.1.1",
54
+ "fast-glob": "^3.2.5",
55
+ "glob": "^7.1.7",
56
+ "globby": "^11",
57
+ "inquirer": "^8.1.5",
58
+ "json5": "^2.2.0",
59
+ "lodash.merge": "^4.6.2",
60
+ "normalize-path": "^3.0.0",
61
+ "p-map": "^4",
62
+ "process.argv": "^0.6.0",
63
+ "signale": "^1.4.0",
64
+ "tsconfig-paths": "^3.10.1"
65
+ },
66
+ "devDependencies": {
67
+ "@babel/preset-typescript": "^7.15.0",
68
+ "@modern-js/babel-chain": "^1.0.0-alpha.3",
69
+ "@types/babel__core": "^7.1.15",
70
+ "@types/babel__generator": "^7.6.3",
71
+ "@types/babel__traverse": "^7.14.2",
72
+ "@types/glob": "^7.1.4",
73
+ "@types/inquirer": "^8.1.1",
74
+ "@types/jest": "^26",
75
+ "@types/lodash.merge": "^4.6.6",
76
+ "@types/node": "^14",
77
+ "@types/normalize-path": "^3.0.0",
78
+ "@types/signale": "^1.4.2",
79
+ "commander": "^8.1.0",
80
+ "typescript": "^4",
81
+ "@modern-js/plugin-testing": "^1.0.0-alpha.3",
82
+ "@modern-js/module-tools": "^1.0.0-alpha.3"
83
+ },
84
+ "sideEffects": false,
85
+ "modernConfig": {
86
+ "output": {
87
+ "packageMode": "node-js"
88
+ }
89
+ },
90
+ "scripts": {
91
+ "new": "modern new",
92
+ "build": "modern build",
93
+ "test": "modern test --passWithNoTests"
94
+ }
95
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "rules": {
3
+ "react-hooks/rules-of-hooks": "off"
4
+ }
5
+ }
@@ -0,0 +1,39 @@
1
+ import { Import } from '@modern-js/utils';
2
+ import type { Command } from 'commander';
3
+ import type { IBuildOption } from '../commands/build';
4
+
5
+ const local: typeof import('../locale') = Import.lazy(
6
+ '../locale/index',
7
+ require,
8
+ );
9
+ const commands: typeof import('../commands') = Import.lazy(
10
+ '../commands',
11
+ require,
12
+ );
13
+
14
+ export const buildCli = (program: Command) => {
15
+ // TODO: 初始化环境变量
16
+ program
17
+ .command('build')
18
+ .usage('[options]')
19
+ .description(local.i18n.t(local.localeKeys.command.build.describe))
20
+ .option('-w, --watch', local.i18n.t(local.localeKeys.command.build.watch))
21
+ .option(
22
+ '--tsconfig [tsconfig]',
23
+ local.i18n.t(local.localeKeys.command.build.tsconfig),
24
+ './tsconfig.json',
25
+ )
26
+ .option(
27
+ '--style-only',
28
+ local.i18n.t(local.localeKeys.command.build.style_only),
29
+ )
30
+ .option(
31
+ '--platform [platform]',
32
+ local.i18n.t(local.localeKeys.command.build.platform),
33
+ )
34
+ .option('--no-tsc', local.i18n.t(local.localeKeys.command.build.no_tsc))
35
+ .option('--no-clear', local.i18n.t(local.localeKeys.command.build.no_clear))
36
+ .action(async (subCommand: IBuildOption) => {
37
+ await commands.build(subCommand);
38
+ });
39
+ };
package/src/cli/dev.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { Import } from '@modern-js/utils';
2
+ import type { Command } from 'commander';
3
+ import type { IDevOption } from '@/commands/dev';
4
+
5
+ const local: typeof import('../locale') = Import.lazy('../locale', require);
6
+ const commands: typeof import('../commands') = Import.lazy(
7
+ '../commands',
8
+ require,
9
+ );
10
+
11
+ export const devCli = (program: Command) => {
12
+ program
13
+ .command('dev')
14
+ .usage('[options]')
15
+ .description(local.i18n.t(local.localeKeys.command.dev.describe))
16
+ .option(
17
+ '--tsconfig [tsconfig]',
18
+ local.i18n.t(local.localeKeys.command.build.tsconfig),
19
+ './tsconfig.json',
20
+ )
21
+ .action(async (params: IDevOption) => {
22
+ await commands.dev(params);
23
+ });
24
+ };
@@ -0,0 +1,3 @@
1
+ export * from './build';
2
+ export * from './dev';
3
+ export * from './new';
package/src/cli/new.ts ADDED
@@ -0,0 +1,32 @@
1
+ import type { Command } from 'commander';
2
+ import { Import } from '@modern-js/utils';
3
+
4
+ const newAction: typeof import('@modern-js/new-action') = Import.lazy(
5
+ '@modern-js/new-action',
6
+ require,
7
+ );
8
+ const local: typeof import('../locale') = Import.lazy('../locale', require);
9
+
10
+ export const newCli = (program: Command, locale?: string) => {
11
+ program
12
+ .command('new')
13
+ .usage('[options]')
14
+ .description(local.i18n.t(local.localeKeys.command.new.describe))
15
+ .option(
16
+ '-d, --debug',
17
+ local.i18n.t(local.localeKeys.command.new.debug),
18
+ false,
19
+ )
20
+ .option(
21
+ '-c, --config <config>',
22
+ local.i18n.t(local.localeKeys.command.new.config),
23
+ )
24
+ .option(
25
+ '--dist-tag <tag>',
26
+ local.i18n.t(local.localeKeys.command.new.distTag),
27
+ )
28
+ .option('--registry', local.i18n.t(local.localeKeys.command.new.registry))
29
+ .action(async options => {
30
+ await newAction.ModuleNewAction({ ...options, locale });
31
+ });
32
+ };
@@ -0,0 +1,81 @@
1
+ import * as path from 'path';
2
+ import { fs, Import } from '@modern-js/utils';
3
+ import type { Platform } from '@/types';
4
+
5
+ const tsConfigutils: typeof import('../utils/tsconfig') = Import.lazy(
6
+ '../utils/tsconfig',
7
+ require,
8
+ );
9
+
10
+ const valid: typeof import('../utils/valide') = Import.lazy(
11
+ '../utils/valide',
12
+ require,
13
+ );
14
+ const buildFeature: typeof import('../features/build') = Import.lazy(
15
+ '../features/build',
16
+ require,
17
+ );
18
+ const core: typeof import('@modern-js/core') = Import.lazy(
19
+ '@modern-js/core',
20
+ require,
21
+ );
22
+ const dotenv: typeof import('dotenv') = Import.lazy('dotenv', require);
23
+
24
+ export interface IBuildOption {
25
+ watch: boolean;
26
+ tsconfig: string;
27
+ platform: boolean | Exclude<Platform, 'all'>;
28
+ styleOnly: boolean;
29
+ tsc: boolean;
30
+ clear: boolean;
31
+ }
32
+
33
+ export const build = async ({
34
+ watch = false,
35
+ tsconfig: tsconfigName,
36
+ tsc,
37
+ clear = true,
38
+ platform,
39
+ }: IBuildOption) => {
40
+ const {
41
+ value: { appDirectory },
42
+ } = core.useAppContext();
43
+ const modernConfig = core.useResolvedConfigContext().value;
44
+ const {
45
+ output: { path: outputPath = 'dist' },
46
+ } = modernConfig;
47
+ const tsconfigPath = path.join(appDirectory, tsconfigName);
48
+ dotenv.config();
49
+ const isTsProject = tsConfigutils.existTsConfigFile(tsconfigPath);
50
+ const enableTscCompiler = isTsProject && tsc;
51
+
52
+ valid.valideBeforeTask({ modernConfig, tsconfigPath });
53
+
54
+ if (clear) {
55
+ fs.removeSync(path.join(appDirectory, outputPath));
56
+ }
57
+
58
+ // TODO: 一些配置只需要从modernConfig中获取
59
+ await buildFeature.build(
60
+ {
61
+ enableWatchMode: watch,
62
+ isTsProject,
63
+ platform,
64
+ sourceDir: 'src',
65
+ tsconfigName,
66
+ enableTscCompiler,
67
+ },
68
+ modernConfig,
69
+ );
70
+
71
+ process.on('SIGBREAK', () => {
72
+ console.info('exit');
73
+ const tempTsconfigFilePath = path.join(
74
+ appDirectory,
75
+ './tsconfig.temp.json',
76
+ );
77
+ if (fs.existsSync(tempTsconfigFilePath)) {
78
+ fs.removeSync(tempTsconfigFilePath);
79
+ }
80
+ });
81
+ };
@@ -0,0 +1,41 @@
1
+ import * as path from 'path';
2
+ import { Import } from '@modern-js/utils';
3
+
4
+ const devFeature: typeof import('../features/dev') = Import.lazy(
5
+ '../features/dev',
6
+ require,
7
+ );
8
+ const core: typeof import('@modern-js/core') = Import.lazy(
9
+ '@modern-js/core',
10
+ require,
11
+ );
12
+ const dotenv: typeof import('dotenv') = Import.lazy('dotenv', require);
13
+ const tsConfigutils: typeof import('../utils/tsconfig') = Import.lazy(
14
+ '../utils/tsconfig',
15
+ require,
16
+ );
17
+ const valid: typeof import('../utils/valide') = Import.lazy(
18
+ '../utils/valide',
19
+ require,
20
+ );
21
+
22
+ export interface IDevOption {
23
+ tsconfig: string;
24
+ }
25
+
26
+ export const dev = async (option: IDevOption) => {
27
+ const { tsconfig: tsconfigName } = option;
28
+ const appContext = core.useAppContext().value;
29
+ const modernConfig = core.useResolvedConfigContext().value;
30
+ const { appDirectory } = appContext;
31
+ const tsconfigPath = path.join(appDirectory, tsconfigName);
32
+
33
+ dotenv.config();
34
+
35
+ valid.valideBeforeTask({ modernConfig, tsconfigPath });
36
+
37
+ const isTsProject = tsConfigutils.existTsConfigFile(tsconfigPath);
38
+
39
+ // await devFeature.showMenu({ isTsProject, appDirectory });
40
+ await devFeature.devStorybook({ isTsProject, appDirectory });
41
+ };
@@ -0,0 +1,2 @@
1
+ export { dev } from './dev';
2
+ export { build } from './build';
@@ -0,0 +1,76 @@
1
+ import { Import, chalk } from '@modern-js/utils';
2
+ import type { LoggerText } from './logger';
3
+
4
+ const core: typeof import('@modern-js/core') = Import.lazy(
5
+ '@modern-js/core',
6
+ require,
7
+ );
8
+ const execa: typeof import('execa') = Import.lazy('execa', require);
9
+ const lg: typeof import('./logger') = Import.lazy('./logger', require);
10
+ const pMap: typeof import('p-map') = Import.lazy('p-map', require);
11
+
12
+ export type Platform = 'all' | 'docs' | 'storybook';
13
+
14
+ export interface IBuildPlatformOption {
15
+ platform: Platform;
16
+ isTsProject?: boolean;
17
+ }
18
+
19
+ export const buildPlatform = async (option: IBuildPlatformOption) => {
20
+ const { isTsProject = false, platform } = option;
21
+ const lm = new lg.LoggerManager();
22
+ // 获取platforms的参数
23
+ const buildTasks = await (core.mountHook() as any).platformBuild({
24
+ isTsProject,
25
+ });
26
+
27
+ if (buildTasks.length <= 0) {
28
+ console.info(
29
+ chalk.yellow(
30
+ 'No build tasks detected.\nYou can use the `new` command to enable the more features',
31
+ ),
32
+ );
33
+ return;
34
+ }
35
+
36
+ const loggerMap: Record<string, LoggerText> = {};
37
+ const taskMapper = buildTasks
38
+ .filter((task: any) => platform === 'all' || task.name === platform)
39
+ .map((params: any) => {
40
+ const logger = lm.createLoggerText({ title: params.title });
41
+ loggerMap[params.name] = logger;
42
+ return {
43
+ logger,
44
+ ...params,
45
+ };
46
+ });
47
+
48
+ if (taskMapper.length <= 0) {
49
+ console.info(chalk.yellow(`'${platform}' is undefined task`));
50
+ return;
51
+ }
52
+
53
+ lm.showCompiling();
54
+ await pMap(taskMapper, async ({ taskPath, params, logger }: any) => {
55
+ const childProcess = execa.node(taskPath, params, {
56
+ stdio: 'pipe',
57
+ all: true,
58
+ });
59
+
60
+ lm.addStdout(logger, childProcess.stdout, {
61
+ event: { data: true, error: true },
62
+ });
63
+
64
+ lm.addStderr(logger, childProcess.stderr);
65
+
66
+ await childProcess;
67
+ // lm.disappearCompiling();
68
+ // console.info(lg.colors.title(title));
69
+ // console.info(a.all);
70
+ });
71
+
72
+ lm.disappearCompiling();
73
+ for (const key of Object.keys(loggerMap)) {
74
+ console.info(loggerMap[key].value);
75
+ }
76
+ };
@@ -0,0 +1,93 @@
1
+ import * as path from 'path';
2
+ import * as os from 'os';
3
+ import { Import } from '@modern-js/utils';
4
+
5
+ import type { NormalizedConfig } from '@modern-js/core';
6
+ import type { IBuildConfig, ITaskMapper } from '../../types';
7
+
8
+ const core: typeof import('@modern-js/core') = Import.lazy(
9
+ '@modern-js/core',
10
+ require,
11
+ );
12
+ const execa: typeof import('execa') = Import.lazy('execa', require);
13
+ const lg: typeof import('./logger') = Import.lazy('./logger', require);
14
+ const pMap: typeof import('p-map') = Import.lazy('p-map', require);
15
+ const utils: typeof import('./utils') = Import.lazy('./utils', require);
16
+ const constants: typeof import('./constants') = Import.lazy(
17
+ './constants',
18
+ require,
19
+ );
20
+
21
+ export const buildInWatchMode = async (
22
+ config: IBuildConfig,
23
+ _: NormalizedConfig,
24
+ ) => {
25
+ const { appDirectory } = core.useAppContext().value;
26
+ const { sourceDir, enableTscCompiler } = config;
27
+ const srcRootDir = path.join(appDirectory, sourceDir);
28
+ const concurrency = os.cpus().length;
29
+ const lm = new lg.LoggerManager();
30
+ const codeLog = lm.createLoggerText({
31
+ title: constants.runBabelCompilerTitle,
32
+ });
33
+ const dtsLog = lm.createLoggerText({ title: constants.runTscWatchTitle });
34
+ const styleLog = lm.createLoggerText({
35
+ title: constants.runStyleCompilerTitle,
36
+ });
37
+ const initCodeMapper = utils.getCodeInitMapper(config);
38
+ const taskMapper: ITaskMapper[] = [
39
+ ...utils.getCodeMapper({
40
+ logger: codeLog,
41
+ taskPath: require.resolve('../../tasks/build-watch-source-code'),
42
+ config,
43
+ willCompilerDirOrFile: sourceDir,
44
+ initMapper: initCodeMapper,
45
+ srcRootDir,
46
+ }),
47
+ ...(enableTscCompiler ? utils.getDtsMapper(config, dtsLog) : []),
48
+ {
49
+ logger: styleLog,
50
+ taskPath: require.resolve('../../tasks/build-watch-style'),
51
+ },
52
+ ];
53
+ lm.on('data', () => {
54
+ console.info(constants.clearFlag);
55
+ enableTscCompiler && console.info(dtsLog.value);
56
+ console.info(codeLog.value);
57
+ console.info(styleLog.value);
58
+ });
59
+ await pMap(
60
+ taskMapper,
61
+ async ({ logger, taskPath, params }) => {
62
+ const childProcess = execa.node(taskPath, params, { stdio: 'pipe' });
63
+ if (logger === codeLog) {
64
+ lm.addStdout(logger, childProcess.stdout, {
65
+ event: { error: true, data: true },
66
+ // colors: { data: s => s },
67
+ });
68
+ lm.addStderr(logger, childProcess.stderr);
69
+ }
70
+
71
+ if (logger === dtsLog) {
72
+ lm.addStdout(logger, childProcess.stdout, {
73
+ event: { data: true, error: true },
74
+ colors: {
75
+ // tsc 的log信息无论是错误还是正确都是从 data event 中获取到的
76
+ data: s => s,
77
+ },
78
+ });
79
+ }
80
+
81
+ if (logger === styleLog) {
82
+ lm.addStdout(logger, childProcess.stdout, {
83
+ event: { error: true, data: true },
84
+ // colors: { data: s => s },
85
+ });
86
+ lm.addStderr(logger, childProcess.stderr);
87
+ }
88
+
89
+ await childProcess;
90
+ },
91
+ { concurrency },
92
+ );
93
+ };
@@ -0,0 +1,111 @@
1
+ /* eslint-disable max-statements */
2
+ import * as path from 'path';
3
+ import * as os from 'os';
4
+ import { Import } from '@modern-js/utils';
5
+ import type { NormalizedConfig } from '@modern-js/core';
6
+ import type { IBuildConfig, ITaskMapper } from '../../types';
7
+
8
+ const pMap: typeof import('p-map') = Import.lazy('p-map', require);
9
+ const utils: typeof import('./utils') = Import.lazy('./utils', require);
10
+ const execa: typeof import('execa') = Import.lazy('execa', require);
11
+ const lg: typeof import('./logger') = Import.lazy('./logger', require);
12
+ const constants: typeof import('./constants') = Import.lazy(
13
+ './constants',
14
+ require,
15
+ );
16
+ const core: typeof import('@modern-js/core') = Import.lazy(
17
+ '@modern-js/core',
18
+ require,
19
+ );
20
+
21
+ export const buildSourceCode = async (
22
+ config: IBuildConfig,
23
+ _: NormalizedConfig,
24
+ ) => {
25
+ const { sourceDir, enableTscCompiler } = config;
26
+ const { appDirectory } = core.useAppContext().value;
27
+ const concurrency = os.cpus().length;
28
+ const srcRootDir = path.join(appDirectory, sourceDir);
29
+ const lm = new lg.LoggerManager();
30
+ const codeLog = lm.createLoggerText({
31
+ title: constants.runBabelCompilerTitle,
32
+ });
33
+ const dtsLog = lm.createLoggerText({ title: constants.runTscTitle });
34
+ const styleLog = lm.createLoggerText({
35
+ title: constants.runStyleCompilerTitle,
36
+ });
37
+ const copyLog = lm.createLoggerText({ title: 'Copy Log:' });
38
+ const initCodeMapper = utils.getCodeInitMapper(config);
39
+ const taskMapper: ITaskMapper[] = [
40
+ ...utils.getCodeMapper({
41
+ logger: codeLog,
42
+ taskPath: require.resolve('../../tasks/build-source-code'),
43
+ config,
44
+ willCompilerDirOrFile: sourceDir,
45
+ initMapper: initCodeMapper,
46
+ srcRootDir,
47
+ }),
48
+ ...(enableTscCompiler ? utils.getDtsMapper(config, dtsLog) : []),
49
+ {
50
+ logger: styleLog,
51
+ taskPath: require.resolve('../../tasks/build-style'),
52
+ },
53
+ {
54
+ logger: copyLog,
55
+ taskPath: require.resolve('../../tasks/copy-assets'),
56
+ },
57
+ ];
58
+
59
+ lm.showCompiling();
60
+ await pMap(
61
+ taskMapper,
62
+ async ({ logger, taskPath, params }) => {
63
+ const childProcess = execa.node(taskPath, params, { stdio: 'pipe' });
64
+
65
+ if (logger === codeLog || logger === copyLog) {
66
+ lm.addStdout(logger, childProcess.stdout, {
67
+ event: { data: true, error: true },
68
+ });
69
+ lm.addStderr(logger, childProcess.stderr);
70
+ }
71
+
72
+ if (logger === dtsLog) {
73
+ lm.addStdout(dtsLog, childProcess.stdout, {
74
+ event: { data: true, error: true },
75
+ });
76
+ lm.addStderr(dtsLog, childProcess.stderr);
77
+ }
78
+ if (logger === styleLog) {
79
+ lm.addStdout(logger, childProcess.stdout, {
80
+ event: { data: true, error: true },
81
+ });
82
+ lm.addStderr(logger, childProcess.stderr);
83
+ }
84
+ await childProcess;
85
+ },
86
+ { concurrency },
87
+ );
88
+
89
+ lm.disappearCompiling();
90
+ enableTscCompiler && console.info(dtsLog.value);
91
+ console.info(codeLog.value);
92
+ if (styleLog.hasMessages()) {
93
+ console.info(styleLog.value);
94
+ }
95
+
96
+ if (copyLog.hasMessages()) {
97
+ console.info(copyLog.value);
98
+ }
99
+
100
+ if (
101
+ dtsLog.hasErrorMessage ||
102
+ codeLog.hasErrorMessage ||
103
+ styleLog.hasErrorMessage ||
104
+ copyLog.hasErrorMessage
105
+ ) {
106
+ // eslint-disable-next-line no-process-exit
107
+ process.exit(1);
108
+ }
109
+ };
110
+
111
+ /* eslint-enable max-statements */