@modern-js/babel-compiler 2.15.0 → 2.17.0

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.
@@ -1,17 +1,8 @@
1
1
  import { fs, logger } from "@modern-js/utils";
2
2
  import { defaultDistFileExtMap } from "./constants";
3
3
  import { compiler } from "./compiler";
4
- const build = async (option, babelConfig = {}) => {
5
- const {
6
- rootDir,
7
- enableVirtualDist,
8
- filenames,
9
- clean,
10
- distDir,
11
- distFileExtMap = defaultDistFileExtMap,
12
- verbose = false,
13
- quiet = false
14
- } = option;
4
+ export const build = async (option, babelConfig = {}) => {
5
+ const { rootDir, enableVirtualDist, filenames, clean, distDir, distFileExtMap = defaultDistFileExtMap, verbose = false, quiet = false } = option;
15
6
  const virtualDists = [];
16
7
  if (clean) {
17
8
  await fs.remove(distDir);
@@ -43,13 +34,9 @@ const build = async (option, babelConfig = {}) => {
43
34
  const happenError = messageDetails.length > 0;
44
35
  if (!quiet) {
45
36
  if (happenError) {
46
- logger.error(
47
- `Compilation failure ${messageDetails.length} ${messageDetails.length !== 1 ? "files" : "file"} with Babel.`
48
- );
37
+ logger.error(`Compilation failure ${messageDetails.length} ${messageDetails.length !== 1 ? "files" : "file"} with Babel.`);
49
38
  } else {
50
- logger.info(
51
- `Successfully compiled ${filenames.length} ${filenames.length !== 1 ? "files" : "file"} with Babel.`
52
- );
39
+ logger.info(`Successfully compiled ${filenames.length} ${filenames.length !== 1 ? "files" : "file"} with Babel.`);
53
40
  }
54
41
  }
55
42
  if (happenError) {
@@ -65,6 +52,3 @@ const build = async (option, babelConfig = {}) => {
65
52
  virtualDists
66
53
  };
67
54
  };
68
- export {
69
- build
70
- };
@@ -1,14 +1,27 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
1
14
  import * as path from "path";
2
15
  import * as Event from "events";
3
16
  import { logger, watch, WatchChangeType } from "@modern-js/utils";
4
17
  import { build } from "./build";
5
18
  import { CompilerErrorResult } from "./compilerErrorResult";
6
- const BuildWatchEvent = {
19
+ export const BuildWatchEvent = {
7
20
  firstCompiler: "first-compiler",
8
21
  compiling: "compiling",
9
22
  watchingCompiler: "watching-compiler"
10
23
  };
11
- class BuildWatchEmitter extends Event.EventEmitter {
24
+ export class BuildWatchEmitter extends Event.EventEmitter {
12
25
  setInitFn(fn) {
13
26
  this._initFn = fn;
14
27
  }
@@ -18,8 +31,12 @@ class BuildWatchEmitter extends Event.EventEmitter {
18
31
  }
19
32
  return null;
20
33
  }
34
+ constructor(...args) {
35
+ super(...args);
36
+ _define_property(this, "_initFn", void 0);
37
+ }
21
38
  }
22
- const runBuildWatch = async (option, babelConfig = {}, emitter) => {
39
+ export const runBuildWatch = async (option, babelConfig = {}, emitter) => {
23
40
  emitter.emit(BuildWatchEvent.compiling);
24
41
  const errorResult = new CompilerErrorResult();
25
42
  const watchDir = option.watchDir;
@@ -32,60 +49,52 @@ const runBuildWatch = async (option, babelConfig = {}, emitter) => {
32
49
  } else {
33
50
  emitter.emit(BuildWatchEvent.firstCompiler, firstBuildResult);
34
51
  }
35
- return watch(
36
- `${watchDir}/**/*.{js,jsx,ts,tsx}`,
37
- async ({ changeType, changedFilePath }) => {
38
- emitter.emit(BuildWatchEvent.compiling);
39
- if (changeType === WatchChangeType.UNLINK) {
40
- const removeFiles = [
41
- path.normalize(
42
- `./${distDir}/${path.relative(watchDir, changedFilePath)}`
43
- )
44
- ];
45
- if (!quiet) {
46
- logger.info(`remove file: ${removeFiles.join(",")}`);
47
- }
48
- const result2 = {
49
- code: 0,
50
- message: `remove file: ${removeFiles.join(",")}`,
51
- removeFiles
52
- };
53
- emitter.emit(BuildWatchEvent.watchingCompiler, result2);
54
- return;
52
+ return watch(`${watchDir}/**/*.{js,jsx,ts,tsx}`, async ({ changeType, changedFilePath }) => {
53
+ emitter.emit(BuildWatchEvent.compiling);
54
+ if (changeType === WatchChangeType.UNLINK) {
55
+ const removeFiles = [
56
+ path.normalize(`./${distDir}/${path.relative(watchDir, changedFilePath)}`)
57
+ ];
58
+ if (!quiet) {
59
+ logger.info(`remove file: ${removeFiles.join(",")}`);
55
60
  }
56
- const result = await build(
57
- { ...option, filenames: [changedFilePath] },
58
- babelConfig
59
- );
60
- if (result.code === 1) {
61
- errorResult.update(result.messageDetails || []);
62
- emitter.emit(BuildWatchEvent.watchingCompiler, errorResult.value);
61
+ const result2 = {
62
+ code: 0,
63
+ message: `remove file: ${removeFiles.join(",")}`,
64
+ removeFiles
65
+ };
66
+ emitter.emit(BuildWatchEvent.watchingCompiler, result2);
67
+ return;
68
+ }
69
+ const result = await build({
70
+ ...option,
71
+ filenames: [
72
+ changedFilePath
73
+ ]
74
+ }, babelConfig);
75
+ if (result.code === 1) {
76
+ errorResult.update(result.messageDetails || []);
77
+ emitter.emit(BuildWatchEvent.watchingCompiler, errorResult.value);
78
+ !quiet && logger.info(errorResult.value.message);
79
+ } else {
80
+ errorResult.removeByFileName(changedFilePath);
81
+ if (errorResult.checkExistError()) {
82
+ emitter.emit(BuildWatchEvent.watchingCompiler, {
83
+ ...errorResult.value,
84
+ virtualDists: result.virtualDists
85
+ });
63
86
  !quiet && logger.info(errorResult.value.message);
64
87
  } else {
65
- errorResult.removeByFileName(changedFilePath);
66
- if (errorResult.checkExistError()) {
67
- emitter.emit(BuildWatchEvent.watchingCompiler, {
68
- ...errorResult.value,
69
- virtualDists: result.virtualDists
70
- });
71
- !quiet && logger.info(errorResult.value.message);
72
- } else {
73
- emitter.emit(BuildWatchEvent.watchingCompiler, result);
74
- !quiet && logger.info(result.message);
75
- }
88
+ emitter.emit(BuildWatchEvent.watchingCompiler, result);
89
+ !quiet && logger.info(result.message);
76
90
  }
77
- },
78
- [`${watchDir}/**/*.d.ts`]
79
- );
91
+ }
92
+ }, [
93
+ `${watchDir}/**/*.d.ts`
94
+ ]);
80
95
  };
81
- const buildWatch = (option, babelConfig = {}) => {
96
+ export const buildWatch = (option, babelConfig = {}) => {
82
97
  const buildWatchEmitter = new BuildWatchEmitter();
83
98
  buildWatchEmitter.setInitFn(runBuildWatch.bind(null, option, babelConfig));
84
99
  return buildWatchEmitter;
85
100
  };
86
- export {
87
- BuildWatchEmitter,
88
- BuildWatchEvent,
89
- buildWatch,
90
- runBuildWatch
91
- };
@@ -4,22 +4,14 @@ import { logger, fs } from "@modern-js/utils";
4
4
  import * as utils from "./utils";
5
5
  import { defaultDistFileExtMap } from "./constants";
6
6
  const defaultDistDir = "dist";
7
- const isRes = (r) => Boolean(r);
8
- const getDistFilePath = (option) => {
7
+ export const isRes = (r) => Boolean(r);
8
+ export const getDistFilePath = (option) => {
9
9
  const { filepath, rootDir, distDir, extMap } = option;
10
10
  const ext = path.extname(filepath);
11
- return path.join(
12
- distDir,
13
- path.relative(rootDir, filepath).replace(ext, extMap[ext])
14
- );
11
+ return path.join(distDir, path.relative(rootDir, filepath).replace(ext, extMap[ext]));
15
12
  };
16
- const resolveSourceMap = (option) => {
17
- const {
18
- babelRes,
19
- sourceFilePath,
20
- distFilePath,
21
- enableVirtualDist = false
22
- } = option;
13
+ export const resolveSourceMap = (option) => {
14
+ const { babelRes, sourceFilePath, distFilePath, enableVirtualDist = false } = option;
23
15
  const mapLoc = `${distFilePath}.map`;
24
16
  babelRes.code = utils.addSourceMappingUrl(babelRes.code, mapLoc);
25
17
  if (babelRes.map) {
@@ -39,17 +31,8 @@ const resolveSourceMap = (option) => {
39
31
  fs.writeFileSync(mapLoc, JSON.stringify(babelRes.map));
40
32
  return sourceMapVirtualDist;
41
33
  };
42
- const compiler = (option) => {
43
- const {
44
- filepath,
45
- rootDir,
46
- enableVirtualDist = false,
47
- distDir = path.join(path.dirname(rootDir), defaultDistDir),
48
- verbose = false,
49
- babelConfig = {},
50
- distFileExtMap = defaultDistFileExtMap,
51
- quiet = false
52
- } = option;
34
+ export const compiler = (option) => {
35
+ const { filepath, rootDir, enableVirtualDist = false, distDir = path.join(path.dirname(rootDir), defaultDistDir), verbose = false, babelConfig = {}, distFileExtMap = defaultDistFileExtMap, quiet = false } = option;
53
36
  const babelRes = babel.transformFileSync(filepath, babelConfig);
54
37
  let virtualDist = null;
55
38
  if (!isRes(babelRes)) {
@@ -69,7 +52,7 @@ const compiler = (option) => {
69
52
  sourcemap: ""
70
53
  };
71
54
  }
72
- if ((babelRes == null ? void 0 : babelRes.map) && babelConfig.sourceMaps && babelConfig.sourceMaps !== "inline") {
55
+ if ((babelRes === null || babelRes === void 0 ? void 0 : babelRes.map) && babelConfig.sourceMaps && babelConfig.sourceMaps !== "inline") {
73
56
  if (virtualDist) {
74
57
  virtualDist = {
75
58
  ...virtualDist,
@@ -104,9 +87,3 @@ const compiler = (option) => {
104
87
  }
105
88
  return virtualDist;
106
89
  };
107
- export {
108
- compiler,
109
- getDistFilePath,
110
- isRes,
111
- resolveSourceMap
112
- };
@@ -1,9 +1,19 @@
1
- class CompilerErrorResult {
2
- constructor(initErrorResult) {
3
- this.init(initErrorResult);
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
4
11
  }
12
+ return obj;
13
+ }
14
+ export class CompilerErrorResult {
5
15
  init(initErrorResult) {
6
- this._messageDetails = (initErrorResult == null ? void 0 : initErrorResult.messageDetails) || [];
16
+ this._messageDetails = (initErrorResult === null || initErrorResult === void 0 ? void 0 : initErrorResult.messageDetails) || [];
7
17
  }
8
18
  update(messageDetails) {
9
19
  for (const messageDetail of messageDetails) {
@@ -20,22 +30,21 @@ class CompilerErrorResult {
20
30
  }
21
31
  }
22
32
  removeByFileName(filename) {
23
- this._messageDetails = this._messageDetails.filter(
24
- (detail) => detail.filename !== filename
25
- );
33
+ this._messageDetails = this._messageDetails.filter((detail) => detail.filename !== filename);
26
34
  }
27
35
  get value() {
28
- var _a;
36
+ var _this__messageDetails;
29
37
  return {
30
38
  code: 1,
31
- message: `Compilation failure ${(_a = this._messageDetails) == null ? void 0 : _a.length} files with Babel.`,
39
+ message: `Compilation failure ${(_this__messageDetails = this._messageDetails) === null || _this__messageDetails === void 0 ? void 0 : _this__messageDetails.length} files with Babel.`,
32
40
  messageDetails: this._messageDetails
33
41
  };
34
42
  }
35
43
  checkExistError() {
36
44
  return this._messageDetails.length > 0;
37
45
  }
46
+ constructor(initErrorResult) {
47
+ _define_property(this, "_messageDetails", void 0);
48
+ this.init(initErrorResult);
49
+ }
38
50
  }
39
- export {
40
- CompilerErrorResult
41
- };
@@ -1,9 +1,6 @@
1
- const defaultDistFileExtMap = {
1
+ export const defaultDistFileExtMap = {
2
2
  ".js": ".js",
3
3
  ".jsx": ".js",
4
4
  ".ts": ".js",
5
5
  ".tsx": ".js"
6
6
  };
7
- export {
8
- defaultDistFileExtMap
9
- };
@@ -10,10 +10,7 @@ const defaultOptions = {
10
10
  verbose: false,
11
11
  clean: false
12
12
  };
13
- const mergeDefaultOption = (compilerOptions) => ({
13
+ export const mergeDefaultOption = (compilerOptions) => ({
14
14
  ...defaultOptions,
15
15
  ...compilerOptions
16
16
  });
17
- export {
18
- mergeDefaultOption
19
- };
@@ -1,7 +1,7 @@
1
1
  import { glob } from "@modern-js/utils";
2
2
  import { DEFAULT_EXTENSIONS } from "@babel/core";
3
3
  import { mergeDefaultOption } from "./defaults";
4
- const getGlobPattern = (dir, extensions) => {
4
+ export const getGlobPattern = (dir, extensions) => {
5
5
  if (extensions.length > 1) {
6
6
  return `${dir}/**/*{${extensions.join(",")}}`;
7
7
  } else if (extensions.length === 1) {
@@ -10,36 +10,31 @@ const getGlobPattern = (dir, extensions) => {
10
10
  return `${dir}/**/*`;
11
11
  }
12
12
  };
13
- const getFinalExtensions = (extensions) => {
13
+ export const getFinalExtensions = (extensions) => {
14
14
  const isExtensions = (ext) => Array.isArray(ext);
15
15
  const isExtensionsFunc = (ext) => typeof ext === "function";
16
16
  if (isExtensions(extensions)) {
17
- return [...extensions, ...DEFAULT_EXTENSIONS];
17
+ return [
18
+ ...extensions,
19
+ ...DEFAULT_EXTENSIONS
20
+ ];
18
21
  } else if (isExtensionsFunc(extensions)) {
19
22
  return extensions(DEFAULT_EXTENSIONS);
20
23
  } else {
21
24
  return DEFAULT_EXTENSIONS;
22
25
  }
23
26
  };
24
- const getFilesFromDir = ({
25
- dir,
26
- finalExt = [],
27
- ignore = []
28
- }) => {
27
+ export const getFilesFromDir = ({ dir, finalExt = [], ignore = [] }) => {
29
28
  let globFindFilenames = [];
30
29
  const globPattern = getGlobPattern(dir, finalExt);
31
- globFindFilenames = glob.sync(globPattern, { ignore });
30
+ globFindFilenames = glob.sync(globPattern, {
31
+ ignore
32
+ });
32
33
  return globFindFilenames;
33
34
  };
34
- const getFinalCompilerOption = (option) => {
35
+ export const getFinalCompilerOption = (option) => {
35
36
  const optionWithDefault = mergeDefaultOption(option);
36
- const {
37
- sourceDir,
38
- ignore,
39
- enableWatch = false,
40
- watchDir,
41
- extensions = DEFAULT_EXTENSIONS
42
- } = option;
37
+ const { sourceDir, ignore, enableWatch = false, watchDir, extensions = DEFAULT_EXTENSIONS } = option;
43
38
  let globFindFilenames = [];
44
39
  const finalExt = getFinalExtensions(extensions);
45
40
  if (sourceDir) {
@@ -58,12 +53,9 @@ const getFinalCompilerOption = (option) => {
58
53
  }
59
54
  return {
60
55
  ...optionWithDefault,
61
- filenames: [...optionWithDefault.filenames, ...globFindFilenames]
56
+ filenames: [
57
+ ...optionWithDefault.filenames,
58
+ ...globFindFilenames
59
+ ]
62
60
  };
63
61
  };
64
- export {
65
- getFilesFromDir,
66
- getFinalCompilerOption,
67
- getFinalExtensions,
68
- getGlobPattern
69
- };
@@ -2,7 +2,7 @@ import { getFinalCompilerOption } from "./getFinalOption";
2
2
  import { build } from "./build";
3
3
  import { buildWatch } from "./buildWatch";
4
4
  import { validate } from "./validate";
5
- async function compiler(compilerOptions, babelOptions = {}) {
5
+ export async function compiler(compilerOptions, babelOptions = {}) {
6
6
  const validRet = validate(compilerOptions);
7
7
  if (validRet) {
8
8
  return validRet;
@@ -16,6 +16,3 @@ async function compiler(compilerOptions, babelOptions = {}) {
16
16
  }
17
17
  export * from "./buildWatch";
18
18
  export * from "./type";
19
- export {
20
- compiler
21
- };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,8 +1,5 @@
1
1
  import * as path from "path";
2
- function addSourceMappingUrl(code, loc) {
2
+ export function addSourceMappingUrl(code, loc) {
3
3
  return `${code}
4
4
  //# sourceMappingURL=${path.normalize(path.basename(loc))}`;
5
5
  }
6
- export {
7
- addSourceMappingUrl
8
- };
@@ -1,7 +1,7 @@
1
1
  import { logger } from "@modern-js/utils";
2
- const sourceDirAndFileNamesValidMessage = "At least one of the sourceDir and filenames configurations must be configured";
3
- const watchDirValidMessage = "should set watchDir when enableWatch is true";
4
- const validateSourceDirAndFileNames = (compilerOptions) => {
2
+ export const sourceDirAndFileNamesValidMessage = "At least one of the sourceDir and filenames configurations must be configured";
3
+ export const watchDirValidMessage = "should set watchDir when enableWatch is true";
4
+ export const validateSourceDirAndFileNames = (compilerOptions) => {
5
5
  const { sourceDir, filenames, quiet } = compilerOptions;
6
6
  if (!sourceDir && !filenames) {
7
7
  if (!quiet) {
@@ -15,26 +15,23 @@ const validateSourceDirAndFileNames = (compilerOptions) => {
15
15
  }
16
16
  return null;
17
17
  };
18
- const validateWatchDir = (compilerOptions) => {
18
+ export const validateWatchDir = (compilerOptions) => {
19
19
  const { watchDir, enableWatch, quiet } = compilerOptions;
20
20
  if (enableWatch && !watchDir) {
21
21
  if (!quiet) {
22
22
  logger.error(watchDirValidMessage);
23
23
  }
24
- return { code: 1, message: watchDirValidMessage, virtualDists: [] };
24
+ return {
25
+ code: 1,
26
+ message: watchDirValidMessage,
27
+ virtualDists: []
28
+ };
25
29
  }
26
30
  return null;
27
31
  };
28
- const validate = (compilerOptions) => {
32
+ export const validate = (compilerOptions) => {
29
33
  if (compilerOptions.enableWatch) {
30
34
  return validateWatchDir(compilerOptions);
31
35
  }
32
36
  return validateSourceDirAndFileNames(compilerOptions);
33
37
  };
34
- export {
35
- sourceDirAndFileNamesValidMessage,
36
- validate,
37
- validateSourceDirAndFileNames,
38
- validateWatchDir,
39
- watchDirValidMessage
40
- };
package/package.json CHANGED
@@ -3,7 +3,11 @@
3
3
  "description": "A Progressive React Framework for modern web development.",
4
4
  "homepage": "https://modernjs.dev",
5
5
  "bugs": "https://github.com/web-infra-dev/modern.js/issues",
6
- "repository": "web-infra-dev/modern.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/web-infra-dev/modern.js",
9
+ "directory": "packages/toolkit/compiler/babel"
10
+ },
7
11
  "license": "MIT",
8
12
  "keywords": [
9
13
  "react",
@@ -11,7 +15,7 @@
11
15
  "modern",
12
16
  "modern.js"
13
17
  ],
14
- "version": "2.15.0",
18
+ "version": "2.17.0",
15
19
  "jsnext:source": "./src/index.ts",
16
20
  "types": "./dist/types/index.d.ts",
17
21
  "main": "./dist/cjs/index.js",
@@ -29,7 +33,7 @@
29
33
  "dependencies": {
30
34
  "@babel/core": "^7.18.0",
31
35
  "@babel/runtime": "^7.18.0",
32
- "@modern-js/utils": "2.15.0"
36
+ "@modern-js/utils": "2.17.0"
33
37
  },
34
38
  "devDependencies": {
35
39
  "@babel/plugin-transform-classes": "^7.18.0",
@@ -40,8 +44,8 @@
40
44
  "@types/node": "^14",
41
45
  "typescript": "^4",
42
46
  "jest": "^29",
43
- "@scripts/build": "2.15.0",
44
- "@scripts/jest-config": "2.15.0"
47
+ "@scripts/build": "2.17.0",
48
+ "@scripts/jest-config": "2.17.0"
45
49
  },
46
50
  "sideEffects": false,
47
51
  "modernConfig": {