@modern-js/babel-compiler 2.69.4 → 3.0.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 (55) hide show
  1. package/dist/cjs/build.js +68 -73
  2. package/dist/cjs/buildWatch.js +111 -116
  3. package/dist/cjs/compiler.js +103 -116
  4. package/dist/cjs/compilerErrorResult.js +61 -56
  5. package/dist/cjs/constants.js +36 -28
  6. package/dist/cjs/defaults.js +46 -38
  7. package/dist/cjs/getFinalOption.js +82 -83
  8. package/dist/cjs/index.js +92 -44
  9. package/dist/cjs/type.js +17 -15
  10. package/dist/cjs/utils.js +34 -37
  11. package/dist/cjs/validate.js +70 -64
  12. package/dist/esm/build.mjs +42 -0
  13. package/dist/esm/buildWatch.mjs +78 -0
  14. package/dist/esm/compiler.mjs +76 -0
  15. package/dist/esm/compilerErrorResult.mjs +34 -0
  16. package/dist/esm/constants.mjs +7 -0
  17. package/dist/esm/defaults.mjs +17 -0
  18. package/dist/esm/getFinalOption.mjs +50 -0
  19. package/dist/{esm-node/index.js → esm/index.mjs} +7 -14
  20. package/dist/esm/utils.mjs +5 -0
  21. package/dist/esm/validate.mjs +32 -0
  22. package/dist/esm-node/build.mjs +42 -0
  23. package/dist/esm-node/buildWatch.mjs +78 -0
  24. package/dist/esm-node/compiler.mjs +76 -0
  25. package/dist/esm-node/compilerErrorResult.mjs +34 -0
  26. package/dist/esm-node/constants.mjs +7 -0
  27. package/dist/esm-node/defaults.mjs +17 -0
  28. package/dist/esm-node/getFinalOption.mjs +50 -0
  29. package/dist/esm-node/index.mjs +14 -0
  30. package/dist/esm-node/utils.mjs +5 -0
  31. package/dist/esm-node/validate.mjs +32 -0
  32. package/package.json +24 -17
  33. package/rslib.config.mts +4 -0
  34. package/rstest.config.ts +7 -0
  35. package/dist/esm/build.js +0 -105
  36. package/dist/esm/buildWatch.js +0 -157
  37. package/dist/esm/compiler.js +0 -95
  38. package/dist/esm/compilerErrorResult.js +0 -71
  39. package/dist/esm/constants.js +0 -9
  40. package/dist/esm/defaults.js +0 -19
  41. package/dist/esm/getFinalOption.js +0 -68
  42. package/dist/esm/index.js +0 -46
  43. package/dist/esm/utils.js +0 -7
  44. package/dist/esm/validate.js +0 -44
  45. package/dist/esm-node/build.js +0 -57
  46. package/dist/esm-node/buildWatch.js +0 -89
  47. package/dist/esm-node/compiler.js +0 -95
  48. package/dist/esm-node/compilerErrorResult.js +0 -39
  49. package/dist/esm-node/constants.js +0 -9
  50. package/dist/esm-node/defaults.js +0 -19
  51. package/dist/esm-node/getFinalOption.js +0 -67
  52. package/dist/esm-node/utils.js +0 -8
  53. package/dist/esm-node/validate.js +0 -44
  54. /package/dist/esm/{type.js → type.mjs} +0 -0
  55. /package/dist/esm-node/{type.js → type.mjs} +0 -0
