@modern-js/babel-compiler 1.2.0 → 1.2.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/.eslintrc.js +8 -0
- package/CHANGELOG.md +31 -0
- package/dist/js/modern/getFinalOption.js +1 -1
- package/dist/js/modern/type.js +0 -1
- package/dist/js/node/getFinalOption.js +2 -6
- package/dist/js/node/type.js +0 -5
- package/dist/types/buildWatch.d.ts +1 -1
- package/dist/types/getFinalOption.d.ts +2 -2
- package/dist/types/type.d.ts +5 -5
- package/jest.config.js +0 -1
- package/modern.config.js +0 -1
- package/package.json +3 -7
- package/src/build.ts +0 -95
- package/src/buildWatch.ts +0 -113
- package/src/compiler.ts +0 -151
- package/src/compilerErrorResult.ts +0 -49
- package/src/constants.ts +0 -6
- package/src/defaults.ts +0 -21
- package/src/getFinalOption.ts +0 -93
- package/src/index.ts +0 -35
- package/src/type.ts +0 -76
- package/src/utils.ts +0 -5
- package/src/validate.ts +0 -47
- package/tests/.eslintrc.js +0 -6
- package/tests/build.test.ts +0 -122
- package/tests/buildWatch.test.ts +0 -251
- package/tests/compiler.test.ts +0 -112
- package/tests/compilerErrorResult.test.ts +0 -119
- package/tests/constants.test.ts +0 -12
- package/tests/defaults.test.ts +0 -29
- package/tests/fixtures/build/src/error +0 -1
- package/tests/fixtures/build/src/error1 +0 -1
- package/tests/fixtures/build/src/far.js +0 -0
- package/tests/fixtures/build/src/index.js +0 -0
- package/tests/fixtures/buildWatch/src/error +0 -2
- package/tests/fixtures/buildWatch/src/index.js +0 -0
- package/tests/fixtures/compiler/src/index.js +0 -2
- 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/src/index.js +0 -2
- package/tests/fixtures/utils/far +0 -2
- package/tests/getFinalOption.test.ts +0 -108
- package/tests/index.test.ts +0 -89
- package/tests/tsconfig.json +0 -13
- package/tests/utils.test.ts +0 -20
- package/tests/validate.test.ts +0 -68
package/.eslintrc.js
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
# @modern-js/babel-compiler
|
|
2
2
|
|
|
3
|
+
## 1.2.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 6cffe99d: chore:
|
|
8
|
+
remove react eslint rules for `modern-js` rule set.
|
|
9
|
+
add .eslintrc for each package to speed up linting
|
|
10
|
+
- 04ae5262: chore: bump @modern-js/utils to v1.4.1 in dependencies
|
|
11
|
+
- 60f7d8bf: feat: add tests dir to npmignore
|
|
12
|
+
- Updated dependencies [b8599d09]
|
|
13
|
+
- Updated dependencies [60f7d8bf]
|
|
14
|
+
- Updated dependencies [3bf4f8b0]
|
|
15
|
+
- @modern-js/utils@1.5.0
|
|
16
|
+
|
|
17
|
+
## 1.2.2
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- 55e18278: chore: remove unused dependencies and devDependencies
|
|
22
|
+
- Updated dependencies [4c792f68]
|
|
23
|
+
- Updated dependencies [a7f42f48]
|
|
24
|
+
- @modern-js/utils@1.3.3
|
|
25
|
+
|
|
26
|
+
## 1.2.1
|
|
27
|
+
|
|
28
|
+
### Patch Changes
|
|
29
|
+
|
|
30
|
+
- 83166714: change .npmignore
|
|
31
|
+
- Updated dependencies [83166714]
|
|
32
|
+
- @modern-js/utils@1.2.2
|
|
33
|
+
|
|
3
34
|
## 1.2.0
|
|
4
35
|
|
|
5
36
|
### Minor Changes
|
|
@@ -4,7 +4,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
4
4
|
|
|
5
5
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
6
|
|
|
7
|
-
import
|
|
7
|
+
import { glob } from '@modern-js/utils';
|
|
8
8
|
import { DEFAULT_EXTENSIONS } from '@babel/core';
|
|
9
9
|
import { mergeDefaultOption } from "./defaults";
|
|
10
10
|
export const getGlobPattern = (dir, extensions) => {
|
package/dist/js/modern/type.js
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -5,16 +5,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.getGlobPattern = exports.getFinalExtensions = exports.getFinalCompilerOption = exports.getFilesFromDir = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _utils = require("@modern-js/utils");
|
|
9
9
|
|
|
10
10
|
var _core = require("@babel/core");
|
|
11
11
|
|
|
12
12
|
var _defaults = require("./defaults");
|
|
13
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
14
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
19
15
|
|
|
20
16
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
@@ -56,7 +52,7 @@ const getFilesFromDir = ({
|
|
|
56
52
|
}) => {
|
|
57
53
|
let globFindFilenames = [];
|
|
58
54
|
const globPattern = getGlobPattern(dir, _finalExt);
|
|
59
|
-
globFindFilenames = glob.sync(globPattern, {
|
|
55
|
+
globFindFilenames = _utils.glob.sync(globPattern, {
|
|
60
56
|
ignore: _ignore
|
|
61
57
|
});
|
|
62
58
|
return globFindFilenames;
|
package/dist/js/node/type.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import * as Event from 'events';
|
|
3
|
-
import { FSWatcher } from '
|
|
3
|
+
import { FSWatcher } from '@modern-js/utils';
|
|
4
4
|
import type { IFinaleCompilerOptions, BabelOptions } from './type';
|
|
5
5
|
export declare const BuildWatchEvent: {
|
|
6
6
|
firstCompiler: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { GlobOptions } from '@modern-js/utils';
|
|
2
2
|
import { Extensions, ExtensionsFunc, ICompilerOptions, IFinaleCompilerOptions } from './type';
|
|
3
3
|
export declare const getGlobPattern: (dir: string, extensions: Extensions) => string;
|
|
4
4
|
export declare const getFinalExtensions: (extensions: Extensions | ExtensionsFunc | undefined) => string[];
|
|
@@ -9,6 +9,6 @@ export declare const getFilesFromDir: ({
|
|
|
9
9
|
}: {
|
|
10
10
|
dir: string;
|
|
11
11
|
finalExt?: string[] | undefined;
|
|
12
|
-
ignore?:
|
|
12
|
+
ignore?: GlobOptions['ignore'];
|
|
13
13
|
}) => string[];
|
|
14
14
|
export declare const getFinalCompilerOption: (option: ICompilerOptions) => IFinaleCompilerOptions;
|
package/dist/types/type.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { TransformOptions } from '@babel/core';
|
|
2
|
-
import type {
|
|
1
|
+
import type { TransformOptions } from '@babel/core';
|
|
2
|
+
import type { GlobOptions } from '@modern-js/utils';
|
|
3
3
|
export declare type BabelOptions = TransformOptions;
|
|
4
4
|
export declare type Extensions = string[];
|
|
5
5
|
export declare type ExtensionsFunc = (defaultExtensions: Extensions) => Extensions;
|
|
@@ -13,7 +13,7 @@ export interface ICompilerOptions {
|
|
|
13
13
|
extensions?: Extensions | ExtensionsFunc;
|
|
14
14
|
filenames?: string[];
|
|
15
15
|
distFileExtMap?: Record<string, string>;
|
|
16
|
-
ignore?:
|
|
16
|
+
ignore?: GlobOptions['ignore'];
|
|
17
17
|
quiet?: boolean;
|
|
18
18
|
verbose?: boolean;
|
|
19
19
|
clean?: boolean;
|
|
@@ -28,7 +28,7 @@ export interface ICompilerOptionsWithDefault {
|
|
|
28
28
|
extensions: Extensions | ExtensionsFunc;
|
|
29
29
|
filenames: string[];
|
|
30
30
|
distFileExtMap?: Record<string, string>;
|
|
31
|
-
ignore:
|
|
31
|
+
ignore: GlobOptions['ignore'];
|
|
32
32
|
quiet?: boolean;
|
|
33
33
|
verbose?: boolean;
|
|
34
34
|
clean?: boolean;
|
|
@@ -36,7 +36,7 @@ export interface ICompilerOptionsWithDefault {
|
|
|
36
36
|
export interface IFinaleCompilerOptions {
|
|
37
37
|
rootDir: string;
|
|
38
38
|
filenames: string[];
|
|
39
|
-
ignore?:
|
|
39
|
+
ignore?: GlobOptions['ignore'];
|
|
40
40
|
enableVirtualDist?: boolean;
|
|
41
41
|
distDir: string;
|
|
42
42
|
watchDir?: string;
|
package/jest.config.js
CHANGED
package/modern.config.js
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.2.
|
|
14
|
+
"version": "1.2.3",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -30,9 +30,7 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@babel/core": "7.16.7",
|
|
32
32
|
"@babel/runtime": "^7",
|
|
33
|
-
"@modern-js/utils": "^1.
|
|
34
|
-
"chokidar": "^3.5.2",
|
|
35
|
-
"glob": "^7.1.6"
|
|
33
|
+
"@modern-js/utils": "^1.5.0"
|
|
36
34
|
},
|
|
37
35
|
"devDependencies": {
|
|
38
36
|
"@babel/plugin-transform-classes": "^7.14.9",
|
|
@@ -41,7 +39,6 @@
|
|
|
41
39
|
"@types/glob": "^7.1.4",
|
|
42
40
|
"@types/jest": "^26",
|
|
43
41
|
"@types/node": "^14",
|
|
44
|
-
"jest-jasmine2": "^27.2.2",
|
|
45
42
|
"typescript": "^4",
|
|
46
43
|
"@scripts/build": "0.0.0",
|
|
47
44
|
"jest": "^27",
|
|
@@ -55,8 +52,7 @@
|
|
|
55
52
|
},
|
|
56
53
|
"publishConfig": {
|
|
57
54
|
"registry": "https://registry.npmjs.org/",
|
|
58
|
-
"access": "public"
|
|
59
|
-
"types": "./dist/types/index.d.ts"
|
|
55
|
+
"access": "public"
|
|
60
56
|
},
|
|
61
57
|
"scripts": {
|
|
62
58
|
"new": "modern new",
|
package/src/build.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { fs, logger } from '@modern-js/utils';
|
|
2
|
-
import { defaultDistFileExtMap } from './constants';
|
|
3
|
-
import { compiler } from './compiler';
|
|
4
|
-
import type {
|
|
5
|
-
IFinaleCompilerOptions,
|
|
6
|
-
BabelOptions,
|
|
7
|
-
ICompilerResult,
|
|
8
|
-
IVirtualDist,
|
|
9
|
-
ICompilerMessageDetail,
|
|
10
|
-
} from './type';
|
|
11
|
-
|
|
12
|
-
export const build = async (
|
|
13
|
-
option: IFinaleCompilerOptions,
|
|
14
|
-
babelConfig: BabelOptions = {},
|
|
15
|
-
): Promise<ICompilerResult> => {
|
|
16
|
-
const {
|
|
17
|
-
rootDir,
|
|
18
|
-
enableVirtualDist,
|
|
19
|
-
filenames,
|
|
20
|
-
clean,
|
|
21
|
-
distDir,
|
|
22
|
-
distFileExtMap = defaultDistFileExtMap,
|
|
23
|
-
verbose = false,
|
|
24
|
-
quiet = false,
|
|
25
|
-
} = option;
|
|
26
|
-
|
|
27
|
-
const virtualDists: IVirtualDist[] = [];
|
|
28
|
-
|
|
29
|
-
if (clean) {
|
|
30
|
-
await fs.remove(distDir);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
fs.ensureDir(distDir);
|
|
34
|
-
const messageDetails: ICompilerMessageDetail[] = [];
|
|
35
|
-
|
|
36
|
-
for (const filename of filenames) {
|
|
37
|
-
try {
|
|
38
|
-
const dist = compiler({
|
|
39
|
-
rootDir,
|
|
40
|
-
enableVirtualDist,
|
|
41
|
-
filepath: filename,
|
|
42
|
-
distDir,
|
|
43
|
-
verbose,
|
|
44
|
-
quiet,
|
|
45
|
-
babelConfig,
|
|
46
|
-
distFileExtMap,
|
|
47
|
-
});
|
|
48
|
-
if (enableVirtualDist && dist) {
|
|
49
|
-
virtualDists.push(dist);
|
|
50
|
-
}
|
|
51
|
-
} catch (e: any) {
|
|
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
|
-
logger.error(
|
|
64
|
-
`Compilation failure ${messageDetails.length} ${
|
|
65
|
-
messageDetails.length !== 1 ? 'files' : 'file'
|
|
66
|
-
} with Babel.`,
|
|
67
|
-
);
|
|
68
|
-
// TODO: 具体的报错信息打印
|
|
69
|
-
} else {
|
|
70
|
-
logger.info(
|
|
71
|
-
`Successfully compiled ${filenames.length} ${
|
|
72
|
-
filenames.length !== 1 ? 'files' : 'file'
|
|
73
|
-
} with Babel.`,
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (happenError) {
|
|
79
|
-
return {
|
|
80
|
-
code: 1,
|
|
81
|
-
message: `Compilation failure ${messageDetails.length} ${
|
|
82
|
-
messageDetails.length !== 1 ? 'files' : 'file'
|
|
83
|
-
} with Babel.`,
|
|
84
|
-
messageDetails,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
code: 0,
|
|
90
|
-
message: `Successfully compiled ${filenames.length} ${
|
|
91
|
-
filenames.length !== 1 ? 'files' : 'file'
|
|
92
|
-
} with Babel.`,
|
|
93
|
-
virtualDists,
|
|
94
|
-
};
|
|
95
|
-
};
|
package/src/buildWatch.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import * as Event from 'events';
|
|
3
|
-
import { logger, watch, WatchChangeType } from '@modern-js/utils';
|
|
4
|
-
import { FSWatcher } from 'chokidar';
|
|
5
|
-
import { build } from './build';
|
|
6
|
-
import { CompilerErrorResult } from './compilerErrorResult';
|
|
7
|
-
import type {
|
|
8
|
-
IFinaleCompilerOptions,
|
|
9
|
-
BabelOptions,
|
|
10
|
-
ICompilerResult,
|
|
11
|
-
} from './type';
|
|
12
|
-
|
|
13
|
-
export const BuildWatchEvent = {
|
|
14
|
-
firstCompiler: 'first-compiler',
|
|
15
|
-
compiling: 'compiling',
|
|
16
|
-
watchingCompiler: 'watching-compiler',
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export class BuildWatchEmitter extends Event.EventEmitter {
|
|
20
|
-
private _initFn!: (
|
|
21
|
-
emitter: BuildWatchEmitter,
|
|
22
|
-
) => Promise<FSWatcher> | FSWatcher;
|
|
23
|
-
|
|
24
|
-
setInitFn(fn: (emitter: BuildWatchEmitter) => Promise<any> | any) {
|
|
25
|
-
this._initFn = fn;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async watch() {
|
|
29
|
-
if (typeof this._initFn === 'function') {
|
|
30
|
-
return this._initFn(this);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export const runBuildWatch = async (
|
|
38
|
-
option: IFinaleCompilerOptions,
|
|
39
|
-
babelConfig: BabelOptions = {},
|
|
40
|
-
emitter: BuildWatchEmitter,
|
|
41
|
-
) => {
|
|
42
|
-
emitter.emit(BuildWatchEvent.compiling);
|
|
43
|
-
const errorResult = new CompilerErrorResult();
|
|
44
|
-
const watchDir = option.watchDir as string;
|
|
45
|
-
const { distDir, quiet } = option;
|
|
46
|
-
// 第一次正常构建
|
|
47
|
-
const fisrtBuildResult = await build(option, babelConfig);
|
|
48
|
-
|
|
49
|
-
const { code } = fisrtBuildResult;
|
|
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(
|
|
58
|
-
`${watchDir}/**/*.{js,jsx,ts,tsx}`,
|
|
59
|
-
async ({ changeType, changedFilePath }) => {
|
|
60
|
-
emitter.emit(BuildWatchEvent.compiling);
|
|
61
|
-
if (changeType === WatchChangeType.UNLINK) {
|
|
62
|
-
const removeFiles = [
|
|
63
|
-
path.normalize(
|
|
64
|
-
`./${distDir}/${path.relative(watchDir, changedFilePath)}`,
|
|
65
|
-
),
|
|
66
|
-
];
|
|
67
|
-
if (!quiet) {
|
|
68
|
-
logger.info(`remove file: ${removeFiles.join(',')}`);
|
|
69
|
-
}
|
|
70
|
-
const result: ICompilerResult = {
|
|
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(
|
|
80
|
-
{ ...option, 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, {
|
|
92
|
-
...errorResult.value,
|
|
93
|
-
virtualDists: result.virtualDists,
|
|
94
|
-
} as ICompilerResult);
|
|
95
|
-
!quiet && logger.info(errorResult.value.message);
|
|
96
|
-
} else {
|
|
97
|
-
emitter.emit(BuildWatchEvent.watchingCompiler, result);
|
|
98
|
-
!quiet && logger.info(result.message);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
},
|
|
102
|
-
[`${watchDir}/**/*.d.ts`],
|
|
103
|
-
);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
export const buildWatch = (
|
|
107
|
-
option: IFinaleCompilerOptions,
|
|
108
|
-
babelConfig: BabelOptions = {},
|
|
109
|
-
) => {
|
|
110
|
-
const buildWatchEmitter = new BuildWatchEmitter();
|
|
111
|
-
buildWatchEmitter.setInitFn(runBuildWatch.bind(null, option, babelConfig));
|
|
112
|
-
return buildWatchEmitter;
|
|
113
|
-
};
|
package/src/compiler.ts
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import * as babel from '@babel/core';
|
|
3
|
-
import { logger, fs } from '@modern-js/utils';
|
|
4
|
-
import * as utils from './utils';
|
|
5
|
-
import { BabelOptions, IVirtualDist } from './type';
|
|
6
|
-
import { defaultDistFileExtMap } from './constants';
|
|
7
|
-
|
|
8
|
-
export interface ISingleFileCompilerOption {
|
|
9
|
-
filepath: string;
|
|
10
|
-
rootDir: string;
|
|
11
|
-
enableVirtualDist?: boolean;
|
|
12
|
-
distDir?: string;
|
|
13
|
-
verbose?: boolean;
|
|
14
|
-
quiet?: boolean;
|
|
15
|
-
babelConfig?: BabelOptions;
|
|
16
|
-
distFileExtMap?: Record<string, string>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const defaultDistDir = 'dist';
|
|
20
|
-
|
|
21
|
-
export const isRes = (
|
|
22
|
-
r: babel.BabelFileResult | null,
|
|
23
|
-
): r is babel.BabelFileResult => Boolean(r);
|
|
24
|
-
|
|
25
|
-
export const getDistFilePath = (option: {
|
|
26
|
-
filepath: string;
|
|
27
|
-
rootDir: string;
|
|
28
|
-
distDir: string;
|
|
29
|
-
extMap: Record<string, string>;
|
|
30
|
-
}) => {
|
|
31
|
-
const { filepath, rootDir, distDir, extMap } = option;
|
|
32
|
-
const ext = path.extname(filepath);
|
|
33
|
-
return path.join(
|
|
34
|
-
distDir,
|
|
35
|
-
path.relative(rootDir, filepath).replace(ext, extMap[ext]),
|
|
36
|
-
);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const resolveSourceMap = (option: {
|
|
40
|
-
babelRes: babel.BabelFileResult;
|
|
41
|
-
sourceFilePath: string;
|
|
42
|
-
distFilePath: string;
|
|
43
|
-
enableVirtualDist?: boolean;
|
|
44
|
-
}) => {
|
|
45
|
-
const {
|
|
46
|
-
babelRes,
|
|
47
|
-
sourceFilePath,
|
|
48
|
-
distFilePath,
|
|
49
|
-
enableVirtualDist = false,
|
|
50
|
-
} = option;
|
|
51
|
-
const mapLoc = `${distFilePath}.map`;
|
|
52
|
-
babelRes.code = utils.addSourceMappingUrl(babelRes.code as string, mapLoc);
|
|
53
|
-
|
|
54
|
-
if (babelRes.map) {
|
|
55
|
-
babelRes.map.file = path.basename(distFilePath);
|
|
56
|
-
babelRes.map.sources = [
|
|
57
|
-
path.relative(path.dirname(distFilePath), sourceFilePath),
|
|
58
|
-
];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const sourceMapVirtualDist = {
|
|
62
|
-
sourcemap: JSON.stringify(babelRes.map),
|
|
63
|
-
sourceMapPath: mapLoc,
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
if (enableVirtualDist) {
|
|
67
|
-
return sourceMapVirtualDist;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
fs.ensureDirSync(path.dirname(mapLoc));
|
|
71
|
-
fs.writeFileSync(mapLoc, JSON.stringify(babelRes.map));
|
|
72
|
-
|
|
73
|
-
return sourceMapVirtualDist;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
export const compiler = (option: ISingleFileCompilerOption) => {
|
|
77
|
-
const {
|
|
78
|
-
filepath,
|
|
79
|
-
rootDir,
|
|
80
|
-
enableVirtualDist = false,
|
|
81
|
-
distDir = path.join(path.dirname(rootDir), defaultDistDir),
|
|
82
|
-
verbose = false,
|
|
83
|
-
babelConfig = {},
|
|
84
|
-
distFileExtMap = defaultDistFileExtMap,
|
|
85
|
-
quiet = false,
|
|
86
|
-
} = option;
|
|
87
|
-
const babelRes = babel.transformFileSync(filepath, babelConfig);
|
|
88
|
-
let virtualDist: IVirtualDist | null = null;
|
|
89
|
-
|
|
90
|
-
if (!isRes(babelRes)) {
|
|
91
|
-
throw new Error(`${filepath} happen error`);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const distFilePath = getDistFilePath({
|
|
95
|
-
filepath,
|
|
96
|
-
rootDir,
|
|
97
|
-
distDir,
|
|
98
|
-
extMap: distFileExtMap,
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
if (enableVirtualDist) {
|
|
102
|
-
virtualDist = {
|
|
103
|
-
distPath: distFilePath,
|
|
104
|
-
sourceMapPath: '',
|
|
105
|
-
code: '',
|
|
106
|
-
sourcemap: '',
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (
|
|
111
|
-
babelRes?.map &&
|
|
112
|
-
babelConfig.sourceMaps &&
|
|
113
|
-
babelConfig.sourceMaps !== 'inline'
|
|
114
|
-
) {
|
|
115
|
-
if (virtualDist) {
|
|
116
|
-
virtualDist = {
|
|
117
|
-
...virtualDist,
|
|
118
|
-
...resolveSourceMap({
|
|
119
|
-
babelRes,
|
|
120
|
-
sourceFilePath: filepath,
|
|
121
|
-
distFilePath,
|
|
122
|
-
enableVirtualDist,
|
|
123
|
-
}),
|
|
124
|
-
};
|
|
125
|
-
} else {
|
|
126
|
-
resolveSourceMap({
|
|
127
|
-
babelRes,
|
|
128
|
-
sourceFilePath: filepath,
|
|
129
|
-
distFilePath,
|
|
130
|
-
enableVirtualDist,
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (virtualDist) {
|
|
136
|
-
virtualDist = {
|
|
137
|
-
...virtualDist,
|
|
138
|
-
distPath: distFilePath,
|
|
139
|
-
code: babelRes.code as string,
|
|
140
|
-
};
|
|
141
|
-
} else {
|
|
142
|
-
fs.ensureDirSync(path.dirname(distFilePath));
|
|
143
|
-
fs.writeFileSync(distFilePath, babelRes.code as string);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (verbose && !quiet) {
|
|
147
|
-
logger.info(`${filepath} => ${distFilePath}`);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return virtualDist;
|
|
151
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { ICompilerResult, ICompilerMessageDetail } from './type';
|
|
2
|
-
|
|
3
|
-
export class CompilerErrorResult {
|
|
4
|
-
_messageDetails!: ICompilerMessageDetail[];
|
|
5
|
-
|
|
6
|
-
constructor(initErrorResult?: ICompilerResult) {
|
|
7
|
-
this.init(initErrorResult);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
init(initErrorResult?: ICompilerResult) {
|
|
11
|
-
this._messageDetails = initErrorResult?.messageDetails || [];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
update(messageDetails: ICompilerMessageDetail[]) {
|
|
15
|
-
for (const messageDetail of messageDetails) {
|
|
16
|
-
// 遍历存不存在该文件报错信息,不存在则增加,否则更新内容
|
|
17
|
-
const addError = !this._messageDetails.some(detail => {
|
|
18
|
-
if (detail.filename === messageDetail.filename) {
|
|
19
|
-
// 如果错误栈里存在该文件报错信息,则更新内容
|
|
20
|
-
detail.content = messageDetail.content;
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
if (addError) {
|
|
27
|
-
this._messageDetails.push(messageDetail);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
removeByFileName(filename: string) {
|
|
33
|
-
this._messageDetails = this._messageDetails.filter(
|
|
34
|
-
detail => detail.filename !== filename,
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get value(): ICompilerResult {
|
|
39
|
-
return {
|
|
40
|
-
code: 1,
|
|
41
|
-
message: `Compilation failure ${this._messageDetails?.length} files with Babel.`,
|
|
42
|
-
messageDetails: this._messageDetails,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
checkExistError() {
|
|
47
|
-
return this._messageDetails.length > 0;
|
|
48
|
-
}
|
|
49
|
-
}
|
package/src/constants.ts
DELETED
package/src/defaults.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { ICompilerOptions, ICompilerOptionsWithDefault } from './type';
|
|
2
|
-
import { defaultDistFileExtMap } from './constants';
|
|
3
|
-
|
|
4
|
-
const defaultOptions = {
|
|
5
|
-
enableWatch: false,
|
|
6
|
-
enableVirtualDist: false,
|
|
7
|
-
extensions: [],
|
|
8
|
-
filenames: [],
|
|
9
|
-
distFileExtMap: defaultDistFileExtMap,
|
|
10
|
-
ignore: [],
|
|
11
|
-
quiet: false,
|
|
12
|
-
verbose: false,
|
|
13
|
-
clean: false,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const mergeDefaultOption = (
|
|
17
|
-
compilerOptions: ICompilerOptions,
|
|
18
|
-
): ICompilerOptionsWithDefault => ({
|
|
19
|
-
...defaultOptions,
|
|
20
|
-
...compilerOptions,
|
|
21
|
-
});
|