@modern-js/module-tools 1.5.7 → 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 +34 -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 +4 -1
  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 +4 -1
  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 +28 -15
  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
@@ -1,143 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.buildSourceCode = void 0;
7
-
8
- var path = _interopRequireWildcard(require("path"));
9
-
10
- var os = _interopRequireWildcard(require("os"));
11
-
12
- var _utils = require("@modern-js/utils");
13
-
14
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
-
16
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
-
18
- const pMap = _utils.Import.lazy('p-map', require);
19
-
20
- const utils = _utils.Import.lazy('./utils', require);
21
-
22
- const lg = _utils.Import.lazy('./logger', require);
23
-
24
- const constants = _utils.Import.lazy('./constants', require);
25
-
26
- const buildSourceCode = async (api, config, _) => {
27
- const {
28
- sourceDir,
29
- enableTscCompiler,
30
- styleOnly
31
- } = config;
32
- const {
33
- appDirectory
34
- } = api.useAppContext();
35
- const concurrency = os.cpus().length;
36
- const srcRootDir = path.join(appDirectory, sourceDir);
37
- const lm = new lg.LoggerManager();
38
- const codeLog = lm.createLoggerText({
39
- title: constants.runBabelCompilerTitle
40
- });
41
- const dtsLog = lm.createLoggerText({
42
- title: constants.runTscTitle
43
- });
44
- const styleLog = lm.createLoggerText({
45
- title: constants.runStyleCompilerTitle
46
- });
47
- const copyLog = lm.createLoggerText({
48
- title: 'Copy Log:'
49
- });
50
- const initCodeMapper = utils.getCodeInitMapper(api, config);
51
- let taskMapper = [];
52
-
53
- if (styleOnly) {
54
- taskMapper = [{
55
- logger: styleLog,
56
- taskPath: require.resolve("../../tasks/build-style")
57
- }, {
58
- logger: copyLog,
59
- taskPath: require.resolve("../../tasks/copy-assets")
60
- }];
61
- } else {
62
- taskMapper = [...utils.getCodeMapper(api, {
63
- logger: codeLog,
64
- taskPath: require.resolve("../../tasks/build-source-code"),
65
- config,
66
- willCompilerDirOrFile: sourceDir,
67
- initMapper: initCodeMapper,
68
- srcRootDir
69
- }), ...(enableTscCompiler ? utils.getDtsMapper(api, config, dtsLog) : []), {
70
- logger: styleLog,
71
- taskPath: require.resolve("../../tasks/build-style")
72
- }, {
73
- logger: copyLog,
74
- taskPath: require.resolve("../../tasks/copy-assets")
75
- }];
76
- }
77
-
78
- lm.showCompiling();
79
- await pMap(taskMapper, async ({
80
- logger,
81
- taskPath,
82
- params
83
- }) => {
84
- const childProcess = _utils.execa.node(taskPath, params, {
85
- stdio: 'pipe'
86
- });
87
-
88
- if (logger === codeLog || logger === copyLog) {
89
- lm.addStdout(logger, childProcess.stdout, {
90
- event: {
91
- data: true,
92
- error: true
93
- }
94
- });
95
- lm.addStderr(logger, childProcess.stderr);
96
- }
97
-
98
- if (logger === dtsLog) {
99
- lm.addStdout(dtsLog, childProcess.stdout, {
100
- event: {
101
- data: true,
102
- error: true
103
- }
104
- });
105
- lm.addStderr(dtsLog, childProcess.stderr);
106
- }
107
-
108
- if (logger === styleLog) {
109
- lm.addStdout(logger, childProcess.stdout, {
110
- event: {
111
- data: true,
112
- error: true
113
- }
114
- });
115
- lm.addStderr(logger, childProcess.stderr);
116
- }
117
-
118
- await childProcess;
119
- }, {
120
- concurrency
121
- });
122
- lm.disappearCompiling();
123
-
124
- if (!styleOnly) {
125
- enableTscCompiler && console.info(dtsLog.value);
126
- console.info(codeLog.value);
127
- }
128
-
129
- if (styleLog.hasMessages()) {
130
- console.info(styleLog.value);
131
- }
132
-
133
- if (copyLog.hasMessages()) {
134
- console.info(copyLog.value);
135
- }
136
-
137
- if (dtsLog.hasErrorMessage || codeLog.hasErrorMessage || styleLog.hasErrorMessage || copyLog.hasErrorMessage) {
138
- // eslint-disable-next-line no-process-exit
139
- process.exit(1);
140
- }
141
- };
142
-
143
- exports.buildSourceCode = buildSourceCode;
@@ -1,233 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getWillCompilerCode = exports.buildSourceCode = exports.Compiler = void 0;
7
-
8
- var _utils = require("@modern-js/utils");
9
-
10
- var _initEnv = require("../utils/init-env");
11
-
12
- 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; }
13
-
14
- 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; }
15
-
16
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
-
18
- const babelCompiler = _utils.Import.lazy('@modern-js/babel-compiler', require);
19
-
20
- const argv = _utils.Import.lazy('process.argv', require);
21
-
22
- const core = _utils.Import.lazy('@modern-js/core', require);
23
-
24
- const bc = _utils.Import.lazy('../utils/babel', require);
25
-
26
- const ts = _utils.Import.lazy('../utils/tsconfig', require);
27
-
28
- let Compiler;
29
- exports.Compiler = Compiler;
30
-
31
- (function (Compiler) {
32
- Compiler[Compiler["babel"] = 0] = "babel";
33
- Compiler[Compiler["esbuild"] = 1] = "esbuild";
34
- Compiler[Compiler["swc"] = 2] = "swc";
35
- })(Compiler || (exports.Compiler = Compiler = {}));
36
-
37
- const runBabelCompiler = async (willCompilerFiles, config, babelConfig = {}) => {
38
- const {
39
- srcRootDir,
40
- distDir
41
- } = config; // TODO: 判断lynx模式下,修改distFileExtMap: {'js': 'js', 'jsx': 'jsx', 'ts': 'js', 'tsx': 'jsx'}
42
-
43
- return babelCompiler.compiler({
44
- quiet: true,
45
- enableVirtualDist: true,
46
- rootDir: srcRootDir,
47
- filenames: willCompilerFiles,
48
- distDir,
49
- ignore: ['*.d.ts']
50
- }, babelConfig);
51
- };
52
-
53
- const getWillCompilerCode = (srcDirOrFile, option) => {
54
- const {
55
- tsconfig,
56
- isTsProject
57
- } = option; // 如果是一个文件路径,则直接返回
58
-
59
- if (_utils.fs.existsSync(srcDirOrFile) && _utils.fs.lstatSync(srcDirOrFile).isFile()) {
60
- return [srcDirOrFile];
61
- }
62
-
63
- const getExts = isTs => {
64
- // TODO: 是否受控tsconfig.json 里的jsx配置
65
- let exts = [];
66
-
67
- if (isTs) {
68
- var _tsconfig$compilerOpt;
69
-
70
- exts = tsconfig !== null && tsconfig !== void 0 && (_tsconfig$compilerOpt = tsconfig.compilerOptions) !== null && _tsconfig$compilerOpt !== void 0 && _tsconfig$compilerOpt.allowJs ? ['.ts', '.tsx', '.js', '.jsx'] : ['.ts', '.tsx'];
71
- } else {
72
- exts = ['.js', '.jsx'];
73
- }
74
-
75
- return exts;
76
- };
77
-
78
- const exts = getExts(isTsProject);
79
- const globPattern = `${srcDirOrFile}/**/*{${exts.join(',')}}`;
80
-
81
- const files = _utils.glob.sync(globPattern, {
82
- ignore: [`${srcDirOrFile}/**/*.d.ts`],
83
- absolute: true
84
- });
85
-
86
- return files;
87
- };
88
-
89
- exports.getWillCompilerCode = getWillCompilerCode;
90
-
91
- const buildSourceCode = async config => {
92
- const {
93
- compiler,
94
- willCompilerDirOrFile,
95
- tsconfigPath,
96
- sourceMaps,
97
- babelConfig
98
- } = config;
99
- const tsconfig = ts.readTsConfig(tsconfigPath);
100
- const willCompilerFiles = getWillCompilerCode(willCompilerDirOrFile, {
101
- tsconfig,
102
- isTsProject: Boolean(tsconfig)
103
- });
104
- let distSet = null;
105
-
106
- if (compiler === Compiler.babel) {
107
- distSet = await runBabelCompiler(willCompilerFiles, config, _objectSpread(_objectSpread({}, babelConfig), {}, {
108
- sourceMaps
109
- }));
110
- } else {
111
- distSet = {
112
- code: 1,
113
- message: 'no compiler'
114
- };
115
- }
116
-
117
- return distSet;
118
- };
119
-
120
- exports.buildSourceCode = buildSourceCode;
121
-
122
- const generatorRealFiles = virtualDists => {
123
- for (const virtualDist of virtualDists) {
124
- const {
125
- distPath,
126
- code,
127
- sourcemap,
128
- sourceMapPath
129
- } = virtualDist;
130
-
131
- _utils.fs.ensureFileSync(distPath);
132
-
133
- _utils.fs.writeFileSync(distPath, code);
134
-
135
- if (sourcemap.length > 0) {
136
- _utils.fs.ensureFileSync(sourceMapPath);
137
-
138
- _utils.fs.writeFileSync(sourceMapPath, sourcemap);
139
- }
140
- }
141
- };
142
-
143
- const defaultConfig = {
144
- srcRootDir: `${process.cwd()}/src`,
145
- willCompilerDirOrFile: `${process.cwd()}/src`,
146
- distDir: './dist/js/temp',
147
- compiler: 'babel',
148
- appDirectory: '',
149
- sourceMaps: false,
150
- tsconfigPath: '',
151
- syntax: 'es5',
152
- type: 'module',
153
- watch: false
154
- };
155
-
156
- const taskMain = async ({
157
- modernConfig
158
- }) => {
159
- // Execution of the script's parameter handling and related required configuration acquisition
160
- const processArgv = argv(process.argv.slice(2));
161
- const config = processArgv(defaultConfig);
162
- process.env.BUILD_FORMAT = (0, _initEnv.initEnv)(config);
163
- const compiler = Compiler.babel; // Currently, only babel is supported.
164
-
165
- const babelConfig = bc.resolveBabelConfig(config.appDirectory, modernConfig, {
166
- sourceAbsDir: config.srcRootDir,
167
- tsconfigPath: config.tsconfigPath,
168
- syntax: config.syntax,
169
- type: config.type
170
- });
171
- const {
172
- code,
173
- message,
174
- messageDetails,
175
- virtualDists = []
176
- } = await buildSourceCode({
177
- distDir: config.distDir,
178
- srcRootDir: config.srcRootDir,
179
- willCompilerDirOrFile: config.willCompilerDirOrFile,
180
- sourceMaps: config.sourceMaps,
181
- compiler,
182
- projectData: {
183
- appDirectory: config.appDirectory
184
- },
185
- tsconfigPath: config.tsconfigPath,
186
- babelConfig
187
- });
188
-
189
- if (code === 0) {
190
- generatorRealFiles(virtualDists); // 执行成功log使用 console.info
191
-
192
- console.info('[Babel Compiler]: Successfully');
193
- } else if (messageDetails && messageDetails.length > 0) {
194
- console.error(message);
195
-
196
- for (const detail of messageDetails || []) {
197
- console.error(detail.content);
198
- }
199
- }
200
-
201
- if (code === 0 && config.copyDirs) {
202
- const copyList = config.copyDirs.split(',');
203
-
204
- for (const copyDir of copyList) {
205
- _utils.fs.ensureDirSync(copyDir);
206
-
207
- _utils.fs.copySync(config.distDir, copyDir);
208
- }
209
- }
210
- };
211
-
212
- (async () => {
213
- let options;
214
-
215
- if (process.env.CORE_INIT_OPTION_FILE) {
216
- ({
217
- options
218
- } = require(process.env.CORE_INIT_OPTION_FILE));
219
- }
220
-
221
- const {
222
- resolved
223
- } = await core.cli.init([], options);
224
- await core.manager.run(async () => {
225
- try {
226
- await taskMain({
227
- modernConfig: resolved
228
- });
229
- } catch (e) {
230
- console.error(e);
231
- }
232
- });
233
- })();
@@ -1,183 +0,0 @@
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 hooks = _utils.Import.lazy('../hooks', require);
18
-
19
- const STYLE_DIRS = 'styles';
20
- const SRC_STYLE_DIRS = 'src';
21
-
22
- const checkStylesDirExist = option => {
23
- const {
24
- appDirectory
25
- } = option;
26
- return _utils.fs.existsSync(path.join(appDirectory, STYLE_DIRS));
27
- };
28
-
29
- const generatorFileOrLogError = (result, successMessage = '') => {
30
- if (result.code === 0 && result.dists.length > 0) {
31
- for (const file of result.dists) {
32
- _utils.fs.ensureFileSync(file.filename);
33
-
34
- _utils.fs.writeFileSync(file.filename, file.content);
35
- }
36
-
37
- if (successMessage) {
38
- console.info(successMessage);
39
- }
40
- } else {
41
- for (const file of result.errors) {
42
- console.error(file.error);
43
- }
44
- }
45
- };
46
-
47
- const getPostcssOption = (appDirectory, modernConfig) => {
48
- var _postcssOption$postcs;
49
-
50
- const postcssOption = cssConfig.getPostcssConfig(appDirectory, modernConfig, false);
51
- return {
52
- plugins: (postcssOption === null || postcssOption === void 0 ? void 0 : (_postcssOption$postcs = postcssOption.postcssOptions) === null || _postcssOption$postcs === void 0 ? void 0 : _postcssOption$postcs.plugins) || [],
53
- enableSourceMap: (postcssOption === null || postcssOption === void 0 ? void 0 : postcssOption.sourceMap) || false,
54
- options: {}
55
- };
56
- };
57
-
58
- const copyOriginStyleFiles = ({
59
- targetDir,
60
- outputDir
61
- }) => {
62
- const styleFiles = _utils.glob.sync(`${targetDir}/**/*.{css,sass,scss,less}`);
63
-
64
- if (styleFiles.length > 0) {
65
- _utils.fs.ensureDirSync(outputDir);
66
- }
67
-
68
- for (const styleFile of styleFiles) {
69
- const file = path.relative(targetDir, styleFile);
70
-
71
- _utils.fs.ensureDirSync(path.dirname(path.join(outputDir, file)));
72
-
73
- _utils.fs.copyFileSync(styleFile, path.join(outputDir, file));
74
- }
75
- };
76
-
77
- const taskMain = async ({
78
- modernConfig,
79
- appContext
80
- }) => {
81
- const {
82
- assetsPath = 'styles',
83
- path: outputPath = 'dist',
84
- jsPath = 'js'
85
- } = modernConfig.output;
86
- const {
87
- appDirectory
88
- } = appContext;
89
- const lessOption = await core.mountHook().moduleLessConfig({
90
- modernConfig
91
- }, {
92
- onLast: async _ => undefined
93
- });
94
- const sassOption = await core.mountHook().moduleSassConfig({
95
- modernConfig
96
- }, {
97
- onLast: async _ => undefined
98
- });
99
- const tailwindPlugin = await core.mountHook().moduleTailwindConfig({
100
- modernConfig
101
- }, {
102
- onLast: async _ => undefined
103
- });
104
- const postcssOption = getPostcssOption(appDirectory, modernConfig);
105
-
106
- if (tailwindPlugin) {
107
- var _postcssOption$plugin;
108
-
109
- (_postcssOption$plugin = postcssOption.plugins) === null || _postcssOption$plugin === void 0 ? void 0 : _postcssOption$plugin.push(tailwindPlugin);
110
- }
111
-
112
- const {
113
- importStyle
114
- } = modernConfig.output;
115
- const existStylesDir = checkStylesDirExist({
116
- appDirectory
117
- }); // 编译 styles 目录样式
118
-
119
- if (existStylesDir) {
120
- const styleResult = await compiler.styleCompiler({
121
- projectDir: appDirectory,
122
- stylesDir: path.resolve(appDirectory, STYLE_DIRS),
123
- outDir: path.join(appDirectory, outputPath, assetsPath),
124
- enableVirtualDist: true,
125
- compilerOption: {
126
- less: lessOption,
127
- sass: sassOption,
128
- postcss: postcssOption
129
- }
130
- });
131
- generatorFileOrLogError(styleResult, `[Style Compiler] Successfully for 'styles' dir`);
132
- } // 编译 src 内的样式代码
133
-
134
-
135
- const srcDir = path.resolve(appDirectory, SRC_STYLE_DIRS);
136
- const outputDirToSrc = path.join(appDirectory, outputPath, jsPath, assetsPath);
137
-
138
- if (importStyle === 'compiled-code') {
139
- const srcStyleResult = await compiler.styleCompiler({
140
- projectDir: appDirectory,
141
- stylesDir: srcDir,
142
- outDir: outputDirToSrc,
143
- enableVirtualDist: true,
144
- compilerOption: {
145
- less: lessOption,
146
- sass: sassOption,
147
- postcss: postcssOption
148
- }
149
- });
150
- generatorFileOrLogError(srcStyleResult, `[Style Compiler] Successfully for 'src' dir`);
151
- } else {
152
- copyOriginStyleFiles({
153
- targetDir: srcDir,
154
- outputDir: outputDirToSrc
155
- });
156
- }
157
- };
158
-
159
- (async () => {
160
- let options;
161
-
162
- if (process.env.CORE_INIT_OPTION_FILE) {
163
- ({
164
- options
165
- } = require(process.env.CORE_INIT_OPTION_FILE));
166
- }
167
-
168
- hooks.buildLifeCycle();
169
- const {
170
- resolved: modernConfig,
171
- appContext
172
- } = await core.cli.init([], options);
173
- await core.manager.run(async () => {
174
- try {
175
- await taskMain({
176
- modernConfig,
177
- appContext
178
- });
179
- } catch (e) {
180
- console.error(e.toString());
181
- }
182
- });
183
- })();