@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
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.runBuildWatch = exports.buildWatch = exports.BuildWatchEvent = exports.BuildWatchEmitter = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var Event = _interopRequireWildcard(require("events"));
|
|
11
|
+
|
|
12
|
+
var _utils = require("@modern-js/utils");
|
|
13
|
+
|
|
14
|
+
var _build = require("./build");
|
|
15
|
+
|
|
16
|
+
var _compilerErrorResult = require("./compilerErrorResult");
|
|
17
|
+
|
|
18
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
19
|
+
|
|
20
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
|
+
|
|
22
|
+
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; }
|
|
23
|
+
|
|
24
|
+
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; }
|
|
25
|
+
|
|
26
|
+
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; }
|
|
27
|
+
|
|
28
|
+
const BuildWatchEvent = {
|
|
29
|
+
firstCompiler: 'first-compiler',
|
|
30
|
+
compiling: 'compiling',
|
|
31
|
+
watchingCompiler: 'watching-compiler'
|
|
32
|
+
};
|
|
33
|
+
exports.BuildWatchEvent = BuildWatchEvent;
|
|
34
|
+
|
|
35
|
+
class BuildWatchEmitter extends Event.EventEmitter {
|
|
36
|
+
constructor(...args) {
|
|
37
|
+
super(...args);
|
|
38
|
+
this._initFn = void 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
setInitFn(fn) {
|
|
42
|
+
this._initFn = fn;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async watch() {
|
|
46
|
+
if (typeof this._initFn === 'function') {
|
|
47
|
+
return this._initFn(this);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
exports.BuildWatchEmitter = BuildWatchEmitter;
|
|
56
|
+
|
|
57
|
+
const runBuildWatch = async (option, babelConfig = {}, emitter) => {
|
|
58
|
+
emitter.emit(BuildWatchEvent.compiling);
|
|
59
|
+
const errorResult = new _compilerErrorResult.CompilerErrorResult();
|
|
60
|
+
const watchDir = option.watchDir;
|
|
61
|
+
const {
|
|
62
|
+
distDir,
|
|
63
|
+
quiet
|
|
64
|
+
} = option; // 第一次正常构建
|
|
65
|
+
|
|
66
|
+
const fisrtBuildResult = await (0, _build.build)(option, babelConfig);
|
|
67
|
+
const {
|
|
68
|
+
code
|
|
69
|
+
} = fisrtBuildResult;
|
|
70
|
+
|
|
71
|
+
if (code === 1) {
|
|
72
|
+
errorResult.init(fisrtBuildResult);
|
|
73
|
+
emitter.emit(BuildWatchEvent.firstCompiler, errorResult.value);
|
|
74
|
+
} else {
|
|
75
|
+
emitter.emit(BuildWatchEvent.firstCompiler, fisrtBuildResult);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return (0, _utils.watch)(watchDir, async ({
|
|
79
|
+
changeType,
|
|
80
|
+
changedFilePath
|
|
81
|
+
}) => {
|
|
82
|
+
emitter.emit(BuildWatchEvent.compiling);
|
|
83
|
+
|
|
84
|
+
if (changeType === _utils.WatchChangeType.UNLINK) {
|
|
85
|
+
const removeFiles = [path.normalize(`./${distDir}/${path.relative(watchDir, changedFilePath)}`)];
|
|
86
|
+
|
|
87
|
+
if (!quiet) {
|
|
88
|
+
_utils.logger.info(`remove file: ${removeFiles.join(',')}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const result = {
|
|
92
|
+
code: 0,
|
|
93
|
+
message: `remove file: ${removeFiles.join(',')}`,
|
|
94
|
+
removeFiles
|
|
95
|
+
};
|
|
96
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, result);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const result = await (0, _build.build)(_objectSpread(_objectSpread({}, option), {}, {
|
|
101
|
+
filenames: [changedFilePath]
|
|
102
|
+
}), babelConfig);
|
|
103
|
+
|
|
104
|
+
if (result.code === 1) {
|
|
105
|
+
errorResult.update(result.messageDetails || []);
|
|
106
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, errorResult.value);
|
|
107
|
+
!quiet && _utils.logger.info(errorResult.value.message);
|
|
108
|
+
} else {
|
|
109
|
+
errorResult.removeByFileName(changedFilePath); // 如果该文件没有报错,则更新该文件状态并检查是否还存在其他报错文件
|
|
110
|
+
|
|
111
|
+
if (errorResult.checkExistError()) {
|
|
112
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, _objectSpread(_objectSpread({}, errorResult.value), {}, {
|
|
113
|
+
virtualDists: result.virtualDists
|
|
114
|
+
}));
|
|
115
|
+
!quiet && _utils.logger.info(errorResult.value.message);
|
|
116
|
+
} else {
|
|
117
|
+
emitter.emit(BuildWatchEvent.watchingCompiler, result);
|
|
118
|
+
!quiet && _utils.logger.info(result.message);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}, [`${watchDir}/**/*.d.ts`]);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
exports.runBuildWatch = runBuildWatch;
|
|
125
|
+
|
|
126
|
+
const buildWatch = (option, babelConfig = {}) => {
|
|
127
|
+
const buildWatchEmitter = new BuildWatchEmitter();
|
|
128
|
+
buildWatchEmitter.setInitFn(runBuildWatch.bind(null, option, babelConfig));
|
|
129
|
+
return buildWatchEmitter;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
exports.buildWatch = buildWatch;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.resolveSourceMap = exports.isRes = exports.getDistFilePath = exports.compiler = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var babel = _interopRequireWildcard(require("@babel/core"));
|
|
11
|
+
|
|
12
|
+
var _utils = require("@modern-js/utils");
|
|
13
|
+
|
|
14
|
+
var utils = _interopRequireWildcard(require("./utils"));
|
|
15
|
+
|
|
16
|
+
var _constants = require("./constants");
|
|
17
|
+
|
|
18
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
19
|
+
|
|
20
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
|
+
|
|
22
|
+
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; }
|
|
23
|
+
|
|
24
|
+
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; }
|
|
25
|
+
|
|
26
|
+
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; }
|
|
27
|
+
|
|
28
|
+
const defaultDistDir = 'dist';
|
|
29
|
+
|
|
30
|
+
const isRes = r => Boolean(r);
|
|
31
|
+
|
|
32
|
+
exports.isRes = isRes;
|
|
33
|
+
|
|
34
|
+
const getDistFilePath = option => {
|
|
35
|
+
const {
|
|
36
|
+
filepath,
|
|
37
|
+
rootDir,
|
|
38
|
+
distDir,
|
|
39
|
+
extMap
|
|
40
|
+
} = option;
|
|
41
|
+
const ext = path.extname(filepath);
|
|
42
|
+
return path.join(distDir, path.relative(rootDir, filepath).replace(ext, extMap[ext]));
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
exports.getDistFilePath = getDistFilePath;
|
|
46
|
+
|
|
47
|
+
const resolveSourceMap = option => {
|
|
48
|
+
const {
|
|
49
|
+
babelRes,
|
|
50
|
+
distFilePath,
|
|
51
|
+
enableVirtualDist = false
|
|
52
|
+
} = option;
|
|
53
|
+
const mapLoc = `${distFilePath}.map`;
|
|
54
|
+
babelRes.code = utils.addSourceMappingUrl(babelRes.code, mapLoc);
|
|
55
|
+
|
|
56
|
+
if (babelRes.map) {
|
|
57
|
+
babelRes.map.file = distFilePath;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const sourceMapVirtualDist = {
|
|
61
|
+
sourcemap: JSON.stringify(babelRes.map),
|
|
62
|
+
sourceMapPath: mapLoc
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
if (enableVirtualDist) {
|
|
66
|
+
return sourceMapVirtualDist;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
_utils.fs.ensureDirSync(path.dirname(mapLoc));
|
|
70
|
+
|
|
71
|
+
_utils.fs.writeFileSync(mapLoc, JSON.stringify(babelRes.map));
|
|
72
|
+
|
|
73
|
+
return sourceMapVirtualDist;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
exports.resolveSourceMap = resolveSourceMap;
|
|
77
|
+
|
|
78
|
+
const compiler = option => {
|
|
79
|
+
const {
|
|
80
|
+
filepath,
|
|
81
|
+
rootDir,
|
|
82
|
+
enableVirtualDist = false,
|
|
83
|
+
distDir = path.join(path.dirname(rootDir), defaultDistDir),
|
|
84
|
+
verbose = false,
|
|
85
|
+
babelConfig = {},
|
|
86
|
+
distFileExtMap = _constants.defaultDistFileExtMap,
|
|
87
|
+
quiet = false
|
|
88
|
+
} = option;
|
|
89
|
+
const babelRes = babel.transformFileSync(filepath, babelConfig);
|
|
90
|
+
let virtualDist = null;
|
|
91
|
+
|
|
92
|
+
if (!isRes(babelRes)) {
|
|
93
|
+
throw new Error(`${filepath} happen error`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const distFilePath = getDistFilePath({
|
|
97
|
+
filepath,
|
|
98
|
+
rootDir,
|
|
99
|
+
distDir,
|
|
100
|
+
extMap: distFileExtMap
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (enableVirtualDist) {
|
|
104
|
+
virtualDist = {
|
|
105
|
+
distPath: distFilePath,
|
|
106
|
+
sourceMapPath: '',
|
|
107
|
+
code: '',
|
|
108
|
+
sourcemap: ''
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (babelRes !== null && babelRes !== void 0 && babelRes.map && babelConfig.sourceMaps && babelConfig.sourceMaps !== 'inline') {
|
|
113
|
+
if (virtualDist) {
|
|
114
|
+
virtualDist = _objectSpread(_objectSpread({}, virtualDist), resolveSourceMap({
|
|
115
|
+
babelRes,
|
|
116
|
+
distFilePath,
|
|
117
|
+
enableVirtualDist
|
|
118
|
+
}));
|
|
119
|
+
} else {
|
|
120
|
+
resolveSourceMap({
|
|
121
|
+
babelRes,
|
|
122
|
+
distFilePath,
|
|
123
|
+
enableVirtualDist
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (virtualDist) {
|
|
129
|
+
virtualDist = _objectSpread(_objectSpread({}, virtualDist), {}, {
|
|
130
|
+
distPath: distFilePath,
|
|
131
|
+
code: babelRes.code
|
|
132
|
+
});
|
|
133
|
+
} else {
|
|
134
|
+
_utils.fs.ensureDirSync(path.dirname(distFilePath));
|
|
135
|
+
|
|
136
|
+
_utils.fs.writeFileSync(distFilePath, babelRes.code);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (verbose && !quiet) {
|
|
140
|
+
_utils.logger.info(`${filepath} => ${distFilePath}`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return virtualDist;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
exports.compiler = compiler;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.CompilerErrorResult = void 0;
|
|
7
|
+
|
|
8
|
+
class CompilerErrorResult {
|
|
9
|
+
constructor(initErrorResult) {
|
|
10
|
+
this._messageDetails = void 0;
|
|
11
|
+
this.init(initErrorResult);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
init(initErrorResult) {
|
|
15
|
+
this._messageDetails = (initErrorResult === null || initErrorResult === void 0 ? void 0 : initErrorResult.messageDetails) || [];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
update(messageDetails) {
|
|
19
|
+
for (const messageDetail of messageDetails) {
|
|
20
|
+
// 遍历存不存在该文件报错信息,不存在则增加,否则更新内容
|
|
21
|
+
const addError = !this._messageDetails.some(detail => {
|
|
22
|
+
if (detail.filename === messageDetail.filename) {
|
|
23
|
+
// 如果错误栈里存在该文件报错信息,则更新内容
|
|
24
|
+
detail.content = messageDetail.content;
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return false;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (addError) {
|
|
32
|
+
this._messageDetails.push(messageDetail);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
removeByFileName(filename) {
|
|
38
|
+
this._messageDetails = this._messageDetails.filter(detail => detail.filename !== filename);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get value() {
|
|
42
|
+
var _this$_messageDetails;
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
code: 1,
|
|
46
|
+
message: `Compilation failure ${(_this$_messageDetails = this._messageDetails) === null || _this$_messageDetails === void 0 ? void 0 : _this$_messageDetails.length} files with Babel.`,
|
|
47
|
+
messageDetails: this._messageDetails
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
checkExistError() {
|
|
52
|
+
return this._messageDetails.length > 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
exports.CompilerErrorResult = CompilerErrorResult;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.defaultDistFileExtMap = void 0;
|
|
7
|
+
const defaultDistFileExtMap = {
|
|
8
|
+
'.js': '.js',
|
|
9
|
+
'.jsx': '.js',
|
|
10
|
+
'.ts': '.js',
|
|
11
|
+
'.tsx': '.js'
|
|
12
|
+
};
|
|
13
|
+
exports.defaultDistFileExtMap = defaultDistFileExtMap;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.mergeDefaultOption = void 0;
|
|
7
|
+
|
|
8
|
+
var _constants = require("./constants");
|
|
9
|
+
|
|
10
|
+
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; }
|
|
11
|
+
|
|
12
|
+
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; }
|
|
13
|
+
|
|
14
|
+
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; }
|
|
15
|
+
|
|
16
|
+
const defaultOptions = {
|
|
17
|
+
enableWatch: false,
|
|
18
|
+
enableVirtualDist: false,
|
|
19
|
+
extensions: [],
|
|
20
|
+
filenames: [],
|
|
21
|
+
distFileExtMap: _constants.defaultDistFileExtMap,
|
|
22
|
+
ignore: [],
|
|
23
|
+
quiet: false,
|
|
24
|
+
verbose: false,
|
|
25
|
+
clean: false
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const mergeDefaultOption = compilerOptions => _objectSpread(_objectSpread({}, defaultOptions), compilerOptions);
|
|
29
|
+
|
|
30
|
+
exports.mergeDefaultOption = mergeDefaultOption;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getGlobPattern = exports.getFinalExtensions = exports.getFinalCompilerOption = exports.getFilesFromDir = void 0;
|
|
7
|
+
|
|
8
|
+
var glob = _interopRequireWildcard(require("glob"));
|
|
9
|
+
|
|
10
|
+
var _core = require("@babel/core");
|
|
11
|
+
|
|
12
|
+
var _defaults = require("./defaults");
|
|
13
|
+
|
|
14
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
15
|
+
|
|
16
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
17
|
+
|
|
18
|
+
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; }
|
|
19
|
+
|
|
20
|
+
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; }
|
|
21
|
+
|
|
22
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
23
|
+
|
|
24
|
+
const getGlobPattern = (dir, extensions) => {
|
|
25
|
+
if (extensions.length > 1) {
|
|
26
|
+
return `${dir}/**/*{${extensions.join(',')}}`;
|
|
27
|
+
} else if (extensions.length === 1) {
|
|
28
|
+
return `${dir}/**/*${extensions[0]}`;
|
|
29
|
+
} else {
|
|
30
|
+
return `${dir}/**/*`;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
exports.getGlobPattern = getGlobPattern;
|
|
35
|
+
|
|
36
|
+
const getFinalExtensions = extensions => {
|
|
37
|
+
const isExtensions = ext => Array.isArray(ext);
|
|
38
|
+
|
|
39
|
+
const isExtensionsFunc = ext => typeof ext === 'function';
|
|
40
|
+
|
|
41
|
+
if (isExtensions(extensions)) {
|
|
42
|
+
return [...extensions, ..._core.DEFAULT_EXTENSIONS];
|
|
43
|
+
} else if (isExtensionsFunc(extensions)) {
|
|
44
|
+
return extensions(_core.DEFAULT_EXTENSIONS);
|
|
45
|
+
} else {
|
|
46
|
+
return _core.DEFAULT_EXTENSIONS;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
exports.getFinalExtensions = getFinalExtensions;
|
|
51
|
+
|
|
52
|
+
const getFilesFromDir = ({
|
|
53
|
+
dir,
|
|
54
|
+
finalExt: _finalExt = [],
|
|
55
|
+
ignore: _ignore = []
|
|
56
|
+
}) => {
|
|
57
|
+
let globFindFilenames = [];
|
|
58
|
+
const globPattern = getGlobPattern(dir, _finalExt);
|
|
59
|
+
globFindFilenames = glob.sync(globPattern, {
|
|
60
|
+
ignore: _ignore
|
|
61
|
+
});
|
|
62
|
+
return globFindFilenames;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
exports.getFilesFromDir = getFilesFromDir;
|
|
66
|
+
|
|
67
|
+
const getFinalCompilerOption = option => {
|
|
68
|
+
const optionWithDefault = (0, _defaults.mergeDefaultOption)(option);
|
|
69
|
+
const {
|
|
70
|
+
sourceDir,
|
|
71
|
+
ignore,
|
|
72
|
+
enableWatch = false,
|
|
73
|
+
watchDir,
|
|
74
|
+
extensions = _core.DEFAULT_EXTENSIONS
|
|
75
|
+
} = option;
|
|
76
|
+
let globFindFilenames = [];
|
|
77
|
+
const finalExt = getFinalExtensions(extensions);
|
|
78
|
+
|
|
79
|
+
if (sourceDir) {
|
|
80
|
+
globFindFilenames = getFilesFromDir({
|
|
81
|
+
dir: sourceDir,
|
|
82
|
+
ignore,
|
|
83
|
+
finalExt
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (enableWatch) {
|
|
88
|
+
// 开启watch模式,清空通过 sourceDir 找到的文件,而改为使用watchDirs
|
|
89
|
+
globFindFilenames = getFilesFromDir({
|
|
90
|
+
dir: watchDir,
|
|
91
|
+
ignore,
|
|
92
|
+
finalExt
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return _objectSpread(_objectSpread({}, optionWithDefault), {}, {
|
|
97
|
+
filenames: [...optionWithDefault.filenames, ...globFindFilenames]
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
exports.getFinalCompilerOption = getFinalCompilerOption;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
compiler: true
|
|
8
|
+
};
|
|
9
|
+
exports.compiler = compiler;
|
|
10
|
+
|
|
11
|
+
var _getFinalOption = require("./getFinalOption");
|
|
12
|
+
|
|
13
|
+
var _build = require("./build");
|
|
14
|
+
|
|
15
|
+
var _buildWatch = require("./buildWatch");
|
|
16
|
+
|
|
17
|
+
Object.keys(_buildWatch).forEach(function (key) {
|
|
18
|
+
if (key === "default" || key === "__esModule") return;
|
|
19
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
20
|
+
if (key in exports && exports[key] === _buildWatch[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _buildWatch[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
var _validate = require("./validate");
|
|
30
|
+
|
|
31
|
+
var _type = require("./type");
|
|
32
|
+
|
|
33
|
+
Object.keys(_type).forEach(function (key) {
|
|
34
|
+
if (key === "default" || key === "__esModule") return;
|
|
35
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
36
|
+
if (key in exports && exports[key] === _type[key]) return;
|
|
37
|
+
Object.defineProperty(exports, key, {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () {
|
|
40
|
+
return _type[key];
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
async function compiler(compilerOptions, babelOptions = {}) {
|
|
46
|
+
const validRet = (0, _validate.validate)(compilerOptions);
|
|
47
|
+
|
|
48
|
+
if (validRet) {
|
|
49
|
+
return validRet;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const finalCompilerOption = (0, _getFinalOption.getFinalCompilerOption)(compilerOptions);
|
|
53
|
+
|
|
54
|
+
if (compilerOptions.enableWatch) {
|
|
55
|
+
return (0, _buildWatch.buildWatch)(finalCompilerOption, babelOptions);
|
|
56
|
+
} else {
|
|
57
|
+
return (0, _build.build)(finalCompilerOption, babelOptions);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.addSourceMappingUrl = addSourceMappingUrl;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
+
|
|
12
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
+
|
|
14
|
+
function addSourceMappingUrl(code, loc) {
|
|
15
|
+
return `${code}\n//# sourceMappingURL=${path.basename(loc)}`;
|
|
16
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.watchDirValidMessage = exports.validateWatchDir = exports.validateSourceDirAndFileNames = exports.validate = exports.sourceDirAndFileNamesValidMessage = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = require("@modern-js/utils");
|
|
9
|
+
|
|
10
|
+
const sourceDirAndFileNamesValidMessage = 'At least one of the sourceDir and filenames configurations must be configured';
|
|
11
|
+
exports.sourceDirAndFileNamesValidMessage = sourceDirAndFileNamesValidMessage;
|
|
12
|
+
const watchDirValidMessage = 'should set watchDir when enableWatch is true';
|
|
13
|
+
exports.watchDirValidMessage = watchDirValidMessage;
|
|
14
|
+
|
|
15
|
+
const validateSourceDirAndFileNames = compilerOptions => {
|
|
16
|
+
const {
|
|
17
|
+
sourceDir,
|
|
18
|
+
filenames,
|
|
19
|
+
quiet
|
|
20
|
+
} = compilerOptions;
|
|
21
|
+
|
|
22
|
+
if (!sourceDir && !filenames) {
|
|
23
|
+
if (!quiet) {
|
|
24
|
+
_utils.logger.error(sourceDirAndFileNamesValidMessage);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
code: 1,
|
|
29
|
+
message: sourceDirAndFileNamesValidMessage,
|
|
30
|
+
virtualDists: []
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return null;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
exports.validateSourceDirAndFileNames = validateSourceDirAndFileNames;
|
|
38
|
+
|
|
39
|
+
const validateWatchDir = compilerOptions => {
|
|
40
|
+
const {
|
|
41
|
+
watchDir,
|
|
42
|
+
enableWatch,
|
|
43
|
+
quiet
|
|
44
|
+
} = compilerOptions;
|
|
45
|
+
|
|
46
|
+
if (enableWatch && !watchDir) {
|
|
47
|
+
if (!quiet) {
|
|
48
|
+
_utils.logger.error(watchDirValidMessage);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
code: 1,
|
|
53
|
+
message: watchDirValidMessage,
|
|
54
|
+
virtualDists: []
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return null;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
exports.validateWatchDir = validateWatchDir;
|
|
62
|
+
|
|
63
|
+
const validate = compilerOptions => {
|
|
64
|
+
if (compilerOptions.enableWatch) {
|
|
65
|
+
return validateWatchDir(compilerOptions);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return validateSourceDirAndFileNames(compilerOptions);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
exports.validate = validate;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import * as Event from 'events';
|
|
3
|
+
import { FSWatcher } from 'chokidar';
|
|
4
|
+
import type { IFinaleCompilerOptions, BabelOptions } from './type';
|
|
5
|
+
export declare const BuildWatchEvent: {
|
|
6
|
+
firstCompiler: string;
|
|
7
|
+
compiling: string;
|
|
8
|
+
watchingCompiler: string;
|
|
9
|
+
};
|
|
10
|
+
export declare class BuildWatchEmitter extends Event.EventEmitter {
|
|
11
|
+
private _initFn;
|
|
12
|
+
setInitFn(fn: (emitter: BuildWatchEmitter) => Promise<any> | any): void;
|
|
13
|
+
watch(): Promise<FSWatcher | null>;
|
|
14
|
+
}
|
|
15
|
+
export declare const runBuildWatch: (option: IFinaleCompilerOptions, babelConfig: import("@babel/core").TransformOptions | undefined, emitter: BuildWatchEmitter) => Promise<FSWatcher>;
|
|
16
|
+
export declare const buildWatch: (option: IFinaleCompilerOptions, babelConfig?: BabelOptions) => BuildWatchEmitter;
|