@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/src/getFinalOption.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import * as glob from 'glob';
|
|
2
|
-
import type { IOptions } from 'glob';
|
|
3
|
-
import { DEFAULT_EXTENSIONS } from '@babel/core';
|
|
4
|
-
import {
|
|
5
|
-
Extensions,
|
|
6
|
-
ExtensionsFunc,
|
|
7
|
-
ICompilerOptions,
|
|
8
|
-
IFinaleCompilerOptions,
|
|
9
|
-
} from './type';
|
|
10
|
-
import { mergeDefaultOption } from './defaults';
|
|
11
|
-
|
|
12
|
-
export const getGlobPattern = (dir: string, extensions: Extensions) => {
|
|
13
|
-
if (extensions.length > 1) {
|
|
14
|
-
return `${dir}/**/*{${extensions.join(',')}}`;
|
|
15
|
-
} else if (extensions.length === 1) {
|
|
16
|
-
return `${dir}/**/*${extensions[0]}`;
|
|
17
|
-
} else {
|
|
18
|
-
return `${dir}/**/*`;
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
export const getFinalExtensions = (
|
|
22
|
-
extensions: Extensions | ExtensionsFunc | undefined,
|
|
23
|
-
) => {
|
|
24
|
-
const isExtensions = (
|
|
25
|
-
ext: Extensions | ExtensionsFunc | undefined,
|
|
26
|
-
): ext is Extensions => Array.isArray(ext);
|
|
27
|
-
|
|
28
|
-
const isExtensionsFunc = (
|
|
29
|
-
ext: Extensions | ExtensionsFunc | undefined,
|
|
30
|
-
): ext is ExtensionsFunc => typeof ext === 'function';
|
|
31
|
-
|
|
32
|
-
if (isExtensions(extensions)) {
|
|
33
|
-
return [...extensions, ...DEFAULT_EXTENSIONS];
|
|
34
|
-
} else if (isExtensionsFunc(extensions)) {
|
|
35
|
-
return extensions(DEFAULT_EXTENSIONS);
|
|
36
|
-
} else {
|
|
37
|
-
return DEFAULT_EXTENSIONS;
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export const getFilesFromDir = ({
|
|
42
|
-
dir,
|
|
43
|
-
finalExt = [],
|
|
44
|
-
ignore = [],
|
|
45
|
-
}: {
|
|
46
|
-
dir: string;
|
|
47
|
-
finalExt?: string[];
|
|
48
|
-
ignore?: IOptions['ignore'];
|
|
49
|
-
}) => {
|
|
50
|
-
let globFindFilenames: string[] = [];
|
|
51
|
-
const globPattern = getGlobPattern(dir, finalExt);
|
|
52
|
-
|
|
53
|
-
globFindFilenames = glob.sync(globPattern, { ignore });
|
|
54
|
-
|
|
55
|
-
return globFindFilenames;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export const getFinalCompilerOption = (
|
|
59
|
-
option: ICompilerOptions,
|
|
60
|
-
): IFinaleCompilerOptions => {
|
|
61
|
-
const optionWithDefault = mergeDefaultOption(option);
|
|
62
|
-
const {
|
|
63
|
-
sourceDir,
|
|
64
|
-
ignore,
|
|
65
|
-
enableWatch = false,
|
|
66
|
-
watchDir,
|
|
67
|
-
extensions = DEFAULT_EXTENSIONS,
|
|
68
|
-
} = option;
|
|
69
|
-
let globFindFilenames: string[] = [];
|
|
70
|
-
|
|
71
|
-
const finalExt = getFinalExtensions(extensions);
|
|
72
|
-
if (sourceDir) {
|
|
73
|
-
globFindFilenames = getFilesFromDir({
|
|
74
|
-
dir: sourceDir,
|
|
75
|
-
ignore,
|
|
76
|
-
finalExt,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (enableWatch) {
|
|
81
|
-
// 开启watch模式,清空通过 sourceDir 找到的文件,而改为使用watchDirs
|
|
82
|
-
globFindFilenames = getFilesFromDir({
|
|
83
|
-
dir: watchDir as string,
|
|
84
|
-
ignore,
|
|
85
|
-
finalExt,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return {
|
|
90
|
-
...optionWithDefault,
|
|
91
|
-
filenames: [...optionWithDefault.filenames, ...globFindFilenames],
|
|
92
|
-
};
|
|
93
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { ICompilerOptions, ICompilerResult, BabelOptions } from './type';
|
|
2
|
-
import { getFinalCompilerOption } from './getFinalOption';
|
|
3
|
-
import { build } from './build';
|
|
4
|
-
import { buildWatch, BuildWatchEmitter } from './buildWatch';
|
|
5
|
-
import { validate } from './validate';
|
|
6
|
-
|
|
7
|
-
export async function compiler(
|
|
8
|
-
compilerOptions: ICompilerOptions & { enableWatch: true },
|
|
9
|
-
babelOptions?: BabelOptions,
|
|
10
|
-
): Promise<BuildWatchEmitter>;
|
|
11
|
-
export async function compiler(
|
|
12
|
-
compilerOptions: ICompilerOptions & { enableWatch?: false },
|
|
13
|
-
babelOptions?: BabelOptions,
|
|
14
|
-
): Promise<ICompilerResult>;
|
|
15
|
-
|
|
16
|
-
export async function compiler(
|
|
17
|
-
compilerOptions: ICompilerOptions,
|
|
18
|
-
babelOptions: BabelOptions = {},
|
|
19
|
-
) {
|
|
20
|
-
const validRet = validate(compilerOptions);
|
|
21
|
-
|
|
22
|
-
if (validRet) {
|
|
23
|
-
return validRet;
|
|
24
|
-
}
|
|
25
|
-
const finalCompilerOption = getFinalCompilerOption(compilerOptions);
|
|
26
|
-
|
|
27
|
-
if (compilerOptions.enableWatch) {
|
|
28
|
-
return buildWatch(finalCompilerOption, babelOptions);
|
|
29
|
-
} else {
|
|
30
|
-
return build(finalCompilerOption, babelOptions);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export * from './buildWatch';
|
|
35
|
-
export * from './type';
|
package/src/type.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { TransformOptions } from '@babel/core';
|
|
2
|
-
import type { IOptions } from 'glob';
|
|
3
|
-
|
|
4
|
-
export type BabelOptions = TransformOptions;
|
|
5
|
-
export type Extensions = string[];
|
|
6
|
-
export type ExtensionsFunc = (defaultExtensions: Extensions) => Extensions;
|
|
7
|
-
|
|
8
|
-
export interface ICompilerOptions {
|
|
9
|
-
rootDir: string;
|
|
10
|
-
distDir: string;
|
|
11
|
-
sourceDir?: string;
|
|
12
|
-
watchDir?: string;
|
|
13
|
-
enableWatch?: boolean;
|
|
14
|
-
enableVirtualDist?: boolean;
|
|
15
|
-
extensions?: Extensions | ExtensionsFunc;
|
|
16
|
-
filenames?: string[];
|
|
17
|
-
distFileExtMap?: Record<string, string>;
|
|
18
|
-
ignore?: IOptions['ignore'];
|
|
19
|
-
quiet?: boolean;
|
|
20
|
-
verbose?: boolean;
|
|
21
|
-
clean?: boolean;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface ICompilerOptionsWithDefault {
|
|
25
|
-
rootDir: string;
|
|
26
|
-
enableVirtualDist?: boolean;
|
|
27
|
-
sourceDir?: string;
|
|
28
|
-
watchDir?: string;
|
|
29
|
-
enableWatch?: boolean;
|
|
30
|
-
distDir: string;
|
|
31
|
-
extensions: Extensions | ExtensionsFunc;
|
|
32
|
-
filenames: string[];
|
|
33
|
-
distFileExtMap?: Record<string, string>;
|
|
34
|
-
ignore: IOptions['ignore'];
|
|
35
|
-
quiet?: boolean;
|
|
36
|
-
verbose?: boolean;
|
|
37
|
-
clean?: boolean;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface IFinaleCompilerOptions {
|
|
41
|
-
rootDir: string;
|
|
42
|
-
filenames: string[];
|
|
43
|
-
ignore?: IOptions['ignore'];
|
|
44
|
-
enableVirtualDist?: boolean;
|
|
45
|
-
distDir: string;
|
|
46
|
-
watchDir?: string;
|
|
47
|
-
enableWatch?: boolean;
|
|
48
|
-
distFileExtMap?: Record<string, string>;
|
|
49
|
-
quiet?: boolean;
|
|
50
|
-
verbose?: boolean;
|
|
51
|
-
clean?: boolean;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface BuildOptions {
|
|
55
|
-
babelOptions: BabelOptions;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface IVirtualDist {
|
|
59
|
-
distPath: string;
|
|
60
|
-
sourceMapPath: string;
|
|
61
|
-
code: string;
|
|
62
|
-
sourcemap: string;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export interface ICompilerMessageDetail {
|
|
66
|
-
filename: string;
|
|
67
|
-
content: string;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export interface ICompilerResult {
|
|
71
|
-
code: number;
|
|
72
|
-
message: string;
|
|
73
|
-
messageDetails?: ICompilerMessageDetail[];
|
|
74
|
-
virtualDists?: IVirtualDist[];
|
|
75
|
-
removeFiles?: string[];
|
|
76
|
-
}
|
package/src/utils.ts
DELETED
package/src/validate.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { logger } from '@modern-js/utils';
|
|
2
|
-
import { ICompilerOptions, ICompilerResult } from './type';
|
|
3
|
-
|
|
4
|
-
export const sourceDirAndFileNamesValidMessage =
|
|
5
|
-
'At least one of the sourceDir and filenames configurations must be configured';
|
|
6
|
-
export const watchDirValidMessage =
|
|
7
|
-
'should set watchDir when enableWatch is true';
|
|
8
|
-
|
|
9
|
-
export const validateSourceDirAndFileNames = (
|
|
10
|
-
compilerOptions: ICompilerOptions,
|
|
11
|
-
): ICompilerResult | null => {
|
|
12
|
-
const { sourceDir, filenames, quiet } = compilerOptions;
|
|
13
|
-
if (!sourceDir && !filenames) {
|
|
14
|
-
if (!quiet) {
|
|
15
|
-
logger.error(sourceDirAndFileNamesValidMessage);
|
|
16
|
-
}
|
|
17
|
-
return {
|
|
18
|
-
code: 1,
|
|
19
|
-
message: sourceDirAndFileNamesValidMessage,
|
|
20
|
-
virtualDists: [],
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return null;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const validateWatchDir = (
|
|
28
|
-
compilerOptions: ICompilerOptions,
|
|
29
|
-
): ICompilerResult | null => {
|
|
30
|
-
const { watchDir, enableWatch, quiet } = compilerOptions;
|
|
31
|
-
if (enableWatch && !watchDir) {
|
|
32
|
-
if (!quiet) {
|
|
33
|
-
logger.error(watchDirValidMessage);
|
|
34
|
-
}
|
|
35
|
-
return { code: 1, message: watchDirValidMessage, virtualDists: [] };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return null;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export const validate = (compilerOptions: ICompilerOptions) => {
|
|
42
|
-
if (compilerOptions.enableWatch) {
|
|
43
|
-
return validateWatchDir(compilerOptions);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return validateSourceDirAndFileNames(compilerOptions);
|
|
47
|
-
};
|
package/tests/.eslintrc.js
DELETED
package/tests/build.test.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import * as glob from 'glob';
|
|
3
|
-
import { fs, logger } from '@modern-js/utils';
|
|
4
|
-
import { build } from '../src/build';
|
|
5
|
-
|
|
6
|
-
const projectDir = path.join(__dirname, './fixtures/build');
|
|
7
|
-
const srcDir = path.join(projectDir, 'src');
|
|
8
|
-
const distDir = path.join(projectDir, 'dist');
|
|
9
|
-
|
|
10
|
-
const originalLogInfo = logger.info;
|
|
11
|
-
const originalLogError = logger.error;
|
|
12
|
-
|
|
13
|
-
describe('test build', () => {
|
|
14
|
-
let testLogs: any[] = [];
|
|
15
|
-
const mockedLogInfo = (s: string | number | Error | undefined) =>
|
|
16
|
-
testLogs.push(s);
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
logger.info = mockedLogInfo;
|
|
19
|
-
logger.error = mockedLogInfo;
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('normal build and success', async () => {
|
|
23
|
-
const filenames = [path.join(srcDir, './index.js')];
|
|
24
|
-
await build({ rootDir: srcDir, filenames, distDir });
|
|
25
|
-
const globFindFiles = glob.sync(`${distDir}/**/*.js`);
|
|
26
|
-
expect(globFindFiles.length).toBe(1);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('build with enableVirtualDist and success', async () => {
|
|
30
|
-
const filenames = [path.join(srcDir, './index.js')];
|
|
31
|
-
const ret_1 = await build({
|
|
32
|
-
rootDir: srcDir,
|
|
33
|
-
filenames,
|
|
34
|
-
distDir,
|
|
35
|
-
});
|
|
36
|
-
expect(ret_1.code).toBe(0);
|
|
37
|
-
expect(ret_1.message.includes('Successfully compiled')).toBe(true);
|
|
38
|
-
expect(ret_1.virtualDists?.length).toBe(0);
|
|
39
|
-
|
|
40
|
-
const ret = await build({
|
|
41
|
-
rootDir: srcDir,
|
|
42
|
-
filenames,
|
|
43
|
-
distDir,
|
|
44
|
-
enableVirtualDist: true,
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
expect(ret.code).toBe(0);
|
|
48
|
-
expect(ret.virtualDists?.length).toBe(1);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('build with quiet and success', async () => {
|
|
52
|
-
const filenames = [path.join(srcDir, './index.js')];
|
|
53
|
-
await build({
|
|
54
|
-
rootDir: srcDir,
|
|
55
|
-
filenames,
|
|
56
|
-
distDir,
|
|
57
|
-
});
|
|
58
|
-
expect(testLogs.length).toBe(1);
|
|
59
|
-
testLogs = [];
|
|
60
|
-
|
|
61
|
-
await build({
|
|
62
|
-
rootDir: srcDir,
|
|
63
|
-
filenames,
|
|
64
|
-
distDir,
|
|
65
|
-
quiet: true,
|
|
66
|
-
});
|
|
67
|
-
expect(testLogs.length).toBe(0);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('build with clean and success', async () => {
|
|
71
|
-
fs.ensureFileSync(path.join(distDir, './test1.js'));
|
|
72
|
-
fs.ensureFileSync(path.join(distDir, './test2.js'));
|
|
73
|
-
const filenames = [path.join(srcDir, './index.js')];
|
|
74
|
-
await build({
|
|
75
|
-
rootDir: srcDir,
|
|
76
|
-
filenames,
|
|
77
|
-
distDir,
|
|
78
|
-
clean: true,
|
|
79
|
-
});
|
|
80
|
-
const globFindFiles = glob.sync(`${distDir}/**/*.js`);
|
|
81
|
-
expect(globFindFiles.length).toBe(1);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('build multiple files and success', async () => {
|
|
85
|
-
const filenames = [
|
|
86
|
-
path.join(srcDir, './index.js'),
|
|
87
|
-
path.join(srcDir, './far.js'),
|
|
88
|
-
];
|
|
89
|
-
const ret = await build({ rootDir: srcDir, filenames, distDir });
|
|
90
|
-
expect(ret.code).toBe(0);
|
|
91
|
-
expect(ret.message.includes('files')).toBe(true);
|
|
92
|
-
expect((testLogs[0] as string).includes('files')).toBe(true);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('build and fail', async () => {
|
|
96
|
-
const filenames = [path.join(srcDir, './error')];
|
|
97
|
-
const ret = await build({ rootDir: srcDir, filenames, distDir });
|
|
98
|
-
expect(ret.code).toBe(1);
|
|
99
|
-
expect(ret.messageDetails?.length).toBe(1);
|
|
100
|
-
expect(ret.message.includes('Compilation failure')).toBe(true);
|
|
101
|
-
expect(testLogs.length).toBe(1);
|
|
102
|
-
expect((testLogs[0] as string).includes('Compilation failure')).toBe(true);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('build multiple files and fail', async () => {
|
|
106
|
-
const filenames = [
|
|
107
|
-
path.join(srcDir, './error'),
|
|
108
|
-
path.join(srcDir, './error1'),
|
|
109
|
-
];
|
|
110
|
-
const ret = await build({ rootDir: srcDir, filenames, distDir });
|
|
111
|
-
expect(ret.code).toBe(1);
|
|
112
|
-
expect(ret.message.includes('files')).toBe(true);
|
|
113
|
-
expect((testLogs[0] as string).includes('files')).toBe(true);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
afterEach(() => {
|
|
117
|
-
logger.info = originalLogInfo;
|
|
118
|
-
logger.error = originalLogError;
|
|
119
|
-
testLogs = [];
|
|
120
|
-
fs.removeSync(distDir);
|
|
121
|
-
});
|
|
122
|
-
});
|
package/tests/buildWatch.test.ts
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import { fs, logger } from '@modern-js/utils';
|
|
3
|
-
import {
|
|
4
|
-
buildWatch,
|
|
5
|
-
BuildWatchEmitter,
|
|
6
|
-
runBuildWatch,
|
|
7
|
-
BuildWatchEvent,
|
|
8
|
-
} from '../src/buildWatch';
|
|
9
|
-
import { ICompilerResult } from '../src/type';
|
|
10
|
-
|
|
11
|
-
const projectDir = path.join(__dirname, './fixtures/buildWatch');
|
|
12
|
-
const srcDir = path.join(projectDir, 'src');
|
|
13
|
-
const distDir = path.join(projectDir, 'dist');
|
|
14
|
-
|
|
15
|
-
const originalLogInfo = logger.info;
|
|
16
|
-
const originalLogError = logger.error;
|
|
17
|
-
|
|
18
|
-
describe('test build watch', () => {
|
|
19
|
-
let testLogs: any[] = [];
|
|
20
|
-
const mockedLogInfo = (s: string | number | Error | undefined) =>
|
|
21
|
-
testLogs.push(s);
|
|
22
|
-
beforeEach(() => {
|
|
23
|
-
logger.info = mockedLogInfo;
|
|
24
|
-
logger.error = mockedLogInfo;
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('class BuildWatchEmitter', async () => {
|
|
28
|
-
let run = false;
|
|
29
|
-
const emitter = new BuildWatchEmitter();
|
|
30
|
-
emitter.setInitFn(() => {
|
|
31
|
-
run = true;
|
|
32
|
-
});
|
|
33
|
-
await emitter.watch();
|
|
34
|
-
expect(run).toBe(true);
|
|
35
|
-
|
|
36
|
-
const emitter_1 = new BuildWatchEmitter();
|
|
37
|
-
emitter_1.setInitFn(true as any);
|
|
38
|
-
const ret = await emitter_1.watch();
|
|
39
|
-
expect(ret).toBe(null);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('runBuildWatch and success', async () => {
|
|
43
|
-
let done: any;
|
|
44
|
-
const promise = new Promise(resolve => (done = resolve));
|
|
45
|
-
let compiling = false;
|
|
46
|
-
const emitter = new BuildWatchEmitter();
|
|
47
|
-
emitter.on(BuildWatchEvent.firstCompiler, (result: ICompilerResult) => {
|
|
48
|
-
expect(result.code).toBe(0);
|
|
49
|
-
});
|
|
50
|
-
emitter.on(BuildWatchEvent.compiling, () => {
|
|
51
|
-
expect(compiling).toBe(true);
|
|
52
|
-
});
|
|
53
|
-
emitter.on(BuildWatchEvent.watchingCompiler, (result: ICompilerResult) => {
|
|
54
|
-
expect(result.code).toBe(0);
|
|
55
|
-
watcher.close();
|
|
56
|
-
done();
|
|
57
|
-
});
|
|
58
|
-
const filenames = [path.join(srcDir, 'index.js')];
|
|
59
|
-
compiling = true;
|
|
60
|
-
const watcher = await runBuildWatch(
|
|
61
|
-
{ rootDir: srcDir, filenames, distDir, watchDir: srcDir },
|
|
62
|
-
{},
|
|
63
|
-
emitter,
|
|
64
|
-
);
|
|
65
|
-
compiling = false;
|
|
66
|
-
watcher.on('ready', () => {
|
|
67
|
-
fs.ensureFileSync(path.join(srcDir, 'far.js'));
|
|
68
|
-
compiling = true;
|
|
69
|
-
});
|
|
70
|
-
return promise;
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('runBuildWatch and fail', async () => {
|
|
74
|
-
let done: any;
|
|
75
|
-
const promise = new Promise(resolve => (done = resolve));
|
|
76
|
-
const emitter = new BuildWatchEmitter();
|
|
77
|
-
emitter.on(BuildWatchEvent.firstCompiler, (result: ICompilerResult) => {
|
|
78
|
-
expect(result.code).toBe(1);
|
|
79
|
-
expect(result.messageDetails?.length).toBe(1);
|
|
80
|
-
});
|
|
81
|
-
emitter.on(BuildWatchEvent.watchingCompiler, (result: ICompilerResult) => {
|
|
82
|
-
expect(result.code).toBe(1);
|
|
83
|
-
expect(result.messageDetails?.length).toBe(2);
|
|
84
|
-
watcher.close();
|
|
85
|
-
done();
|
|
86
|
-
});
|
|
87
|
-
const filenames = [path.join(srcDir, 'error')];
|
|
88
|
-
const watcher = await runBuildWatch(
|
|
89
|
-
{ rootDir: srcDir, filenames, distDir, watchDir: srcDir },
|
|
90
|
-
{},
|
|
91
|
-
emitter,
|
|
92
|
-
);
|
|
93
|
-
watcher.on('ready', () => {
|
|
94
|
-
fs.ensureFileSync(path.join(srcDir, 'error1.js'));
|
|
95
|
-
fs.writeFileSync(path.join(srcDir, 'error1.js'), 'conta a = 1');
|
|
96
|
-
});
|
|
97
|
-
return promise;
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('buildWatch and success', async () => {
|
|
101
|
-
let done: any;
|
|
102
|
-
const promise = new Promise(resolve => (done = resolve));
|
|
103
|
-
const emitter = buildWatch({
|
|
104
|
-
rootDir: srcDir,
|
|
105
|
-
watchDir: srcDir,
|
|
106
|
-
filenames: [path.join(srcDir, 'index.js')],
|
|
107
|
-
distDir,
|
|
108
|
-
enableVirtualDist: true,
|
|
109
|
-
clean: true,
|
|
110
|
-
});
|
|
111
|
-
let compiling = false;
|
|
112
|
-
emitter.on(BuildWatchEvent.compiling, () => {
|
|
113
|
-
expect(compiling).toBe(true);
|
|
114
|
-
});
|
|
115
|
-
emitter.on(BuildWatchEvent.firstCompiler, (result: ICompilerResult) => {
|
|
116
|
-
expect(result.code).toBe(0);
|
|
117
|
-
expect(result.virtualDists?.length).toBe(1);
|
|
118
|
-
});
|
|
119
|
-
emitter.on(BuildWatchEvent.watchingCompiler, (result: ICompilerResult) => {
|
|
120
|
-
expect(result.code).toBe(0);
|
|
121
|
-
expect(result.virtualDists?.length).toBe(1);
|
|
122
|
-
expect(result.virtualDists![0].distPath.includes('far.js')).toBe(true);
|
|
123
|
-
watcher?.close();
|
|
124
|
-
done();
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
compiling = true;
|
|
128
|
-
const watcher = await emitter.watch();
|
|
129
|
-
compiling = false;
|
|
130
|
-
if (watcher) {
|
|
131
|
-
watcher.on('ready', () => {
|
|
132
|
-
fs.ensureFileSync(path.join(srcDir, 'far.js'));
|
|
133
|
-
compiling = true;
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
return promise;
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it('buildWatch and fail', async () => {
|
|
140
|
-
let done: any;
|
|
141
|
-
const promise = new Promise(resolve => (done = resolve));
|
|
142
|
-
const emitter = buildWatch({
|
|
143
|
-
rootDir: srcDir,
|
|
144
|
-
watchDir: srcDir,
|
|
145
|
-
filenames: [path.join(srcDir, 'error')],
|
|
146
|
-
distDir,
|
|
147
|
-
enableVirtualDist: true,
|
|
148
|
-
clean: true,
|
|
149
|
-
});
|
|
150
|
-
let compiling = false;
|
|
151
|
-
emitter.on(BuildWatchEvent.compiling, () => {
|
|
152
|
-
expect(compiling).toBe(true);
|
|
153
|
-
});
|
|
154
|
-
emitter.on(BuildWatchEvent.firstCompiler, (result: ICompilerResult) => {
|
|
155
|
-
expect(result.code).toBe(1);
|
|
156
|
-
expect(result.messageDetails?.length).toBe(1);
|
|
157
|
-
});
|
|
158
|
-
emitter.on(BuildWatchEvent.watchingCompiler, (result: ICompilerResult) => {
|
|
159
|
-
expect(result.code).toBe(1);
|
|
160
|
-
expect(result.messageDetails?.length).toBe(2);
|
|
161
|
-
watcher?.close();
|
|
162
|
-
done();
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
compiling = true;
|
|
166
|
-
const watcher = await emitter.watch();
|
|
167
|
-
compiling = false;
|
|
168
|
-
if (watcher) {
|
|
169
|
-
watcher.on('ready', () => {
|
|
170
|
-
fs.ensureFileSync(path.join(srcDir, 'error1.js'));
|
|
171
|
-
fs.writeFileSync(path.join(srcDir, 'error1.js'), 'cast a = 1;');
|
|
172
|
-
compiling = true;
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
return promise;
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it('buildWatch and remove file', async () => {
|
|
179
|
-
let done: any;
|
|
180
|
-
const promise = new Promise(resolve => (done = resolve));
|
|
181
|
-
const emitter = buildWatch({
|
|
182
|
-
rootDir: srcDir,
|
|
183
|
-
watchDir: srcDir,
|
|
184
|
-
filenames: [path.join(srcDir, 'index.js')],
|
|
185
|
-
distDir,
|
|
186
|
-
enableVirtualDist: true,
|
|
187
|
-
clean: true,
|
|
188
|
-
quiet: true,
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
emitter.on(BuildWatchEvent.watchingCompiler, (result: ICompilerResult) => {
|
|
192
|
-
expect(result.code).toBe(0);
|
|
193
|
-
expect(result.message.includes('remove file')).toBe(true);
|
|
194
|
-
expect(result.removeFiles?.length).toBe(1);
|
|
195
|
-
watcher?.close();
|
|
196
|
-
done();
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
fs.ensureFileSync(path.join(srcDir, 'far.js'));
|
|
200
|
-
const watcher = await emitter.watch();
|
|
201
|
-
|
|
202
|
-
if (watcher) {
|
|
203
|
-
watcher.on('ready', () => {
|
|
204
|
-
fs.removeSync(path.join(srcDir, 'far.js'));
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
return promise;
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
it('buildWatch and reRight file', async () => {
|
|
211
|
-
let done: any;
|
|
212
|
-
const promise = new Promise(resolve => (done = resolve));
|
|
213
|
-
fs.ensureFileSync(path.join(srcDir, 'error1.js'));
|
|
214
|
-
fs.writeFileSync(path.join(srcDir, 'error1.js'), 'cast a = 1;');
|
|
215
|
-
const emitter = buildWatch({
|
|
216
|
-
rootDir: srcDir,
|
|
217
|
-
watchDir: srcDir,
|
|
218
|
-
filenames: [path.join(srcDir, 'error'), path.join(srcDir, 'error1.js')],
|
|
219
|
-
distDir,
|
|
220
|
-
enableVirtualDist: true,
|
|
221
|
-
clean: true,
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
emitter.on(BuildWatchEvent.firstCompiler, (result: ICompilerResult) => {
|
|
225
|
-
expect(result.code).toBe(1);
|
|
226
|
-
expect(result.messageDetails?.length).toBe(2);
|
|
227
|
-
});
|
|
228
|
-
emitter.on(BuildWatchEvent.watchingCompiler, (result: ICompilerResult) => {
|
|
229
|
-
expect(result.code).toBe(1);
|
|
230
|
-
expect(result.messageDetails?.length).toBe(1);
|
|
231
|
-
watcher?.close();
|
|
232
|
-
done();
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
const watcher = await emitter.watch();
|
|
236
|
-
if (watcher) {
|
|
237
|
-
watcher.on('ready', () => {
|
|
238
|
-
fs.writeFileSync(path.join(srcDir, 'error1.js'), 'const a = 1;');
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
return promise;
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
afterEach(() => {
|
|
245
|
-
logger.info = originalLogInfo;
|
|
246
|
-
logger.error = originalLogError;
|
|
247
|
-
testLogs = [];
|
|
248
|
-
fs.removeSync(path.join(srcDir, 'far.js'));
|
|
249
|
-
fs.removeSync(path.join(srcDir, 'error1.js'));
|
|
250
|
-
});
|
|
251
|
-
});
|