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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/LICENSE +21 -0
  3. package/README.md +32 -0
  4. package/bin/modern.js +3 -0
  5. package/dist/js/modern/cli/build.js +9 -0
  6. package/dist/js/modern/cli/dev.js +8 -0
  7. package/dist/js/modern/cli/index.js +3 -0
  8. package/dist/js/modern/cli/new.js +16 -0
  9. package/dist/js/modern/commands/build.js +56 -0
  10. package/dist/js/modern/commands/dev.js +29 -0
  11. package/dist/js/modern/commands/index.js +2 -0
  12. package/dist/js/modern/features/build/build-platform.js +70 -0
  13. package/dist/js/modern/features/build/build-watch.js +96 -0
  14. package/dist/js/modern/features/build/build.js +110 -0
  15. package/dist/js/modern/features/build/constants.js +66 -0
  16. package/dist/js/modern/features/build/index.js +29 -0
  17. package/dist/js/modern/features/build/logger/index.js +2 -0
  18. package/dist/js/modern/features/build/logger/logText.js +63 -0
  19. package/dist/js/modern/features/build/logger/loggerManager.js +101 -0
  20. package/dist/js/modern/features/build/utils.js +198 -0
  21. package/dist/js/modern/features/dev/index.js +41 -0
  22. package/dist/js/modern/index.js +46 -0
  23. package/dist/js/modern/locale/en.js +23 -0
  24. package/dist/js/modern/locale/index.js +10 -0
  25. package/dist/js/modern/locale/zh.js +23 -0
  26. package/dist/js/modern/schema/index.js +3 -0
  27. package/dist/js/modern/schema/output.js +29 -0
  28. package/dist/js/modern/schema/source.js +15 -0
  29. package/dist/js/modern/tasks/build-source-code.js +213 -0
  30. package/dist/js/modern/tasks/build-style.js +152 -0
  31. package/dist/js/modern/tasks/build-watch-source-code.js +144 -0
  32. package/dist/js/modern/tasks/build-watch-style.js +213 -0
  33. package/dist/js/modern/tasks/constants.js +1 -0
  34. package/dist/js/modern/tasks/copy-assets.js +108 -0
  35. package/dist/js/modern/tasks/generator-dts.js +220 -0
  36. package/dist/js/modern/types.js +0 -0
  37. package/dist/js/modern/utils/babel.js +85 -0
  38. package/dist/js/modern/utils/color.js +2 -0
  39. package/dist/js/modern/utils/copy.js +80 -0
  40. package/dist/js/modern/utils/json.js +13 -0
  41. package/dist/js/modern/utils/language.js +6 -0
  42. package/dist/js/modern/utils/logger.js +110 -0
  43. package/dist/js/modern/utils/readline.js +19 -0
  44. package/dist/js/modern/utils/tsconfig.js +15 -0
  45. package/dist/js/modern/utils/tspaths-transform/constants.js +3 -0
  46. package/dist/js/modern/utils/tspaths-transform/index.js +130 -0
  47. package/dist/js/modern/utils/tspaths-transform/utils.js +21 -0
  48. package/dist/js/modern/utils/valide.js +28 -0
  49. package/dist/js/node/cli/build.js +21 -0
  50. package/dist/js/node/cli/dev.js +20 -0
  51. package/dist/js/node/cli/index.js +44 -0
  52. package/dist/js/node/cli/new.js +28 -0
  53. package/dist/js/node/commands/build.js +76 -0
  54. package/dist/js/node/commands/dev.js +49 -0
  55. package/dist/js/node/commands/index.js +21 -0
  56. package/dist/js/node/features/build/build-platform.js +84 -0
  57. package/dist/js/node/features/build/build-watch.js +118 -0
  58. package/dist/js/node/features/build/build.js +133 -0
  59. package/dist/js/node/features/build/constants.js +79 -0
  60. package/dist/js/node/features/build/index.js +41 -0
  61. package/dist/js/node/features/build/logger/index.js +31 -0
  62. package/dist/js/node/features/build/logger/logText.js +79 -0
  63. package/dist/js/node/features/build/logger/loggerManager.js +111 -0
  64. package/dist/js/node/features/build/utils.js +231 -0
  65. package/dist/js/node/features/dev/index.js +60 -0
  66. package/dist/js/node/index.js +64 -0
  67. package/dist/js/node/locale/en.js +30 -0
  68. package/dist/js/node/locale/index.js +22 -0
  69. package/dist/js/node/locale/zh.js +30 -0
  70. package/dist/js/node/schema/index.js +14 -0
  71. package/dist/js/node/schema/output.js +36 -0
  72. package/dist/js/node/schema/source.js +22 -0
  73. package/dist/js/node/tasks/build-source-code.js +240 -0
  74. package/dist/js/node/tasks/build-style.js +168 -0
  75. package/dist/js/node/tasks/build-watch-source-code.js +156 -0
  76. package/dist/js/node/tasks/build-watch-style.js +231 -0
  77. package/dist/js/node/tasks/constants.js +8 -0
  78. package/dist/js/node/tasks/copy-assets.js +126 -0
  79. package/dist/js/node/tasks/generator-dts.js +243 -0
  80. package/dist/js/node/types.js +0 -0
  81. package/dist/js/node/utils/babel.js +99 -0
  82. package/dist/js/node/utils/color.js +12 -0
  83. package/dist/js/node/utils/copy.js +99 -0
  84. package/dist/js/node/utils/json.js +22 -0
  85. package/dist/js/node/utils/language.js +15 -0
  86. package/dist/js/node/utils/logger.js +133 -0
  87. package/dist/js/node/utils/readline.js +33 -0
  88. package/dist/js/node/utils/tsconfig.js +30 -0
  89. package/dist/js/node/utils/tspaths-transform/constants.js +10 -0
  90. package/dist/js/node/utils/tspaths-transform/index.js +155 -0
  91. package/dist/js/node/utils/tspaths-transform/utils.js +32 -0
  92. package/dist/js/node/utils/valide.js +41 -0
  93. package/dist/types/cli/build.d.ts +2 -0
  94. package/dist/types/cli/dev.d.ts +2 -0
  95. package/dist/types/cli/index.d.ts +3 -0
  96. package/dist/types/cli/new.d.ts +2 -0
  97. package/dist/types/commands/build.d.ts +16 -0
  98. package/dist/types/commands/dev.d.ts +4 -0
  99. package/dist/types/commands/index.d.ts +2 -0
  100. package/dist/types/features/build/build-platform.d.ts +6 -0
  101. package/dist/types/features/build/build-watch.d.ts +3 -0
  102. package/dist/types/features/build/build.d.ts +3 -0
  103. package/dist/types/features/build/constants.d.ts +14 -0
  104. package/dist/types/features/build/index.d.ts +3 -0
  105. package/dist/types/features/build/logger/index.d.ts +2 -0
  106. package/dist/types/features/build/logger/logText.d.ts +39 -0
  107. package/dist/types/features/build/logger/loggerManager.d.ts +39 -0
  108. package/dist/types/features/build/utils.d.ts +58 -0
  109. package/dist/types/features/dev/index.d.ts +7 -0
  110. package/dist/types/index.d.ts +17 -0
  111. package/dist/types/locale/en.d.ts +23 -0
  112. package/dist/types/locale/index.d.ts +47 -0
  113. package/dist/types/locale/zh.d.ts +23 -0
  114. package/dist/types/schema/index.d.ts +36 -0
  115. package/dist/types/schema/output.d.ts +22 -0
  116. package/dist/types/schema/source.d.ts +15 -0
  117. package/dist/types/tasks/build-source-code.d.ts +42 -0
  118. package/dist/types/tasks/build-style.d.ts +1 -0
  119. package/dist/types/tasks/build-watch-source-code.d.ts +1 -0
  120. package/dist/types/tasks/build-watch-style.d.ts +1 -0
  121. package/dist/types/tasks/constants.d.ts +1 -0
  122. package/dist/types/tasks/copy-assets.d.ts +1 -0
  123. package/dist/types/tasks/generator-dts.d.ts +1 -0
  124. package/dist/types/types.d.ts +48 -0
  125. package/dist/types/utils/babel.d.ts +8 -0
  126. package/dist/types/utils/color.d.ts +2 -0
  127. package/dist/types/utils/copy.d.ts +5 -0
  128. package/dist/types/utils/json.d.ts +4 -0
  129. package/dist/types/utils/language.d.ts +1 -0
  130. package/dist/types/utils/logger.d.ts +56 -0
  131. package/dist/types/utils/readline.d.ts +9 -0
  132. package/dist/types/utils/tsconfig.d.ts +16 -0
  133. package/dist/types/utils/tspaths-transform/constants.d.ts +1 -0
  134. package/dist/types/utils/tspaths-transform/index.d.ts +15 -0
  135. package/dist/types/utils/tspaths-transform/utils.d.ts +4 -0
  136. package/dist/types/utils/valide.d.ts +12 -0
  137. package/modern.config.js +6 -0
  138. package/package.json +95 -0
  139. package/src/.eslintrc.json +5 -0
  140. package/src/cli/build.ts +39 -0
  141. package/src/cli/dev.ts +24 -0
  142. package/src/cli/index.ts +3 -0
  143. package/src/cli/new.ts +32 -0
  144. package/src/commands/build.ts +81 -0
  145. package/src/commands/dev.ts +41 -0
  146. package/src/commands/index.ts +2 -0
  147. package/src/features/build/build-platform.ts +76 -0
  148. package/src/features/build/build-watch.ts +93 -0
  149. package/src/features/build/build.ts +111 -0
  150. package/src/features/build/constants.ts +52 -0
  151. package/src/features/build/index.ts +36 -0
  152. package/src/features/build/logger/index.ts +2 -0
  153. package/src/features/build/logger/logText.ts +80 -0
  154. package/src/features/build/logger/loggerManager.ts +132 -0
  155. package/src/features/build/utils.ts +250 -0
  156. package/src/features/dev/index.ts +62 -0
  157. package/src/index.ts +55 -0
  158. package/src/locale/en.ts +21 -0
  159. package/src/locale/index.ts +15 -0
  160. package/src/locale/zh.ts +21 -0
  161. package/src/schema/index.ts +4 -0
  162. package/src/schema/output.ts +34 -0
  163. package/src/schema/schema.d.ts +13 -0
  164. package/src/schema/source.ts +16 -0
  165. package/src/tasks/build-source-code.ts +245 -0
  166. package/src/tasks/build-style.ts +175 -0
  167. package/src/tasks/build-watch-source-code.ts +185 -0
  168. package/src/tasks/build-watch-style.ts +260 -0
  169. package/src/tasks/constants.ts +1 -0
  170. package/src/tasks/copy-assets.ts +117 -0
  171. package/src/tasks/generator-dts.ts +277 -0
  172. package/src/type.d.ts +1 -0
  173. package/src/types.ts +63 -0
  174. package/src/utils/babel.ts +100 -0
  175. package/src/utils/color.ts +3 -0
  176. package/src/utils/copy.ts +70 -0
  177. package/src/utils/json.ts +13 -0
  178. package/src/utils/language.ts +9 -0
  179. package/src/utils/logger.ts +144 -0
  180. package/src/utils/readline.ts +28 -0
  181. package/src/utils/tsconfig.ts +37 -0
  182. package/src/utils/tspaths-transform/constants.ts +19 -0
  183. package/src/utils/tspaths-transform/index.ts +139 -0
  184. package/src/utils/tspaths-transform/utils.ts +25 -0
  185. package/src/utils/valide.ts +37 -0
  186. package/tsconfig.json +13 -0
  187. package/types.d.ts +1 -0
