@modern-js/module-tools 1.5.7 → 1.6.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 (119) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/dist/js/modern/cli/build.js +5 -4
  3. package/dist/js/modern/commands/build.js +55 -28
  4. package/dist/js/modern/features/build/bundle/index.js +9 -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 +10 -10
  10. package/dist/js/modern/features/build/bundleless/index.js +23 -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 +87 -18
  16. package/dist/js/modern/features/build/legacy-constants.js +99 -0
  17. package/dist/js/modern/features/build/normalize.js +301 -0
  18. package/dist/js/modern/features/build/types.js +0 -0
  19. package/dist/js/modern/features/build/utils.js +39 -136
  20. package/dist/js/modern/index.js +4 -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 +111 -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/modern/utils/copy.js +2 -2
  32. package/dist/js/node/cli/build.js +5 -4
  33. package/dist/js/node/commands/build.js +62 -29
  34. package/dist/js/node/features/build/bundle/index.js +23 -0
  35. package/dist/js/node/features/build/bundle/runRollup.js +174 -0
  36. package/dist/js/node/features/build/bundle/runSpeedy.js +209 -0
  37. package/dist/js/node/{tasks → features/build/bundleless}/copy-assets.js +37 -55
  38. package/dist/js/node/{tasks → features/build/bundleless}/generator-dts/index.js +59 -73
  39. package/dist/js/node/{tasks → features/build/bundleless}/generator-dts/utils.js +9 -10
  40. package/dist/js/node/features/build/bundleless/index.js +40 -0
  41. package/dist/js/node/features/build/bundleless/runBabel.js +309 -0
  42. package/dist/js/node/features/build/bundleless/style.js +299 -0
  43. package/dist/js/node/features/build/constants.js +115 -67
  44. package/dist/js/node/features/build/error.js +175 -0
  45. package/dist/js/node/features/build/index.js +104 -18
  46. package/dist/js/node/features/build/legacy-constants.js +107 -0
  47. package/dist/js/node/features/build/normalize.js +330 -0
  48. package/dist/js/node/features/build/types.js +0 -0
  49. package/dist/js/node/features/build/utils.js +48 -145
  50. package/dist/js/node/index.js +22 -1
  51. package/dist/js/node/locale/en.js +5 -3
  52. package/dist/js/node/locale/zh.js +5 -3
  53. package/dist/js/node/schema/build-config.js +120 -0
  54. package/dist/js/node/schema/index.js +3 -1
  55. package/dist/js/node/schema/output.js +20 -9
  56. package/dist/js/node/schema/source.js +2 -10
  57. package/dist/js/node/schema/tools.js +13 -0
  58. package/dist/js/node/schema/types.js +0 -0
  59. package/dist/js/node/utils/babel.js +4 -1
  60. package/dist/js/node/utils/constants.js +11 -0
  61. package/dist/js/node/utils/copy.js +1 -1
  62. package/dist/types/cli/build.d.ts +1 -1
  63. package/dist/types/cli/new.d.ts +1 -1
  64. package/dist/types/commands/build.d.ts +12 -12
  65. package/dist/types/features/build/bundle/index.d.ts +3 -0
  66. package/dist/types/features/build/bundle/runRollup.d.ts +3 -0
  67. package/dist/types/features/build/bundle/runSpeedy.d.ts +10 -0
  68. package/dist/types/features/build/bundleless/copy-assets.d.ts +3 -0
  69. package/dist/types/features/build/bundleless/generator-dts/index.d.ts +3 -0
  70. package/dist/types/{tasks → features/build/bundleless}/generator-dts/utils.d.ts +3 -6
  71. package/dist/types/features/build/bundleless/index.d.ts +4 -0
  72. package/dist/types/features/build/bundleless/runBabel.d.ts +32 -0
  73. package/dist/types/features/build/bundleless/style.d.ts +25 -0
  74. package/dist/types/features/build/constants.d.ts +17 -13
  75. package/dist/types/features/build/error.d.ts +33 -0
  76. package/dist/types/features/build/index.d.ts +12 -3
  77. package/dist/types/features/build/legacy-constants.d.ts +8 -0
  78. package/dist/types/features/build/logger/loggerManager.d.ts +2 -0
  79. package/dist/types/features/build/normalize.d.ts +19 -0
  80. package/dist/types/features/build/types.d.ts +15 -0
  81. package/dist/types/features/build/utils.d.ts +10 -31
  82. package/dist/types/index.d.ts +1 -0
  83. package/dist/types/locale/en.d.ts +2 -0
  84. package/dist/types/locale/index.d.ts +4 -0
  85. package/dist/types/locale/zh.d.ts +2 -0
  86. package/dist/types/schema/build-config.d.ts +197 -0
  87. package/dist/types/schema/index.d.ts +184 -18
  88. package/dist/types/schema/output.d.ts +188 -10
  89. package/dist/types/schema/source.d.ts +2 -11
  90. package/dist/types/schema/tools.d.ts +6 -0
  91. package/dist/types/schema/types.d.ts +46 -0
  92. package/dist/types/types.d.ts +29 -28
  93. package/dist/types/utils/babel.d.ts +3 -2
  94. package/dist/types/utils/constants.d.ts +2 -0
  95. package/dist/types/utils/logger.d.ts +2 -0
  96. package/package.json +28 -13
  97. package/dist/js/modern/features/build/build-watch.js +0 -101
  98. package/dist/js/modern/features/build/build.js +0 -123
  99. package/dist/js/modern/tasks/build-source-code.js +0 -207
  100. package/dist/js/modern/tasks/build-style.js +0 -168
  101. package/dist/js/modern/tasks/build-watch-source-code.js +0 -161
  102. package/dist/js/modern/tasks/build-watch-style.js +0 -215
  103. package/dist/js/modern/tasks/constants.js +0 -1
  104. package/dist/js/node/features/build/build-watch.js +0 -121
  105. package/dist/js/node/features/build/build.js +0 -143
  106. package/dist/js/node/tasks/build-source-code.js +0 -233
  107. package/dist/js/node/tasks/build-style.js +0 -183
  108. package/dist/js/node/tasks/build-watch-source-code.js +0 -173
  109. package/dist/js/node/tasks/build-watch-style.js +0 -232
  110. package/dist/js/node/tasks/constants.js +0 -8
  111. package/dist/types/features/build/build-watch.d.ts +0 -3
  112. package/dist/types/features/build/build.d.ts +0 -3
  113. package/dist/types/tasks/build-source-code.d.ts +0 -25
  114. package/dist/types/tasks/build-style.d.ts +0 -1
  115. package/dist/types/tasks/build-watch-source-code.d.ts +0 -1
  116. package/dist/types/tasks/build-watch-style.d.ts +0 -1
  117. package/dist/types/tasks/constants.d.ts +0 -1
  118. package/dist/types/tasks/copy-assets.d.ts +0 -1
  119. package/dist/types/tasks/generator-dts/index.d.ts +0 -1
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.5.7",
14
+ "version": "1.6.0",
15
15
  "bin": {
16
16
  "modern": "./bin/modern.js"
17
17
  },
