@modern-js/module-tools 1.5.8 → 1.6.1

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 +41 -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 -56
  8. package/dist/js/modern/{tasks → features/build/bundleless}/generator-dts/index.js +46 -71
  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 +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 +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 -56
  38. package/dist/js/node/{tasks → features/build/bundleless}/generator-dts/index.js +58 -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 +20 -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/commands/build.d.ts +12 -12
  64. package/dist/types/features/build/bundle/index.d.ts +3 -0
  65. package/dist/types/features/build/bundle/runRollup.d.ts +3 -0
  66. package/dist/types/features/build/bundle/runSpeedy.d.ts +10 -0
  67. package/dist/types/features/build/bundleless/copy-assets.d.ts +3 -0
  68. package/dist/types/features/build/bundleless/generator-dts/index.d.ts +3 -0
  69. package/dist/types/{tasks → features/build/bundleless}/generator-dts/utils.d.ts +3 -6
  70. package/dist/types/features/build/bundleless/index.d.ts +4 -0
  71. package/dist/types/features/build/bundleless/runBabel.d.ts +32 -0
  72. package/dist/types/features/build/bundleless/style.d.ts +25 -0
  73. package/dist/types/features/build/constants.d.ts +17 -13
  74. package/dist/types/features/build/error.d.ts +33 -0
  75. package/dist/types/features/build/index.d.ts +12 -3
  76. package/dist/types/features/build/legacy-constants.d.ts +8 -0
  77. package/dist/types/features/build/normalize.d.ts +19 -0
  78. package/dist/types/features/build/types.d.ts +15 -0
  79. package/dist/types/features/build/utils.d.ts +10 -31
  80. package/dist/types/index.d.ts +1 -0
  81. package/dist/types/locale/en.d.ts +2 -0
  82. package/dist/types/locale/index.d.ts +4 -0
  83. package/dist/types/locale/zh.d.ts +2 -0
  84. package/dist/types/schema/build-config.d.ts +197 -0
  85. package/dist/types/schema/index.d.ts +184 -18
  86. package/dist/types/schema/output.d.ts +188 -10
  87. package/dist/types/schema/source.d.ts +2 -11
  88. package/dist/types/schema/tools.d.ts +6 -0
  89. package/dist/types/schema/types.d.ts +46 -0
  90. package/dist/types/types.d.ts +29 -28
  91. package/dist/types/utils/babel.d.ts +3 -2
  92. package/dist/types/utils/constants.d.ts +2 -0
  93. package/package.json +23 -9
  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 -208
  97. package/dist/js/modern/tasks/build-style.js +0 -169
  98. package/dist/js/modern/tasks/build-watch-source-code.js +0 -162
  99. package/dist/js/modern/tasks/build-watch-style.js +0 -216
  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 -234
  104. package/dist/js/node/tasks/build-style.js +0 -184
  105. package/dist/js/node/tasks/build-watch-source-code.js +0 -174
  106. package/dist/js/node/tasks/build-watch-style.js +0 -233
  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,16 +1,23 @@
1
1
  "use strict";
2
2
 
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.genDts = void 0;
7
+
8
+ var _path = _interopRequireDefault(require("path"));
9
+
3
10
  var _utils = require("@modern-js/utils");
4
11
 
5
- var _utils2 = require("./utils");
12
+ var _error = require("../../error");
6
13
 
7
- const core = _utils.Import.lazy('@modern-js/core', require);
14
+ var _utils2 = require("../../utils");
8
15
 
9
- const argv = _utils.Import.lazy('process.argv', require);
16
+ var _utils3 = require("./utils");
10
17
 
11
- const utils = _utils.Import.lazy('./utils', require);
18
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
19
 
13
- let removeTsconfigPath = '';
20
+ const utils = _utils.Import.lazy('./utils', require);
14
21
 
