@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.
Files changed (75) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENSE +21 -0
  3. package/README.md +32 -0
  4. package/dist/js/modern/build.js +71 -0
  5. package/dist/js/modern/buildWatch.js +106 -0
  6. package/dist/js/modern/compiler.js +115 -0
  7. package/dist/js/modern/compilerErrorResult.js +48 -0
  8. package/dist/js/modern/constants.js +6 -0
  9. package/dist/js/modern/defaults.js +19 -0
  10. package/dist/js/modern/getFinalOption.js +76 -0
  11. package/dist/js/modern/index.js +21 -0
  12. package/dist/js/modern/type.js +1 -0
  13. package/dist/js/modern/utils.js +4 -0
  14. package/dist/js/modern/validate.js +52 -0
  15. package/dist/js/node/build.js +85 -0
  16. package/dist/js/node/buildWatch.js +132 -0
  17. package/dist/js/node/compiler.js +146 -0
  18. package/dist/js/node/compilerErrorResult.js +57 -0
  19. package/dist/js/node/constants.js +13 -0
  20. package/dist/js/node/defaults.js +30 -0
  21. package/dist/js/node/getFinalOption.js +101 -0
  22. package/dist/js/node/index.js +59 -0
  23. package/dist/js/node/type.js +5 -0
  24. package/dist/js/node/utils.js +16 -0
  25. package/dist/js/node/validate.js +71 -0
  26. package/dist/types/build.d.ts +2 -0
  27. package/dist/types/buildWatch.d.ts +16 -0
  28. package/dist/types/compiler.d.ts +28 -0
  29. package/dist/types/compilerErrorResult.d.ts +10 -0
  30. package/dist/types/constants.d.ts +6 -0
  31. package/dist/types/defaults.d.ts +2 -0
  32. package/dist/types/getFinalOption.d.ts +14 -0
  33. package/dist/types/index.d.ts +10 -0
  34. package/dist/types/type.d.ts +68 -0
  35. package/dist/types/utils.d.ts +1 -0
  36. package/dist/types/validate.d.ts +6 -0
  37. package/modern.config.js +19 -0
  38. package/package.json +48 -0
  39. package/src/build.ts +95 -0
  40. package/src/buildWatch.ts +113 -0
  41. package/src/compiler.ts +132 -0
  42. package/src/compilerErrorResult.ts +49 -0
  43. package/src/constants.ts +6 -0
  44. package/src/defaults.ts +21 -0
  45. package/src/getFinalOption.ts +93 -0
  46. package/src/index.ts +35 -0
  47. package/src/type.ts +76 -0
  48. package/src/utils.ts +5 -0
  49. package/src/validate.ts +47 -0
  50. package/tests/build.test.ts +124 -0
  51. package/tests/buildWatch.test.ts +233 -0
  52. package/tests/compiler.test.ts +106 -0
  53. package/tests/compilerErrorResult.test.ts +119 -0
  54. package/tests/constants.test.ts +12 -0
  55. package/tests/defaults.test.ts +29 -0
  56. package/tests/fixtures/build/src/error +1 -0
  57. package/tests/fixtures/build/src/error1 +1 -0
  58. package/tests/fixtures/build/src/far.js +0 -0
  59. package/tests/fixtures/build/src/index.js +0 -0
  60. package/tests/fixtures/buildWatch/src/error +2 -0
  61. package/tests/fixtures/buildWatch/src/index.js +0 -0
  62. package/tests/fixtures/compiler/src/index.js +2 -0
  63. package/tests/fixtures/getFinalOption/sourceDir/bar.js +0 -0
  64. package/tests/fixtures/getFinalOption/watchDir/far.js +0 -0
  65. package/tests/fixtures/getFinalOption/watchDir/foo.jsx +0 -0
  66. package/tests/fixtures/lib/src/index.js +0 -0
  67. package/tests/fixtures/resolveSourceMap/dist/far.js.map +1 -0
  68. package/tests/fixtures/resolveSourceMap/src/index.js +2 -0
  69. package/tests/fixtures/utils/far +2 -0
  70. package/tests/getFinalOption.test.ts +106 -0
  71. package/tests/index.test.ts +89 -0
  72. package/tests/tsconfig.json +15 -0
  73. package/tests/utils.test.ts +18 -0
  74. package/tests/validate.test.ts +68 -0
  75. package/tsconfig.json +16 -0
