@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,9 +1,9 @@
1
- import { Import, execa, fs, json5, isObject } from '@modern-js/utils';
1
+ import path from 'path';
2
+ import { Import, execa, fs, json5 } from '@modern-js/utils';
3
+ import { InternalDTSError } from "../../error";
4
+ import { SectionTitleStatus, watchSectionTitle } from "../../utils";
2
5
  import { getTscBinPath } from "./utils";
3
- const core = Import.lazy('@modern-js/core', require);
4
- const argv = Import.lazy('process.argv', require);
5
6
  const utils = Import.lazy('./utils', require);
6
- let removeTsconfigPath = '';
7
7
 
8
8
  const getProjectTsconfig = tsconfigPath => {
9
9
  if (!tsconfigPath || !fs.existsSync(tsconfigPath)) {
@@ -14,7 +14,6 @@ const getProjectTsconfig = tsconfigPath => {
14
14
  };
15
15
 
16
16
  const resolveLog = (childProgress, {
17
- tsCheck: _tsCheck = false,
18
17
  watch: _watch = false
19
18
  } = {}) => {
20
19
  var _childProgress$stdout, _childProgress$stdout2, _childProgress$stderr;
@@ -25,11 +24,8 @@ const resolveLog = (childProgress, {
25
24
  * watch 模式下,则使用这里的信息
26
25
  */
27
26
  (_childProgress$stdout = childProgress.stdout) === null || _childProgress$stdout === void 0 ? void 0 : _childProgress$stdout.on('data', data => {
28
- if (!_tsCheck) {
29
- return;
30
- }
31
-
32
27
  if (_watch) {
28
+ console.info(watchSectionTitle('[Bundleless:DTS]', SectionTitleStatus.Log));
33
29
  console.info(data.toString());
34
30
  }
35
31
  }); // 正常以下内容都不会触发,因为tsc 不会产生以下类型的log信息,不过防止意外情况
@@ -46,96 +42,75 @@ const generatorDts = async (_, config) => {
46
42
  const {
47
43
  tsconfigPath,
48
44
  distDir,
49
- sourceDirName = 'src',
50
- projectData: {
51
- appDirectory
52
- },
53
- tsCheck = false,
45
+ sourceDir = 'src',
46
+ appDirectory,
54
47
  watch = false
55
48
  } = config;
56
49
  const userTsconfig = getProjectTsconfig(tsconfigPath);
57
50
  const willDeleteTsconfigPath = utils.generatorTsConfig(userTsconfig, {
58
51
  appDirectory,
59
52
  distDir,
60
- sourceDir: sourceDirName
53
+ sourceDir
61
54
  });
62
- removeTsconfigPath = willDeleteTsconfigPath;
63
55
  const tscBinFile = getTscBinPath(appDirectory);
64
56
  const watchParams = watch ? ['-w'] : [];
65
- const childProgress = execa(tscBinFile, ['-p', willDeleteTsconfigPath, ...watchParams], {
57
+ const childProgress = execa(tscBinFile, ['-p', willDeleteTsconfigPath,
58
+ /* Required parameter, use it stdout have color */
59
+ '--pretty', // https://github.com/microsoft/TypeScript/issues/21824
60
+ '--preserveWatchOutput', ...watchParams], {
66
61
  stdio: 'pipe',
67
62
  cwd: appDirectory
68
63
  });
69
64
  resolveLog(childProgress, {
70
- tsCheck,
71
65
  watch
72
66
  });
73
67
 
74
68
  try {
75
69
  await childProgress;
76
- console.info('[TSC Compiler]: Successfully');
77
70
  } catch (e) {
78
- if (!tsCheck) {
79
- console.info(`There are some type warnings, which can be checked by configuring 'output.disableTsChecker = false'`);
80
- } // 通过使用 execa,可以将 tsc 的 data 类型的报错信息变为异常错误信息
81
- else if (isObject(e) && e.stdout) {
82
- console.error(e.stdout);
83
- } else {
84
- console.error(e);
71
+ if (e instanceof Error) {
72
+ throw new InternalDTSError(e, {
73
+ buildType: 'bundleless'
74
+ });
85
75
  }
86
76
  }
87
77
 
88
78
  fs.removeSync(willDeleteTsconfigPath);
89
79
  };
90
80
 
91
- const taskMain = async ({
92
- modernConfig
93
- }) => {
94
- const processArgv = argv(process.argv.slice(2));
95
- const config = processArgv({
96
- appDirectory: process.cwd(),
97
- srcDir: 'src',
98
- distDir: 'dist/types',
99
- tsconfigPath: './tsconfig.json',
100
- sourceDirName: 'src'
101
- });
102
- const option = {
103
- srcDir: config.srcDir,
104
- distDir: config.distDir,
105
- projectData: {
106
- appDirectory: config.appDirectory
81
+ export const genDts = async (api, config) => {
82
+ const {
83
+ outputPath,
84
+ tsconfig: tsconfigPath,
85
+ watch,
86
+ bundlelessOptions: {
87
+ sourceDir
107
88
  },
108
- tsconfigPath: config.tsconfigPath,
109
- watch: config.watch,
110
- tsCheck: config.tsCheck,
111
- sourceDirName: config.sourceDirName
112
- };
113
- await generatorDts(modernConfig, option); // // TODO: watch 模式下无法转换
114
-
115
- utils.resolveAlias(modernConfig, option);
116
- };
117
-
118
- (async () => {
119
- let options;
89
+ enableDts
90
+ } = config;
120
91
 
121
- if (process.env.CORE_INIT_OPTION_FILE) {
122
- ({
123
- options
124
- } = require(process.env.CORE_INIT_OPTION_FILE));
92
+ if (!enableDts) {
93
+ return;
125
94
  }
126
95
 
127
96
  const {
128
- resolved
129
- } = await core.cli.init([], options);
130
-
131
- (async () => {
132
- try {
133
- await taskMain({
134
- modernConfig: resolved
135
- });
136
- } catch (e) {
137
- console.error(e.message);
138
- fs.removeSync(removeTsconfigPath);
97
+ appDirectory
98
+ } = api.useAppContext();
99
+ const modernConfig = api.useResolvedConfigContext();
100
+ const {
101
+ output: {
102
+ path: distPath = 'dist'
139
103
  }
140
- })();
141
- })();
104
+ } = modernConfig;
105
+ const distDir = path.join(appDirectory, distPath, outputPath);
106
+ const option = {
107
+ appDirectory,
108
+ distDir,
109
+ tsconfigPath,
110
+ watch,
111
+ sourceDir
112
+ };
113
+ await generatorDts(modernConfig, option); // TODO: watch 模式下无法转换
114
+
115
+ utils.resolveAlias(modernConfig, option);
116
+ };
@@ -5,17 +5,16 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
5
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
6
 
7
7
  import * as path from 'path';
8
- import { Import, glob, fs } from '@modern-js/utils';
8
+ import { Import, glob, fs, nanoid } from '@modern-js/utils';
9
9
  import { merge as deepMerge } from '@modern-js/utils/lodash';
10
- const babel = Import.lazy('../../utils/babel', require);
11
- const tsPathsTransform = Import.lazy('../../utils/tspaths-transform', require);
12
- const constants = Import.lazy('../constants', require);
10
+ const babel = Import.lazy('../../../../utils/babel', require);
11
+ const tsPathsTransform = Import.lazy('../../../../utils/tspaths-transform', require);
13
12
  export const generatorTsConfig = (projectTsconfig, {
14
13
  appDirectory,
15
14
  distDir,
16
15
  sourceDir: _sourceDir = 'src'
17
16
  }) => {
18
- var _projectTsconfig$comp, _projectTsconfig$comp2, _projectTsconfig$incl, _projectTsconfig$excl;
17
+ var _projectTsconfig$comp, _projectTsconfig$comp2, _ref, _projectTsconfig$excl;
19
18
 
20
19
  const tempPath = path.resolve(appDirectory, './node_modules');
21
20
  const resolvePath = path.relative(tempPath, appDirectory); // const rootDir = projectTsconfig.compilerOptions?.rootDir
@@ -25,8 +24,9 @@ export const generatorTsConfig = (projectTsconfig, {
25
24
 
26
25
  const rootDir = path.join(resolvePath, _sourceDir);
27
26
  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']
27
+ // const include = projectTsconfig.include?.map(includePath =>
28
28
 
29
- const include = (_projectTsconfig$incl = projectTsconfig.include) === null || _projectTsconfig$incl === void 0 ? void 0 : _projectTsconfig$incl.map(includePath => path.join(resolvePath, includePath));
29
+ const include = (_ref = [_sourceDir]) === null || _ref === void 0 ? void 0 : _ref.map(includePath => path.join(resolvePath, includePath));
30
30
  const exclude = (_projectTsconfig$excl = projectTsconfig.exclude) === null || _projectTsconfig$excl === void 0 ? void 0 : _projectTsconfig$excl.map(excludePath => path.join(resolvePath, excludePath));
31
31
  const resetConfig = {
32
32
  compilerOptions: {
@@ -46,7 +46,7 @@ export const generatorTsConfig = (projectTsconfig, {
46
46
  isolatedModules: true
47
47
  }
48
48
  };
49
- const tempTsconfigPath = path.join(tempPath, constants.tempTsconfigName);
49
+ const tempTsconfigPath = path.join(tempPath, `tsconfig.${Date.now()}.${nanoid()}.json`);
50
50
  fs.ensureFileSync(tempTsconfigPath);
51
51
  fs.writeJSONSync(tempTsconfigPath, deepMerge(recommendOption, projectTsconfig, // 此处是必须要覆盖用户默认配置
52
52
  resetConfig));
@@ -64,14 +64,14 @@ export const resolveAlias = (modernConfig, config, watchFilenames = []) => {
64
64
  absolute: true
65
65
  });
66
66
  const alias = babel.getFinalAlias(modernConfig, {
67
- appDirectory: config.projectData.appDirectory,
68
- tsconfigPath: config.tsconfigPath || path.join(config.projectData.appDirectory, './tsconfig.json'),
67
+ appDirectory: config.appDirectory,
68
+ tsconfigPath: config.tsconfigPath || path.join(config.appDirectory, './tsconfig.json'),
69
69
  sourceAbsDir: config.distDir
70
70
  });
71
71
  const mergedPaths = alias.isTsPath ? alias.paths || {} : _objectSpread(_objectSpread({}, defaultPaths), alias.paths || {});
72
72
  const result = tsPathsTransform.transformDtsAlias({
73
73
  filenames: dtsFilenames,
74
- baseUrl: path.join(config.projectData.appDirectory, output.path || 'dist'),
74
+ baseUrl: path.join(config.appDirectory, output.path || 'dist'),
75
75
  paths: mergedPaths
76
76
  });
77
77
 
@@ -0,0 +1,23 @@
1
+ import { chalk } from '@modern-js/utils';
2
+ import pMap from 'p-map';
3
+ import { runBabelBuild } from "./runBabel";
4
+ import { buildStyle } from "./style";
5
+ import { genDts } from "./generator-dts";
6
+ import { copyStaticAssets } from "./copy-assets";
7
+ export const build = async (api, config, legacyOptions) => {
8
+ // valid format is umd
9
+ if (config.format === 'umd') {
10
+ console.info(chalk.yellowBright('bundleless 构建暂时不支持 umd 格式'));
11
+ return;
12
+ }
13
+
14
+ if (legacyOptions !== null && legacyOptions !== void 0 && legacyOptions.styleOnly) {
15
+ await buildStyle(api, config);
16
+ return;
17
+ }
18
+
19
+ const tasks = config.dtsOnly ? [genDts] : [runBabelBuild, genDts, buildStyle, copyStaticAssets];
20
+ await pMap(tasks, async task => {
21
+ await task(api, config);
22
+ });
23
+ };
@@ -0,0 +1,269 @@
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 path from 'path';
8
+ import { Import, glob, fs, chalk, globby, slash } from '@modern-js/utils';
9
+ import { InternalBuildError } from "../error";
10
+ import { SectionTitleStatus, watchSectionTitle } from "../utils";
11
+ export class BabelBuildError extends Error {
12
+ constructor(message, opts) {
13
+ super(message);
14
+
15
+ _defineProperty(this, "summary", void 0);
16
+
17
+ _defineProperty(this, "details", void 0);
18
+
19
+ Error.captureStackTrace(this, this.constructor);
20
+ this.summary = opts === null || opts === void 0 ? void 0 : opts.summary;
21
+ this.details = opts === null || opts === void 0 ? void 0 : opts.details;
22
+ }
23
+
24
+ toString() {
25
+ return this.formatError().join('\n');
26
+ }
27
+
28
+ formatError() {
29
+ const msgs = [];
30
+ const {
31
+ summary,
32
+ details = []
33
+ } = this;
34
+ msgs.push(chalk.red.bold(summary));
35
+
36
+ for (const detail of details) {
37
+ msgs.push(detail.content);
38
+ msgs.push('\n');
39
+ }
40
+
41
+ return msgs;
42
+ }
43
+
44
+ }
45
+ const babelCompiler = Import.lazy('@modern-js/babel-compiler', require);
46
+ const bc = Import.lazy('../../../utils/babel', require);
47
+ const ts = Import.lazy('../../../utils/tsconfig', require); // const logger: typeof import('../logger') = Import.lazy('../logger', require);
48
+
49
+ const getExts = (isTs, tsconfig) => {
50
+ // TODO: 是否受控tsconfig.json 里的jsx配置
51
+ let exts = [];
52
+
53
+ if (isTs) {
54
+ var _tsconfig$compilerOpt;
55
+
56
+ exts = tsconfig !== null && tsconfig !== void 0 && (_tsconfig$compilerOpt = tsconfig.compilerOptions) !== null && _tsconfig$compilerOpt !== void 0 && _tsconfig$compilerOpt.allowJs ? ['.ts', '.tsx', '.js', '.jsx'] : ['.ts', '.tsx'];
57
+ } else {
58
+ exts = ['.js', '.jsx'];
59
+ }
60
+
61
+ return exts;
62
+ };
63
+
64
+ export const getWillCompilerCode = (srcDirOrFile, option) => {
65
+ const {
66
+ tsconfig,
67
+ isTsProject
68
+ } = option; // 如果是一个文件路径,则直接返回
69
+
70
+ if (fs.existsSync(srcDirOrFile) && fs.lstatSync(srcDirOrFile).isFile()) {
71
+ return [srcDirOrFile];
72
+ }
73
+
74
+ const exts = getExts(isTsProject, tsconfig);
75
+ const globPattern = `${srcDirOrFile}/**/*{${exts.join(',')}}`;
76
+ const files = glob.sync(globPattern, {
77
+ ignore: [`${srcDirOrFile}/**/*.d.ts`],
78
+ absolute: true
79
+ });
80
+ return files;
81
+ };
82
+ export const buildSourceCode = async config => {
83
+ const {
84
+ willCompilerDirOrFile,
85
+ tsconfigPath,
86
+ babelConfig,
87
+ srcRootDir,
88
+ distDir,
89
+ watch
90
+ } = config;
91
+ const tsconfig = ts.readTsConfig(tsconfigPath);
92
+ const willCompilerFiles = getWillCompilerCode(willCompilerDirOrFile, {
93
+ tsconfig,
94
+ isTsProject: Boolean(tsconfig)
95
+ });
96
+
97
+ if (watch) {
98
+ const emitter = await babelCompiler.compiler({
99
+ quiet: true,
100
+ enableVirtualDist: true,
101
+ enableWatch: true,
102
+ rootDir: srcRootDir,
103
+ filenames: willCompilerFiles,
104
+ distDir,
105
+ watchDir: srcRootDir,
106
+ extensions: getExts(Boolean(tsconfig), tsconfig),
107
+ ignore: ['*.d.ts']
108
+ }, babelConfig);
109
+ return emitter;
110
+ } else {
111
+ return babelCompiler.compiler({
112
+ quiet: true,
113
+ enableVirtualDist: true,
114
+ rootDir: srcRootDir,
115
+ filenames: willCompilerFiles,
116
+ distDir,
117
+ // enableWatch: watch,
118
+ ignore: ['*.d.ts']
119
+ }, babelConfig);
120
+ }
121
+ };
122
+
123
+ const generatorRealFiles = virtualDists => {
124
+ for (const virtualDist of virtualDists) {
125
+ const {
126
+ distPath,
127
+ code,
128
+ sourcemap,
129
+ sourceMapPath
130
+ } = virtualDist;
131
+ fs.ensureFileSync(distPath);
132
+ fs.writeFileSync(distPath, code);
133
+
134
+ if (sourcemap.length > 0) {
135
+ fs.ensureFileSync(sourceMapPath);
136
+ fs.writeFileSync(sourceMapPath, sourcemap);
137
+ }
138
+ }
139
+ };
140
+ /**
141
+ * when modern build, only throw Error or silent
142
+ * @param outputResults
143
+ * @param context
144
+ */
145
+
146
+
147
+ const outputDist = (outputResults, context) => {
148
+ const {
149
+ code,
150
+ message,
151
+ messageDetails,
152
+ virtualDists = []
153
+ } = outputResults;
154
+
155
+ if (code === 0) {
156
+ generatorRealFiles(virtualDists);
157
+ } else if (messageDetails && messageDetails.length > 0) {
158
+ const babelError = new BabelBuildError('bundleless failed', {
159
+ summary: message,
160
+ details: messageDetails
161
+ });
162
+ throw new InternalBuildError(babelError, _objectSpread(_objectSpread({}, context), {}, {
163
+ buildType: 'bundleless'
164
+ }));
165
+ }
166
+ };
167
+
168
+ export const jsFileSuffix = ['js', 'jsx', 'ts', 'tsx'];
169
+ export const haveNotAnyJsFile = async sourceDir => {
170
+ const files = await globby(slash(`${sourceDir}/**/*.{${jsFileSuffix.join(',')}}`));
171
+ return files.length === 0;
172
+ };
173
+ export const runBabelBuild = async (api, config) => {
174
+ const {
175
+ bundlelessOptions,
176
+ tsconfig,
177
+ target,
178
+ format,
179
+ outputPath,
180
+ watch,
181
+ sourceMap
182
+ } = config;
183
+ const {
184
+ appDirectory
185
+ } = api.useAppContext();
186
+ const modernConfig = api.useResolvedConfigContext();
187
+ const {
188
+ output: {
189
+ path: distPath = 'dist'
190
+ }
191
+ } = modernConfig;
192
+ const {
193
+ sourceDir = './src'
194
+ } = bundlelessOptions;
195
+ const sourceAbsDir = path.join(appDirectory, sourceDir);
196
+ const tsconfigPath = path.join(appDirectory, tsconfig);
197
+
198
+ if (await haveNotAnyJsFile(sourceAbsDir)) {
199
+ return;
200
+ } // TODO: Refactoring based on format and target
201
+
202
+
203
+ const syntax = target === 'es5' ? 'es5' : 'es6+';
204
+ const type = format === 'cjs' ? 'commonjs' : 'module';
205
+ const titleText = `[Bundleless:Babel: ${format}_${target}]`;
206
+ const buildConfig = {
207
+ format,
208
+ target,
209
+ babelConfig: bc.resolveBabelConfig(appDirectory, modernConfig, sourceMap, bundlelessOptions, {
210
+ sourceAbsDir,
211
+ tsconfigPath,
212
+ syntax,
213
+ type
214
+ })
215
+ };
216
+ const distDir = path.join(appDirectory, distPath, outputPath);
217
+ const result = await buildSourceCode({
218
+ appDirectory,
219
+ distDir,
220
+ srcRootDir: sourceAbsDir,
221
+ willCompilerDirOrFile: sourceAbsDir,
222
+ tsconfigPath,
223
+ babelConfig: buildConfig.babelConfig,
224
+ watch
225
+ });
226
+
227
+ if (watch) {
228
+ const emitter = result;
229
+ emitter.on(babelCompiler.BuildWatchEvent.firstCompiler, result => {
230
+ if (result.code === 1) {
231
+ console.info(watchSectionTitle(titleText, SectionTitleStatus.Fail));
232
+ console.error(result.message);
233
+
234
+ for (const detail of result.messageDetails || []) {
235
+ console.error(detail.content);
236
+ }
237
+ } else {
238
+ generatorRealFiles(result.virtualDists);
239
+ console.info(watchSectionTitle(titleText, SectionTitleStatus.Success));
240
+ }
241
+ });
242
+ emitter.on(babelCompiler.BuildWatchEvent.watchingCompiler, result => {
243
+ if (result.code === 1) {
244
+ var _result$virtualDists;
245
+
246
+ // console.error(logger.clearFlag);
247
+ console.info(watchSectionTitle(titleText, SectionTitleStatus.Fail));
248
+ console.error(result.message);
249
+
250
+ for (const detail of result.messageDetails || []) {
251
+ console.error(detail.content);
252
+ }
253
+
254
+ if (Array.isArray(result.virtualDists) && ((_result$virtualDists = result.virtualDists) === null || _result$virtualDists === void 0 ? void 0 : _result$virtualDists.length) > 0) {
255
+ generatorRealFiles(result.virtualDists);
256
+ }
257
+ } else {
258
+ generatorRealFiles(result.virtualDists);
259
+ console.info(watchSectionTitle(titleText, SectionTitleStatus.Success));
260
+ }
261
+ });
262
+ await emitter.watch();
263
+ } else {
264
+ outputDist(result, {
265
+ target,
266
+ format
267
+ });
268
+ }
269
+ };