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