@@ -0,0 +1,106 @@
1
+ import * as path from 'path';
2
+ import { fs } from '@modern-js/utils';
3
+ import * as babel from '@babel/core';
4
+ import {
5
+ isRes,
6
+ getDistFilePath,
7
+ resolveSourceMap,
8
+ compiler,
9
+ } from '../src/compiler';
10
+ import { defaultDistFileExtMap } from '../src/constants';
11
+
12
+ describe('compiler', () => {
13
+ it('isRes', () => {
14
+ const res_1 = isRes({ code: '' });
15
+ expect(res_1).toBe(true);
16
+ const res_2 = isRes(null);
17
+ expect(res_2).toBe(false);
18
+ });
19
+
20
+ it('getDistFilePath', () => {
21
+ const distpath_1 = getDistFilePath({
22
+ rootDir: '/project',
23
+ filepath: '/project/src/b.js',
24
+ distDir: '/project/dist',
25
+ extMap: defaultDistFileExtMap,
26
+ });
27
+ expect(distpath_1).toBe('/project/dist/src/b.js');
28
+
29
+ const distpath_2 = getDistFilePath({
30
+ rootDir: '/project/src',
31
+ filepath: '/project/src/b.js',
32
+ distDir: '/project/dist',
33
+ extMap: defaultDistFileExtMap,
34
+ });
35
+ expect(distpath_2).toBe('/project/dist/b.js');
36
+ });
37
+
38
+ it('resolveSourceMap', () => {
39
+ const projectDir = path.join(__dirname, './fixtures/resolveSourceMap');
40
+ fs.removeSync(path.join(projectDir, 'dist'));
41
+ const babelRes = babel.transformFileSync(
42
+ path.join(projectDir, 'src/index.js'),
43
+ { sourceMaps: true, cwd: projectDir },
44
+ );
45
+ const distFilePath = path.join(projectDir, 'dist/far.js');
46
+ const sourcemap_1 = resolveSourceMap({
47
+ babelRes: babelRes as babel.BabelFileResult,
48
+ distFilePath,
49
+ enableVirtualDist: true,
50
+ });
51
+ expect(sourcemap_1.sourceMapPath).toBe(
52
+ path.join(projectDir, 'dist/far.js.map'),
53
+ );
54
+ // TODO test sourcemap content
55
+
56
+ const sourcemap_2 = resolveSourceMap({
57
+ babelRes: babelRes as babel.BabelFileResult,
58
+ distFilePath,
59
+ });
60
+ expect(fs.readFileSync(sourcemap_2.sourceMapPath, 'utf-8')).toBe(
61
+ sourcemap_2.sourcemap,
62
+ );
63
+ });
64
+
65
+ it('compiler', () => {
66
+ const projectPath = path.join(__dirname, './fixtures/compiler');
67
+ const sourceFilePath = path.join(projectPath, './src/index.js');
68
+ const distPath = path.join(projectPath, 'dist');
69
+
70
+ compiler({
71
+ rootDir: path.join(projectPath, 'src'),
72
+ filepath: sourceFilePath,
73
+ });
74
+ expect(fs.existsSync(path.join(distPath, 'index.js'))).toBe(true);
75
+ fs.removeSync(distPath);
76
+
77
+ const dist1Path = path.join(projectPath, 'dist1');
78
+ compiler({
79
+ rootDir: path.join(projectPath, 'src'),
80
+ filepath: sourceFilePath,
81
+ distDir: dist1Path,
82
+ babelConfig: { sourceMaps: true },
83
+ });
84
+ expect(fs.existsSync(dist1Path)).toBe(true);
85
+ fs.removeSync(dist1Path);
86
+
87
+ compiler({
88
+ rootDir: path.join(projectPath, 'src'),
89
+ filepath: sourceFilePath,
90
+ babelConfig: { sourceMaps: true },
91
+ });
92
+ expect(fs.existsSync(path.join(distPath, 'index.js'))).toBe(true);
93
+ expect(fs.existsSync(path.join(distPath, 'index.js.map'))).toBe(true);
94
+ fs.removeSync(distPath);
95
+
96
+ compiler({
97
+ rootDir: path.join(projectPath, 'src'),
98
+ filepath: sourceFilePath,
99
+ enableVirtualDist: true,
100
+ babelConfig: { sourceMaps: true },
101
+ });
102
+ expect(fs.existsSync(path.join(distPath, 'index.js'))).toBe(false);
103
+ expect(fs.existsSync(path.join(distPath, 'index.js.map'))).toBe(false);
104
+ fs.removeSync(distPath);
105
+ });
106
+ });
@@ -0,0 +1,119 @@
1
+ import { CompilerErrorResult } from '../src/compilerErrorResult';
2
+
3
+ describe('CompilerErrorResult Class', () => {
4
+ it('call constructor without params', () => {
5
+ const cer_1 = new CompilerErrorResult();
6
+ expect(Array.isArray(cer_1._messageDetails)).toBe(true);
7
+ expect(cer_1._messageDetails.length).toBe(0);
8
+ });
9
+
10
+ it('call constructor with params', () => {
11
+ const cer_1 = new CompilerErrorResult({ code: 1, message: 'fail' });
12
+ expect(Array.isArray(cer_1._messageDetails)).toBe(true);
13
+ expect(cer_1._messageDetails.length).toBe(0);
14
+
15
+ const cer_2 = new CompilerErrorResult({
16
+ code: 1,
17
+ message: 'fail',
18
+ messageDetails: [{ content: 'error', filename: 'far.js' }],
19
+ });
20
+ expect(Array.isArray(cer_1._messageDetails)).toBe(true);
21
+ expect(cer_2._messageDetails.length).toBe(1);
22
+ });
23
+
24
+ it('use init', () => {
25
+ const cer_1 = new CompilerErrorResult();
26
+ cer_1.init({ code: 1, message: 'fail' });
27
+ expect(Array.isArray(cer_1._messageDetails)).toBe(true);
28
+ expect(cer_1._messageDetails.length).toBe(0);
29
+
30
+ const cer_2 = new CompilerErrorResult();
31
+ cer_2.init({
32
+ code: 1,
33
+ message: 'fail',
34
+ messageDetails: [{ content: 'error', filename: 'far.js' }],
35
+ });
36
+ expect(Array.isArray(cer_1._messageDetails)).toBe(true);
37
+ expect(cer_2._messageDetails.length).toBe(1);
38
+ });
39
+
40
+ it('update diff messageDetails', () => {
41
+ const cer_1 = new CompilerErrorResult({ code: 1, message: 'fail' });
42
+ const firstCount = cer_1._messageDetails.length;
43
+ cer_1.update([{ content: 'error', filename: 'far.js' }]);
44
+ const secondCount = cer_1._messageDetails.length;
45
+ cer_1.update([{ content: 'error', filename: 'bar.js' }]);
46
+ const thirdCount = cer_1._messageDetails.length;
47
+ expect(firstCount).toBe(0);
48
+ expect(secondCount).toBe(1);
49
+ expect(thirdCount).toBe(2);
50
+ });
51
+
52
+ it('update same messageDetails', () => {
53
+ const cer_1 = new CompilerErrorResult({ code: 1, message: 'fail' });
54
+ const firstCount = cer_1._messageDetails.length;
55
+ cer_1.update([{ content: 'error-1', filename: 'far.js' }]);
56
+ const secondCount = cer_1._messageDetails.length;
57
+ cer_1.update([{ content: 'error-2', filename: 'far.js' }]);
58
+ const thirdCount = cer_1._messageDetails.length;
59
+ expect(firstCount).toBe(0);
60
+ expect(secondCount).toBe(1);
61
+ expect(thirdCount).toBe(1);
62
+ });
63
+
64
+ it('remove by file name', () => {
65
+ const cer_1 = new CompilerErrorResult({
66
+ code: 1,
67
+ message: 'fail',
68
+ messageDetails: [{ content: 'error-1', filename: 'far.js' }],
69
+ });
70
+ const firstCount = cer_1._messageDetails.length;
71
+ cer_1.removeByFileName('far.js');
72
+ const secondCount = cer_1._messageDetails.length;
73
+ expect(firstCount).toBe(1);
74
+ expect(secondCount).toBe(0);
75
+ });
76
+
77
+ it('check exist error', () => {
78
+ const cer_1 = new CompilerErrorResult();
79
+ expect(cer_1.checkExistError()).toBe(false);
80
+ const cer_2 = new CompilerErrorResult({
81
+ code: 1,
82
+ message: 'fail',
83
+ messageDetails: [{ content: 'error-1', filename: 'far.js' }],
84
+ });
85
+ expect(cer_2.checkExistError()).toBe(true);
86
+ });
87
+
88
+ it('get value', () => {
89
+ const cer_1 = new CompilerErrorResult();
90
+ expect(cer_1.value).toStrictEqual({
91
+ code: 1,
92
+ message: `Compilation failure 0 files with Babel.`,
93
+ messageDetails: [],
94
+ });
95
+ const cer_2 = new CompilerErrorResult({
96
+ code: 1,
97
+ message: 'fail',
98
+ messageDetails: [{ content: 'error-1', filename: 'far.js' }],
99
+ });
100
+ expect(cer_2.value).toStrictEqual({
101
+ code: 1,
102
+ message: `Compilation failure 1 files with Babel.`,
103
+ messageDetails: [{ content: 'error-1', filename: 'far.js' }],
104
+ });
105
+ });
106
+
107
+ it('_messageDetails', () => {
108
+ const cer_1 = new CompilerErrorResult();
109
+ expect(cer_1._messageDetails).toStrictEqual([]);
110
+ const cer_2 = new CompilerErrorResult({
111
+ code: 1,
112
+ message: 'fail',
113
+ messageDetails: [{ content: 'error-1', filename: 'far.js' }],
114
+ });
115
+ expect(cer_2._messageDetails).toStrictEqual([
116
+ { content: 'error-1', filename: 'far.js' },
117
+ ]);
118
+ });
119
+ });
@@ -0,0 +1,12 @@
1
+ import { defaultDistFileExtMap } from '../src/constants';
2
+
3
+ describe('constants', () => {
4
+ it('defaultDistFileExtMap', () => {
5
+ expect(defaultDistFileExtMap).toStrictEqual({
6
+ '.js': '.js',
7
+ '.jsx': '.js',
8
+ '.ts': '.js',
9
+ '.tsx': '.js',
10
+ });
11
+ });
12
+ });
@@ -0,0 +1,29 @@
1
+ import { mergeDefaultOption } from '../src/defaults';
2
+
3
+ describe('defaults', () => {
4
+ it('should merge default option', () => {
5
+ const option_1 = mergeDefaultOption({
6
+ rootDir: './',
7
+ distDir: './dist',
8
+ filenames: ['far.js'],
9
+ });
10
+ expect(option_1).toStrictEqual({
11
+ rootDir: './',
12
+ distDir: './dist',
13
+ filenames: ['far.js'],
14
+ enableWatch: false,
15
+ enableVirtualDist: false,
16
+ extensions: [],
17
+ distFileExtMap: {
18
+ '.js': '.js',
19
+ '.jsx': '.js',
20
+ '.ts': '.js',
21
+ '.tsx': '.js',
22
+ },
23
+ ignore: [],
24
+ quiet: false,
25
+ verbose: false,
26
+ clean: false,
27
+ });
28
+ });
29
+ });
@@ -0,0 +1 @@
1
+ conts a = 1;
@@ -0,0 +1 @@
1
+ conts a = 1;
File without changes
File without changes
@@ -0,0 +1,2 @@
1
+ // eslint-disable-next-line
2
+ conts a = 1;
File without changes
@@ -0,0 +1,2 @@
1
+ const a = 1;
2
+ console.info('this is test', a);
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["index.js"],"names":["a","console","info"],"mappings":"AAAA,MAAMA,CAAC,GAAG,CAAV;AACAC,OAAO,CAACC,IAAR,CAAaF,CAAb","sourcesContent":["const a = 1;\nconsole.info(a);\n"],"file":"/Users/admin/github/modern.js/packages/toolkit/compiler/babel/tests/fixtures/resolveSourceMap/dist/far.js"}
@@ -0,0 +1,2 @@
1
+ const a = 1;
2
+ console.info(a);
@@ -0,0 +1,2 @@
1
+ const far = 1;
2
+ //# sourceMappingURL=far.js.map
@@ -0,0 +1,106 @@
1
+ import * as path from 'path';
2
+ import { DEFAULT_EXTENSIONS } from '@babel/core';
3
+ import {
4
+ getFinalCompilerOption,
5
+ getFilesFromDir,
6
+ getFinalExtensions,
7
+ getGlobPattern,
8
+ } from '../src/getFinalOption';
9
+ import { defaultDistFileExtMap } from '../src/constants';
10
+
11
+ const projectDir = path.join(__dirname, './fixtures/getFinalOption');
12
+ const sourceDir = path.join(projectDir, 'sourceDir');
13
+ const watchDir = path.join(projectDir, 'watchDir');
14
+
15
+ describe('get final compilerOption', () => {
16
+ it('getGlobPattern', () => {
17
+ const pattern_1 = getGlobPattern(sourceDir, ['.js']);
18
+ expect(pattern_1).toBe(`${sourceDir}/**/*.js`);
19
+ const pattern_2 = getGlobPattern(watchDir, ['.js', '.jsx']);
20
+ expect(pattern_2).toBe(`${watchDir}/**/*{.js,.jsx}`);
21
+ const pattern_3 = getGlobPattern(watchDir, []);
22
+ expect(pattern_3).toBe(`${watchDir}/**/*`);
23
+ });
24
+
25
+ it('getFinalExtensions', () => {
26
+ const finalexts_1 = getFinalExtensions(['.ts', '.tsx']);
27
+ expect(finalexts_1).toStrictEqual(['.ts', '.tsx', ...DEFAULT_EXTENSIONS]);
28
+ const finalexts_2 = getFinalExtensions(defaultExts => [
29
+ '.ts',
30
+ '.tsx',
31
+ ...defaultExts,
32
+ ]);
33
+ expect(finalexts_2).toStrictEqual(['.ts', '.tsx', ...DEFAULT_EXTENSIONS]);
34
+ const finalexts_3 = getFinalExtensions(() => ['.ts', '.tsx']);
35
+ expect(finalexts_3).toStrictEqual(['.ts', '.tsx']);
36
+ const finalexts_4 = getFinalExtensions(undefined);
37
+ expect(finalexts_4).toStrictEqual(DEFAULT_EXTENSIONS);
38
+ });
39
+
40
+ it('getFilesFromDir', () => {
41
+ const files_1 = getFilesFromDir({ dir: watchDir, finalExt: ['.js'] });
42
+ expect(files_1.length).toBe(1);
43
+ const files_2 = getFilesFromDir({
44
+ dir: watchDir,
45
+ finalExt: ['.js', '.jsx'],
46
+ });
47
+ expect(files_2.length).toBe(2);
48
+ const files_3 = getFilesFromDir({
49
+ dir: watchDir,
50
+ finalExt: ['.js', '.jsx'],
51
+ ignore: ['**/far.js'],
52
+ });
53
+ expect(files_3.length).toBe(1);
54
+ const files_4 = getFilesFromDir({ dir: watchDir });
55
+ expect(files_4.length).toBe(2);
56
+ });
57
+
58
+ it('getFinalCompilerOption', () => {
59
+ const baseOption = {
60
+ rootDir: projectDir,
61
+ distDir: path.join(projectDir, 'dist'),
62
+ };
63
+ const baseFinalOption = {
64
+ ...baseOption,
65
+ enableWatch: false,
66
+ enableVirtualDist: false,
67
+ extensions: [],
68
+ distFileExtMap: defaultDistFileExtMap,
69
+ ignore: [],
70
+ quiet: false,
71
+ verbose: false,
72
+ clean: false,
73
+ };
74
+ const option_1 = getFinalCompilerOption({ ...baseOption });
75
+ expect(option_1).toStrictEqual({
76
+ ...baseFinalOption,
77
+ filenames: [],
78
+ });
79
+ const option_2 = getFinalCompilerOption({ ...baseOption, sourceDir });
80
+ expect(option_2.filenames.length).toBe(1);
81
+ expect(option_2.filenames[0]).toBe(path.join(sourceDir, './bar.js'));
82
+ const option_3 = getFinalCompilerOption({ ...baseOption, watchDir });
83
+ expect(option_3.filenames.length).toBe(0);
84
+ const option_4 = getFinalCompilerOption({
85
+ ...baseOption,
86
+ enableWatch: true,
87
+ watchDir,
88
+ });
89
+ expect(option_4.filenames.length).toBe(2);
90
+ expect(
91
+ option_4.filenames.every(filename =>
92
+ [
93
+ path.join(watchDir, './far.js'),
94
+ path.join(watchDir, './foo.jsx'),
95
+ ].includes(filename),
96
+ ),
97
+ ).toBe(true);
98
+
99
+ const option_5 = getFinalCompilerOption({
100
+ ...baseOption,
101
+ filenames: ['./c.js'],
102
+ sourceDir,
103
+ });
104
+ expect(option_5.filenames.length).toBe(2);
105
+ });
106
+ });
@@ -0,0 +1,89 @@
1
+ import * as path from 'path';
2
+ import { logger } from '@modern-js/utils';
3
+ import { compiler } from '../src';
4
+ import {
5
+ sourceDirAndFileNamesValidMessage,
6
+ watchDirValidMessage,
7
+ } from '../src/validate';
8
+
9
+ const projectDir = path.join(__dirname, './fixtures/lib');
10
+ const srcDir = path.join(projectDir, 'src');
11
+ const distDir = path.join(projectDir, 'dist');
12
+
13
+ const originalLogInfo = logger.info;
14
+ const originalLogError = logger.error;
15
+
16
+ describe('babel compiler', () => {
17
+ let testLogs: any[] = [];
18
+ const mockedLogInfo = (s: string | number | Error | undefined) =>
19
+ testLogs.push(s);
20
+ beforeEach(() => {
21
+ logger.info = mockedLogInfo;
22
+ logger.error = mockedLogInfo;
23
+ });
24
+
25
+ it('normal compiler', async () => {
26
+ const ret = await compiler(
27
+ {
28
+ rootDir: srcDir,
29
+ filenames: [path.join(srcDir, 'index.js')],
30
+ distDir,
31
+ enableVirtualDist: true,
32
+ },
33
+ {},
34
+ );
35
+
36
+ expect(ret.code).toBe(0);
37
+ expect(ret.virtualDists?.length).toBe(1);
38
+ });
39
+
40
+ it('compiler with valid', async () => {
41
+ const ret = await compiler(
42
+ {
43
+ rootDir: srcDir,
44
+ distDir,
45
+ enableVirtualDist: true,
46
+ },
47
+ {},
48
+ );
49
+
50
+ expect(ret.code).toBe(1);
51
+ expect(ret.message).toBe(sourceDirAndFileNamesValidMessage);
52
+
53
+ const ret_1 = await compiler(
54
+ {
55
+ rootDir: srcDir,
56
+ distDir,
57
+ filenames: [path.join(srcDir, 'index.js')],
58
+ enableWatch: true,
59
+ enableVirtualDist: true,
60
+ },
61
+ {},
62
+ );
63
+
64
+ expect((ret_1 as any).code).toBe(1);
65
+ expect((ret_1 as any).message).toBe(watchDirValidMessage);
66
+ });
67
+
68
+ it('compiler in watch mode', async () => {
69
+ const emitter = await compiler(
70
+ {
71
+ rootDir: srcDir,
72
+ filenames: [path.join(srcDir, 'index.js')],
73
+ distDir,
74
+ enableWatch: true,
75
+ watchDir: srcDir,
76
+ enableVirtualDist: true,
77
+ },
78
+ {},
79
+ );
80
+
81
+ expect('on' in emitter).toBe(true);
82
+ });
83
+
84
+ afterEach(() => {
85
+ logger.info = originalLogInfo;
86
+ logger.error = originalLogError;
87
+ testLogs = [];
88
+ });
89
+ });
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "@modern-js/tsconfig/base",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "jsx": "preserve",
6
+ "baseUrl": "./",
7
+ "outDir": "./out",
8
+ "emitDeclarationOnly": true,
9
+ "isolatedModules": true,
10
+ "paths": {
11
+ "@/*": ["../src/*"]
12
+ },
13
+ "types": ["node", "jest"]
14
+ }
15
+ }
@@ -0,0 +1,18 @@
1
+ import * as path from 'path';
2
+ import { fs } from '@modern-js/utils';
3
+ import { addSourceMappingUrl } from '../src/utils';
4
+
5
+ describe('utils test', () => {
6
+ it('addSourceMappingUrl should right', () => {
7
+ const code = 'const far = 1;';
8
+ const mapLoc = `far.js.map`;
9
+ const codeWithSourceMappingUrl = addSourceMappingUrl(code, mapLoc);
10
+ const rightCodeWithSourceMappingUrl = fs.readFileSync(
11
+ path.join(__dirname, './fixtures/utils/far'),
12
+ 'utf-8',
13
+ );
14
+ expect(codeWithSourceMappingUrl.trim()).toEqual(
15
+ rightCodeWithSourceMappingUrl.trim(),
16
+ );
17
+ });
18
+ });
@@ -0,0 +1,68 @@
1
+ import {
2
+ validate,
3
+ validateWatchDir,
4
+ validateSourceDirAndFileNames,
5
+ sourceDirAndFileNamesValidMessage,
6
+ watchDirValidMessage,
7
+ } from '../src/validate';
8
+
9
+ describe('valide', () => {
10
+ const baseOption = { rootDir: './', distDir: './dist', quiet: true };
11
+
12
+ it('valide sourceDir and filenams config', () => {
13
+ const ret_1 = validateSourceDirAndFileNames({
14
+ ...baseOption,
15
+ sourceDir: './src',
16
+ });
17
+ expect(ret_1).toBe(null);
18
+ const ret_2 = validateSourceDirAndFileNames({
19
+ ...baseOption,
20
+ filenames: ['./far.js'],
21
+ });
22
+ expect(ret_2).toBe(null);
23
+
24
+ const ret_3 = validateSourceDirAndFileNames({
25
+ ...baseOption,
26
+ sourceDir: './src',
27
+ filenames: ['./far.js'],
28
+ });
29
+ expect(ret_3).toBe(null);
30
+
31
+ const ret_4 = validateSourceDirAndFileNames({ ...baseOption });
32
+ expect(ret_4).toStrictEqual({
33
+ code: 1,
34
+ message: sourceDirAndFileNamesValidMessage,
35
+ virtualDists: [],
36
+ });
37
+ });
38
+
39
+ it('valid watchDir config', () => {
40
+ const ret_1 = validateWatchDir({ ...baseOption, enableWatch: true });
41
+ expect(ret_1).toStrictEqual({
42
+ code: 1,
43
+ message: watchDirValidMessage,
44
+ virtualDists: [],
45
+ });
46
+
47
+ const ret_2 = validateWatchDir({
48
+ ...baseOption,
49
+ enableWatch: true,
50
+ watchDir: './src',
51
+ });
52
+ expect(ret_2).toBe(null);
53
+ });
54
+
55
+ it('validate', () => {
56
+ const ret_1 = validate({ ...baseOption, sourceDir: './src' });
57
+ expect(ret_1).toBe(null);
58
+ const ret_2 = validate({ ...baseOption, filenames: ['./far.js'] });
59
+ expect(ret_2).toBe(null);
60
+ const ret_3 = validate({
61
+ ...baseOption,
62
+ sourceDir: './src',
63
+ enableWatch: true,
64
+ watchDir: './src',
65
+ });
66
+ expect(ret_3).toBe(null);
67
+ });
68
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "extends": "@modern-js/tsconfig/base",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "jsx": "preserve",
6
+ "baseUrl": "./",
7
+ "outDir": "./out",
8
+ "emitDeclarationOnly": true,
9
+ "isolatedModules": true,
10
+ "paths": {
11
+ "@/*": ["./src/*"]
12
+ },
13
+ "types": ["node", "jest"]
14
+ },
15
+ "include": ["src"]
16
+ }