@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.
- package/dist/cjs/build.js +68 -73
- package/dist/cjs/buildWatch.js +111 -116
- package/dist/cjs/compiler.js +103 -116
- package/dist/cjs/compilerErrorResult.js +61 -56
- package/dist/cjs/constants.js +36 -28
- package/dist/cjs/defaults.js +46 -38
- package/dist/cjs/getFinalOption.js +82 -83
- package/dist/cjs/index.js +92 -44
- package/dist/cjs/type.js +17 -15
- package/dist/cjs/utils.js +34 -37
- package/dist/cjs/validate.js +70 -64
- package/dist/esm/build.mjs +42 -0
- package/dist/esm/buildWatch.mjs +78 -0
- package/dist/esm/compiler.mjs +76 -0
- package/dist/esm/compilerErrorResult.mjs +34 -0
- package/dist/esm/constants.mjs +7 -0
- package/dist/esm/defaults.mjs +17 -0
- package/dist/esm/getFinalOption.mjs +50 -0
- package/dist/{esm-node/index.js → esm/index.mjs} +7 -14
- package/dist/esm/utils.mjs +5 -0
- package/dist/esm/validate.mjs +32 -0
- package/dist/esm-node/build.mjs +42 -0
- package/dist/esm-node/buildWatch.mjs +78 -0
- package/dist/esm-node/compiler.mjs +76 -0
- package/dist/esm-node/compilerErrorResult.mjs +34 -0
- package/dist/esm-node/constants.mjs +7 -0
- package/dist/esm-node/defaults.mjs +17 -0
- package/dist/esm-node/getFinalOption.mjs +50 -0
- package/dist/esm-node/index.mjs +14 -0
- package/dist/esm-node/utils.mjs +5 -0
- package/dist/esm-node/validate.mjs +32 -0
- package/package.json +24 -17
- package/rslib.config.mts +4 -0
- package/rstest.config.ts +7 -0
- package/dist/esm/build.js +0 -105
- package/dist/esm/buildWatch.js +0 -157
- package/dist/esm/compiler.js +0 -95
- package/dist/esm/compilerErrorResult.js +0 -71
- package/dist/esm/constants.js +0 -9
- package/dist/esm/defaults.js +0 -19
- package/dist/esm/getFinalOption.js +0 -68
- package/dist/esm/index.js +0 -46
- package/dist/esm/utils.js +0 -7
- package/dist/esm/validate.js +0 -44
- package/dist/esm-node/build.js +0 -57
- package/dist/esm-node/buildWatch.js +0 -89
- package/dist/esm-node/compiler.js +0 -95
- package/dist/esm-node/compilerErrorResult.js +0 -39
- package/dist/esm-node/constants.js +0 -9
- package/dist/esm-node/defaults.js +0 -19
- package/dist/esm-node/getFinalOption.js +0 -67
- package/dist/esm-node/utils.js +0 -8
- package/dist/esm-node/validate.js +0 -44
- /package/dist/esm/{type.js → type.mjs} +0 -0
- /package/dist/esm-node/{type.js → type.mjs} +0 -0
package/dist/esm/compiler.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
-
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
|
-
import * as path from "path";
|
|
4
|
-
import * as babel from "@babel/core";
|
|
5
|
-
import { fs, logger } from "@modern-js/utils";
|
|
6
|
-
import { defaultDistFileExtMap } from "./constants";
|
|
7
|
-
import * as utils from "./utils";
|
|
8
|
-
var defaultDistDir = "dist";
|
|
9
|
-
var isRes = function(r) {
|
|
10
|
-
return Boolean(r);
|
|
11
|
-
};
|
|
12
|
-
var getDistFilePath = function(option) {
|
|
13
|
-
var filepath = option.filepath, rootDir = option.rootDir, distDir = option.distDir, extMap = option.extMap;
|
|
14
|
-
var ext = path.extname(filepath);
|
|
15
|
-
return path.join(distDir, path.relative(rootDir, filepath).replace(ext, extMap[ext]));
|
|
16
|
-
};
|
|
17
|
-
var resolveSourceMap = function(option) {
|
|
18
|
-
var babelRes = option.babelRes, sourceFilePath = option.sourceFilePath, distFilePath = option.distFilePath, _option_enableVirtualDist = option.enableVirtualDist, enableVirtualDist = _option_enableVirtualDist === void 0 ? false : _option_enableVirtualDist;
|
|
19
|
-
var mapLoc = "".concat(distFilePath, ".map");
|
|
20
|
-
babelRes.code = utils.addSourceMappingUrl(babelRes.code, mapLoc);
|
|
21
|
-
if (babelRes.map) {
|
|
22
|
-
babelRes.map.file = path.basename(distFilePath);
|
|
23
|
-
babelRes.map.sources = [
|
|
24
|
-
path.relative(path.dirname(distFilePath), sourceFilePath)
|
|
25
|
-
];
|
|
26
|
-
}
|
|
27
|
-
var sourceMapVirtualDist = {
|
|
28
|
-
sourcemap: JSON.stringify(babelRes.map),
|
|
29
|
-
sourceMapPath: mapLoc
|
|
30
|
-
};
|
|
31
|
-
if (enableVirtualDist) {
|
|
32
|
-
return sourceMapVirtualDist;
|
|
33
|
-
}
|
|
34
|
-
fs.ensureDirSync(path.dirname(mapLoc));
|
|
35
|
-
fs.writeFileSync(mapLoc, JSON.stringify(babelRes.map));
|
|
36
|
-
return sourceMapVirtualDist;
|
|
37
|
-
};
|
|
38
|
-
var compiler = function(option) {
|
|
39
|
-
var filepath = option.filepath, rootDir = option.rootDir, _option_enableVirtualDist = option.enableVirtualDist, enableVirtualDist = _option_enableVirtualDist === void 0 ? false : _option_enableVirtualDist, _option_distDir = option.distDir, distDir = _option_distDir === void 0 ? path.join(path.dirname(rootDir), defaultDistDir) : _option_distDir, _option_verbose = option.verbose, verbose = _option_verbose === void 0 ? false : _option_verbose, _option_babelConfig = option.babelConfig, babelConfig = _option_babelConfig === void 0 ? {} : _option_babelConfig, _option_distFileExtMap = option.distFileExtMap, distFileExtMap = _option_distFileExtMap === void 0 ? defaultDistFileExtMap : _option_distFileExtMap, _option_quiet = option.quiet, quiet = _option_quiet === void 0 ? false : _option_quiet;
|
|
40
|
-
var babelRes = babel.transformFileSync(filepath, babelConfig);
|
|
41
|
-
var virtualDist = null;
|
|
42
|
-
if (!isRes(babelRes)) {
|
|
43
|
-
throw new Error("".concat(filepath, " happen error"));
|
|
44
|
-
}
|
|
45
|
-
var distFilePath = getDistFilePath({
|
|
46
|
-
filepath,
|
|
47
|
-
rootDir,
|
|
48
|
-
distDir,
|
|
49
|
-
extMap: distFileExtMap
|
|
50
|
-
});
|
|
51
|
-
if (enableVirtualDist) {
|
|
52
|
-
virtualDist = {
|
|
53
|
-
distPath: distFilePath,
|
|
54
|
-
sourceMapPath: "",
|
|
55
|
-
code: "",
|
|
56
|
-
sourcemap: ""
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
if ((babelRes === null || babelRes === void 0 ? void 0 : babelRes.map) && babelConfig.sourceMaps && babelConfig.sourceMaps !== "inline") {
|
|
60
|
-
if (virtualDist) {
|
|
61
|
-
virtualDist = _object_spread({}, virtualDist, resolveSourceMap({
|
|
62
|
-
babelRes,
|
|
63
|
-
sourceFilePath: filepath,
|
|
64
|
-
distFilePath,
|
|
65
|
-
enableVirtualDist
|
|
66
|
-
}));
|
|
67
|
-
} else {
|
|
68
|
-
resolveSourceMap({
|
|
69
|
-
babelRes,
|
|
70
|
-
sourceFilePath: filepath,
|
|
71
|
-
distFilePath,
|
|
72
|
-
enableVirtualDist
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
if (virtualDist) {
|
|
77
|
-
virtualDist = _object_spread_props(_object_spread({}, virtualDist), {
|
|
78
|
-
distPath: distFilePath,
|
|
79
|
-
code: babelRes.code
|
|
80
|
-
});
|
|
81
|
-
} else {
|
|
82
|
-
fs.ensureDirSync(path.dirname(distFilePath));
|
|
83
|
-
fs.writeFileSync(distFilePath, babelRes.code);
|
|
84
|
-
}
|
|
85
|
-
if (verbose && !quiet) {
|
|
86
|
-
logger.info("".concat(filepath, " => ").concat(distFilePath));
|
|
87
|
-
}
|
|
88
|
-
return virtualDist;
|
|
89
|
-
};
|
|
90
|
-
export {
|
|
91
|
-
compiler,
|
|
92
|
-
getDistFilePath,
|
|
93
|
-
isRes,
|
|
94
|
-
resolveSourceMap
|
|
95
|
-
};
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
2
|
-
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
3
|
-
var CompilerErrorResult = /* @__PURE__ */ function() {
|
|
4
|
-
"use strict";
|
|
5
|
-
function CompilerErrorResult2(initErrorResult) {
|
|
6
|
-
_class_call_check(this, CompilerErrorResult2);
|
|
7
|
-
this.init(initErrorResult);
|
|
8
|
-
}
|
|
9
|
-
var _proto = CompilerErrorResult2.prototype;
|
|
10
|
-
_proto.init = function init(initErrorResult) {
|
|
11
|
-
this._messageDetails = (initErrorResult === null || initErrorResult === void 0 ? void 0 : initErrorResult.messageDetails) || [];
|
|
12
|
-
};
|
|
13
|
-
_proto.update = function update(messageDetails) {
|
|
14
|
-
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
15
|
-
try {
|
|
16
|
-
var _this, _loop = function() {
|
|
17
|
-
var messageDetail = _step.value;
|
|
18
|
-
var addError = !_this._messageDetails.some(function(detail) {
|
|
19
|
-
if (detail.filename === messageDetail.filename) {
|
|
20
|
-
detail.content = messageDetail.content;
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
});
|
|
25
|
-
if (addError) {
|
|
26
|
-
_this._messageDetails.push(messageDetail);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
for (var _iterator = messageDetails[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)
|
|
30
|
-
_this = this, _loop();
|
|
31
|
-
} catch (err) {
|
|
32
|
-
_didIteratorError = true;
|
|
33
|
-
_iteratorError = err;
|
|
34
|
-
} finally {
|
|
35
|
-
try {
|
|
36
|
-
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
37
|
-
_iterator.return();
|
|
38
|
-
}
|
|
39
|
-
} finally {
|
|
40
|
-
if (_didIteratorError) {
|
|
41
|
-
throw _iteratorError;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
_proto.removeByFileName = function removeByFileName(filename) {
|
|
47
|
-
this._messageDetails = this._messageDetails.filter(function(detail) {
|
|
48
|
-
return detail.filename !== filename;
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
_proto.checkExistError = function checkExistError() {
|
|
52
|
-
return this._messageDetails.length > 0;
|
|
53
|
-
};
|
|
54
|
-
_create_class(CompilerErrorResult2, [
|
|
55
|
-
{
|
|
56
|
-
key: "value",
|
|
57
|
-
get: function get() {
|
|
58
|
-
var _this__messageDetails;
|
|
59
|
-
return {
|
|
60
|
-
code: 1,
|
|
61
|
-
message: "Compilation failure ".concat((_this__messageDetails = this._messageDetails) === null || _this__messageDetails === void 0 ? void 0 : _this__messageDetails.length, " files with Babel."),
|
|
62
|
-
messageDetails: this._messageDetails
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
]);
|
|
67
|
-
return CompilerErrorResult2;
|
|
68
|
-
}();
|
|
69
|
-
export {
|
|
70
|
-
CompilerErrorResult
|
|
71
|
-
};
|
package/dist/esm/constants.js
DELETED
package/dist/esm/defaults.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
-
import { defaultDistFileExtMap } from "./constants";
|
|
3
|
-
var defaultOptions = {
|
|
4
|
-
enableWatch: false,
|
|
5
|
-
enableVirtualDist: false,
|
|
6
|
-
extensions: [],
|
|
7
|
-
filenames: [],
|
|
8
|
-
distFileExtMap: defaultDistFileExtMap,
|
|
9
|
-
ignore: [],
|
|
10
|
-
quiet: false,
|
|
11
|
-
verbose: false,
|
|
12
|
-
clean: false
|
|
13
|
-
};
|
|
14
|
-
var mergeDefaultOption = function(compilerOptions) {
|
|
15
|
-
return _object_spread({}, defaultOptions, compilerOptions);
|
|
16
|
-
};
|
|
17
|
-
export {
|
|
18
|
-
mergeDefaultOption
|
|
19
|
-
};
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
-
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
|
-
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
4
|
-
import { DEFAULT_EXTENSIONS } from "@babel/core";
|
|
5
|
-
import { glob } from "@modern-js/utils";
|
|
6
|
-
import { mergeDefaultOption } from "./defaults";
|
|
7
|
-
var getGlobPattern = function(dir, extensions) {
|
|
8
|
-
if (extensions.length > 1) {
|
|
9
|
-
return "".concat(dir, "/**/*{").concat(extensions.join(","), "}");
|
|
10
|
-
} else if (extensions.length === 1) {
|
|
11
|
-
return "".concat(dir, "/**/*").concat(extensions[0]);
|
|
12
|
-
} else {
|
|
13
|
-
return "".concat(dir, "/**/*");
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
var getFinalExtensions = function(extensions) {
|
|
17
|
-
var isExtensions = function(ext) {
|
|
18
|
-
return Array.isArray(ext);
|
|
19
|
-
};
|
|
20
|
-
var isExtensionsFunc = function(ext) {
|
|
21
|
-
return typeof ext === "function";
|
|
22
|
-
};
|
|
23
|
-
if (isExtensions(extensions)) {
|
|
24
|
-
return _to_consumable_array(extensions).concat(_to_consumable_array(DEFAULT_EXTENSIONS));
|
|
25
|
-
} else if (isExtensionsFunc(extensions)) {
|
|
26
|
-
return extensions(DEFAULT_EXTENSIONS);
|
|
27
|
-
} else {
|
|
28
|
-
return DEFAULT_EXTENSIONS;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
var getFilesFromDir = function(param) {
|
|
32
|
-
var dir = param.dir, _param_finalExt = param.finalExt, finalExt = _param_finalExt === void 0 ? [] : _param_finalExt, _param_ignore = param.ignore, ignore = _param_ignore === void 0 ? [] : _param_ignore;
|
|
33
|
-
var globFindFilenames = [];
|
|
34
|
-
var globPattern = getGlobPattern(dir, finalExt);
|
|
35
|
-
globFindFilenames = glob.sync(globPattern, {
|
|
36
|
-
ignore
|
|
37
|
-
});
|
|
38
|
-
return globFindFilenames;
|
|
39
|
-
};
|
|
40
|
-
var getFinalCompilerOption = function(option) {
|
|
41
|
-
var optionWithDefault = mergeDefaultOption(option);
|
|
42
|
-
var sourceDir = option.sourceDir, ignore = option.ignore, _option_enableWatch = option.enableWatch, enableWatch = _option_enableWatch === void 0 ? false : _option_enableWatch, watchDir = option.watchDir, _option_extensions = option.extensions, extensions = _option_extensions === void 0 ? DEFAULT_EXTENSIONS : _option_extensions;
|
|
43
|
-
var globFindFilenames = [];
|
|
44
|
-
var finalExt = getFinalExtensions(extensions);
|
|
45
|
-
if (sourceDir) {
|
|
46
|
-
globFindFilenames = getFilesFromDir({
|
|
47
|
-
dir: sourceDir,
|
|
48
|
-
ignore,
|
|
49
|
-
finalExt
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
if (enableWatch) {
|
|
53
|
-
globFindFilenames = getFilesFromDir({
|
|
54
|
-
dir: watchDir,
|
|
55
|
-
ignore,
|
|
56
|
-
finalExt
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
return _object_spread_props(_object_spread({}, optionWithDefault), {
|
|
60
|
-
filenames: _to_consumable_array(optionWithDefault.filenames).concat(_to_consumable_array(globFindFilenames))
|
|
61
|
-
});
|
|
62
|
-
};
|
|
63
|
-
export {
|
|
64
|
-
getFilesFromDir,
|
|
65
|
-
getFinalCompilerOption,
|
|
66
|
-
getFinalExtensions,
|
|
67
|
-
getGlobPattern
|
|
68
|
-
};
|
package/dist/esm/index.js
DELETED
|
@@ -1,46 +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 { build } from "./build";
|
|
4
|
-
import { buildWatch } from "./buildWatch";
|
|
5
|
-
import { getFinalCompilerOption } from "./getFinalOption";
|
|
6
|
-
import { validate } from "./validate";
|
|
7
|
-
function compiler(compilerOptions) {
|
|
8
|
-
return _compiler.apply(this, arguments);
|
|
9
|
-
}
|
|
10
|
-
function _compiler() {
|
|
11
|
-
_compiler = _async_to_generator(function(compilerOptions) {
|
|
12
|
-
var babelOptions, validRet, finalCompilerOption;
|
|
13
|
-
var _arguments = arguments;
|
|
14
|
-
return _ts_generator(this, function(_state) {
|
|
15
|
-
babelOptions = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {};
|
|
16
|
-
validRet = validate(compilerOptions);
|
|
17
|
-
if (validRet) {
|
|
18
|
-
return [
|
|
19
|
-
2,
|
|
20
|
-
validRet
|
|
21
|
-
];
|
|
22
|
-
}
|
|
23
|
-
finalCompilerOption = getFinalCompilerOption(compilerOptions);
|
|
24
|
-
if (compilerOptions.enableWatch) {
|
|
25
|
-
return [
|
|
26
|
-
2,
|
|
27
|
-
buildWatch(finalCompilerOption, babelOptions)
|
|
28
|
-
];
|
|
29
|
-
} else {
|
|
30
|
-
return [
|
|
31
|
-
2,
|
|
32
|
-
build(finalCompilerOption, babelOptions)
|
|
33
|
-
];
|
|
34
|
-
}
|
|
35
|
-
return [
|
|
36
|
-
2
|
|
37
|
-
];
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
return _compiler.apply(this, arguments);
|
|
41
|
-
}
|
|
42
|
-
export * from "./buildWatch";
|
|
43
|
-
export * from "./type";
|
|
44
|
-
export {
|
|
45
|
-
compiler
|
|
46
|
-
};
|
package/dist/esm/utils.js
DELETED
package/dist/esm/validate.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { logger } from "@modern-js/utils";
|
|
2
|
-
var sourceDirAndFileNamesValidMessage = "At least one of the sourceDir and filenames configurations must be configured";
|
|
3
|
-
var watchDirValidMessage = "should set watchDir when enableWatch is true";
|
|
4
|
-
var validateSourceDirAndFileNames = function(compilerOptions) {
|
|
5
|
-
var sourceDir = compilerOptions.sourceDir, filenames = compilerOptions.filenames, quiet = compilerOptions.quiet;
|
|
6
|
-
if (!sourceDir && !filenames) {
|
|
7
|
-
if (!quiet) {
|
|
8
|
-
logger.error(sourceDirAndFileNamesValidMessage);
|
|
9
|
-
}
|
|
10
|
-
return {
|
|
11
|
-
code: 1,
|
|
12
|
-
message: sourceDirAndFileNamesValidMessage,
|
|
13
|
-
virtualDists: []
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
return null;
|
|
17
|
-
};
|
|
18
|
-
var validateWatchDir = function(compilerOptions) {
|
|
19
|
-
var watchDir = compilerOptions.watchDir, enableWatch = compilerOptions.enableWatch, quiet = compilerOptions.quiet;
|
|
20
|
-
if (enableWatch && !watchDir) {
|
|
21
|
-
if (!quiet) {
|
|
22
|
-
logger.error(watchDirValidMessage);
|
|
23
|
-
}
|
|
24
|
-
return {
|
|
25
|
-
code: 1,
|
|
26
|
-
message: watchDirValidMessage,
|
|
27
|
-
virtualDists: []
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
return null;
|
|
31
|
-
};
|
|
32
|
-
var validate = function(compilerOptions) {
|
|
33
|
-
if (compilerOptions.enableWatch) {
|
|
34
|
-
return validateWatchDir(compilerOptions);
|
|
35
|
-
}
|
|
36
|
-
return validateSourceDirAndFileNames(compilerOptions);
|
|
37
|
-
};
|
|
38
|
-
export {
|
|
39
|
-
sourceDirAndFileNamesValidMessage,
|
|
40
|
-
validate,
|
|
41
|
-
validateSourceDirAndFileNames,
|
|
42
|
-
validateWatchDir,
|
|
43
|
-
watchDirValidMessage
|
|
44
|
-
};
|
package/dist/esm-node/build.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { fs, logger } from "@modern-js/utils";
|
|
2
|
-
import { compiler } from "./compiler";
|
|
3
|
-
import { defaultDistFileExtMap } from "./constants";
|
|
4
|
-
const build = async (option, babelConfig = {}) => {
|
|
5
|
-
const { rootDir, enableVirtualDist, filenames, clean, distDir, distFileExtMap = defaultDistFileExtMap, verbose = false, quiet = false } = option;
|
|
6
|
-
const virtualDists = [];
|
|
7
|
-
if (clean) {
|
|
8
|
-
await fs.remove(distDir);
|
|
9
|
-
}
|
|
10
|
-
fs.ensureDir(distDir);
|
|
11
|
-
const messageDetails = [];
|
|
12
|
-
for (const filename of filenames) {
|
|
13
|
-
try {
|
|
14
|
-
const dist = compiler({
|
|
15
|
-
rootDir,
|
|
16
|
-
enableVirtualDist,
|
|
17
|
-
filepath: filename,
|
|
18
|
-
distDir,
|
|
19
|
-
verbose,
|
|
20
|
-
quiet,
|
|
21
|
-
babelConfig,
|
|
22
|
-
distFileExtMap
|
|
23
|
-
});
|
|
24
|
-
if (enableVirtualDist && dist) {
|
|
25
|
-
virtualDists.push(dist);
|
|
26
|
-
}
|
|
27
|
-
} catch (e) {
|
|
28
|
-
messageDetails.push({
|
|
29
|
-
filename,
|
|
30
|
-
content: e.toString()
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
const happenError = messageDetails.length > 0;
|
|
35
|
-
if (!quiet) {
|
|
36
|
-
if (happenError) {
|
|
37
|
-
logger.error(`Compilation failure ${messageDetails.length} ${messageDetails.length !== 1 ? "files" : "file"} with Babel.`);
|
|
38
|
-
} else {
|
|
39
|
-
logger.info(`Successfully compiled ${filenames.length} ${filenames.length !== 1 ? "files" : "file"} with Babel.`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (happenError) {
|
|
43
|
-
return {
|
|
44
|
-
code: 1,
|
|
45
|
-
message: `Compilation failure ${messageDetails.length} ${messageDetails.length !== 1 ? "files" : "file"} with Babel.`,
|
|
46
|
-
messageDetails
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
return {
|
|
50
|
-
code: 0,
|
|
51
|
-
message: `Successfully compiled ${filenames.length} ${filenames.length !== 1 ? "files" : "file"} with Babel.`,
|
|
52
|
-
virtualDists
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
export {
|
|
56
|
-
build
|
|
57
|
-
};
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import * as Event from "events";
|
|
2
|
-
import * as path from "path";
|
|
3
|
-
import { WatchChangeType, logger, watch } from "@modern-js/utils";
|
|
4
|
-
import { build } from "./build";
|
|
5
|
-
import { CompilerErrorResult } from "./compilerErrorResult";
|
|
6
|
-
const BuildWatchEvent = {
|
|
7
|
-
firstCompiler: "first-compiler",
|
|
8
|
-
compiling: "compiling",
|
|
9
|
-
watchingCompiler: "watching-compiler"
|
|
10
|
-
};
|
|
11
|
-
class BuildWatchEmitter extends Event.EventEmitter {
|
|
12
|
-
setInitFn(fn) {
|
|
13
|
-
this._initFn = fn;
|
|
14
|
-
}
|
|
15
|
-
async watch() {
|
|
16
|
-
if (typeof this._initFn === "function") {
|
|
17
|
-
return this._initFn(this);
|
|
18
|
-
}
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
const runBuildWatch = async (option, babelConfig = {}, emitter) => {
|
|
23
|
-
emitter.emit(BuildWatchEvent.compiling);
|
|
24
|
-
const errorResult = new CompilerErrorResult();
|
|
25
|
-
const watchDir = option.watchDir;
|
|
26
|
-
const { distDir, quiet } = option;
|
|
27
|
-
const firstBuildResult = await build(option, babelConfig);
|
|
28
|
-
const { code } = firstBuildResult;
|
|
29
|
-
if (code === 1) {
|
|
30
|
-
errorResult.init(firstBuildResult);
|
|
31
|
-
emitter.emit(BuildWatchEvent.firstCompiler, errorResult.value);
|
|
32
|
-
} else {
|
|
33
|
-
emitter.emit(BuildWatchEvent.firstCompiler, firstBuildResult);
|
|
34
|
-
}
|
|
35
|
-
return watch(`${watchDir}/**/*.{js,jsx,ts,tsx}`, async ({ changeType, changedFilePath }) => {
|
|
36
|
-
emitter.emit(BuildWatchEvent.compiling);
|
|
37
|
-
if (changeType === WatchChangeType.UNLINK) {
|
|
38
|
-
const removeFiles = [
|
|
39
|
-
path.normalize(`./${distDir}/${path.relative(watchDir, changedFilePath)}`)
|
|
40
|
-
];
|
|
41
|
-
if (!quiet) {
|
|
42
|
-
logger.info(`remove file: ${removeFiles.join(",")}`);
|
|
43
|
-
}
|
|
44
|
-
const result2 = {
|
|
45
|
-
code: 0,
|
|
46
|
-
message: `remove file: ${removeFiles.join(",")}`,
|
|
47
|
-
removeFiles
|
|
48
|
-
};
|
|
49
|
-
emitter.emit(BuildWatchEvent.watchingCompiler, result2);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
const result = await build({
|
|
53
|
-
...option,
|
|
54
|
-
filenames: [
|
|
55
|
-
changedFilePath
|
|
56
|
-
]
|
|
57
|
-
}, babelConfig);
|
|
58
|
-
if (result.code === 1) {
|
|
59
|
-
errorResult.update(result.messageDetails || []);
|
|
60
|
-
emitter.emit(BuildWatchEvent.watchingCompiler, errorResult.value);
|
|
61
|
-
!quiet && logger.info(errorResult.value.message);
|
|
62
|
-
} else {
|
|
63
|
-
errorResult.removeByFileName(changedFilePath);
|
|
64
|
-
if (errorResult.checkExistError()) {
|
|
65
|
-
emitter.emit(BuildWatchEvent.watchingCompiler, {
|
|
66
|
-
...errorResult.value,
|
|
67
|
-
virtualDists: result.virtualDists
|
|
68
|
-
});
|
|
69
|
-
!quiet && logger.info(errorResult.value.message);
|
|
70
|
-
} else {
|
|
71
|
-
emitter.emit(BuildWatchEvent.watchingCompiler, result);
|
|
72
|
-
!quiet && logger.info(result.message);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}, [
|
|
76
|
-
`${watchDir}/**/*.d.ts`
|
|
77
|
-
]);
|
|
78
|
-
};
|
|
79
|
-
const buildWatch = (option, babelConfig = {}) => {
|
|
80
|
-
const buildWatchEmitter = new BuildWatchEmitter();
|
|
81
|
-
buildWatchEmitter.setInitFn(runBuildWatch.bind(null, option, babelConfig));
|
|
82
|
-
return buildWatchEmitter;
|
|
83
|
-
};
|
|
84
|
-
export {
|
|
85
|
-
BuildWatchEmitter,
|
|
86
|
-
BuildWatchEvent,
|
|
87
|
-
buildWatch,
|
|
88
|
-
runBuildWatch
|
|
89
|
-
};
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import * as path from "path";
|
|
2
|
-
import * as babel from "@babel/core";
|
|
3
|
-
import { fs, logger } from "@modern-js/utils";
|
|
4
|
-
import { defaultDistFileExtMap } from "./constants";
|
|
5
|
-
import * as utils from "./utils";
|
|
6
|
-
const defaultDistDir = "dist";
|
|
7
|
-
const isRes = (r) => Boolean(r);
|
|
8
|
-
const getDistFilePath = (option) => {
|
|
9
|
-
const { filepath, rootDir, distDir, extMap } = option;
|
|
10
|
-
const ext = path.extname(filepath);
|
|
11
|
-
return path.join(distDir, path.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 = utils.addSourceMappingUrl(babelRes.code, mapLoc);
|
|
17
|
-
if (babelRes.map) {
|
|
18
|
-
babelRes.map.file = path.basename(distFilePath);
|
|
19
|
-
babelRes.map.sources = [
|
|
20
|
-
path.relative(path.dirname(distFilePath), sourceFilePath)
|
|
21
|
-
];
|
|
22
|
-
}
|
|
23
|
-
const sourceMapVirtualDist = {
|
|
24
|
-
sourcemap: JSON.stringify(babelRes.map),
|
|
25
|
-
sourceMapPath: mapLoc
|
|
26
|
-
};
|
|
27
|
-
if (enableVirtualDist) {
|
|
28
|
-
return sourceMapVirtualDist;
|
|
29
|
-
}
|
|
30
|
-
fs.ensureDirSync(path.dirname(mapLoc));
|
|
31
|
-
fs.writeFileSync(mapLoc, JSON.stringify(babelRes.map));
|
|
32
|
-
return sourceMapVirtualDist;
|
|
33
|
-
};
|
|
34
|
-
const compiler = (option) => {
|
|
35
|
-
const { filepath, rootDir, enableVirtualDist = false, distDir = path.join(path.dirname(rootDir), defaultDistDir), verbose = false, babelConfig = {}, distFileExtMap = defaultDistFileExtMap, quiet = false } = option;
|
|
36
|
-
const babelRes = babel.transformFileSync(filepath, babelConfig);
|
|
37
|
-
let virtualDist = null;
|
|
38
|
-
if (!isRes(babelRes)) {
|
|
39
|
-
throw new Error(`${filepath} happen error`);
|
|
40
|
-
}
|
|
41
|
-
const distFilePath = getDistFilePath({
|
|
42
|
-
filepath,
|
|
43
|
-
rootDir,
|
|
44
|
-
distDir,
|
|
45
|
-
extMap: distFileExtMap
|
|
46
|
-
});
|
|
47
|
-
if (enableVirtualDist) {
|
|
48
|
-
virtualDist = {
|
|
49
|
-
distPath: distFilePath,
|
|
50
|
-
sourceMapPath: "",
|
|
51
|
-
code: "",
|
|
52
|
-
sourcemap: ""
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
if ((babelRes === null || babelRes === void 0 ? void 0 : babelRes.map) && babelConfig.sourceMaps && babelConfig.sourceMaps !== "inline") {
|
|
56
|
-
if (virtualDist) {
|
|
57
|
-
virtualDist = {
|
|
58
|
-
...virtualDist,
|
|
59
|
-
...resolveSourceMap({
|
|
60
|
-
babelRes,
|
|
61
|
-
sourceFilePath: filepath,
|
|
62
|
-
distFilePath,
|
|
63
|
-
enableVirtualDist
|
|
64
|
-
})
|
|
65
|
-
};
|
|
66
|
-
} else {
|
|
67
|
-
resolveSourceMap({
|
|
68
|
-
babelRes,
|
|
69
|
-
sourceFilePath: filepath,
|
|
70
|
-
distFilePath,
|
|
71
|
-
enableVirtualDist
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
if (virtualDist) {
|
|
76
|
-
virtualDist = {
|
|
77
|
-
...virtualDist,
|
|
78
|
-
distPath: distFilePath,
|
|
79
|
-
code: babelRes.code
|
|
80
|
-
};
|
|
81
|
-
} else {
|
|
82
|
-
fs.ensureDirSync(path.dirname(distFilePath));
|
|
83
|
-
fs.writeFileSync(distFilePath, babelRes.code);
|
|
84
|
-
}
|
|
85
|
-
if (verbose && !quiet) {
|
|
86
|
-
logger.info(`${filepath} => ${distFilePath}`);
|
|
87
|
-
}
|
|
88
|
-
return virtualDist;
|
|
89
|
-
};
|
|
90
|
-
export {
|
|
91
|
-
compiler,
|
|
92
|
-
getDistFilePath,
|
|
93
|
-
isRes,
|
|
94
|
-
resolveSourceMap
|
|
95
|
-
};
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
class CompilerErrorResult {
|
|
2
|
-
init(initErrorResult) {
|
|
3
|
-
this._messageDetails = (initErrorResult === null || initErrorResult === void 0 ? void 0 : 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) {
|
|
15
|
-
this._messageDetails.push(messageDetail);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
removeByFileName(filename) {
|
|
20
|
-
this._messageDetails = this._messageDetails.filter((detail) => detail.filename !== filename);
|
|
21
|
-
}
|
|
22
|
-
get value() {
|
|
23
|
-
var _this__messageDetails;
|
|
24
|
-
return {
|
|
25
|
-
code: 1,
|
|
26
|
-
message: `Compilation failure ${(_this__messageDetails = this._messageDetails) === null || _this__messageDetails === void 0 ? void 0 : _this__messageDetails.length} files with Babel.`,
|
|
27
|
-
messageDetails: this._messageDetails
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
checkExistError() {
|
|
31
|
-
return this._messageDetails.length > 0;
|
|
32
|
-
}
|
|
33
|
-
constructor(initErrorResult) {
|
|
34
|
-
this.init(initErrorResult);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
export {
|
|
38
|
-
CompilerErrorResult
|
|
39
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { defaultDistFileExtMap } from "./constants";
|
|
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 {
|
|
18
|
-
mergeDefaultOption
|
|
19
|
-
};
|