@@ -55,24 +55,34 @@
55
55
  "@babel/traverse": "^7.18.0",
56
56
  "@babel/types": "^7.18.0",
57
57
  "@modern-js/babel-compiler": "^1.2.6",
58
- "@modern-js/babel-preset-module": "^1.3.8",
59
- "@modern-js/core": "^1.12.0",
58
+ "@modern-js/babel-preset-module": "^1.3.9",
59
+ "@modern-js/core": "^1.12.3",
60
60
  "@modern-js/css-config": "^1.2.7",
61
61
  "@modern-js/i18n-cli-language-detector": "^1.2.4",
62
- "@modern-js/new-action": "^1.3.10",
63
- "@modern-js/plugin": "^1.3.7",
64
- "@modern-js/plugin-analyze": "^1.4.6",
65
- "@modern-js/plugin-changeset": "^1.2.8",
62
+ "@modern-js/new-action": "^1.3.11",
63
+ "@modern-js/plugin": "^1.4.0",
64
+ "@modern-js/plugin-analyze": "^1.4.7",
65
+ "@modern-js/plugin-changeset": "^1.3.1",
66
66
  "@modern-js/plugin-i18n": "^1.2.7",
67
- "@modern-js/style-compiler": "^1.2.10",
68
- "@modern-js/utils": "^1.7.7",
67
+ "@modern-js/plugin-jarvis": "^1.2.14",
68
+ "@modern-js/style-compiler": "^1.2.11",
69
+ "@modern-js/utils": "^1.7.10",
70
+ "@rollup/plugin-json": "~4.1.0",
71
+ "@speedy-js/speedy-types": "0.13.2-alpha.3",
72
+ "@speedy-js/speedy-core": "0.13.2-alpha.3",
73
+ "@speedy-js/speedy-plugin-es5": "0.13.2-alpha.3",
69
74
  "normalize-path": "^3.0.0",