@@ -0,0 +1,78 @@
1
+ import { EventEmitter } from "events";
2
+ import { normalize, relative } from "path";
3
+ import { WatchChangeType, logger, watch } from "@modern-js/utils";
4
+ import { build } from "./build.mjs";
5
+ import { CompilerErrorResult } from "./compilerErrorResult.mjs";
6
+ const BuildWatchEvent = {
7
+ firstCompiler: 'first-compiler',
8
+ compiling: 'compiling',
9
+ watchingCompiler: 'watching-compiler'
10
+ };
11
+ class BuildWatchEmitter extends EventEmitter {
12
+ setInitFn(fn) {
13
+ this._initFn = fn;
14
+ }
15
+ async watch() {
16
+ if ('function' == typeof this._initFn) return this._initFn(this);
17
+ return null;
18
+ }
19
+ }
20
+ const runBuildWatch = async (option, babelConfig = {}, emitter)=>{
21
+ emitter.emit(BuildWatchEvent.compiling);
22
+ const errorResult = new CompilerErrorResult();
23
+ const watchDir = option.watchDir;
24
+ const { distDir, quiet } = option;
25
+ const firstBuildResult = await build(option, babelConfig);
26
+ const { code } = firstBuildResult;
27
+ if (1 === code) {
28
+ errorResult.init(firstBuildResult);
29
+ emitter.emit(BuildWatchEvent.firstCompiler, errorResult.value);
30
+ } else emitter.emit(BuildWatchEvent.firstCompiler, firstBuildResult);
31
+ return watch(`${watchDir}/**/*.{js,jsx,ts,tsx}`, async ({ changeType, changedFilePath })=>{
32
+ emitter.emit(BuildWatchEvent.compiling);
33
+ if (changeType === WatchChangeType.UNLINK) {
34
+ const removeFiles = [
35
+ normalize(`./${distDir}/${relative(watchDir, changedFilePath)}`)
36
+ ];
37
+ if (!quiet) logger.info(`remove file: ${removeFiles.join(',')}`);
38
+ const result = {
39
+ code: 0,
40
+ message: `remove file: ${removeFiles.join(',')}`,
41
+ removeFiles
42
+ };
43
+ emitter.emit(BuildWatchEvent.watchingCompiler, result);
44
+ return;
45
+ }
46
+ const result = await build({
47
+ ...option,
48
+ filenames: [
49
+ changedFilePath
50
+ ]
51
+ }, babelConfig);
52
+ if (1 === result.code) {
53
+ errorResult.update(result.messageDetails || []);
54
+ emitter.emit(BuildWatchEvent.watchingCompiler, errorResult.value);
55
+ quiet || logger.info(errorResult.value.message);
56
+ } else {
57
+ errorResult.removeByFileName(changedFilePath);
58
+ if (errorResult.checkExistError()) {
59
+ emitter.emit(BuildWatchEvent.watchingCompiler, {
60
+ ...errorResult.value,
61
+ virtualDists: result.virtualDists
62
+ });
63
+ quiet || logger.info(errorResult.value.message);
64
+ } else {
65
+ emitter.emit(BuildWatchEvent.watchingCompiler, result);
66
+ quiet || logger.info(result.message);
67
+ }
68
+ }
69
+ }, [
70
+ `${watchDir}/**/*.d.ts`
71
+ ]);
72
+ };
73
+ const buildWatch = (option, babelConfig = {})=>{
74
+ const buildWatchEmitter = new BuildWatchEmitter();
75
+ buildWatchEmitter.setInitFn(runBuildWatch.bind(null, option, babelConfig));
76
+ return buildWatchEmitter;
77
+ };
78
+ export { BuildWatchEmitter, BuildWatchEvent, buildWatch, runBuildWatch };
@@ -0,0 +1,76 @@
1
+ import { basename, dirname, extname, join, relative } from "path";
2
+ import { transformFileSync } from "@babel/core";
3
+ import { fs, logger } from "@modern-js/utils";
4
+ import { defaultDistFileExtMap } from "./constants.mjs";
5
+ import { addSourceMappingUrl } from "./utils.mjs";
6
+ const defaultDistDir = 'dist';
7
+ const isRes = (r)=>Boolean(r);
8
+ const getDistFilePath = (option)=>{
9
+ const { filepath, rootDir, distDir, extMap } = option;
10
+ const ext = extname(filepath);
11
+ return join(distDir, relative(rootDir, filepath).replace(ext, extMap[ext]));
12
+ };
13
+ const resolveSourceMap = (option)=>{
14
+ const { babelRes, sourceFilePath, distFilePath, enableVirtualDist = false } = option;
15
+ const mapLoc = `${distFilePath}.map`;
16
+ babelRes.code = addSourceMappingUrl(babelRes.code, mapLoc);
17
+ if (babelRes.map) {
18
+ babelRes.map.file = basename(distFilePath);
19
+ babelRes.map.sources = [
20
+ relative(dirname(distFilePath), sourceFilePath)
21
+ ];
22
+ }
23
+ const sourceMapVirtualDist = {
24
+ sourcemap: JSON.stringify(babelRes.map),
25
+ sourceMapPath: mapLoc
26
+ };
27
+ if (enableVirtualDist) return sourceMapVirtualDist;
28
+ fs.ensureDirSync(dirname(mapLoc));
29
+ fs.writeFileSync(mapLoc, JSON.stringify(babelRes.map));
30
+ return sourceMapVirtualDist;
31
+ };
32
+ const compiler = (option)=>{
33
+ const { filepath, rootDir, enableVirtualDist = false, distDir = join(dirname(rootDir), defaultDistDir), verbose = false, babelConfig = {}, distFileExtMap = defaultDistFileExtMap, quiet = false } = option;
34
+ const babelRes = transformFileSync(filepath, babelConfig);
35
+ let virtualDist = null;
36
+ if (!isRes(babelRes)) throw new Error(`${filepath} happen error`);
37
+ const distFilePath = getDistFilePath({
38
+ filepath,
39
+ rootDir,
40
+ distDir,
41
+ extMap: distFileExtMap
42
+ });
43
+ if (enableVirtualDist) virtualDist = {
44
+ distPath: distFilePath,
45
+ sourceMapPath: '',
46
+ code: '',
47
+ sourcemap: ''
48
+ };
49
+ if (babelRes?.map && babelConfig.sourceMaps && 'inline' !== babelConfig.sourceMaps) if (virtualDist) virtualDist = {
50
+ ...virtualDist,
51
+ ...resolveSourceMap({
52
+ babelRes,
53
+ sourceFilePath: filepath,
54
+ distFilePath,
55
+ enableVirtualDist
56
+ })
57
+ };
58
+ else resolveSourceMap({
59
+ babelRes,
60
+ sourceFilePath: filepath,
61
+ distFilePath,
62
+ enableVirtualDist
63
+ });
64
+ if (virtualDist) virtualDist = {
65
+ ...virtualDist,
66
+ distPath: distFilePath,
67
+ code: babelRes.code
68
+ };
69
+ else {
70
+ fs.ensureDirSync(dirname(distFilePath));
71
+ fs.writeFileSync(distFilePath, babelRes.code);
72
+ }
73
+ if (verbose && !quiet) logger.info(`${filepath} => ${distFilePath}`);
74
+ return virtualDist;
75
+ };
76
+ export { compiler, getDistFilePath, isRes, resolveSourceMap };
@@ -0,0 +1,34 @@
1
+ class CompilerErrorResult {
2
+ init(initErrorResult) {
3
+ this._messageDetails = initErrorResult?.messageDetails || [];
4
+ }
5
+ update(messageDetails) {
6
+ for (const messageDetail of messageDetails){
7
+ const addError = !this._messageDetails.some((detail)=>{
8
+ if (detail.filename === messageDetail.filename) {
9
+ detail.content = messageDetail.content;
10
+ return true;
11
+ }
12
+ return false;
13
+ });
14
+ if (addError) this._messageDetails.push(messageDetail);
15
+ }
16
+ }
17
+ removeByFileName(filename) {
18
+ this._messageDetails = this._messageDetails.filter((detail)=>detail.filename !== filename);
19
+ }
20
+ get value() {
21
+ return {
22
+ code: 1,
23
+ message: `Compilation failure ${this._messageDetails?.length} files with Babel.`,
24
+ messageDetails: this._messageDetails
25
+ };
26
+ }
27
+ checkExistError() {
28
+ return this._messageDetails.length > 0;
29
+ }
30
+ constructor(initErrorResult){
31
+ this.init(initErrorResult);
32
+ }
33
+ }
34
+ export { CompilerErrorResult };
@@ -0,0 +1,7 @@
1
+ const defaultDistFileExtMap = {
2
+ '.js': '.js',
3
+ '.jsx': '.js',
4
+ '.ts': '.js',
5
+ '.tsx': '.js'
6
+ };
7
+ export { defaultDistFileExtMap };
@@ -0,0 +1,17 @@
1
+ import { defaultDistFileExtMap } from "./constants.mjs";
2
+ const defaultOptions = {
3
+ enableWatch: false,
4
+ enableVirtualDist: false,
5
+ extensions: [],
6
+ filenames: [],
7
+ distFileExtMap: defaultDistFileExtMap,
8
+ ignore: [],
9
+ quiet: false,
10
+ verbose: false,
11
+ clean: false
12
+ };
13
+ const mergeDefaultOption = (compilerOptions)=>({
14
+ ...defaultOptions,
15
+ ...compilerOptions
16
+ });
17
+ export { mergeDefaultOption };
@@ -0,0 +1,50 @@
1
+ import { DEFAULT_EXTENSIONS } from "@babel/core";
2
+ import { glob } from "@modern-js/utils";
3
+ import { mergeDefaultOption } from "./defaults.mjs";
4
+ const getGlobPattern = (dir, extensions)=>{
5
+ if (extensions.length > 1) return `${dir}/**/*{${extensions.join(',')}}`;
6
+ if (1 === extensions.length) return `${dir}/**/*${extensions[0]}`;
7
+ return `${dir}/**/*`;
8
+ };
9
+ const getFinalExtensions = (extensions)=>{
10
+ const isExtensions = (ext)=>Array.isArray(ext);
11
+ const isExtensionsFunc = (ext)=>'function' == typeof ext;
12
+ if (isExtensions(extensions)) return [
13
+ ...extensions,
14
+ ...DEFAULT_EXTENSIONS
15
+ ];
16
+ if (isExtensionsFunc(extensions)) return extensions(DEFAULT_EXTENSIONS);
17
+ return DEFAULT_EXTENSIONS;
18
+ };
19
+ const getFilesFromDir = ({ dir, finalExt = [], ignore = [] })=>{
20
+ let globFindFilenames = [];
21
+ const globPattern = getGlobPattern(dir, finalExt);
22
+ globFindFilenames = glob.sync(globPattern, {
23
+ ignore
24
+ });
25
+ return globFindFilenames;
26
+ };
27
+ const getFinalCompilerOption = (option)=>{
28
+ const optionWithDefault = mergeDefaultOption(option);
29
+ const { sourceDir, ignore, enableWatch = false, watchDir, extensions = DEFAULT_EXTENSIONS } = option;
30
+ let globFindFilenames = [];
31
+ const finalExt = getFinalExtensions(extensions);
32
+ if (sourceDir) globFindFilenames = getFilesFromDir({
33
+ dir: sourceDir,
34
+ ignore,
35
+ finalExt
36
+ });
37
+ if (enableWatch) globFindFilenames = getFilesFromDir({
38
+ dir: watchDir,
39
+ ignore,
40
+ finalExt
41
+ });
42
+ return {
43
+ ...optionWithDefault,
44
+ filenames: [
45
+ ...optionWithDefault.filenames,
46
+ ...globFindFilenames
47
+ ]
48
+ };
49
+ };
50
+ export { getFilesFromDir, getFinalCompilerOption, getFinalExtensions, getGlobPattern };
@@ -0,0 +1,14 @@
1
+ import { build } from "./build.mjs";
2
+ import { buildWatch } from "./buildWatch.mjs";
3
+ import { getFinalCompilerOption } from "./getFinalOption.mjs";
4
+ import { validate } from "./validate.mjs";
5
+ export * from "./buildWatch.mjs";
6
+ export * from "./type.mjs";
7
+ async function compiler(compilerOptions, babelOptions = {}) {
8
+ const validRet = validate(compilerOptions);
9
+ if (validRet) return validRet;
10
+ const finalCompilerOption = getFinalCompilerOption(compilerOptions);
11
+ if (compilerOptions.enableWatch) return buildWatch(finalCompilerOption, babelOptions);
12
+ return build(finalCompilerOption, babelOptions);
13
+ }
14
+ export { compiler };
@@ -0,0 +1,5 @@
1
+ import { basename, normalize } from "path";
2
+ function addSourceMappingUrl(code, loc) {
3
+ return `${code}\n//# sourceMappingURL=${normalize(basename(loc))}`;
4
+ }
5
+ export { addSourceMappingUrl };
@@ -0,0 +1,32 @@
1
+ import { logger } from "@modern-js/utils";
2
+ const sourceDirAndFileNamesValidMessage = 'At least one of the sourceDir and filenames configurations must be configured';
3
+ const watchDirValidMessage = 'should set watchDir when enableWatch is true';
4
+ const validateSourceDirAndFileNames = (compilerOptions)=>{
5
+ const { sourceDir, filenames, quiet } = compilerOptions;
6
+ if (!sourceDir && !filenames) {
7
+ if (!quiet) logger.error(sourceDirAndFileNamesValidMessage);
8
+ return {
9
+ code: 1,
10
+ message: sourceDirAndFileNamesValidMessage,
11
+ virtualDists: []
12
+ };
13
+ }
14
+ return null;
15
+ };
16
+ const validateWatchDir = (compilerOptions)=>{
17
+ const { watchDir, enableWatch, quiet } = compilerOptions;
18
+ if (enableWatch && !watchDir) {
19
+ if (!quiet) logger.error(watchDirValidMessage);
20
+ return {
21
+ code: 1,
22
+ message: watchDirValidMessage,
23
+ virtualDists: []
24
+ };
25
+ }
26
+ return null;
27
+ };
28
+ const validate = (compilerOptions)=>{
29
+ if (compilerOptions.enableWatch) return validateWatchDir(compilerOptions);
30
+ return validateSourceDirAndFileNames(compilerOptions);
31
+ };
32
+ export { sourceDirAndFileNamesValidMessage, validate, validateSourceDirAndFileNames, validateWatchDir, watchDirValidMessage };
package/package.json CHANGED
@@ -15,37 +15,44 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.69.4",
18
+ "version": "3.0.0-alpha.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
22
- "module": "./dist/esm/index.js",
22
+ "module": "./dist/esm/index.mjs",
23
23
  "exports": {
24
24
  ".": {
25
+ "types": "./dist/types/index.d.ts",
26
+ "jsnext:source": "./src/index.ts",
25
27
  "node": {
26
- "jsnext:source": "./src/index.ts",
27
- "import": "./dist/esm-node/index.js",
28
+ "import": "./dist/esm-node/index.mjs",
28
29
  "require": "./dist/cjs/index.js"
29
30
  },
30
- "default": "./dist/esm/index.js"
31
+ "default": "./dist/esm/index.mjs"
32
+ }
33
+ },
34
+ "typesVersions": {
35
+ "*": {
36
+ ".": [
37
+ "./dist/types/index.d.ts"
38
+ ]
31
39
  }
32
40
  },
