@modern-js/babel-compiler 1.0.0-alpha.3
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/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/dist/js/modern/build.js +71 -0
- package/dist/js/modern/buildWatch.js +106 -0
- package/dist/js/modern/compiler.js +115 -0
- package/dist/js/modern/compilerErrorResult.js +48 -0
- package/dist/js/modern/constants.js +6 -0
- package/dist/js/modern/defaults.js +19 -0
- package/dist/js/modern/getFinalOption.js +76 -0
- package/dist/js/modern/index.js +21 -0
- package/dist/js/modern/type.js +1 -0
- package/dist/js/modern/utils.js +4 -0
- package/dist/js/modern/validate.js +52 -0
- package/dist/js/node/build.js +85 -0
- package/dist/js/node/buildWatch.js +132 -0
- package/dist/js/node/compiler.js +146 -0
- package/dist/js/node/compilerErrorResult.js +57 -0
- package/dist/js/node/constants.js +13 -0
- package/dist/js/node/defaults.js +30 -0
- package/dist/js/node/getFinalOption.js +101 -0
- package/dist/js/node/index.js +59 -0
- package/dist/js/node/type.js +5 -0
- package/dist/js/node/utils.js +16 -0
- package/dist/js/node/validate.js +71 -0
- package/dist/types/build.d.ts +2 -0
- package/dist/types/buildWatch.d.ts +16 -0
- package/dist/types/compiler.d.ts +28 -0
- package/dist/types/compilerErrorResult.d.ts +10 -0
- package/dist/types/constants.d.ts +6 -0
- package/dist/types/defaults.d.ts +2 -0
- package/dist/types/getFinalOption.d.ts +14 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/type.d.ts +68 -0
- package/dist/types/utils.d.ts +1 -0
- package/dist/types/validate.d.ts +6 -0
- package/modern.config.js +19 -0
- package/package.json +48 -0
- package/src/build.ts +95 -0
- package/src/buildWatch.ts +113 -0
- package/src/compiler.ts +132 -0
- package/src/compilerErrorResult.ts +49 -0
- package/src/constants.ts +6 -0
- package/src/defaults.ts +21 -0
- package/src/getFinalOption.ts +93 -0
- package/src/index.ts +35 -0
- package/src/type.ts +76 -0
- package/src/utils.ts +5 -0
- package/src/validate.ts +47 -0
- package/tests/build.test.ts +124 -0
- package/tests/buildWatch.test.ts +233 -0
- package/tests/compiler.test.ts +106 -0
- package/tests/compilerErrorResult.test.ts +119 -0
- package/tests/constants.test.ts +12 -0
- package/tests/defaults.test.ts +29 -0
- package/tests/fixtures/build/src/error +1 -0
- package/tests/fixtures/build/src/error1 +1 -0
- package/tests/fixtures/build/src/far.js +0 -0
- package/tests/fixtures/build/src/index.js +0 -0
- package/tests/fixtures/buildWatch/src/error +2 -0
- package/tests/fixtures/buildWatch/src/index.js +0 -0
- package/tests/fixtures/compiler/src/index.js +2 -0
- package/tests/fixtures/getFinalOption/sourceDir/bar.js +0 -0
- package/tests/fixtures/getFinalOption/watchDir/far.js +0 -0
- package/tests/fixtures/getFinalOption/watchDir/foo.jsx +0 -0
- package/tests/fixtures/lib/src/index.js +0 -0
- package/tests/fixtures/resolveSourceMap/dist/far.js.map +1 -0
- package/tests/fixtures/resolveSourceMap/src/index.js +2 -0
- package/tests/fixtures/utils/far +2 -0
- package/tests/getFinalOption.test.ts +106 -0
- package/tests/index.test.ts +89 -0
- package/tests/tsconfig.json +15 -0
- package/tests/utils.test.ts +18 -0
- package/tests/validate.test.ts +68 -0
- package/tsconfig.json +16 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Modern.js
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
<p align="center">
|
|
3
|
+
<a href="https://modernjs.dev" target="blank"><img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png" width="300" alt="Modern.js Logo" /></a>
|
|
4
|
+
</p>
|
|
5
|
+
|
|
6
|
+
<p align="center">现代 Web 工程体系</p>
|
|
7
|
+
|
|
8
|
+
## 背景
|
|
9
|
+
- [迈入现代 Web 开发](https://zhuanlan.zhihu.com/p/386607009)
|
|
10
|
+
- [现代 Web 开发者问卷调查报告](https://zhuanlan.zhihu.com/p/403206195)
|
|
11
|
+
|
|
12
|
+
## 计划
|
|
13
|
+
|
|
14
|
+
Modern.js 的 1.0.0.rc 版已经发到 npm,目前在做测试改进,README 文档之后统一提供(现阶段加入测试和开发,可以发 [issue](https://github.com/modern-js-dev/modern.js/issues) 留微信联系),完整的文档站计划在10月14日上线
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { fs, logger } from '@modern-js/utils';
|
|
2
|
+
import { defaultDistFileExtMap } from "./constants";
|
|
3
|
+
import { compiler } from "./compiler";
|
|
4
|
+
export const build = async (option, babelConfig = {}) => {
|
|
5
|
+
const {
|
|
6
|
+
rootDir,
|
|
7
|
+
enableVirtualDist,
|
|
8
|
+
filenames,
|
|
9
|
+
clean,
|
|
10
|
+
distDir,
|
|
11
|
+
distFileExtMap = defaultDistFileExtMap,
|
|
12
|
+
verbose = false,
|
|
13
|
+
quiet = false
|
|
14
|
+
} = option;
|
|
15
|
+
const virtualDists = [];
|
|
16
|
+
|
|
17
|
+
if (clean) {
|
|
18
|
+
await fs.remove(distDir);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
fs.ensureDir(distDir);
|
|
22
|
+
const messageDetails = [];
|
|
23
|
+
|
|
24
|
+
for (const filename of filenames) {
|
|
25
|
+
try {
|
|
26
|
+
const dist = compiler({
|
|
27
|
+
rootDir,
|
|
28
|
+
enableVirtualDist,
|
|
29
|
+
filepath: filename,
|
|
30
|
+
distDir,
|
|
31
|
+
verbose,
|
|
32
|
+
quiet,
|
|
33
|
+
babelConfig,
|
|
34
|
+
distFileExtMap
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (enableVirtualDist && dist) {
|
|
38
|
+
virtualDists.push(dist);
|
|
39
|
+
}
|
|
40
|
+
} catch (e) {
|
|
41
|
+
messageDetails.push({
|
|
42
|
+
filename,
|
|
43
|
+
content: e.toString()
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const happenError = messageDetails.length > 0;
|
|
49
|
+
|
|
50
|
+
if (!quiet) {
|
|
51
|
+
if (happenError) {
|
|
52
|
+
logger.error(`Compilation failure ${messageDetails.length} ${messageDetails.length !== 1 ? 'files' : 'file'} with Babel.`); // TODO: 具体的报错信息打印
|
|
53
|
+
} else {
|
|
54
|
+
logger.info(`Successfully compiled ${filenames.length} ${filenames.length !== 1 ? 'files' : 'file'} with Babel.`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (happenError) {
|
|
59
|
+
return {
|
|
60
|
+
code: 1,
|
|
61
|
+
message: `Compilation failure ${messageDetails.length} ${messageDetails.length !== 1 ? 'files' : 'file'} with Babel.`,
|
|
62
|
+
messageDetails
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
code: 0,
|
|
68
|
+
message: `Successfully compiled ${filenames.length} ${filenames.length !== 1 ? 'files' : 'file'} with Babel.`,
|
|
69
|
+
virtualDists
|
|
70
|
+
};
|
|
71
|
+
};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 * as path from 'path';
|
|
8
|
+
import * as Event from 'events';
|
|
9
|
+
import { logger, watch, WatchChangeType } from '@modern-js/utils';
|
|
10
|
+
import { build } from "./build";
|
|
11
|
+
import { CompilerErrorResult } from "./compilerErrorResult";
|
|
12
|
+
export const BuildWatchEvent = {
|
|
13
|
+
firstCompiler: 'first-compiler',
|
|
14
|
+
compiling: 'compiling',
|
|
15
|
+
watchingCompiler: 'watching-compiler'
|
|
16
|
+
};
|
|
17
|
+
export class BuildWatchEmitter extends Event.EventEmitter {
|
|
18
|
+
constructor(...args) {
|
|
19
|
+
super(...args);
|
|
20
|
+
this._initFn = void 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
setInitFn(fn) {
|
|
24
|
+
this._initFn = fn;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async watch() {
|
|
28
|
+
if (typeof this._initFn === 'function') {
|
|
29
|
+
return this._initFn(this);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
}
|
|
36
|
+
export const runBuildWatch = async (option, babelConfig = {}, emitter) => {
|
|
37
|
+
emitter.emit(BuildWatchEvent.compiling);
|
|
38
|
+
const errorResult = new CompilerErrorResult();
|
|
39
|
+
const watchDir = option.watchDir;
|
|
40
|
+
const {
|
|
41
|
+
distDir,
|
|
42
|
+
quiet
|
|
43
|
+
} = option; // 第一次正常构建
|
|
44
|
+
|
|
45
|
+
const fisrtBuildResult = await build(option, babelConfig);
|
|
46
|
+
const {
|
|
47
|
+
code
|
|
48
|
+
} = fisrtBuildResult;
|
|
49
|
+
|
|
50
|
+
if (code === 1) {
|
|
51
|
+
errorResult.init(fisrtBuildResult);
|
|
52
|
+
emitter.emit(BuildWatchEvent.firstCompiler, errorResult.value);
|
|
53
|
+
} else {
|
|
54
|
+
emitter.emit(BuildWatchEvent.firstCompiler, fisrtBuildResult);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return watch(watchDir, async ({
|
|
58
|
+
changeType,
|
|
59
|
+
changedFilePath
|
|
60
|
+
}) => {
|
|
61
|
+
emitter.emit(BuildWatchEvent.compiling);
|
|
62
|
+
|
|
63
|
+
if (changeType === WatchChangeType.UNLINK) {
|
|
64
|
+
const removeFiles = [path.normalize(`./${distDir}/${path.relative(watchDir, changedFilePath)}`)];
|
|
65
|
+
|
|
66
|
+
if (!quiet) {
|
|
67
|
+
logger.info(`remove file: ${removeFiles.join(',')}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const result = {
|
|
71
|
+
code: 0,
|
|
72
|
+
message: `remove file: ${removeFiles.join(',')}`,
|
|
73
|
+
removeFiles
|
|
74
|
+
};
|
|
75
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, result);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const result = await build(_objectSpread(_objectSpread({}, option), {}, {
|
|
80
|
+
filenames: [changedFilePath]
|
|
81
|
+
}), babelConfig);
|
|
82
|
+
|
|
83
|
+
if (result.code === 1) {
|
|
84
|
+
errorResult.update(result.messageDetails || []);
|
|
85
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, errorResult.value);
|
|
86
|
+
!quiet && logger.info(errorResult.value.message);
|
|
87
|
+
} else {
|
|
88
|
+
errorResult.removeByFileName(changedFilePath); // 如果该文件没有报错,则更新该文件状态并检查是否还存在其他报错文件
|
|
89
|
+
|
|
90
|
+
if (errorResult.checkExistError()) {
|
|
91
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, _objectSpread(_objectSpread({}, errorResult.value), {}, {
|
|
92
|
+
virtualDists: result.virtualDists
|
|
93
|
+
}));
|
|
94
|
+
!quiet && logger.info(errorResult.value.message);
|
|
95
|
+
} else {
|
|
96
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, result);
|
|
97
|
+
!quiet && logger.info(result.message);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}, [`${watchDir}/**/*.d.ts`]);
|
|
101
|
+
};
|
|
102
|
+
export const buildWatch = (option, babelConfig = {}) => {
|
|
103
|
+
const buildWatchEmitter = new BuildWatchEmitter();
|
|
104
|
+
buildWatchEmitter.setInitFn(runBuildWatch.bind(null, option, babelConfig));
|
|
105
|
+
return buildWatchEmitter;
|
|
106
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 * as path from 'path';
|
|
8
|
+
import * as babel from '@babel/core';
|
|
9
|
+
import { logger, fs } from '@modern-js/utils';
|
|
10
|
+
import * as utils from "./utils";
|
|
11
|
+
import { defaultDistFileExtMap } from "./constants";
|
|
12
|
+
const defaultDistDir = 'dist';
|
|
13
|
+
export const isRes = r => Boolean(r);
|
|
14
|
+
export const getDistFilePath = option => {
|
|
15
|
+
const {
|
|
16
|
+
filepath,
|
|
17
|
+
rootDir,
|
|
18
|
+
distDir,
|
|
19
|
+
extMap
|
|
20
|
+
} = option;
|
|
21
|
+
const ext = path.extname(filepath);
|
|
22
|
+
return path.join(distDir, path.relative(rootDir, filepath).replace(ext, extMap[ext]));
|
|
23
|
+
};
|
|
24
|
+
export const resolveSourceMap = option => {
|
|
25
|
+
const {
|
|
26
|
+
babelRes,
|
|
27
|
+
distFilePath,
|
|
28
|
+
enableVirtualDist = false
|
|
29
|
+
} = option;
|
|
30
|
+
const mapLoc = `${distFilePath}.map`;
|
|
31
|
+
babelRes.code = utils.addSourceMappingUrl(babelRes.code, mapLoc);
|
|
32
|
+
|
|
33
|
+
if (babelRes.map) {
|
|
34
|
+
babelRes.map.file = distFilePath;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const sourceMapVirtualDist = {
|
|
38
|
+
sourcemap: JSON.stringify(babelRes.map),
|
|
39
|
+
sourceMapPath: mapLoc
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
if (enableVirtualDist) {
|
|
43
|
+
return sourceMapVirtualDist;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fs.ensureDirSync(path.dirname(mapLoc));
|
|
47
|
+
fs.writeFileSync(mapLoc, JSON.stringify(babelRes.map));
|
|
48
|
+
return sourceMapVirtualDist;
|
|
49
|
+
};
|
|
50
|
+
export const compiler = option => {
|
|
51
|
+
const {
|
|
52
|
+
filepath,
|
|
53
|
+
rootDir,
|
|
54
|
+
enableVirtualDist = false,
|
|
55
|
+
distDir = path.join(path.dirname(rootDir), defaultDistDir),
|
|
56
|
+
verbose = false,
|
|
57
|
+
babelConfig = {},
|
|
58
|
+
distFileExtMap = defaultDistFileExtMap,
|
|
59
|
+
quiet = false
|
|
60
|
+
} = option;
|
|
61
|
+
const babelRes = babel.transformFileSync(filepath, babelConfig);
|
|
62
|
+
let virtualDist = null;
|
|
63
|
+
|
|
64
|
+
if (!isRes(babelRes)) {
|
|
65
|
+
throw new Error(`${filepath} happen error`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const distFilePath = getDistFilePath({
|
|
69
|
+
filepath,
|
|
70
|
+
rootDir,
|
|
71
|
+
distDir,
|
|
72
|
+
extMap: distFileExtMap
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (enableVirtualDist) {
|
|
76
|
+
virtualDist = {
|
|
77
|
+
distPath: distFilePath,
|
|
78
|
+
sourceMapPath: '',
|
|
79
|
+
code: '',
|
|
80
|
+
sourcemap: ''
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (babelRes !== null && babelRes !== void 0 && babelRes.map && babelConfig.sourceMaps && babelConfig.sourceMaps !== 'inline') {
|
|
85
|
+
if (virtualDist) {
|
|
86
|
+
virtualDist = _objectSpread(_objectSpread({}, virtualDist), resolveSourceMap({
|
|
87
|
+
babelRes,
|
|
88
|
+
distFilePath,
|
|
89
|
+
enableVirtualDist
|
|
90
|
+
}));
|
|
91
|
+
} else {
|
|
92
|
+
resolveSourceMap({
|
|
93
|
+
babelRes,
|
|
94
|
+
distFilePath,
|
|
95
|
+
enableVirtualDist
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (virtualDist) {
|
|
101
|
+
virtualDist = _objectSpread(_objectSpread({}, virtualDist), {}, {
|
|
102
|
+
distPath: distFilePath,
|
|
103
|
+
code: babelRes.code
|
|
104
|
+
});
|
|
105
|
+
} else {
|
|
106
|
+
fs.ensureDirSync(path.dirname(distFilePath));
|
|
107
|
+
fs.writeFileSync(distFilePath, babelRes.code);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (verbose && !quiet) {
|
|
111
|
+
logger.info(`${filepath} => ${distFilePath}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return virtualDist;
|
|
115
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export class CompilerErrorResult {
|
|
2
|
+
constructor(initErrorResult) {
|
|
3
|
+
this._messageDetails = void 0;
|
|
4
|
+
this.init(initErrorResult);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
init(initErrorResult) {
|
|
8
|
+
this._messageDetails = (initErrorResult === null || initErrorResult === void 0 ? void 0 : initErrorResult.messageDetails) || [];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
update(messageDetails) {
|
|
12
|
+
for (const messageDetail of messageDetails) {
|
|
13
|
+
// 遍历存不存在该文件报错信息,不存在则增加,否则更新内容
|
|
14
|
+
const addError = !this._messageDetails.some(detail => {
|
|
15
|
+
if (detail.filename === messageDetail.filename) {
|
|
16
|
+
// 如果错误栈里存在该文件报错信息,则更新内容
|
|
17
|
+
detail.content = messageDetail.content;
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return false;
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (addError) {
|
|
25
|
+
this._messageDetails.push(messageDetail);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
removeByFileName(filename) {
|
|
31
|
+
this._messageDetails = this._messageDetails.filter(detail => detail.filename !== filename);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get value() {
|
|
35
|
+
var _this$_messageDetails;
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
code: 1,
|
|
39
|
+
message: `Compilation failure ${(_this$_messageDetails = this._messageDetails) === null || _this$_messageDetails === void 0 ? void 0 : _this$_messageDetails.length} files with Babel.`,
|
|
40
|
+
messageDetails: this._messageDetails
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
checkExistError() {
|
|
45
|
+
return this._messageDetails.length > 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 { defaultDistFileExtMap } from "./constants";
|
|
8
|
+
const defaultOptions = {
|
|
9
|
+
enableWatch: false,
|
|
10
|
+
enableVirtualDist: false,
|
|
11
|
+
extensions: [],
|
|
12
|
+
filenames: [],
|
|
13
|
+
distFileExtMap: defaultDistFileExtMap,
|
|
14
|
+
ignore: [],
|
|
15
|
+
quiet: false,
|
|
16
|
+
verbose: false,
|
|
17
|
+
clean: false
|
|
18
|
+
};
|
|
19
|
+
export const mergeDefaultOption = compilerOptions => _objectSpread(_objectSpread({}, defaultOptions), compilerOptions);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 * as glob from 'glob';
|
|
8
|
+
import { DEFAULT_EXTENSIONS } from '@babel/core';
|
|
9
|
+
import { mergeDefaultOption } from "./defaults";
|
|
10
|
+
export const getGlobPattern = (dir, extensions) => {
|
|
11
|
+
if (extensions.length > 1) {
|
|
12
|
+
return `${dir}/**/*{${extensions.join(',')}}`;
|
|
13
|
+
} else if (extensions.length === 1) {
|
|
14
|
+
return `${dir}/**/*${extensions[0]}`;
|
|
15
|
+
} else {
|
|
16
|
+
return `${dir}/**/*`;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
export const getFinalExtensions = extensions => {
|
|
20
|
+
const isExtensions = ext => Array.isArray(ext);
|
|
21
|
+
|
|
22
|
+
const isExtensionsFunc = ext => typeof ext === 'function';
|
|
23
|
+
|
|
24
|
+
if (isExtensions(extensions)) {
|
|
25
|
+
return [...extensions, ...DEFAULT_EXTENSIONS];
|
|
26
|
+
} else if (isExtensionsFunc(extensions)) {
|
|
27
|
+
return extensions(DEFAULT_EXTENSIONS);
|
|
28
|
+
} else {
|
|
29
|
+
return DEFAULT_EXTENSIONS;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
export const getFilesFromDir = ({
|
|
33
|
+
dir,
|
|
34
|
+
finalExt: _finalExt = [],
|
|
35
|
+
ignore: _ignore = []
|
|
36
|
+
}) => {
|
|
37
|
+
let globFindFilenames = [];
|
|
38
|
+
const globPattern = getGlobPattern(dir, _finalExt);
|
|
39
|
+
globFindFilenames = glob.sync(globPattern, {
|
|
40
|
+
ignore: _ignore
|
|
41
|
+
});
|
|
42
|
+
return globFindFilenames;
|
|
43
|
+
};
|
|
44
|
+
export const getFinalCompilerOption = option => {
|
|
45
|
+
const optionWithDefault = mergeDefaultOption(option);
|
|
46
|
+
const {
|
|
47
|
+
sourceDir,
|
|
48
|
+
ignore,
|
|
49
|
+
enableWatch = false,
|
|
50
|
+
watchDir,
|
|
51
|
+
extensions = DEFAULT_EXTENSIONS
|
|
52
|
+
} = option;
|
|
53
|
+
let globFindFilenames = [];
|
|
54
|
+
const finalExt = getFinalExtensions(extensions);
|
|
55
|
+
|
|
56
|
+
if (sourceDir) {
|
|
57
|
+
globFindFilenames = getFilesFromDir({
|
|
58
|
+
dir: sourceDir,
|
|
59
|
+
ignore,
|
|
60
|
+
finalExt
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (enableWatch) {
|
|
65
|
+
// 开启watch模式,清空通过 sourceDir 找到的文件,而改为使用watchDirs
|
|
66
|
+
globFindFilenames = getFilesFromDir({
|
|
67
|
+
dir: watchDir,
|
|
68
|
+
ignore,
|
|
69
|
+
finalExt
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return _objectSpread(_objectSpread({}, optionWithDefault), {}, {
|
|
74
|
+
filenames: [...optionWithDefault.filenames, ...globFindFilenames]
|
|
75
|
+
});
|
|
76
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getFinalCompilerOption } from "./getFinalOption";
|
|
2
|
+
import { build } from "./build";
|
|
3
|
+
import { buildWatch } from "./buildWatch";
|
|
4
|
+
import { validate } from "./validate";
|
|
5
|
+
export async function compiler(compilerOptions, babelOptions = {}) {
|
|
6
|
+
const validRet = validate(compilerOptions);
|
|
7
|
+
|
|
8
|
+
if (validRet) {
|
|
9
|
+
return validRet;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const finalCompilerOption = getFinalCompilerOption(compilerOptions);
|
|
13
|
+
|
|
14
|
+
if (compilerOptions.enableWatch) {
|
|
15
|
+
return buildWatch(finalCompilerOption, babelOptions);
|
|
16
|
+
} else {
|
|
17
|
+
return build(finalCompilerOption, babelOptions);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export * from "./buildWatch";
|
|
21
|
+
export * from "./type";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { logger } from '@modern-js/utils';
|
|
2
|
+
export const sourceDirAndFileNamesValidMessage = 'At least one of the sourceDir and filenames configurations must be configured';
|
|
3
|
+
export const watchDirValidMessage = 'should set watchDir when enableWatch is true';
|
|
4
|
+
export const validateSourceDirAndFileNames = compilerOptions => {
|
|
5
|
+
const {
|
|
6
|
+
sourceDir,
|
|
7
|
+
filenames,
|
|
8
|
+
quiet
|
|
9
|
+
} = compilerOptions;
|
|
10
|
+
|
|
11
|
+
if (!sourceDir && !filenames) {
|
|
12
|
+
if (!quiet) {
|
|
13
|
+
logger.error(sourceDirAndFileNamesValidMessage);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
code: 1,
|
|
18
|
+
message: sourceDirAndFileNamesValidMessage,
|
|
19
|
+
virtualDists: []
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return null;
|
|
24
|
+
};
|
|
25
|
+
export const validateWatchDir = compilerOptions => {
|
|
26
|
+
const {
|
|
27
|
+
watchDir,
|
|
28
|
+
enableWatch,
|
|
29
|
+
quiet
|
|
30
|
+
} = compilerOptions;
|
|
31
|
+
|
|
32
|
+
if (enableWatch && !watchDir) {
|
|
33
|
+
if (!quiet) {
|
|
34
|
+
logger.error(watchDirValidMessage);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
code: 1,
|
|
39
|
+
message: watchDirValidMessage,
|
|
40
|
+
virtualDists: []
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return null;
|
|
45
|
+
};
|
|
46
|
+
export const validate = compilerOptions => {
|
|
47
|
+
if (compilerOptions.enableWatch) {
|
|
48
|
+
return validateWatchDir(compilerOptions);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return validateSourceDirAndFileNames(compilerOptions);
|
|
52
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
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 _constants = require("./constants");
|
|
11
|
+
|
|
12
|
+
var _compiler = require("./compiler");
|
|
13
|
+
|
|
14
|
+
const build = async (option, babelConfig = {}) => {
|
|
15
|
+
const {
|
|
16
|
+
rootDir,
|
|
17
|
+
enableVirtualDist,
|
|
18
|
+
filenames,
|
|
19
|
+
clean,
|
|
20
|
+
distDir,
|
|
21
|
+
distFileExtMap = _constants.defaultDistFileExtMap,
|
|
22
|
+
verbose = false,
|
|
23
|
+
quiet = false
|
|
24
|
+
} = option;
|
|
25
|
+
const virtualDists = [];
|
|
26
|
+
|
|
27
|
+
if (clean) {
|
|
28
|
+
await _utils.fs.remove(distDir);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
_utils.fs.ensureDir(distDir);
|
|
32
|
+
|
|
33
|
+
const messageDetails = [];
|
|
34
|
+
|
|
35
|
+
for (const filename of filenames) {
|
|
36
|
+
try {
|
|
37
|
+
const dist = (0, _compiler.compiler)({
|
|
38
|
+
rootDir,
|
|
39
|
+
enableVirtualDist,
|
|
40
|
+
filepath: filename,
|
|
41
|
+
distDir,
|
|
42
|
+
verbose,
|
|
43
|
+
quiet,
|
|
44
|
+
babelConfig,
|
|
45
|
+
distFileExtMap
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (enableVirtualDist && dist) {
|
|
49
|
+
virtualDists.push(dist);
|
|
50
|
+
}
|
|
51
|
+
} catch (e) {
|
|
52
|
+
messageDetails.push({
|
|
53
|
+
filename,
|
|
54
|
+
content: e.toString()
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const happenError = messageDetails.length > 0;
|
|
60
|
+
|
|
61
|
+
if (!quiet) {
|
|
62
|
+
if (happenError) {
|
|
63
|
+
_utils.logger.error(`Compilation failure ${messageDetails.length} ${messageDetails.length !== 1 ? 'files' : 'file'} with Babel.`); // TODO: 具体的报错信息打印
|
|
64
|
+
|
|
65
|
+
} else {
|
|
66
|
+
_utils.logger.info(`Successfully compiled ${filenames.length} ${filenames.length !== 1 ? 'files' : 'file'} with Babel.`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (happenError) {
|
|
71
|
+
return {
|
|
72
|
+
code: 1,
|
|
73
|
+
message: `Compilation failure ${messageDetails.length} ${messageDetails.length !== 1 ? 'files' : 'file'} with Babel.`,
|
|
74
|
+
messageDetails
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
code: 0,
|
|
80
|
+
message: `Successfully compiled ${filenames.length} ${filenames.length !== 1 ? 'files' : 'file'} with Babel.`,
|
|
81
|
+
virtualDists
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
exports.build = build;
|