15
22
  const getProjectTsconfig = tsconfigPath => {
16
23
  if (!tsconfigPath || !_utils.fs.existsSync(tsconfigPath)) {
@@ -21,7 +28,6 @@ const getProjectTsconfig = tsconfigPath => {
21
28
  };
22
29
 
23
30
  const resolveLog = (childProgress, {
24
- tsCheck: _tsCheck = false,
25
31
  watch: _watch = false
26
32
  } = {}) => {
27
33
  var _childProgress$stdout, _childProgress$stdout2, _childProgress$stderr;
@@ -32,11 +38,8 @@ const resolveLog = (childProgress, {
32
38
  * watch 模式下,则使用这里的信息
33
39
  */
34
40
  (_childProgress$stdout = childProgress.stdout) === null || _childProgress$stdout === void 0 ? void 0 : _childProgress$stdout.on('data', data => {
35
- if (!_tsCheck) {
36
- return;
37
- }
38
-
39
41
  if (_watch) {
42
+ console.info((0, _utils2.watchSectionTitle)('[Bundleless:DTS]', _utils2.SectionTitleStatus.Log));
40
43
  console.info(data.toString());
41
44
  }
42
45
  }); // 正常以下内容都不会触发,因为tsc 不会产生以下类型的log信息,不过防止意外情况
@@ -53,97 +56,79 @@ const generatorDts = async (_, config) => {
53
56
  const {
54
57
  tsconfigPath,
55
58
  distDir,
56
- sourceDirName = 'src',
57
- projectData: {
58
- appDirectory
59
- },
60
- tsCheck = false,
59
+ sourceDir = 'src',
60
+ appDirectory,
61
61
  watch = false
62
62
  } = config;
63
63
  const userTsconfig = getProjectTsconfig(tsconfigPath);
64
64
  const willDeleteTsconfigPath = utils.generatorTsConfig(userTsconfig, {
65
65
  appDirectory,
66
66
  distDir,
67
- sourceDir: sourceDirName
67
+ sourceDir
68
68
  });
69
- removeTsconfigPath = willDeleteTsconfigPath;
70
- const tscBinFile = (0, _utils2.getTscBinPath)(appDirectory);
69
+ const tscBinFile = (0, _utils3.getTscBinPath)(appDirectory);
71
70
  const watchParams = watch ? ['-w'] : [];
72
- const childProgress = (0, _utils.execa)(tscBinFile, ['-p', willDeleteTsconfigPath, ...watchParams], {
71
+ const childProgress = (0, _utils.execa)(tscBinFile, ['-p', willDeleteTsconfigPath,
72
+ /* Required parameter, use it stdout have color */
73
+ '--pretty', // https://github.com/microsoft/TypeScript/issues/21824
74
+ '--preserveWatchOutput', ...watchParams], {
73
75
  stdio: 'pipe',
74
76
  cwd: appDirectory
75
77
  });
76
78
  resolveLog(childProgress, {
77
- tsCheck,
78
79
  watch
79
80
  });
80
81
 
81
82
  try {
82
83
  await childProgress;
83
- console.info('[TSC Compiler]: Successfully');
84
84
  } catch (e) {
85
- if (!tsCheck) {
86
- console.info(`There are some type warnings, which can be checked by configuring 'output.disableTsChecker = false'`);
87
- } // 通过使用 execa,可以将 tsc 的 data 类型的报错信息变为异常错误信息
88
- else if ((0, _utils.isObject)(e) && e.stdout) {
89
- console.error(e.stdout);
90
- } else {
91
- console.error(e);
85
+ if (e instanceof Error) {
86
+ throw new _error.InternalDTSError(e, {
87
+ buildType: 'bundleless'
88
+ });
92
89
  }
93
90
  }
94
91
 
95
92
  _utils.fs.removeSync(willDeleteTsconfigPath);
96
93
  };
97
94
 
98
- const taskMain = async ({
99
- modernConfig
100
- }) => {
101
- const processArgv = argv(process.argv.slice(2));
102
- const config = processArgv({
103
- appDirectory: process.cwd(),
104
- srcDir: 'src',
105
- distDir: 'dist/types',
106
- tsconfigPath: './tsconfig.json',
107
- sourceDirName: 'src'
108
- });
109
- const option = {
110
- srcDir: config.srcDir,
111
- distDir: config.distDir,
112
- projectData: {
113
- appDirectory: config.appDirectory
95
+ const genDts = async (api, config) => {
96
+ const {
97
+ outputPath,
98
+ tsconfig: tsconfigPath,
99
+ watch,
100
+ bundlelessOptions: {
101
+ sourceDir
114
102
  },
115
- tsconfigPath: config.tsconfigPath,
116
- watch: config.watch,
117
- tsCheck: config.tsCheck,
118
- sourceDirName: config.sourceDirName
119
- };
120
- await generatorDts(modernConfig, option); // // TODO: watch 模式下无法转换
121
-
122
- utils.resolveAlias(modernConfig, option);
123
- };
124
-
125
- (async () => {
126
- let options;
103
+ enableDts
104
+ } = config;
127
105
 
128
- if (process.env.CORE_INIT_OPTION_FILE) {
129
- ({
130
- options
131
- } = require(process.env.CORE_INIT_OPTION_FILE));
106
+ if (!enableDts) {
107
+ return;
132
108
  }
133
109
 
134
110
  const {
135
- resolved
136
- } = await core.cli.init([], options);
111
+ appDirectory
112
+ } = api.useAppContext();
113
+ const modernConfig = api.useResolvedConfigContext();
114
+ const {
115
+ output: {
116
+ path: distPath = 'dist'
117
+ }
118
+ } = modernConfig;
137
119
 
138
- (async () => {
139
- try {
140
- await taskMain({
141
- modernConfig: resolved
142
- });
143
- } catch (e) {
144
- console.error(e.message);
120
+ const distDir = _path.default.join(appDirectory, distPath, outputPath);
145
121
 
146
- _utils.fs.removeSync(removeTsconfigPath);
147
- }
148
- })();
149
- })();
122
+ const option = {
123
+ appDirectory,
124
+ distDir,
125
+ tsconfigPath,
126
+ watch,
127
+ sourceDir
128
+ };
129
+ await generatorDts(modernConfig, option); // TODO: watch 模式下无法转换
130
+
131
+ utils.resolveAlias(modernConfig, option);
132
+ };
133
+
134
+ exports.genDts = genDts;
@@ -21,18 +21,16 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
21
21
 
22
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
23
 
24
- const babel = _utils.Import.lazy('../../utils/babel', require);
24
+ const babel = _utils.Import.lazy('../../../../utils/babel', require);
25
25
 
26
- const tsPathsTransform = _utils.Import.lazy('../../utils/tspaths-transform', require);
27
-
28
- const constants = _utils.Import.lazy('../constants', require);
26
+ const tsPathsTransform = _utils.Import.lazy('../../../../utils/tspaths-transform', require);
29
27
 
30
28
  const generatorTsConfig = (projectTsconfig, {
31
29
  appDirectory,
32
30
  distDir,
33
31
  sourceDir: _sourceDir = 'src'
34
32
  }) => {
35
- var _projectTsconfig$comp, _projectTsconfig$comp2, _projectTsconfig$incl, _projectTsconfig$excl;
33
+ var _projectTsconfig$comp, _projectTsconfig$comp2, _ref, _projectTsconfig$excl;
36
34
 
37
35
  const tempPath = path.resolve(appDirectory, './node_modules');
38
36
  const resolvePath = path.relative(tempPath, appDirectory); // const rootDir = projectTsconfig.compilerOptions?.rootDir
@@ -42,8 +40,9 @@ const generatorTsConfig = (projectTsconfig, {
42
40
 
43
41
  const rootDir = path.join(resolvePath, _sourceDir);
44
42
  const baseUrl = (_projectTsconfig$comp = projectTsconfig.compilerOptions) !== null && _projectTsconfig$comp !== void 0 && _projectTsconfig$comp.baseUrl ? path.join(appDirectory, (_projectTsconfig$comp2 = projectTsconfig.compilerOptions) === null || _projectTsconfig$comp2 === void 0 ? void 0 : _projectTsconfig$comp2.baseUrl) : appDirectory; // if include = ['src'], final include should be ['../src']
43
+ // const include = projectTsconfig.include?.map(includePath =>
45
44
 
46
- const include = (_projectTsconfig$incl = projectTsconfig.include) === null || _projectTsconfig$incl === void 0 ? void 0 : _projectTsconfig$incl.map(includePath => path.join(resolvePath, includePath));
45
+ const include = (_ref = [_sourceDir]) === null || _ref === void 0 ? void 0 : _ref.map(includePath => path.join(resolvePath, includePath));
47
46
  const exclude = (_projectTsconfig$excl = projectTsconfig.exclude) === null || _projectTsconfig$excl === void 0 ? void 0 : _projectTsconfig$excl.map(excludePath => path.join(resolvePath, excludePath));
48
47
  const resetConfig = {
49
48
  compilerOptions: {
@@ -63,7 +62,7 @@ const generatorTsConfig = (projectTsconfig, {
63
62
  isolatedModules: true
64
63
  }
65
64
  };
66
- const tempTsconfigPath = path.join(tempPath, constants.tempTsconfigName);
65
+ const tempTsconfigPath = path.join(tempPath, `tsconfig.${Date.now()}.${(0, _utils.nanoid)()}.json`);
67
66
 
68
67
  _utils.fs.ensureFileSync(tempTsconfigPath);
69
68
 
@@ -87,14 +86,14 @@ const resolveAlias = (modernConfig, config, watchFilenames = []) => {
87
86
  absolute: true
88
87
  });
89
88
  const alias = babel.getFinalAlias(modernConfig, {
90
- appDirectory: config.projectData.appDirectory,
91
- tsconfigPath: config.tsconfigPath || path.join(config.projectData.appDirectory, './tsconfig.json'),
89
+ appDirectory: config.appDirectory,
90
+ tsconfigPath: config.tsconfigPath || path.join(config.appDirectory, './tsconfig.json'),
92
91
  sourceAbsDir: config.distDir
93
92
  });
94
93
  const mergedPaths = alias.isTsPath ? alias.paths || {} : _objectSpread(_objectSpread({}, defaultPaths), alias.paths || {});
95
94
  const result = tsPathsTransform.transformDtsAlias({
96
95
  filenames: dtsFilenames,
97
- baseUrl: path.join(config.projectData.appDirectory, output.path || 'dist'),
96
+ baseUrl: path.join(config.appDirectory, output.path || 'dist'),
98
97
  paths: mergedPaths
99
98
  });
100
99
 
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.build = void 0;
7
+
8
+ var _utils = require("@modern-js/utils");
9
+
10
+ var _pMap = _interopRequireDefault(require("p-map"));
11
+
12
+ var _runBabel = require("./runBabel");
13
+
14
+ var _style = require("./style");
15
+
16
+ var _generatorDts = require("./generator-dts");
17
+
18
+ var _copyAssets = require("./copy-assets");
19
+
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
+
22
+ const build = async (api, config, legacyOptions) => {
23
+ // valid format is umd
24
+ if (config.format === 'umd') {
25
+ console.info(_utils.chalk.yellowBright('bundleless 构建暂时不支持 umd 格式'));
26
+ return;
27
+ }
28
+
29
+ if (legacyOptions !== null && legacyOptions !== void 0 && legacyOptions.styleOnly) {
30
+ await (0, _style.buildStyle)(api, config);
31
+ return;
32
+ }
33
+
34
+ const tasks = config.dtsOnly ? [_generatorDts.genDts] : [_runBabel.runBabelBuild, _generatorDts.genDts, _style.buildStyle, _copyAssets.copyStaticAssets];
35
+ await (0, _pMap.default)(tasks, async task => {
36
+ await task(api, config);
37
+ });
38
+ };
39
+
40
+ exports.build = build;
@@ -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)((0, _utils.slash)(`${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;