33
41
  "dependencies": {
34
- "@babel/core": "^7.26.0",
42
+ "@babel/core": "^7.28.5",
35
43
  "@swc/helpers": "^0.5.17",
36
- "@modern-js/utils": "2.69.4"
44
+ "@modern-js/utils": "3.0.0-alpha.0"
37
45
  },
38
46
  "devDependencies": {
39
- "@babel/plugin-transform-classes": "^7.22.15",
40
- "@babel/preset-typescript": "^7.26.0",
47
+ "@babel/plugin-transform-classes": "^7.28.4",
48
+ "@babel/preset-typescript": "^7.28.5",
49
+ "@rslib/core": "0.18.5",
41
50
  "@types/babel__core": "^7.20.5",
42
51
  "@types/glob": "7.2.0",
43
- "@types/jest": "^29",
44
- "@types/node": "^18",
45
- "jest": "^29",
52
+ "@types/node": "^20",
46
53
  "typescript": "^5",
47
- "@scripts/build": "2.66.0",
48
- "@scripts/jest-config": "2.66.0"
54
+ "@scripts/rstest-config": "2.66.0",
55
+ "@modern-js/rslib": "2.68.10"
49
56
  },
50
57
  "sideEffects": false,
51
58
  "publishConfig": {
@@ -53,8 +60,8 @@
53
60
  "access": "public"
54
61
  },