70
75
  "p-map": "^4",
71
- "process.argv": "^0.6.0"
76
+ "process.argv": "^0.6.0",
77
+ "rollup": "^2.70.2",
78
+ "rollup-plugin-dts": "^4.2.1",
79
+ "rollup-plugin-hashbang": "^3.0.0",
80
+ "signal-exit": "^3.0.7"
72
81
  },
73
82
  "devDependencies": {
74
83
  "@scripts/build": "0.0.0",
75
84
  "@scripts/jest-config": "0.0.0",
85
+ "@speedy-js/speedy-types": "0.13.2-alpha.3",
76
86
  "@types/babel__core": "^7.1.15",
77
87
  "@types/babel__generator": "^7.6.3",
78
88
  "@types/babel__traverse": "^7.14.2",
@@ -82,8 +92,11 @@
82
92
  "@types/normalize-path": "^3.0.0",
83
93
  "@types/react": "^17",
84
94
  "@types/react-dom": "^17",
95
+ "@types/signal-exit": "^3.0.1",
85
96
  "jest": "^27",
86
- "typescript": "^4"
97
+ "typescript": "^4",
98
+ "ajv": "^8",
99
+ "ajv-keywords": "^5"
87
100
  },
88
101
  "sideEffects": false,
89
102
  "modernConfig": {
@@ -101,7 +114,8 @@
101
114
  "files": [
102
115
  "src/**/*",
103
116
  "tsconfig.json",
104
- "package.json"
117
+ "package.json",
118
+ "./modern.config.js"
105
119
  ],
106
120
  "output": [
107
121
  "dist/**/*"
@@ -121,7 +135,8 @@
121
135
  "scripts": {
122
136
  "new": "modern new",
123
137
  "build": "wireit",
124
- "test": "wireit"
138
+ "test": "wireit",
139
+ "dev": "modern build --watch"
125
140
  },
126
141
  "readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
127
142
  }
@@ -1,101 +0,0 @@
1
- import * as path from 'path';
2
- import * as os from 'os';
3
- import { execa, Import } from '@modern-js/utils';
4
- const lg = Import.lazy('./logger', require);
5
- const pMap = Import.lazy('p-map', require);
6
- const utils = Import.lazy('./utils', require);
7
- const constants = Import.lazy('./constants', require);
8
- export const buildInWatchMode = async (api, config, _) => {
9
- const {
10
- appDirectory
11
- } = api.useAppContext();
12
- const {
13
- sourceDir,
14
- enableTscCompiler
15
- } = config;
16
- const srcRootDir = path.join(appDirectory, sourceDir);
17
- const concurrency = os.cpus().length;
18
- const lm = new lg.LoggerManager();
19
- const codeLog = lm.createLoggerText({
20
- title: constants.runBabelCompilerTitle
21
- });
22
- const dtsLog = lm.createLoggerText({
23
- title: constants.runTscWatchTitle
24
- });
25
- const styleLog = lm.createLoggerText({
26
- title: constants.runStyleCompilerTitle
27
- });
28
- const copyLog = lm.createLoggerText({
29
- title: 'Copy Log:'
30
- });
31
- const initCodeMapper = utils.getCodeInitMapper(api, config);
32
- const taskMapper = [...utils.getCodeMapper(api, {
33
- logger: codeLog,
34
- taskPath: require.resolve("../../tasks/build-watch-source-code"),
35
- config,
36
- willCompilerDirOrFile: sourceDir,
37
- initMapper: initCodeMapper,
38
- srcRootDir
39
- }), ...(enableTscCompiler ? utils.getDtsMapper(api, config, dtsLog) : []), {
40
- logger: styleLog,
41
- taskPath: require.resolve("../../tasks/build-watch-style")
42
- }, {
43
- logger: copyLog,
44
- taskPath: require.resolve("../../tasks/copy-assets"),
45
- params: ['--watch']
46
- }];
47
- lm.on('data', () => {
48
- console.info(constants.clearFlag);
49
- enableTscCompiler && console.info(dtsLog.value);
50
- console.info(codeLog.value);
51
- console.info(styleLog.value);
52
- });
53
- await pMap(taskMapper, async ({
54
- logger,
55
- taskPath,
56
- params
57
- }) => {
58
- const childProcess = execa.node(taskPath, params, {
59
- stdio: 'pipe'
60
- });
61
-
62
- if (logger === codeLog) {
63
- lm.addStdout(logger, childProcess.stdout, {
64
- event: {
65
- error: true,
66
- data: true
67
- } // colors: { data: s => s },
68
-
69
- });
70
- lm.addStderr(logger, childProcess.stderr);
71
- }
72
-
73
- if (logger === dtsLog) {
74
- lm.addStdout(logger, childProcess.stdout, {
75
- event: {
76
- data: true,
77
- error: true
78
- },
79
- colors: {
80
- // tsc 的log信息无论是错误还是正确都是从 data event 中获取到的
81
- data: s => s
82
- }
83
- });
84
- }
85
-
86
- if (logger === styleLog || logger === copyLog) {
87
- lm.addStdout(logger, childProcess.stdout, {
88
- event: {
89
- error: true,
90
- data: true
91
- } // colors: { data: s => s },
92
-
93
- });
94
- lm.addStderr(logger, childProcess.stderr);
95
- }
96
-
97
- await childProcess;
98
- }, {
99
- concurrency
100
- });
101
- };
@@ -1,123 +0,0 @@
1
- import * as path from 'path';
2
- import * as os from 'os';
3
- import { execa, Import } from '@modern-js/utils';
4
- const pMap = Import.lazy('p-map', require);
5
- const utils = Import.lazy('./utils', require);
6
- const lg = Import.lazy('./logger', require);
7
- const constants = Import.lazy('./constants', require);
8
- export const buildSourceCode = async (api, config, _) => {
9
- const {
10
- sourceDir,
11
- enableTscCompiler,
12
- styleOnly
13
- } = config;
14
- const {
15
- appDirectory
16
- } = api.useAppContext();
17
- const concurrency = os.cpus().length;
18
- const srcRootDir = path.join(appDirectory, sourceDir);
19
- const lm = new lg.LoggerManager();
20
- const codeLog = lm.createLoggerText({
21
- title: constants.runBabelCompilerTitle
22
- });
23
- const dtsLog = lm.createLoggerText({
24
- title: constants.runTscTitle
25
- });
26
- const styleLog = lm.createLoggerText({
27
- title: constants.runStyleCompilerTitle
28
- });
29
- const copyLog = lm.createLoggerText({
30
- title: 'Copy Log:'
31
- });
32
- const initCodeMapper = utils.getCodeInitMapper(api, config);
33
- let taskMapper = [];
34
-
35
- if (styleOnly) {
36
- taskMapper = [{
37
- logger: styleLog,
38
- taskPath: require.resolve("../../tasks/build-style")
39
- }, {
40
- logger: copyLog,
41
- taskPath: require.resolve("../../tasks/copy-assets")
42
- }];
43
- } else {
44
- taskMapper = [...utils.getCodeMapper(api, {
45
- logger: codeLog,
46
- taskPath: require.resolve("../../tasks/build-source-code"),
47
- config,
48
- willCompilerDirOrFile: sourceDir,
49
- initMapper: initCodeMapper,
50
- srcRootDir
51
- }), ...(enableTscCompiler ? utils.getDtsMapper(api, config, dtsLog) : []), {
52
- logger: styleLog,
53
- taskPath: require.resolve("../../tasks/build-style")
54
- }, {
55
- logger: copyLog,
56
- taskPath: require.resolve("../../tasks/copy-assets")
57
- }];
58
- }
59
-
60
- lm.showCompiling();
61
- await pMap(taskMapper, async ({
62
- logger,
63
- taskPath,
64
- params
65
- }) => {
66
- const childProcess = execa.node(taskPath, params, {
67
- stdio: 'pipe'
68
- });
69
-
70
- if (logger === codeLog || logger === copyLog) {
71
- lm.addStdout(logger, childProcess.stdout, {
72
- event: {
73
- data: true,
74
- error: true
75
- }
76
- });
77
- lm.addStderr(logger, childProcess.stderr);
78
- }
79
-
80
- if (logger === dtsLog) {
81
- lm.addStdout(dtsLog, childProcess.stdout, {
82
- event: {
83
- data: true,
84
- error: true
85
- }
86
- });
87
- lm.addStderr(dtsLog, childProcess.stderr);
88
- }
89
-
90
- if (logger === styleLog) {
91
- lm.addStdout(logger, childProcess.stdout, {
92
- event: {
93
- data: true,
94
- error: true
95
- }
96
- });
97
- lm.addStderr(logger, childProcess.stderr);
98
- }
99
-
100
- await childProcess;
101
- }, {
102
- concurrency
103
- });
104
- lm.disappearCompiling();
105
-
106
- if (!styleOnly) {
107
- enableTscCompiler && console.info(dtsLog.value);
108
- console.info(codeLog.value);
109
- }
110
-
111
- if (styleLog.hasMessages()) {
112
- console.info(styleLog.value);
113
- }
114
-
115
- if (copyLog.hasMessages()) {
116
- console.info(copyLog.value);
117
- }
118
-
119
- if (dtsLog.hasErrorMessage || codeLog.hasErrorMessage || styleLog.hasErrorMessage || copyLog.hasErrorMessage) {
120
- // eslint-disable-next-line no-process-exit
121
- process.exit(1);
122
- }
123
- };
@@ -1,207 +0,0 @@
1
- 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; }
2
-
3
- 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; }
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, glob, fs } from '@modern-js/utils';
8
- import { initEnv } from "../utils/init-env";
9
- const babelCompiler = Import.lazy('@modern-js/babel-compiler', require);
10
- const argv = Import.lazy('process.argv', require);
11
- const core = Import.lazy('@modern-js/core', require);
12
- const bc = Import.lazy('../utils/babel', require);
13
- const ts = Import.lazy('../utils/tsconfig', require);
14
- export let Compiler;
15
-
16
- (function (Compiler) {
17
- Compiler[Compiler["babel"] = 0] = "babel";
18
- Compiler[Compiler["esbuild"] = 1] = "esbuild";
19
- Compiler[Compiler["swc"] = 2] = "swc";
20
- })(Compiler || (Compiler = {}));
21
-
22
- const runBabelCompiler = async (willCompilerFiles, config, babelConfig = {}) => {
23
- const {
24
- srcRootDir,
25
- distDir
26
- } = config; // TODO: 判断lynx模式下,修改distFileExtMap: {'js': 'js', 'jsx': 'jsx', 'ts': 'js', 'tsx': 'jsx'}
27
-
28
- return babelCompiler.compiler({
29
- quiet: true,
30
- enableVirtualDist: true,
31
- rootDir: srcRootDir,
32
- filenames: willCompilerFiles,
33
- distDir,
34
- ignore: ['*.d.ts']
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
- absolute: true
68
- });
69
- return files;
70
- };
71
- export const buildSourceCode = async config => {
72
- const {
73
- compiler,
74
- willCompilerDirOrFile,
75
- tsconfigPath,
76
- sourceMaps,
77
- babelConfig
78
- } = config;
79
- const tsconfig = ts.readTsConfig(tsconfigPath);
80
- const willCompilerFiles = getWillCompilerCode(willCompilerDirOrFile, {
81
- tsconfig,
82
- isTsProject: Boolean(tsconfig)
83
- });
84
- let distSet = null;
85
-
86
- if (compiler === Compiler.babel) {
87
- distSet = await runBabelCompiler(willCompilerFiles, config, _objectSpread(_objectSpread({}, babelConfig), {}, {
88
- sourceMaps
89
- }));
90
- } else {
91
- distSet = {
92
- code: 1,
93
- message: 'no compiler'
94
- };
95
- }
96
-
97
- return distSet;
98
- };
99
-
100
- const generatorRealFiles = virtualDists => {
101
- for (const virtualDist of virtualDists) {
102
- const {
103
- distPath,
104
- code,
105
- sourcemap,
106
- sourceMapPath
107
- } = virtualDist;
108
- fs.ensureFileSync(distPath);
109
- fs.writeFileSync(distPath, code);
110
-
111
- if (sourcemap.length > 0) {
112
- fs.ensureFileSync(sourceMapPath);
113
- fs.writeFileSync(sourceMapPath, sourcemap);
114
- }
115
- }
116
- };
117
-
118
- const defaultConfig = {
119
- srcRootDir: `${process.cwd()}/src`,
120
- willCompilerDirOrFile: `${process.cwd()}/src`,
121
- distDir: './dist/js/temp',
122
- compiler: 'babel',
123
- appDirectory: '',
124
- sourceMaps: false,
125
- tsconfigPath: '',
126
- syntax: 'es5',
127
- type: 'module',
128
- watch: false
129
- };
130
-
131
- const taskMain = async ({
132
- modernConfig
133
- }) => {
134
- // Execution of the script's parameter handling and related required configuration acquisition
135
- const processArgv = argv(process.argv.slice(2));
136
- const config = processArgv(defaultConfig);
137
- process.env.BUILD_FORMAT = initEnv(config);
138
- const compiler = Compiler.babel; // Currently, only babel is supported.
139
-
140
- const babelConfig = bc.resolveBabelConfig(config.appDirectory, modernConfig, {
141
- sourceAbsDir: config.srcRootDir,
142
- tsconfigPath: config.tsconfigPath,
143
- syntax: config.syntax,
144
- type: config.type
145
- });
146
- const {
147
- code,
148
- message,
149
- messageDetails,
150
- virtualDists = []
151
- } = await buildSourceCode({
152
- distDir: config.distDir,
153
- srcRootDir: config.srcRootDir,
154
- willCompilerDirOrFile: config.willCompilerDirOrFile,
155
- sourceMaps: config.sourceMaps,
156
- compiler,
157
- projectData: {
158
- appDirectory: config.appDirectory
159
- },
160
- tsconfigPath: config.tsconfigPath,
161
- babelConfig
162
- });
163
-
164
- if (code === 0) {
165
- generatorRealFiles(virtualDists); // 执行成功log使用 console.info
166
-
167
- console.info('[Babel Compiler]: Successfully');
168
- } else if (messageDetails && messageDetails.length > 0) {
169
- console.error(message);
170
-
171
- for (const detail of messageDetails || []) {
172
- console.error(detail.content);
173
- }
174
- }
175
-
176
- if (code === 0 && config.copyDirs) {
177
- const copyList = config.copyDirs.split(',');
178
-
179
- for (const copyDir of copyList) {
180
- fs.ensureDirSync(copyDir);
181
- fs.copySync(config.distDir, copyDir);
182
- }
183
- }
184
- };
185
-
186
- (async () => {
187
- let options;
188
-
189
- if (process.env.CORE_INIT_OPTION_FILE) {
190
- ({
191
- options
192
- } = require(process.env.CORE_INIT_OPTION_FILE));
193
- }
194
-
195
- const {
196
- resolved
197
- } = await core.cli.init([], options);
198
- await core.manager.run(async () => {
199
- try {
200
- await taskMain({
201
- modernConfig: resolved
202
- });
203
- } catch (e) {
204
- console.error(e);
205
- }
206
- });
207
- })();
@@ -1,168 +0,0 @@
1
- import * as path from 'path';
2
- import { fs, glob, 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 hooks = Import.lazy('../hooks', require);
7
- const STYLE_DIRS = 'styles';
8
- const SRC_STYLE_DIRS = 'src';
9
-
10
- const checkStylesDirExist = option => {
11
- const {
12
- appDirectory
13
- } = option;
14
- return fs.existsSync(path.join(appDirectory, STYLE_DIRS));
15
- };
16
-
17
- const generatorFileOrLogError = (result, successMessage = '') => {
18
- if (result.code === 0 && result.dists.length > 0) {
19
- for (const file of result.dists) {
20
- fs.ensureFileSync(file.filename);
21
- fs.writeFileSync(file.filename, file.content);
22
- }
23
-
24
- if (successMessage) {
25
- console.info(successMessage);
26
- }
27
- } else {
28
- for (const file of result.errors) {
29
- console.error(file.error);
30
- }
31
- }
32
- };
33
-
34
- const getPostcssOption = (appDirectory, modernConfig) => {
35
- var _postcssOption$postcs;
36
-
37
- const postcssOption = cssConfig.getPostcssConfig(appDirectory, modernConfig, false);
38
- return {
39
- plugins: (postcssOption === null || postcssOption === void 0 ? void 0 : (_postcssOption$postcs = postcssOption.postcssOptions) === null || _postcssOption$postcs === void 0 ? void 0 : _postcssOption$postcs.plugins) || [],
40
- enableSourceMap: (postcssOption === null || postcssOption === void 0 ? void 0 : postcssOption.sourceMap) || false,
41
- options: {}
42
- };
43
- };
44
-
45
- const copyOriginStyleFiles = ({
46
- targetDir,
47
- outputDir
48
- }) => {
49
- const styleFiles = glob.sync(`${targetDir}/**/*.{css,sass,scss,less}`);
50
-
51
- if (styleFiles.length > 0) {
52
- fs.ensureDirSync(outputDir);
53
- }
54
-
55
- for (const styleFile of styleFiles) {
56
- const file = path.relative(targetDir, styleFile);
57
- fs.ensureDirSync(path.dirname(path.join(outputDir, file)));
58
- fs.copyFileSync(styleFile, path.join(outputDir, file));
59
- }
60
- };
61
-
62
- const taskMain = async ({
63
- modernConfig,
64
- appContext
65
- }) => {
66
- const {
67
- assetsPath = 'styles',
68
- path: outputPath = 'dist',
69
- jsPath = 'js'
70
- } = modernConfig.output;
71
- const {
72
- appDirectory
73
- } = appContext;
74
- const lessOption = await core.mountHook().moduleLessConfig({
75
- modernConfig
76
- }, {
77
- onLast: async _ => undefined
78
- });
79
- const sassOption = await core.mountHook().moduleSassConfig({
80
- modernConfig
81
- }, {
82
- onLast: async _ => undefined
83
- });
84
- const tailwindPlugin = await core.mountHook().moduleTailwindConfig({
85
- modernConfig
86
- }, {
87
- onLast: async _ => undefined
88
- });
89
- const postcssOption = getPostcssOption(appDirectory, modernConfig);
90
-
91
- if (tailwindPlugin) {
92
- var _postcssOption$plugin;
93
-
94
- (_postcssOption$plugin = postcssOption.plugins) === null || _postcssOption$plugin === void 0 ? void 0 : _postcssOption$plugin.push(tailwindPlugin);
95
- }
96
-
97
- const {
98
- importStyle
99
- } = modernConfig.output;
100
- const existStylesDir = checkStylesDirExist({
101
- appDirectory
102
- }); // 编译 styles 目录样式
103
-
104
- if (existStylesDir) {
105
- const styleResult = await compiler.styleCompiler({
106
- projectDir: appDirectory,
107
- stylesDir: path.resolve(appDirectory, STYLE_DIRS),
108
- outDir: path.join(appDirectory, outputPath, assetsPath),
109
- enableVirtualDist: true,
110
- compilerOption: {
111
- less: lessOption,
112
- sass: sassOption,
113
- postcss: postcssOption
114
- }
115
- });
116
- generatorFileOrLogError(styleResult, `[Style Compiler] Successfully for 'styles' dir`);
117
- } // 编译 src 内的样式代码
118
-
119
-
120
- const srcDir = path.resolve(appDirectory, SRC_STYLE_DIRS);
121
- const outputDirToSrc = path.join(appDirectory, outputPath, jsPath, assetsPath);
122
-
123
- if (importStyle === 'compiled-code') {
124
- const srcStyleResult = await compiler.styleCompiler({
125
- projectDir: appDirectory,
126
- stylesDir: srcDir,
127
- outDir: outputDirToSrc,
128
- enableVirtualDist: true,
129
- compilerOption: {
130
- less: lessOption,
131
- sass: sassOption,
132
- postcss: postcssOption
133
- }
134
- });
135
- generatorFileOrLogError(srcStyleResult, `[Style Compiler] Successfully for 'src' dir`);
136
- } else {
137
- copyOriginStyleFiles({
138
- targetDir: srcDir,
139
- outputDir: outputDirToSrc
140
- });
141
- }
142
- };
143
-
144
- (async () => {
145
- let options;
146
-
147
- if (process.env.CORE_INIT_OPTION_FILE) {
148
- ({
149
- options
150
- } = require(process.env.CORE_INIT_OPTION_FILE));
151
- }
152
-
153
- hooks.buildLifeCycle();
154
- const {
155
- resolved: modernConfig,
156
- appContext
157
- } = await core.cli.init([], options);
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
- })();