@modern-js/module-tools 1.5.5 → 1.6.0-alpha.0

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 (116) hide show
  1. package/CHANGELOG.md +78 -0
  2. package/dist/js/modern/cli/build.js +5 -4
  3. package/dist/js/modern/commands/build.js +40 -27
  4. package/dist/js/modern/features/build/bundle/index.js +6 -0
  5. package/dist/js/modern/features/build/bundle/runRollup.js +149 -0
  6. package/dist/js/modern/features/build/bundle/runSpeedy.js +185 -0
  7. package/dist/js/modern/{tasks → features/build/bundleless}/copy-assets.js +31 -55
  8. package/dist/js/modern/{tasks → features/build/bundleless}/generator-dts/index.js +46 -70
  9. package/dist/js/modern/{tasks → features/build/bundleless}/generator-dts/utils.js +6 -6
  10. package/dist/js/modern/features/build/bundleless/index.js +13 -0
  11. package/dist/js/modern/features/build/bundleless/runBabel.js +269 -0
  12. package/dist/js/modern/features/build/bundleless/style.js +266 -0
  13. package/dist/js/modern/features/build/constants.js +94 -59
  14. package/dist/js/modern/features/build/error.js +150 -0
  15. package/dist/js/modern/features/build/index.js +84 -19
  16. package/dist/js/modern/features/build/legacy-constants.js +99 -0
  17. package/dist/js/modern/features/build/normalize.js +293 -0
  18. package/dist/js/modern/features/build/types.js +0 -0
  19. package/dist/js/modern/features/build/utils.js +29 -137
  20. package/dist/js/modern/index.js +3 -1
  21. package/dist/js/modern/locale/en.js +5 -3
  22. package/dist/js/modern/locale/zh.js +5 -3
  23. package/dist/js/modern/schema/build-config.js +104 -0
  24. package/dist/js/modern/schema/index.js +2 -1
  25. package/dist/js/modern/schema/output.js +18 -9
  26. package/dist/js/modern/schema/source.js +2 -10
  27. package/dist/js/modern/schema/tools.js +6 -0
  28. package/dist/js/modern/schema/types.js +0 -0
  29. package/dist/js/modern/utils/babel.js +6 -3
  30. package/dist/js/modern/utils/constants.js +3 -0
  31. package/dist/js/node/cli/build.js +5 -4
  32. package/dist/js/node/commands/build.js +50 -29
  33. package/dist/js/node/features/build/bundle/index.js +17 -0
  34. package/dist/js/node/features/build/bundle/runRollup.js +174 -0
  35. package/dist/js/node/features/build/bundle/runSpeedy.js +209 -0
  36. package/dist/js/node/{tasks → features/build/bundleless}/copy-assets.js +37 -55
  37. package/dist/js/node/{tasks → features/build/bundleless}/generator-dts/index.js +59 -73
  38. package/dist/js/node/{tasks → features/build/bundleless}/generator-dts/utils.js +6 -6
  39. package/dist/js/node/features/build/bundleless/index.js +26 -0
  40. package/dist/js/node/features/build/bundleless/runBabel.js +309 -0
  41. package/dist/js/node/features/build/bundleless/style.js +299 -0
  42. package/dist/js/node/features/build/constants.js +115 -67
  43. package/dist/js/node/features/build/error.js +175 -0
  44. package/dist/js/node/features/build/index.js +92 -19
  45. package/dist/js/node/features/build/legacy-constants.js +107 -0
  46. package/dist/js/node/features/build/normalize.js +322 -0
  47. package/dist/js/node/features/build/types.js +0 -0
  48. package/dist/js/node/features/build/utils.js +31 -145
  49. package/dist/js/node/index.js +5 -1
  50. package/dist/js/node/locale/en.js +5 -3
  51. package/dist/js/node/locale/zh.js +5 -3
  52. package/dist/js/node/schema/build-config.js +113 -0
  53. package/dist/js/node/schema/index.js +3 -1
  54. package/dist/js/node/schema/output.js +20 -9
  55. package/dist/js/node/schema/source.js +2 -10
  56. package/dist/js/node/schema/tools.js +13 -0
  57. package/dist/js/node/schema/types.js +0 -0
  58. package/dist/js/node/utils/babel.js +5 -2
  59. package/dist/js/node/utils/constants.js +11 -0
  60. package/dist/types/cli/build.d.ts +1 -1
  61. package/dist/types/cli/new.d.ts +1 -1
  62. package/dist/types/commands/build.d.ts +12 -12
  63. package/dist/types/features/build/bundle/index.d.ts +3 -0
  64. package/dist/types/features/build/bundle/runRollup.d.ts +3 -0
  65. package/dist/types/features/build/bundle/runSpeedy.d.ts +10 -0
  66. package/dist/types/features/build/bundleless/copy-assets.d.ts +3 -0
  67. package/dist/types/features/build/bundleless/generator-dts/index.d.ts +3 -0
  68. package/dist/types/{tasks → features/build/bundleless}/generator-dts/utils.d.ts +3 -6
  69. package/dist/types/features/build/bundleless/index.d.ts +4 -0
  70. package/dist/types/features/build/bundleless/runBabel.d.ts +32 -0
  71. package/dist/types/features/build/bundleless/style.d.ts +25 -0
  72. package/dist/types/features/build/constants.d.ts +17 -13
  73. package/dist/types/features/build/error.d.ts +33 -0
  74. package/dist/types/features/build/index.d.ts +4 -3
  75. package/dist/types/features/build/legacy-constants.d.ts +8 -0
  76. package/dist/types/features/build/logger/loggerManager.d.ts +2 -0
  77. package/dist/types/features/build/normalize.d.ts +18 -0
  78. package/dist/types/features/build/types.d.ts +15 -0
  79. package/dist/types/features/build/utils.d.ts +9 -31
  80. package/dist/types/locale/en.d.ts +2 -0
  81. package/dist/types/locale/index.d.ts +4 -0
  82. package/dist/types/locale/zh.d.ts +2 -0
  83. package/dist/types/schema/build-config.d.ts +179 -0
  84. package/dist/types/schema/index.d.ts +166 -18
  85. package/dist/types/schema/output.d.ts +170 -10
  86. package/dist/types/schema/source.d.ts +2 -11
  87. package/dist/types/schema/tools.d.ts +6 -0
  88. package/dist/types/schema/types.d.ts +45 -0
  89. package/dist/types/types.d.ts +29 -28
  90. package/dist/types/utils/babel.d.ts +3 -2
  91. package/dist/types/utils/constants.d.ts +2 -0
  92. package/dist/types/utils/logger.d.ts +2 -0
  93. package/package.json +29 -16
  94. package/dist/js/modern/features/build/build-watch.js +0 -101
  95. package/dist/js/modern/features/build/build.js +0 -123
  96. package/dist/js/modern/tasks/build-source-code.js +0 -207
  97. package/dist/js/modern/tasks/build-style.js +0 -168
  98. package/dist/js/modern/tasks/build-watch-source-code.js +0 -161
  99. package/dist/js/modern/tasks/build-watch-style.js +0 -215
  100. package/dist/js/modern/tasks/constants.js +0 -1
  101. package/dist/js/node/features/build/build-watch.js +0 -121
  102. package/dist/js/node/features/build/build.js +0 -143
  103. package/dist/js/node/tasks/build-source-code.js +0 -233
  104. package/dist/js/node/tasks/build-style.js +0 -183
  105. package/dist/js/node/tasks/build-watch-source-code.js +0 -173
  106. package/dist/js/node/tasks/build-watch-style.js +0 -232
  107. package/dist/js/node/tasks/constants.js +0 -8
  108. package/dist/types/features/build/build-watch.d.ts +0 -3
  109. package/dist/types/features/build/build.d.ts +0 -3
  110. package/dist/types/tasks/build-source-code.d.ts +0 -25
  111. package/dist/types/tasks/build-style.d.ts +0 -1
  112. package/dist/types/tasks/build-watch-source-code.d.ts +0 -1
  113. package/dist/types/tasks/build-watch-style.d.ts +0 -1
  114. package/dist/types/tasks/constants.d.ts +0 -1
  115. package/dist/types/tasks/copy-assets.d.ts +0 -1
  116. package/dist/types/tasks/generator-dts/index.d.ts +0 -1