55
62
  "scripts": {
56
- "new": "modern-lib new",
57
- "build": "modern-lib build",
58
- "test": "jest --passWithNoTests"
63
+ "dev": "rslib build --watch",
64
+ "build": "rslib build",
65
+ "test": "rstest --passWithNoTests"
59
66
  }
60
67
  }
@@ -0,0 +1,4 @@
1
+ import { rslibConfig } from '@modern-js/rslib';
2
+ import { defineConfig } from '@rslib/core';
3
+
4
+ export default defineConfig(rslibConfig);
@@ -0,0 +1,7 @@
1
+ import { withTestPreset } from '@scripts/rstest-config';
2
+
3
+ export default withTestPreset({
4
+ root: __dirname,
5
+ testEnvironment: 'node',
6
+ globals: true,
7
+ });
package/dist/esm/build.js DELETED
@@ -1,105 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
- import { fs, logger } from "@modern-js/utils";
4
- import { compiler } from "./compiler";
5
- import { defaultDistFileExtMap } from "./constants";
6
- var build = function() {
7
- var _ref = _async_to_generator(function(option) {
8
- var babelConfig, rootDir, enableVirtualDist, filenames, clean, distDir, _option_distFileExtMap, distFileExtMap, _option_verbose, verbose, _option_quiet, quiet, virtualDists, messageDetails, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, filename, dist, happenError;
9
- var _arguments = arguments;
10
- return _ts_generator(this, function(_state) {
11
- switch (_state.label) {
12
- case 0:
13
- babelConfig = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {};
14
- rootDir = option.rootDir, enableVirtualDist = option.enableVirtualDist, filenames = option.filenames, clean = option.clean, distDir = option.distDir, _option_distFileExtMap = option.distFileExtMap, distFileExtMap = _option_distFileExtMap === void 0 ? defaultDistFileExtMap : _option_distFileExtMap, _option_verbose = option.verbose, verbose = _option_verbose === void 0 ? false : _option_verbose, _option_quiet = option.quiet, quiet = _option_quiet === void 0 ? false : _option_quiet;
15
- virtualDists = [];
16
- if (!clean)
17
- return [
18
- 3,
19
- 2
20
- ];
21
- return [
22
- 4,
23
- fs.remove(distDir)
24
- ];
25
- case 1:
26
- _state.sent();
27
- _state.label = 2;
28
- case 2:
29
- fs.ensureDir(distDir);
30
- messageDetails = [];
31
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
32
- try {
33
- for (_iterator = filenames[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
34
- filename = _step.value;
35
- try {
36
- dist = compiler({
37
- rootDir,
38
- enableVirtualDist,
39
- filepath: filename,
40
- distDir,
41
- verbose,
42
- quiet,
43
- babelConfig,
44
- distFileExtMap
45
- });
46
- if (enableVirtualDist && dist) {
47
- virtualDists.push(dist);
48
- }
49
- } catch (e) {
50
- messageDetails.push({
51
- filename,
52
- content: e.toString()
53
- });
54
- }
55
- }
56
- } catch (err) {
57
- _didIteratorError = true;
58
- _iteratorError = err;
59
- } finally {
60
- try {
61
- if (!_iteratorNormalCompletion && _iterator.return != null) {
62
- _iterator.return();
63
- }
64
- } finally {
65
- if (_didIteratorError) {
66
- throw _iteratorError;
67
- }
68
- }
69
- }
70
- happenError = messageDetails.length > 0;
71
- if (!quiet) {
72
- if (happenError) {
73
- logger.error("Compilation failure ".concat(messageDetails.length, " ").concat(messageDetails.length !== 1 ? "files" : "file", " with Babel."));
74
- } else {
75
- logger.info("Successfully compiled ".concat(filenames.length, " ").concat(filenames.length !== 1 ? "files" : "file", " with Babel."));
76
- }
77
- }
78
- if (happenError) {
79
- return [
80
- 2,
81
- {
82
- code: 1,
83
- message: "Compilation failure ".concat(messageDetails.length, " ").concat(messageDetails.length !== 1 ? "files" : "file", " with Babel."),
84
- messageDetails
85
- }
86
- ];
87
- }
88
- return [
89
- 2,
90
- {
91
- code: 0,
92
- message: "Successfully compiled ".concat(filenames.length, " ").concat(filenames.length !== 1 ? "files" : "file", " with Babel."),
93
- virtualDists
94
- }
95
- ];
96
- }
97
- });
98
- });
99
- return function build2(option) {
100
- return _ref.apply(this, arguments);
101
- };
102
- }();
103
- export {
104
- build
105
- };
@@ -1,157 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _call_super } from "@swc/helpers/_/_call_super";
3
- import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
4
- import { _ as _inherits } from "@swc/helpers/_/_inherits";
5
- import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
6
- import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
7
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
8
- import * as Event from "events";
9
- import * as path from "path";
10
- import { WatchChangeType, logger, watch } from "@modern-js/utils";
11
- import { build } from "./build";
12
- import { CompilerErrorResult } from "./compilerErrorResult";
13
- var BuildWatchEvent = {
14
- firstCompiler: "first-compiler",
15
- compiling: "compiling",
16
- watchingCompiler: "watching-compiler"
17
- };
18
- var BuildWatchEmitter = /* @__PURE__ */ function(_Event_EventEmitter) {
19
- "use strict";
20
- _inherits(BuildWatchEmitter2, _Event_EventEmitter);
21
- function BuildWatchEmitter2() {
22
- _class_call_check(this, BuildWatchEmitter2);
23
- return _call_super(this, BuildWatchEmitter2, arguments);
24
- }
25
- var _proto = BuildWatchEmitter2.prototype;
26
- _proto.setInitFn = function setInitFn(fn) {
27
- this._initFn = fn;
28
- };
29
- _proto.watch = function watch2() {
30
- var _this = this;
31
- return _async_to_generator(function() {
32
- return _ts_generator(this, function(_state) {
33
- if (typeof _this._initFn === "function") {
34
- return [
35
- 2,
36
- _this._initFn(_this)
37
- ];
38
- }
39
- return [
40
- 2,
41
- null
42
- ];
43
- });
44
- })();
45
- };
46
- return BuildWatchEmitter2;
47
- }(Event.EventEmitter);
48
- var runBuildWatch = function() {
49
- var _ref = _async_to_generator(function(option) {
50
- var babelConfig, emitter, errorResult, watchDir, distDir, quiet, firstBuildResult, code;
51
- var _arguments = arguments;
52
- return _ts_generator(this, function(_state) {
53
- switch (_state.label) {
54
- case 0:
55
- babelConfig = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {}, emitter = _arguments.length > 2 ? _arguments[2] : void 0;
56
- emitter.emit(BuildWatchEvent.compiling);
57
- errorResult = new CompilerErrorResult();
58
- watchDir = option.watchDir;
59
- distDir = option.distDir, quiet = option.quiet;
60
- return [
61
- 4,
62
- build(option, babelConfig)
63
- ];
64
- case 1:
65
- firstBuildResult = _state.sent();
66
- code = firstBuildResult.code;
67
- if (code === 1) {
68
- errorResult.init(firstBuildResult);
69
- emitter.emit(BuildWatchEvent.firstCompiler, errorResult.value);
70
- } else {
71
- emitter.emit(BuildWatchEvent.firstCompiler, firstBuildResult);
72
- }
73
- return [
74
- 2,
75
- watch("".concat(watchDir, "/**/*.{js,jsx,ts,tsx}"), function() {
76
- var _ref2 = _async_to_generator(function(param) {
77
- var changeType, changedFilePath, removeFiles, result, result1;
78
- return _ts_generator(this, function(_state2) {
79
- switch (_state2.label) {
80
- case 0:
81
- changeType = param.changeType, changedFilePath = param.changedFilePath;
82
- emitter.emit(BuildWatchEvent.compiling);
83
- if (changeType === WatchChangeType.UNLINK) {
84
- removeFiles = [
85
- path.normalize("./".concat(distDir, "/").concat(path.relative(watchDir, changedFilePath)))
86
- ];
87
- if (!quiet) {
88
- logger.info("remove file: ".concat(removeFiles.join(",")));
89
- }
90
- result = {
91
- code: 0,
92
- message: "remove file: ".concat(removeFiles.join(",")),
93
- removeFiles
94
- };
95
- emitter.emit(BuildWatchEvent.watchingCompiler, result);
96
- return [
97
- 2
98
- ];
99
- }
100
- return [
101
- 4,
102
- build(_object_spread_props(_object_spread({}, option), {
103
- filenames: [
104
- changedFilePath
105
- ]
106
- }), babelConfig)
107
- ];
108
- case 1:
109
- result1 = _state2.sent();
110
- if (result1.code === 1) {
111
- errorResult.update(result1.messageDetails || []);
112
- emitter.emit(BuildWatchEvent.watchingCompiler, errorResult.value);
113
- !quiet && logger.info(errorResult.value.message);
114
- } else {
115
- errorResult.removeByFileName(changedFilePath);
116
- if (errorResult.checkExistError()) {
117
- emitter.emit(BuildWatchEvent.watchingCompiler, _object_spread_props(_object_spread({}, errorResult.value), {
118
- virtualDists: result1.virtualDists
119
- }));
120
- !quiet && logger.info(errorResult.value.message);
121
- } else {
122
- emitter.emit(BuildWatchEvent.watchingCompiler, result1);
123
- !quiet && logger.info(result1.message);
124
- }
125
- }
126
- return [
127
- 2
128
- ];
129
- }
130
- });
131
- });
132
- return function(_) {
133
- return _ref2.apply(this, arguments);
134
- };
135
- }(), [
136
- "".concat(watchDir, "/**/*.d.ts")
137
- ])
138
- ];
139
- }
140
- });
141
- });
142
- return function runBuildWatch2(option) {
143
- return _ref.apply(this, arguments);
144
- };
145
- }();
146
- var buildWatch = function(option) {
147
- var babelConfig = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
148
- var buildWatchEmitter = new BuildWatchEmitter();
149
- buildWatchEmitter.setInitFn(runBuildWatch.bind(null, option, babelConfig));
150
- return buildWatchEmitter;
151
- };
152
- export {
153
- BuildWatchEmitter,
154
- BuildWatchEvent,
155
- buildWatch,
156
- runBuildWatch
157
- };