@@ -0,0 +1,213 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import { Import, fs } from '@modern-js/utils';
8
+ const babelCompiler = Import.lazy('@modern-js/babel-compiler', require);
9
+ const glob = Import.lazy('glob', require);
10
+ const argv = Import.lazy('process.argv', require);
11
+ const pluginNode = Import.lazy('@modern-js/plugin/node', require);
12
+ const core = Import.lazy('@modern-js/core', require);
13
+ const bc = Import.lazy('../utils/babel', require);
14
+ const ts = Import.lazy('../utils/tsconfig', require);
15
+ export let Compiler;
16
+
17
+ (function (Compiler) {
18
+ Compiler[Compiler["babel"] = 0] = "babel";
19
+ Compiler[Compiler["esbuild"] = 1] = "esbuild";
20
+ Compiler[Compiler["swc"] = 2] = "swc";
21
+ })(Compiler || (Compiler = {}));
22
+
23
+ const runBabelCompiler = async (willCompilerFiles, config, babelConfig = {}) => {
24
+ const {
25
+ srcRootDir,
26
+ distDir
27
+ } = config; // TODO: 判断lynx模式下,修改distFileExtMap: {'js': 'js', 'jsx': 'jsx', 'ts': 'js', 'tsx': 'jsx'}
28
+
29
+ return babelCompiler.compiler({
30
+ quiet: true,
31
+ enableVirtualDist: true,
32
+ rootDir: srcRootDir,
33
+ filenames: willCompilerFiles,
34
+ distDir
35
+ }, babelConfig);
36
+ };
37
+
38
+ export const getWillCompilerCode = (srcDirOrFile, option) => {
39
+ const {
40
+ tsconfig,
41
+ isTsProject
42
+ } = option; // 如果是一个文件路径,则直接返回
43
+
44
+ if (fs.existsSync(srcDirOrFile) && fs.lstatSync(srcDirOrFile).isFile()) {
45
+ return [srcDirOrFile];
46
+ }
47
+
48
+ const getExts = isTs => {
49
+ // TODO: 是否受控tsconfig.json 里的jsx配置
50
+ let exts = [];
51
+
52
+ if (isTs) {
53
+ var _tsconfig$compilerOpt;
54
+
55
+ exts = tsconfig !== null && tsconfig !== void 0 && (_tsconfig$compilerOpt = tsconfig.compilerOptions) !== null && _tsconfig$compilerOpt !== void 0 && _tsconfig$compilerOpt.allowJs ? ['.ts', '.tsx', '.js', '.jsx'] : ['.ts', '.tsx'];
56
+ } else {
57
+ exts = ['.js', '.jsx'];
58
+ }
59
+
60
+ return exts;
61
+ };
62
+
63
+ const exts = getExts(isTsProject);
64
+ const globPattern = `${srcDirOrFile}/**/*{${exts.join(',')}}`;
65
+ const files = glob.sync(globPattern, {
66
+ ignore: [`${srcDirOrFile}/**/*.d.ts`]
67
+ });
68
+ return files;
69
+ };
70
+ export const buildSourceCode = async config => {
71
+ const {
72
+ compiler,
73
+ willCompilerDirOrFile,
74
+ tsconfigPath,
75
+ sourceMaps,
76
+ babelConfig
77
+ } = config;
78
+ const tsconfig = ts.readTsConfig(tsconfigPath);
79
+ const willCompilerFiles = getWillCompilerCode(willCompilerDirOrFile, {
80
+ tsconfig,
81
+ isTsProject: Boolean(tsconfig)
82
+ });
83
+ let distSet = null;
84
+
85
+ if (compiler === Compiler.babel) {
86
+ distSet = await runBabelCompiler(willCompilerFiles, config, _objectSpread(_objectSpread({}, babelConfig), {}, {
87
+ sourceMaps
88
+ }));
89
+ } else {
90
+ distSet = {
91
+ code: 1,
92
+ message: 'no compiler'
93
+ };
94
+ }
95
+
96
+ return distSet;
97
+ };
98
+
99
+ const generatorRealFiles = virtualDists => {
100
+ for (const virtualDist of virtualDists) {
101
+ const {
102
+ distPath,
103
+ code,
104
+ sourcemap,
105
+ sourceMapPath
106
+ } = virtualDist;
107
+ fs.ensureFileSync(distPath);
108
+ fs.writeFileSync(distPath, code);
109
+
110
+ if (sourcemap.length > 0) {
111
+ fs.ensureFileSync(sourceMapPath);
112
+ fs.writeFileSync(sourceMapPath, sourcemap);
113
+ }
114
+ }
115
+ };
116
+
117
+ export const initEnv = ({
118
+ syntax,
119
+ type
120
+ }) => {
121
+ if (syntax === 'es6+' && type === 'commonjs') {
122
+ return 'nodejs';
123
+ } else if (syntax === 'es6+' && type === 'module') {
124
+ return 'modern';
125
+ } else if (syntax === 'es5' && type === 'module') {
126
+ return 'legacy-browser';
127
+ }
128
+
129
+ return '';
130
+ };
131
+ const defaultConfig = {
132
+ srcRootDir: `${process.cwd()}/src`,
133
+ willCompilerDirOrFile: `${process.cwd()}/src`,
134
+ distDir: './dist/js/temp',
135
+ compiler: 'babel',
136
+ appDirectory: '',
137
+ sourceMaps: false,
138
+ tsconfigPath: '',
139
+ syntax: 'es5',
140
+ type: 'module',
141
+ watch: false
142
+ };
143
+
144
+ const taskMain = async ({
145
+ modernConfig
146
+ }) => {
147
+ // 执行脚本的参数处理和相关需要配置的获取
148
+ const processArgv = argv(process.argv.slice(2));
149
+ const config = processArgv(defaultConfig); // process.env.BUILD_MODE = initEnv(config);
150
+
151
+ const compiler = Compiler.babel; // 目前暂时只支持 babel
152
+
153
+ const babelConfig = bc.resolveBabelConfig(config.appDirectory, modernConfig, {
154
+ sourceAbsDir: config.srcRootDir,
155
+ tsconfigPath: config.tsconfigPath,
156
+ syntax: config.syntax,
157
+ type: config.type
158
+ });
159
+ const {
160
+ code,
161
+ message,
162
+ messageDetails,
163
+ virtualDists = []
164
+ } = await buildSourceCode({
165
+ distDir: config.distDir,
166
+ srcRootDir: config.srcRootDir,
167
+ willCompilerDirOrFile: config.willCompilerDirOrFile,
168
+ sourceMaps: config.sourceMaps,
169
+ compiler,
170
+ projectData: {
171
+ appDirectory: config.appDirectory
172
+ },
173
+ tsconfigPath: config.tsconfigPath,
174
+ babelConfig
175
+ });
176
+
177
+ if (code === 0) {
178
+ generatorRealFiles(virtualDists); // 执行成功log使用 console.info
179
+
180
+ console.info('[Babel Compiler]: Successfully');
181
+ } else if (messageDetails && messageDetails.length > 0) {
182
+ console.error(message);
183
+
184
+ for (const detail of messageDetails || []) {
185
+ console.error(detail.content);
186
+ }
187
+ }
188
+
189
+ if (code === 0 && config.copyDirs) {
190
+ const copyList = config.copyDirs.split(',');
191
+
192
+ for (const copyDir of copyList) {
193
+ fs.ensureDirSync(copyDir);
194
+ fs.copySync(config.distDir, copyDir);
195
+ }
196
+ }
197
+ };
198
+
199
+ (async () => {
200
+ pluginNode.enable();
201
+ const {
202
+ resolved
203
+ } = await core.cli.init();
204
+ await core.manager.run(async () => {
205
+ try {
206
+ await taskMain({
207
+ modernConfig: resolved
208
+ });
209
+ } catch (e) {
210
+ console.error(e);
211
+ }
212
+ });
213
+ })();
@@ -0,0 +1,152 @@
1
+ import * as path from 'path';
2
+ import { fs, Import } from '@modern-js/utils';
3
+ const cssConfig = Import.lazy('@modern-js/css-config', require);
4
+ const core = Import.lazy('@modern-js/core', require);
5
+ const compiler = Import.lazy('@modern-js/style-compiler', require);
6
+ const glob = Import.lazy('glob', require);
7
+ const hooks = Import.lazy('@modern-js/module-tools-hooks', require);
8
+ const pluginNode = Import.lazy('@modern-js/plugin/node', require);
9
+ const STYLE_DIRS = 'styles';
10
+ const SRC_STYLE_DIRS = 'src';
11
+
12
+ const checkStylesDirExist = option => {
13
+ const {
14
+ appDirectory
15
+ } = option;
16
+ return fs.existsSync(path.join(appDirectory, STYLE_DIRS));
17
+ };
18
+
19
+ const generatorFileOrLogError = (result, successMessage = '') => {
20
+ if (result.code === 0 && result.dists.length > 0) {
21
+ for (const file of result.dists) {
22
+ fs.ensureFileSync(file.filename);
23
+ fs.writeFileSync(file.filename, file.content);
24
+ }
25
+
26
+ if (successMessage) {
27
+ console.info(successMessage);
28
+ }
29
+ } else {
30
+ for (const file of result.errors) {
31
+ console.error(file.error);
32
+ }
33
+ }
34
+ };
35
+
36
+ const getPostcssOption = (appDirectory, modernConfig) => {
37
+ var _postcssOption$postcs;
38
+
39
+ const postcssOption = cssConfig.getPostcssConfig(appDirectory, modernConfig, false);
40
+ return {
41
+ plugins: (postcssOption === null || postcssOption === void 0 ? void 0 : (_postcssOption$postcs = postcssOption.postcssOptions) === null || _postcssOption$postcs === void 0 ? void 0 : _postcssOption$postcs.plugins) || [],
42
+ enableSourceMap: (postcssOption === null || postcssOption === void 0 ? void 0 : postcssOption.sourceMap) || false,
43
+ options: {}
44
+ };
45
+ };
46
+
47
+ const copyOriginStyleFiles = ({
48
+ targetDir,
49
+ outputDir
50
+ }) => {
51
+ const styleFiles = glob.sync(`${targetDir}/**/*.{css,sass,scss,less}`);
52
+
53
+ if (styleFiles.length > 0) {
54
+ fs.ensureDirSync(outputDir);
55
+ }
56
+
57
+ for (const styleFile of styleFiles) {
58
+ const file = path.relative(targetDir, styleFile);
59
+ fs.copyFileSync(styleFile, path.join(outputDir, file));
60
+ }
61
+ };
62
+
63
+ const taskMain = async ({
64
+ modernConfig,
65
+ appContext
66
+ }) => {
67
+ const {
68
+ assetsPath = 'styles',
69
+ path: outputPath = 'dist',
70
+ jsPath = 'js'
71
+ } = modernConfig.output;
72
+ const {
73
+ appDirectory
74
+ } = appContext;
75
+ const lessOption = await core.mountHook().moduleLessConfig({
76
+ modernConfig
77
+ }, // eslint-disable-next-line @typescript-eslint/require-await
78
+ {
79
+ onLast: async _ => null
80
+ });
81
+ const sassOption = await core.mountHook().moduleSassConfig({
82
+ modernConfig
83
+ }, // eslint-disable-next-line @typescript-eslint/require-await
84
+ {
85
+ onLast: async _ => null
86
+ });
87
+ const postcssOption = getPostcssOption(appDirectory, modernConfig);
88
+ const {
89
+ importStyle
90
+ } = modernConfig.output;
91
+ const existStylesDir = checkStylesDirExist({
92
+ appDirectory
93
+ }); // 编译 styles 目录样式
94
+
95
+ if (existStylesDir) {
96
+ const styleResult = await compiler.styleCompiler({
97
+ projectDir: appDirectory,
98
+ stylesDir: path.resolve(appDirectory, STYLE_DIRS),
99
+ outDir: path.join(appDirectory, outputPath, assetsPath),
100
+ enableVirtualDist: true,
101
+ compilerOption: {
102
+ less: lessOption,
103
+ sass: sassOption,
104
+ postcss: postcssOption
105
+ }
106
+ });
107
+ generatorFileOrLogError(styleResult, `[Style Compiler] Successfully for 'styles' dir`);
108
+ } // 编译 src 内的样式代码
109
+
110
+
111
+ const srcDir = path.resolve(appDirectory, SRC_STYLE_DIRS);
112
+ const outputDirtoSrc = path.join(appDirectory, outputPath, jsPath, assetsPath);
113
+
114
+ if (importStyle === 'compiled-code') {
115
+ const srcStyleResult = await compiler.styleCompiler({
116
+ projectDir: appDirectory,
117
+ stylesDir: srcDir,
118
+ outDir: outputDirtoSrc,
119
+ enableVirtualDist: true,
120
+ compilerOption: {
121
+ less: lessOption,
122
+ sass: sassOption,
123
+ postcss: postcssOption
124
+ }
125
+ });
126
+ generatorFileOrLogError(srcStyleResult, `[Style Compiler] Successfully for 'src' dir`);
127
+ } else {
128
+ copyOriginStyleFiles({
129
+ targetDir: srcDir,
130
+ outputDir: outputDirtoSrc
131
+ });
132
+ }
133
+ };
134
+
135
+ (async () => {
136
+ pluginNode.enable();
137
+ hooks.buildLifeCycle();
138
+ const {
139
+ resolved: modernConfig,
140
+ appContext
141
+ } = await core.cli.init();
142
+ await core.manager.run(async () => {
143
+ try {
144
+ await taskMain({
145
+ modernConfig,
146
+ appContext
147
+ });
148
+ } catch (e) {
149
+ console.error(e.toString());
150
+ }
151
+ });
152
+ })();
@@ -0,0 +1,144 @@
1
+ import { Import, fs } from '@modern-js/utils';
2
+ const babelCompiler = Import.lazy('@modern-js/babel-compiler', require);
3
+ const logger = Import.lazy('../features/build/logger', require);
4
+ const ts = Import.lazy('../utils/tsconfig', require);
5
+ const babel = Import.lazy('../utils/babel', require);
6
+ const core = Import.lazy('@modern-js/core', require);
7
+ const pluginNode = Import.lazy('@modern-js/plugin/node', require);
8
+ const argv = Import.lazy('process.argv', require);
9
+
10
+ const generatorRealFiles = virtualDists => {
11
+ for (const virtualDist of virtualDists) {
12
+ const {
13
+ distPath,
14
+ code,
15
+ sourcemap,
16
+ sourceMapPath
17
+ } = virtualDist;
18
+ fs.ensureFileSync(distPath);
19
+ fs.writeFileSync(distPath, code);
20
+
21
+ if (sourcemap.length > 0) {
22
+ fs.ensureFileSync(sourceMapPath);
23
+ fs.writeFileSync(sourceMapPath, sourcemap);
24
+ }
25
+ }
26
+ };
27
+
28
+ const runBabelCompiler = async (config, modernConfig) => {
29
+ const {
30
+ tsconfigPath
31
+ } = config;
32
+ const babelConfig = babel.resolveBabelConfig(config.appDirectory, modernConfig, {
33
+ sourceAbsDir: config.srcRootDir,
34
+ tsconfigPath,
35
+ syntax: config.syntax,
36
+ type: config.type
37
+ });
38
+ const tsconfig = ts.readTsConfig(tsconfigPath || '', {});
39
+ const isTs = Boolean(tsconfig);
40
+
41
+ const getExts = isTsProject => {
42
+ // TODO: 是否受控tsconfig.json 里的jsx配置
43
+ let exts = [];
44
+
45
+ if (isTsProject) {
46
+ var _tsconfig$compilerOpt;
47
+
48
+ exts = tsconfig !== null && tsconfig !== void 0 && (_tsconfig$compilerOpt = tsconfig.compilerOptions) !== null && _tsconfig$compilerOpt !== void 0 && _tsconfig$compilerOpt.allowJs ? ['.ts', '.tsx', '.js', '.jsx'] : ['.ts', '.tsx'];
49
+ } else {
50
+ exts = ['.js', '.jsx'];
51
+ }
52
+
53
+ return exts;
54
+ };
55
+
56
+ const emitter = await babelCompiler.compiler({
57
+ enableVirtualDist: true,
58
+ quiet: true,
59
+ enableWatch: true,
60
+ rootDir: config.srcRootDir,
61
+ distDir: config.distDir,
62
+ watchDir: config.srcRootDir,
63
+ extensions: getExts(isTs)
64
+ }, babelConfig);
65
+ emitter.on(babelCompiler.BuildWatchEvent.compiling, () => {
66
+ console.info(logger.clearFlag, `Compiling...`);
67
+ });
68
+ emitter.on(babelCompiler.BuildWatchEvent.firstCompiler, result => {
69
+ if (result.code === 1) {
70
+ console.error(logger.clearFlag);
71
+ console.error(result.message);
72
+
73
+ for (const detail of result.messageDetails || []) {
74
+ console.error(detail.content);
75
+ }
76
+ } else {
77
+ generatorRealFiles(result.virtualDists);
78
+ console.info(logger.clearFlag, '[Babel Compiler]: Successfully');
79
+ }
80
+ });
81
+ emitter.on(babelCompiler.BuildWatchEvent.watchingCompiler, result => {
82
+ if (result.code === 1) {
83
+ var _result$virtualDists;
84
+
85
+ console.error(logger.clearFlag);
86
+ console.error(result.message);
87
+
88
+ for (const detail of result.messageDetails || []) {
89
+ console.error(detail.content);
90
+ }
91
+
92
+ if (Array.isArray(result.virtualDists) && ((_result$virtualDists = result.virtualDists) === null || _result$virtualDists === void 0 ? void 0 : _result$virtualDists.length) > 0) {
93
+ generatorRealFiles(result.virtualDists);
94
+ }
95
+ } else {
96
+ generatorRealFiles(result.virtualDists);
97
+ console.info(result.message);
98
+ }
99
+ });
100
+ await emitter.watch();
101
+ };
102
+
103
+ const buildSourceCode = async (config, modernConfig) => {
104
+ const {
105
+ compiler
106
+ } = config;
107
+
108
+ if (compiler === 'babel') {
109
+ await runBabelCompiler(config, modernConfig);
110
+ }
111
+ };
112
+
113
+ const taskMain = async ({
114
+ modernConfig
115
+ }) => {
116
+ const processArgv = argv(process.argv.slice(2));
117
+ const config = processArgv({
118
+ srcRootDir: `${process.cwd()}/src`,
119
+ distDir: '',
120
+ compiler: 'babel',
121
+ appDirectory: '',
122
+ sourceMaps: false,
123
+ tsconfigPath: '',
124
+ syntax: 'es5',
125
+ type: 'module'
126
+ });
127
+ await buildSourceCode(config, modernConfig);
128
+ };
129
+
130
+ (async () => {
131
+ pluginNode.enable();
132
+ const {
133
+ resolved
134
+ } = await core.cli.init();
135
+ await core.manager.run(async () => {
136
+ try {
137
+ await taskMain({
138
+ modernConfig: resolved
139
+ });
140
+ } catch (e) {
141
+ console.error(e);
142
+ }
143
+ });
144
+ })();