@@ -0,0 +1,309 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.runBabelBuild = exports.jsFileSuffix = exports.haveNotAnyJsFile = exports.getWillCompilerCode = exports.buildSourceCode = exports.BabelBuildError = void 0;
7
+
8
+ var _path = _interopRequireDefault(require("path"));
9
+
10
+ var _utils = require("@modern-js/utils");
11
+
12
+ var _error = require("../error");
13
+
14
+ var _utils2 = require("../utils");
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
19
+
20
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
21
+
22
+ 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; }
23
+
24
+ class BabelBuildError extends Error {
25
+ constructor(message, opts) {
26
+ super(message);
27
+
28
+ _defineProperty(this, "summary", void 0);
29
+
30
+ _defineProperty(this, "details", void 0);
31
+
32
+ Error.captureStackTrace(this, this.constructor);
33
+ this.summary = opts === null || opts === void 0 ? void 0 : opts.summary;
34
+ this.details = opts === null || opts === void 0 ? void 0 : opts.details;
35
+ }
36
+
37
+ toString() {
38
+ return this.formatError().join('\n');
39
+ }
40
+
41
+ formatError() {
42
+ const msgs = [];
43
+ const {
44
+ summary,
45
+ details = []
46
+ } = this;
47
+ msgs.push(_utils.chalk.red.bold(summary));
48
+
49
+ for (const detail of details) {
50
+ msgs.push(detail.content);
51
+ msgs.push('\n');
52
+ }
53
+
54
+ return msgs;
55
+ }
56
+
57
+ }
58
+
59
+ exports.BabelBuildError = BabelBuildError;
60
+
61
+ const babelCompiler = _utils.Import.lazy('@modern-js/babel-compiler', require);
62
+
63
+ const bc = _utils.Import.lazy('../../../utils/babel', require);
64
+
65
+ const ts = _utils.Import.lazy('../../../utils/tsconfig', require); // const logger: typeof import('../logger') = Import.lazy('../logger', require);
66
+
67
+
68
+ const getExts = (isTs, tsconfig) => {
69
+ // TODO: 是否受控tsconfig.json 里的jsx配置
70
+ let exts = [];
71
+
72
+ if (isTs) {
73
+ var _tsconfig$compilerOpt;
74
+
75
+ exts = tsconfig !== null && tsconfig !== void 0 && (_tsconfig$compilerOpt = tsconfig.compilerOptions) !== null && _tsconfig$compilerOpt !== void 0 && _tsconfig$compilerOpt.allowJs ? ['.ts', '.tsx', '.js', '.jsx'] : ['.ts', '.tsx'];
76
+ } else {
77
+ exts = ['.js', '.jsx'];
78
+ }
79
+
80
+ return exts;
81
+ };
82
+
83
+ const getWillCompilerCode = (srcDirOrFile, option) => {
84
+ const {
85
+ tsconfig,
86
+ isTsProject
87
+ } = option; // 如果是一个文件路径,则直接返回
88
+
89
+ if (_utils.fs.existsSync(srcDirOrFile) && _utils.fs.lstatSync(srcDirOrFile).isFile()) {
90
+ return [srcDirOrFile];
91
+ }
92
+
93
+ const exts = getExts(isTsProject, tsconfig);
94
+ const globPattern = `${srcDirOrFile}/**/*{${exts.join(',')}}`;
95
+
96
+ const files = _utils.glob.sync(globPattern, {
97
+ ignore: [`${srcDirOrFile}/**/*.d.ts`],
98
+ absolute: true
99
+ });
100
+
101
+ return files;
102
+ };
103
+
104
+ exports.getWillCompilerCode = getWillCompilerCode;
105
+
106
+ const buildSourceCode = async config => {
107
+ const {
108
+ willCompilerDirOrFile,
109
+ tsconfigPath,
110
+ babelConfig,
111
+ srcRootDir,
112
+ distDir,
113
+ watch
114
+ } = config;
115
+ const tsconfig = ts.readTsConfig(tsconfigPath);
116
+ const willCompilerFiles = getWillCompilerCode(willCompilerDirOrFile, {
117
+ tsconfig,
118
+ isTsProject: Boolean(tsconfig)
119
+ });
120
+
121
+ if (watch) {
122
+ const emitter = await babelCompiler.compiler({
123
+ quiet: true,
124
+ enableVirtualDist: true,
125
+ enableWatch: true,
126
+ rootDir: srcRootDir,
127
+ filenames: willCompilerFiles,
128
+ distDir,
129
+ watchDir: srcRootDir,
130
+ extensions: getExts(Boolean(tsconfig), tsconfig),
131
+ ignore: ['*.d.ts']
132
+ }, babelConfig);
133
+ return emitter;
134
+ } else {
135
+ return babelCompiler.compiler({
136
+ quiet: true,
137
+ enableVirtualDist: true,
138
+ rootDir: srcRootDir,
139
+ filenames: willCompilerFiles,
140
+ distDir,
141
+ // enableWatch: watch,
142
+ ignore: ['*.d.ts']
143
+ }, babelConfig);
144
+ }
145
+ };
146
+
147
+ exports.buildSourceCode = buildSourceCode;
148
+
149
+ const generatorRealFiles = virtualDists => {
150
+ for (const virtualDist of virtualDists) {
151
+ const {
152
+ distPath,
153
+ code,
154
+ sourcemap,
155
+ sourceMapPath
156
+ } = virtualDist;
157
+
158
+ _utils.fs.ensureFileSync(distPath);
159
+
160
+ _utils.fs.writeFileSync(distPath, code);
161
+
162
+ if (sourcemap.length > 0) {
163
+ _utils.fs.ensureFileSync(sourceMapPath);
164
+
165
+ _utils.fs.writeFileSync(sourceMapPath, sourcemap);
166
+ }
167
+ }
168
+ };
169
+ /**
170
+ * when modern build, only throw Error or silent
171
+ * @param outputResults
172
+ * @param context
173
+ */
174
+
175
+
176
+ const outputDist = (outputResults, context) => {
177
+ const {
178
+ code,
179
+ message,
180
+ messageDetails,
181
+ virtualDists = []
182
+ } = outputResults;
183
+
184
+ if (code === 0) {
185
+ generatorRealFiles(virtualDists);
186
+ } else if (messageDetails && messageDetails.length > 0) {
187
+ const babelError = new BabelBuildError('bundleless failed', {
188
+ summary: message,
189
+ details: messageDetails
190
+ });
191
+ throw new _error.InternalBuildError(babelError, _objectSpread(_objectSpread({}, context), {}, {
192
+ buildType: 'bundleless'
193
+ }));
194
+ }
195
+ };
196
+
197
+ const jsFileSuffix = ['js', 'jsx', 'ts', 'tsx'];
198
+ exports.jsFileSuffix = jsFileSuffix;
199
+
200
+ const haveNotAnyJsFile = async sourceDir => {
201
+ const files = await (0, _utils.globby)(`${sourceDir}/**/*.{${jsFileSuffix.join(',')}}`);
202
+ return files.length === 0;
203
+ };
204
+
205
+ exports.haveNotAnyJsFile = haveNotAnyJsFile;
206
+
207
+ const runBabelBuild = async (api, config) => {
208
+ const {
209
+ bundlelessOptions,
210
+ tsconfig,
211
+ target,
212
+ format,
213
+ outputPath,
214
+ watch,
215
+ sourceMap
216
+ } = config;
217
+ const {
218
+ appDirectory
219
+ } = api.useAppContext();
220
+ const modernConfig = api.useResolvedConfigContext();
221
+ const {
222
+ output: {
223
+ path: distPath = 'dist'
224
+ }
225
+ } = modernConfig;
226
+ const {
227
+ sourceDir = './src'
228
+ } = bundlelessOptions;
229
+
230
+ const sourceAbsDir = _path.default.join(appDirectory, sourceDir);
231
+
232
+ const tsconfigPath = _path.default.join(appDirectory, tsconfig);
233
+
234
+ if (await haveNotAnyJsFile(sourceAbsDir)) {
235
+ return;
236
+ } // TODO: Refactoring based on format and target
237
+
238
+
239
+ const syntax = target === 'es5' ? 'es5' : 'es6+';
240
+ const type = format === 'cjs' ? 'commonjs' : 'module';
241
+ const titleText = `[Bundleless:Babel: ${format}_${target}]`;
242
+ const buildConfig = {
243
+ format,
244
+ target,
245
+ babelConfig: bc.resolveBabelConfig(appDirectory, modernConfig, sourceMap, bundlelessOptions, {
246
+ sourceAbsDir,
247
+ tsconfigPath,
248
+ syntax,
249
+ type
250
+ })
251
+ };
252
+
253
+ const distDir = _path.default.join(appDirectory, distPath, outputPath);
254
+
255
+ const result = await buildSourceCode({
256
+ appDirectory,
257
+ distDir,
258
+ srcRootDir: sourceAbsDir,
259
+ willCompilerDirOrFile: sourceAbsDir,
260
+ tsconfigPath,
261
+ babelConfig: buildConfig.babelConfig,
262
+ watch
263
+ });
264
+
265
+ if (watch) {
266
+ const emitter = result;
267
+ emitter.on(babelCompiler.BuildWatchEvent.firstCompiler, result => {
268
+ if (result.code === 1) {
269
+ console.info((0, _utils2.watchSectionTitle)(titleText, _utils2.SectionTitleStatus.Fail));
270
+ console.error(result.message);
271
+
272
+ for (const detail of result.messageDetails || []) {
273
+ console.error(detail.content);
274
+ }
275
+ } else {
276
+ generatorRealFiles(result.virtualDists);
277
+ console.info((0, _utils2.watchSectionTitle)(titleText, _utils2.SectionTitleStatus.Success));
278
+ }
279
+ });
280
+ emitter.on(babelCompiler.BuildWatchEvent.watchingCompiler, result => {
281
+ if (result.code === 1) {
282
+ var _result$virtualDists;
283
+
284
+ // console.error(logger.clearFlag);
285
+ console.info((0, _utils2.watchSectionTitle)(titleText, _utils2.SectionTitleStatus.Fail));
286
+ console.error(result.message);
287
+
288
+ for (const detail of result.messageDetails || []) {
289
+ console.error(detail.content);
290
+ }
291
+
292
+ if (Array.isArray(result.virtualDists) && ((_result$virtualDists = result.virtualDists) === null || _result$virtualDists === void 0 ? void 0 : _result$virtualDists.length) > 0) {
293
+ generatorRealFiles(result.virtualDists);
294
+ }
295
+ } else {
296
+ generatorRealFiles(result.virtualDists);
297
+ console.info((0, _utils2.watchSectionTitle)(titleText, _utils2.SectionTitleStatus.Success));
298
+ }
299
+ });
300
+ await emitter.watch();
301
+ } else {
302
+ outputDist(result, {
303
+ target,
304
+ format
305
+ });
306
+ }
307
+ };
308
+
309
+ exports.runBabelBuild = runBabelBuild;
@@ -0,0 +1,299 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.styleFileSuffix = exports.runBuild = exports.haveNotAnyStyles = exports.buildStyle = exports.StyleBuildError = void 0;
7
+
8
+ var path = _interopRequireWildcard(require("path"));
9
+
10
+ var _utils = require("@modern-js/utils");
11
+
12
+ var _error = require("../error");
13
+
14
+ var _utils2 = require("../utils");
15
+
16
+ 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); }
17
+
18
+ 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; }
19
+
20
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
21
+
22
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
23
+
24
+ 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; }
25
+
26
+ const compiler = _utils.Import.lazy('@modern-js/style-compiler', require);
27
+
28
+ class StyleBuildError extends Error {
29
+ constructor(message, opts) {
30
+ super(message);
31
+
32
+ _defineProperty(this, "summary", void 0);
33
+
34
+ _defineProperty(this, "details", void 0);
35
+
36
+ Error.captureStackTrace(this, this.constructor);
37
+ this.summary = opts === null || opts === void 0 ? void 0 : opts.summary;
38
+ this.details = opts === null || opts === void 0 ? void 0 : opts.details;
39
+ }
40
+
41
+ toString() {
42
+ return this.formatError().join('\n');
43
+ }
44
+
45
+ formatError() {
46
+ const msgs = [];
47
+ const {
48
+ summary,
49
+ details = []
50
+ } = this;
51
+ msgs.push(_utils.chalk.red.bold(summary));
52
+
53
+ for (const detail of details) {
54
+ if (detail.error) {
55
+ msgs.push(detail.error);
56
+ msgs.push('\n');
57
+ }
58
+ }
59
+
60
+ return msgs;
61
+ }
62
+
63
+ }
64
+ /**
65
+ * when modern build, only throw Error or silent
66
+ * @param result
67
+ * @param context
68
+ */
69
+
70
+
71
+ exports.StyleBuildError = StyleBuildError;
72
+
73
+ const generatorFileOrLogError = (result, context) => {
74
+ if (result.code === 0) {
75
+ for (const file of result.dists) {
76
+ _utils.fs.ensureFileSync(file.filename);
77
+
78
+ _utils.fs.writeFileSync(file.filename, file.content);
79
+ }
80
+ } else {
81
+ // for (const file of result.errors) {
82
+ // console.error(file.error);
83
+ // }
84
+ const styleError = new StyleBuildError('bundleless failed', {
85
+ summary: 'Style Compiler Failed',
86
+ details: result.errors
87
+ });
88
+ throw new _error.InternalBuildError(styleError, _objectSpread({
89
+ buildType: 'bundleless'
90
+ }, context));
91
+ }
92
+ };
93
+
94
+ const generatorFileAndLog = (result, titleText) => {
95
+ if (result.code === 0) {
96
+ for (const file of result.dists) {
97
+ _utils.fs.ensureFileSync(file.filename);
98
+
99
+ _utils.fs.writeFileSync(file.filename, file.content);
100
+ }
101
+
102
+ console.info((0, _utils2.watchSectionTitle)(titleText, _utils2.SectionTitleStatus.Success));
103
+ } else {
104
+ console.info((0, _utils2.watchSectionTitle)(titleText, _utils2.SectionTitleStatus.Fail));
105
+
106
+ for (const file of result.errors) {
107
+ console.error(file.error);
108
+ }
109
+ }
110
+ };
111
+
112
+ const copyOriginStyleFiles = ({
113
+ targetDir,
114
+ outputDir
115
+ }) => {
116
+ const styleFiles = _utils.glob.sync(`${targetDir}/**/*.{css,sass,scss,less}`);
117
+
118
+ if (styleFiles.length > 0) {
119
+ _utils.fs.ensureDirSync(outputDir);
120
+ }
121
+
122
+ for (const styleFile of styleFiles) {
123
+ const file = path.relative(targetDir, styleFile);
124
+
125
+ _utils.fs.ensureDirSync(path.dirname(path.join(outputDir, file)));
126
+
127
+ _utils.fs.copyFileSync(styleFile, path.join(outputDir, file));
128
+ }
129
+ };
130
+
131
+ const runBuild = async option => {
132
+ const {
133
+ watch,
134
+ appDirectory,
135
+ srcDir,
136
+ outDir,
137
+ lessOption,
138
+ sassOption,
139
+ postcssOption
140
+ } = option;
141
+
142
+ if (watch) {
143
+ const srcStyleEmitter = compiler.styleCompiler({
144
+ watch: true,
145
+ projectDir: appDirectory,
146
+ stylesDir: srcDir,
147
+ outDir,
148
+ enableVirtualDist: true,
149
+ compilerOption: {
150
+ less: lessOption,
151
+ sass: sassOption,
152
+ postcss: postcssOption
153
+ }
154
+ });
155
+ return srcStyleEmitter;
156
+ } else {
157
+ const srcStyleResult = await compiler.styleCompiler({
158
+ projectDir: appDirectory,
159
+ stylesDir: srcDir,
160
+ outDir,
161
+ enableVirtualDist: true,
162
+ compilerOption: {
163
+ less: lessOption,
164
+ sass: sassOption,
165
+ postcss: postcssOption
166
+ }
167
+ });
168
+ return srcStyleResult;
169
+ }
170
+ };
171
+
172
+ exports.runBuild = runBuild;
173
+ const styleFileSuffix = ['css', 'less', 'sass', 'scss'];
174
+ exports.styleFileSuffix = styleFileSuffix;
175
+
176
+ const haveNotAnyStyles = async sourceDir => {
177
+ const files = await (0, _utils.globby)(`${sourceDir}/**/*.{${styleFileSuffix.join(',')}}`);
178
+ return files.length === 0;
179
+ };
180
+
181
+ exports.haveNotAnyStyles = haveNotAnyStyles;
182
+
183
+ const buildStyle = async (api, config) => {
184
+ var _style$path;
185
+
186
+ const modernConfig = api.useResolvedConfigContext();
187
+ const {
188
+ appDirectory
189
+ } = api.useAppContext();
190
+ const {
191
+ watch = false,
192
+ outputPath,
193
+ bundlelessOptions
194
+ } = config;
195
+ const {
196
+ style,
197
+ sourceDir
198
+ } = bundlelessOptions;
199
+ const {
200
+ output: {
201
+ path: distPath = 'dist'
202
+ }
203
+ } = modernConfig;
204
+ const titleText = `[Bundleless:Style:${sourceDir}]`;
205
+
206
+ if ((await haveNotAnyStyles(sourceDir)) || style.compileMode === false) {
207
+ return;
208
+ }
209
+
210
+ const runner = api.useHookRunners();
211
+ const lessOption = await runner.moduleLessConfig({
212
+ modernConfig
213
+ }, {
214
+ onLast: async _ => undefined
215
+ });
216
+ const sassOption = await runner.moduleSassConfig({
217
+ modernConfig
218
+ }, {
219
+ onLast: async _ => undefined
220
+ });
221
+ const tailwindPlugin = await runner.moduleTailwindConfig({
222
+ modernConfig
223
+ }, {
224
+ onLast: async _ => undefined
225
+ });
226
+ const postcssOption = (0, _utils2.getPostcssOption)(appDirectory, modernConfig);
227
+
228
+ if (tailwindPlugin) {
229
+ var _postcssOption$plugin;
230
+
231
+ (_postcssOption$plugin = postcssOption.plugins) === null || _postcssOption$plugin === void 0 ? void 0 : _postcssOption$plugin.push(tailwindPlugin);
232
+ }
233
+
234
+ const srcDir = path.resolve(appDirectory, sourceDir);
235
+ const outputDirToSrc = path.join(appDirectory, distPath, outputPath, (_style$path = style.path) !== null && _style$path !== void 0 ? _style$path : './');
236
+
237
+ if (style.compileMode === 'all' || style.compileMode === 'only-compiled-code') {
238
+ const result = await runBuild({
239
+ appDirectory,
240
+ srcDir,
241
+ watch,
242
+ outDir: outputDirToSrc,
243
+ lessOption,
244
+ sassOption,
245
+ postcssOption
246
+ });
247
+
248
+ if (watch) {
249
+ const emitter = result;
250
+ emitter.on(compiler.BuildWatchEvent.firstCompiler, result => {
251
+ generatorFileAndLog(result, titleText);
252
+ });
253
+ emitter.on(compiler.BuildWatchEvent.watchingCompiler, srcStyleResult => {
254
+ generatorFileAndLog(srcStyleResult, titleText);
255
+ });
256
+ await emitter.watch();
257
+ } else {
258
+ const srcStyleResult = result;
259
+ generatorFileOrLogError(srcStyleResult, {
260
+ target: config.target,
261
+ format: config.format
262
+ });
263
+ }
264
+ }
265
+
266
+ if (style.compileMode === 'all' || style.compileMode === 'only-source-code') {
267
+ if (watch) {
268
+ copyOriginStyleFiles({
269
+ targetDir: srcDir,
270
+ outputDir: outputDirToSrc
271
+ });
272
+ console.info((0, _utils2.watchSectionTitle)(titleText, _utils2.SectionTitleStatus.Success));
273
+ (0, _utils.watch)(`${srcDir}/**/*.{css,less,sass,scss}`, ({
274
+ changeType,
275
+ changedFilePath
276
+ }) => {
277
+ if (changeType === _utils.WatchChangeType.UNLINK) {
278
+ const removeFile = path.normalize(`${outputDirToSrc}/${path.relative(srcDir, changedFilePath)}`);
279
+
280
+ _utils.fs.removeSync(removeFile);
281
+ } else {
282
+ copyOriginStyleFiles({
283
+ targetDir: srcDir,
284
+ outputDir: outputDirToSrc
285
+ });
286
+ }
287
+
288
+ console.info((0, _utils2.watchSectionTitle)(titleText, _utils2.SectionTitleStatus.Success));
289
+ });
290
+ } else {
291
+ copyOriginStyleFiles({
292
+ targetDir: srcDir,
293
+ outputDir: outputDirToSrc
294
+ });
295
+ }
296
+ }
297
+ };
298
+
299
+ exports.buildStyle = buildStyle;