@modern-js/server-utils 2.22.0 → 2.22.1
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 +14 -0
- package/dist/cjs/compilers/babel/index.js +4 -15
- package/dist/cjs/compilers/babel/preset/alias.js +84 -0
- package/dist/cjs/compilers/babel/preset/index.js +58 -0
- package/dist/cjs/compilers/babel/preset/types.js +4 -0
- package/dist/esm/compilers/babel/index.js +2 -9
- package/dist/esm/compilers/babel/preset/alias.js +67 -0
- package/dist/esm/compilers/babel/preset/index.js +39 -0
- package/dist/esm/compilers/babel/preset/types.js +1 -0
- package/dist/esm-node/compilers/babel/index.js +2 -10
- package/dist/esm-node/compilers/babel/preset/alias.js +73 -0
- package/dist/esm-node/compilers/babel/preset/index.js +38 -0
- package/dist/esm-node/compilers/babel/preset/types.js +1 -0
- package/dist/types/compilers/babel/index.d.ts +0 -3
- package/dist/types/compilers/babel/preset/alias.d.ts +3 -0
- package/dist/types/compilers/babel/preset/index.d.ts +4 -0
- package/dist/types/compilers/babel/preset/types.d.ts +21 -0
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @modern-js/server-utils
|
|
2
2
|
|
|
3
|
+
## 2.22.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 4be1da5: chore: merge babel-preset-lib to server-utils, remove unused codes
|
|
8
|
+
|
|
9
|
+
chore: 合并 babel-preset-lib 到 server-utils,移除无用代码
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [e2848a2]
|
|
12
|
+
- Updated dependencies [d4045ed]
|
|
13
|
+
- @modern-js/utils@2.22.1
|
|
14
|
+
- @modern-js/babel-preset-base@2.22.1
|
|
15
|
+
- @modern-js/babel-compiler@2.22.1
|
|
16
|
+
|
|
3
17
|
## 2.22.0
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
|
@@ -16,9 +16,6 @@ _export(exports, {
|
|
|
16
16
|
existTsConfigFile: function() {
|
|
17
17
|
return existTsConfigFile;
|
|
18
18
|
},
|
|
19
|
-
getBabelConfig: function() {
|
|
20
|
-
return getBabelConfig;
|
|
21
|
-
},
|
|
22
19
|
resolveBabelConfig: function() {
|
|
23
20
|
return resolveBabelConfig;
|
|
24
21
|
},
|
|
@@ -29,10 +26,10 @@ _export(exports, {
|
|
|
29
26
|
const _export_star = require("@swc/helpers/_/_export_star");
|
|
30
27
|
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
|
|
31
28
|
const _path = /* @__PURE__ */ _interop_require_wildcard._(require("path"));
|
|
32
|
-
const _babelpresetlib = require("@modern-js/babel-preset-lib");
|
|
33
29
|
const _utils = require("@modern-js/utils");
|
|
34
30
|
const _babelcompiler = require("@modern-js/babel-compiler");
|
|
35
31
|
const _common = require("../../common");
|
|
32
|
+
const _preset = require("./preset");
|
|
36
33
|
_export_star._(require("@babel/core"), exports);
|
|
37
34
|
const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
38
35
|
if (!_utils.fs.existsSync(tsconfigPath)) {
|
|
@@ -45,25 +42,17 @@ const existTsConfigFile = (tsconfigAbsolutePath) => {
|
|
|
45
42
|
const tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
46
43
|
return Boolean(tsconfig);
|
|
47
44
|
};
|
|
48
|
-
const getBabelConfig = (libPresetOption, syntaxOption) => {
|
|
49
|
-
const chain = (0, _babelpresetlib.getBabelChain)(libPresetOption, syntaxOption);
|
|
50
|
-
return {
|
|
51
|
-
sourceType: "unambiguous",
|
|
52
|
-
...chain.toJSON()
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
45
|
const resolveBabelConfig = (appDirectory, config, option) => {
|
|
56
46
|
const { alias, babelConfig } = config;
|
|
57
47
|
const aliasConfig = (0, _utils.getAliasConfig)(alias, {
|
|
58
48
|
appDirectory,
|
|
59
49
|
...option
|
|
60
50
|
});
|
|
61
|
-
const babelChain = (0,
|
|
51
|
+
const babelChain = (0, _preset.getBabelChain)({
|
|
62
52
|
appDirectory,
|
|
63
53
|
enableReactPreset: true,
|
|
64
54
|
enableTypescriptPreset: true,
|
|
65
|
-
alias: aliasConfig
|
|
66
|
-
envVars: []
|
|
55
|
+
alias: aliasConfig
|
|
67
56
|
}, {
|
|
68
57
|
type: option.type,
|
|
69
58
|
syntax: option.syntax
|
|
@@ -89,7 +78,7 @@ const resolveBabelConfig = (appDirectory, config, option) => {
|
|
|
89
78
|
const internalBabelConfig = {
|
|
90
79
|
...babelChain.toJSON()
|
|
91
80
|
};
|
|
92
|
-
return (0,
|
|
81
|
+
return (0, _preset.applyUserBabelConfig)(internalBabelConfig, babelConfig);
|
|
93
82
|
};
|
|
94
83
|
const compileByBabel = async (appDirectory, config, compileOptions) => {
|
|
95
84
|
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "aliasPlugin", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return aliasPlugin;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
|
|
12
|
+
const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
|
|
13
|
+
const _tsconfigpaths = require("@modern-js/utils/tsconfig-paths");
|
|
14
|
+
const _utils = require("@modern-js/utils");
|
|
15
|
+
const { resolvePath } = require("babel-plugin-module-resolver");
|
|
16
|
+
const defaultPaths = {
|
|
17
|
+
"@": [
|
|
18
|
+
"./src"
|
|
19
|
+
]
|
|
20
|
+
};
|
|
21
|
+
const aliasPlugin = (alias) => {
|
|
22
|
+
const { absoluteBaseUrl, isTsPath, isTsProject = false } = alias;
|
|
23
|
+
const mergedPaths = isTsPath ? alias.paths || {} : {
|
|
24
|
+
...defaultPaths,
|
|
25
|
+
...alias.paths || {}
|
|
26
|
+
};
|
|
27
|
+
let tsPaths = {};
|
|
28
|
+
if (isTsProject) {
|
|
29
|
+
tsPaths = (0, _utils.getUserAlias)(mergedPaths);
|
|
30
|
+
}
|
|
31
|
+
tsPaths = Object.keys(tsPaths).reduce((o, key) => {
|
|
32
|
+
if (typeof tsPaths[key] === "string") {
|
|
33
|
+
return {
|
|
34
|
+
...o,
|
|
35
|
+
[`${key}`]: [
|
|
36
|
+
tsPaths[key]
|
|
37
|
+
]
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
...o,
|
|
42
|
+
[`${key}`]: tsPaths[key]
|
|
43
|
+
};
|
|
44
|
+
}, {});
|
|
45
|
+
const resolvePathFn = (sourcePath, currentFile, opts) => {
|
|
46
|
+
if (sourcePath === "." || sourcePath === "./") {
|
|
47
|
+
return sourcePath;
|
|
48
|
+
}
|
|
49
|
+
const matchPath = (0, _tsconfigpaths.createMatchPath)(absoluteBaseUrl, tsPaths, [
|
|
50
|
+
"index"
|
|
51
|
+
]);
|
|
52
|
+
const result = matchPath(sourcePath, void 0, void 0, [
|
|
53
|
+
".js",
|
|
54
|
+
".jsx",
|
|
55
|
+
".ts",
|
|
56
|
+
".tsx"
|
|
57
|
+
]);
|
|
58
|
+
if (result) {
|
|
59
|
+
const relativePath = _path.default.relative(_path.default.dirname(currentFile), _path.default.dirname(result));
|
|
60
|
+
const fileName = _path.default.basename(result);
|
|
61
|
+
const filePath = _path.default.normalize(`${relativePath.length === 0 ? "." : relativePath}/${fileName}`).replace(/\\/, "/");
|
|
62
|
+
return filePath.startsWith(".") ? filePath : `./${filePath}`;
|
|
63
|
+
}
|
|
64
|
+
return resolvePath(sourcePath, currentFile, opts);
|
|
65
|
+
};
|
|
66
|
+
const typescriptExts = [
|
|
67
|
+
".ts",
|
|
68
|
+
".tsx",
|
|
69
|
+
".js",
|
|
70
|
+
".jsx",
|
|
71
|
+
".es",
|
|
72
|
+
".es6",
|
|
73
|
+
".mjs"
|
|
74
|
+
];
|
|
75
|
+
return [
|
|
76
|
+
"babel-plugin-module-resolver",
|
|
77
|
+
{
|
|
78
|
+
root: absoluteBaseUrl,
|
|
79
|
+
alias: mergedPaths,
|
|
80
|
+
resolvePath: isTsPath ? resolvePathFn : void 0,
|
|
81
|
+
extensions: isTsProject ? typescriptExts : void 0
|
|
82
|
+
}
|
|
83
|
+
];
|
|
84
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
Object.defineProperty(target, name, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: all[name]
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
_export(exports, {
|
|
13
|
+
getBabelChain: function() {
|
|
14
|
+
return getBabelChain;
|
|
15
|
+
},
|
|
16
|
+
applyUserBabelConfig: function() {
|
|
17
|
+
return _utils.applyUserBabelConfig;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
const _export_star = require("@swc/helpers/_/_export_star");
|
|
21
|
+
const _babelpresetbase = require("@modern-js/babel-preset-base");
|
|
22
|
+
const _alias = require("./alias");
|
|
23
|
+
_export_star._(require("./types"), exports);
|
|
24
|
+
const _utils = require("@modern-js/utils");
|
|
25
|
+
const getBabelChain = (libPresetOption, syntaxOption) => {
|
|
26
|
+
const { appDirectory, jsxTransformRuntime, enableReactPreset, enableTypescriptPreset, lodashOptions, styledComponentsOptions } = libPresetOption;
|
|
27
|
+
const { syntax, type } = syntaxOption;
|
|
28
|
+
const chain = (0, _babelpresetbase.getBaseBabelChain)({
|
|
29
|
+
appDirectory,
|
|
30
|
+
type,
|
|
31
|
+
syntax,
|
|
32
|
+
presets: {
|
|
33
|
+
envOptions: true,
|
|
34
|
+
reactOptions: enableReactPreset,
|
|
35
|
+
typescriptOptions: enableTypescriptPreset ? {
|
|
36
|
+
allowDeclareFields: true,
|
|
37
|
+
allExtensions: true
|
|
38
|
+
} : false
|
|
39
|
+
},
|
|
40
|
+
plugins: {
|
|
41
|
+
transformRuntime: {
|
|
42
|
+
corejs: false,
|
|
43
|
+
// for es5 code need helper functions
|
|
44
|
+
helpers: syntaxOption.syntax === "es5"
|
|
45
|
+
},
|
|
46
|
+
lodashOptions,
|
|
47
|
+
styledComponentsOptions
|
|
48
|
+
},
|
|
49
|
+
jsxTransformRuntime
|
|
50
|
+
});
|
|
51
|
+
if (libPresetOption.alias) {
|
|
52
|
+
const [name, opt] = (0, _alias.aliasPlugin)(libPresetOption.alias);
|
|
53
|
+
chain.plugin(name).use(require.resolve(name), [
|
|
54
|
+
opt
|
|
55
|
+
]);
|
|
56
|
+
}
|
|
57
|
+
return chain;
|
|
58
|
+
};
|
|
@@ -3,10 +3,10 @@ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
|
3
3
|
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
4
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
5
5
|
import * as path from "path";
|
|
6
|
-
import { getBabelChain, applyUserBabelConfig } from "@modern-js/babel-preset-lib";
|
|
7
6
|
import { fs, json5, getAliasConfig } from "@modern-js/utils";
|
|
8
7
|
import { compiler } from "@modern-js/babel-compiler";
|
|
9
8
|
import { FILE_EXTENSIONS } from "../../common";
|
|
9
|
+
import { getBabelChain, applyUserBabelConfig } from "./preset";
|
|
10
10
|
export * from "@babel/core";
|
|
11
11
|
export var readTsConfig = function(tsconfigPath) {
|
|
12
12
|
var noExistReturn = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : null;
|
|
@@ -20,12 +20,6 @@ export var existTsConfigFile = function(tsconfigAbsolutePath) {
|
|
|
20
20
|
var tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
21
21
|
return Boolean(tsconfig);
|
|
22
22
|
};
|
|
23
|
-
export var getBabelConfig = function(libPresetOption, syntaxOption) {
|
|
24
|
-
var chain = getBabelChain(libPresetOption, syntaxOption);
|
|
25
|
-
return _object_spread({
|
|
26
|
-
sourceType: "unambiguous"
|
|
27
|
-
}, chain.toJSON());
|
|
28
|
-
};
|
|
29
23
|
export var resolveBabelConfig = function(appDirectory, config, option) {
|
|
30
24
|
var alias = config.alias, babelConfig = config.babelConfig;
|
|
31
25
|
var aliasConfig = getAliasConfig(alias, _object_spread({
|
|
@@ -35,8 +29,7 @@ export var resolveBabelConfig = function(appDirectory, config, option) {
|
|
|
35
29
|
appDirectory: appDirectory,
|
|
36
30
|
enableReactPreset: true,
|
|
37
31
|
enableTypescriptPreset: true,
|
|
38
|
-
alias: aliasConfig
|
|
39
|
-
envVars: []
|
|
32
|
+
alias: aliasConfig
|
|
40
33
|
}, {
|
|
41
34
|
type: option.type,
|
|
42
35
|
syntax: option.syntax
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
6
|
+
import { getUserAlias } from "@modern-js/utils";
|
|
7
|
+
var resolvePath = require("babel-plugin-module-resolver").resolvePath;
|
|
8
|
+
var defaultPaths = {
|
|
9
|
+
"@": [
|
|
10
|
+
"./src"
|
|
11
|
+
]
|
|
12
|
+
};
|
|
13
|
+
export var aliasPlugin = function(alias) {
|
|
14
|
+
var absoluteBaseUrl = alias.absoluteBaseUrl, isTsPath = alias.isTsPath, _alias_isTsProject = alias.isTsProject, isTsProject = _alias_isTsProject === void 0 ? false : _alias_isTsProject;
|
|
15
|
+
var mergedPaths = isTsPath ? alias.paths || {} : _object_spread({}, defaultPaths, alias.paths || {});
|
|
16
|
+
var tsPaths = {};
|
|
17
|
+
if (isTsProject) {
|
|
18
|
+
tsPaths = getUserAlias(mergedPaths);
|
|
19
|
+
}
|
|
20
|
+
tsPaths = Object.keys(tsPaths).reduce(function(o, key) {
|
|
21
|
+
if (typeof tsPaths[key] === "string") {
|
|
22
|
+
return _object_spread_props(_object_spread({}, o), _define_property({}, "".concat(key), [
|
|
23
|
+
tsPaths[key]
|
|
24
|
+
]));
|
|
25
|
+
}
|
|
26
|
+
return _object_spread_props(_object_spread({}, o), _define_property({}, "".concat(key), tsPaths[key]));
|
|
27
|
+
}, {});
|
|
28
|
+
var resolvePathFn = function(sourcePath, currentFile, opts) {
|
|
29
|
+
if (sourcePath === "." || sourcePath === "./") {
|
|
30
|
+
return sourcePath;
|
|
31
|
+
}
|
|
32
|
+
var matchPath = createMatchPath(absoluteBaseUrl, tsPaths, [
|
|
33
|
+
"index"
|
|
34
|
+
]);
|
|
35
|
+
var result = matchPath(sourcePath, void 0, void 0, [
|
|
36
|
+
".js",
|
|
37
|
+
".jsx",
|
|
38
|
+
".ts",
|
|
39
|
+
".tsx"
|
|
40
|
+
]);
|
|
41
|
+
if (result) {
|
|
42
|
+
var relativePath = path.relative(path.dirname(currentFile), path.dirname(result));
|
|
43
|
+
var fileName = path.basename(result);
|
|
44
|
+
var filePath = path.normalize("".concat(relativePath.length === 0 ? "." : relativePath, "/").concat(fileName)).replace(/\\/, "/");
|
|
45
|
+
return filePath.startsWith(".") ? filePath : "./".concat(filePath);
|
|
46
|
+
}
|
|
47
|
+
return resolvePath(sourcePath, currentFile, opts);
|
|
48
|
+
};
|
|
49
|
+
var typescriptExts = [
|
|
50
|
+
".ts",
|
|
51
|
+
".tsx",
|
|
52
|
+
".js",
|
|
53
|
+
".jsx",
|
|
54
|
+
".es",
|
|
55
|
+
".es6",
|
|
56
|
+
".mjs"
|
|
57
|
+
];
|
|
58
|
+
return [
|
|
59
|
+
"babel-plugin-module-resolver",
|
|
60
|
+
{
|
|
61
|
+
root: absoluteBaseUrl,
|
|
62
|
+
alias: mergedPaths,
|
|
63
|
+
resolvePath: isTsPath ? resolvePathFn : void 0,
|
|
64
|
+
extensions: isTsProject ? typescriptExts : void 0
|
|
65
|
+
}
|
|
66
|
+
];
|
|
67
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
2
|
+
import { getBaseBabelChain } from "@modern-js/babel-preset-base";
|
|
3
|
+
import { aliasPlugin } from "./alias";
|
|
4
|
+
export var getBabelChain = function(libPresetOption, syntaxOption) {
|
|
5
|
+
var appDirectory = libPresetOption.appDirectory, jsxTransformRuntime = libPresetOption.jsxTransformRuntime, enableReactPreset = libPresetOption.enableReactPreset, enableTypescriptPreset = libPresetOption.enableTypescriptPreset, lodashOptions = libPresetOption.lodashOptions, styledComponentsOptions = libPresetOption.styledComponentsOptions;
|
|
6
|
+
var syntax = syntaxOption.syntax, type = syntaxOption.type;
|
|
7
|
+
var chain = getBaseBabelChain({
|
|
8
|
+
appDirectory: appDirectory,
|
|
9
|
+
type: type,
|
|
10
|
+
syntax: syntax,
|
|
11
|
+
presets: {
|
|
12
|
+
envOptions: true,
|
|
13
|
+
reactOptions: enableReactPreset,
|
|
14
|
+
typescriptOptions: enableTypescriptPreset ? {
|
|
15
|
+
allowDeclareFields: true,
|
|
16
|
+
allExtensions: true
|
|
17
|
+
} : false
|
|
18
|
+
},
|
|
19
|
+
plugins: {
|
|
20
|
+
transformRuntime: {
|
|
21
|
+
corejs: false,
|
|
22
|
+
// for es5 code need helper functions
|
|
23
|
+
helpers: syntaxOption.syntax === "es5"
|
|
24
|
+
},
|
|
25
|
+
lodashOptions: lodashOptions,
|
|
26
|
+
styledComponentsOptions: styledComponentsOptions
|
|
27
|
+
},
|
|
28
|
+
jsxTransformRuntime: jsxTransformRuntime
|
|
29
|
+
});
|
|
30
|
+
if (libPresetOption.alias) {
|
|
31
|
+
var _aliasPlugin = _sliced_to_array(aliasPlugin(libPresetOption.alias), 2), name = _aliasPlugin[0], opt = _aliasPlugin[1];
|
|
32
|
+
chain.plugin(name).use(require.resolve(name), [
|
|
33
|
+
opt
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
36
|
+
return chain;
|
|
37
|
+
};
|
|
38
|
+
export * from "./types";
|
|
39
|
+
export { applyUserBabelConfig } from "@modern-js/utils";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as path from "path";
|
|
2
|
-
import { getBabelChain, applyUserBabelConfig } from "@modern-js/babel-preset-lib";
|
|
3
2
|
import { fs, json5, getAliasConfig } from "@modern-js/utils";
|
|
4
3
|
import { compiler } from "@modern-js/babel-compiler";
|
|
5
4
|
import { FILE_EXTENSIONS } from "../../common";
|
|
5
|
+
import { getBabelChain, applyUserBabelConfig } from "./preset";
|
|
6
6
|
export * from "@babel/core";
|
|
7
7
|
export const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
8
8
|
if (!fs.existsSync(tsconfigPath)) {
|
|
@@ -15,13 +15,6 @@ export const existTsConfigFile = (tsconfigAbsolutePath) => {
|
|
|
15
15
|
const tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
16
16
|
return Boolean(tsconfig);
|
|
17
17
|
};
|
|
18
|
-
export const getBabelConfig = (libPresetOption, syntaxOption) => {
|
|
19
|
-
const chain = getBabelChain(libPresetOption, syntaxOption);
|
|
20
|
-
return {
|
|
21
|
-
sourceType: "unambiguous",
|
|
22
|
-
...chain.toJSON()
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
18
|
export const resolveBabelConfig = (appDirectory, config, option) => {
|
|
26
19
|
const { alias, babelConfig } = config;
|
|
27
20
|
const aliasConfig = getAliasConfig(alias, {
|
|
@@ -32,8 +25,7 @@ export const resolveBabelConfig = (appDirectory, config, option) => {
|
|
|
32
25
|
appDirectory,
|
|
33
26
|
enableReactPreset: true,
|
|
34
27
|
enableTypescriptPreset: true,
|
|
35
|
-
alias: aliasConfig
|
|
36
|
-
envVars: []
|
|
28
|
+
alias: aliasConfig
|
|
37
29
|
}, {
|
|
38
30
|
type: option.type,
|
|
39
31
|
syntax: option.syntax
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
3
|
+
import { getUserAlias } from "@modern-js/utils";
|
|
4
|
+
const { resolvePath } = require("babel-plugin-module-resolver");
|
|
5
|
+
const defaultPaths = {
|
|
6
|
+
"@": [
|
|
7
|
+
"./src"
|
|
8
|
+
]
|
|
9
|
+
};
|
|
10
|
+
export const aliasPlugin = (alias) => {
|
|
11
|
+
const { absoluteBaseUrl, isTsPath, isTsProject = false } = alias;
|
|
12
|
+
const mergedPaths = isTsPath ? alias.paths || {} : {
|
|
13
|
+
...defaultPaths,
|
|
14
|
+
...alias.paths || {}
|
|
15
|
+
};
|
|
16
|
+
let tsPaths = {};
|
|
17
|
+
if (isTsProject) {
|
|
18
|
+
tsPaths = getUserAlias(mergedPaths);
|
|
19
|
+
}
|
|
20
|
+
tsPaths = Object.keys(tsPaths).reduce((o, key) => {
|
|
21
|
+
if (typeof tsPaths[key] === "string") {
|
|
22
|
+
return {
|
|
23
|
+
...o,
|
|
24
|
+
[`${key}`]: [
|
|
25
|
+
tsPaths[key]
|
|
26
|
+
]
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
...o,
|
|
31
|
+
[`${key}`]: tsPaths[key]
|
|
32
|
+
};
|
|
33
|
+
}, {});
|
|
34
|
+
const resolvePathFn = (sourcePath, currentFile, opts) => {
|
|
35
|
+
if (sourcePath === "." || sourcePath === "./") {
|
|
36
|
+
return sourcePath;
|
|
37
|
+
}
|
|
38
|
+
const matchPath = createMatchPath(absoluteBaseUrl, tsPaths, [
|
|
39
|
+
"index"
|
|
40
|
+
]);
|
|
41
|
+
const result = matchPath(sourcePath, void 0, void 0, [
|
|
42
|
+
".js",
|
|
43
|
+
".jsx",
|
|
44
|
+
".ts",
|
|
45
|
+
".tsx"
|
|
46
|
+
]);
|
|
47
|
+
if (result) {
|
|
48
|
+
const relativePath = path.relative(path.dirname(currentFile), path.dirname(result));
|
|
49
|
+
const fileName = path.basename(result);
|
|
50
|
+
const filePath = path.normalize(`${relativePath.length === 0 ? "." : relativePath}/${fileName}`).replace(/\\/, "/");
|
|
51
|
+
return filePath.startsWith(".") ? filePath : `./${filePath}`;
|
|
52
|
+
}
|
|
53
|
+
return resolvePath(sourcePath, currentFile, opts);
|
|
54
|
+
};
|
|
55
|
+
const typescriptExts = [
|
|
56
|
+
".ts",
|
|
57
|
+
".tsx",
|
|
58
|
+
".js",
|
|
59
|
+
".jsx",
|
|
60
|
+
".es",
|
|
61
|
+
".es6",
|
|
62
|
+
".mjs"
|
|
63
|
+
];
|
|
64
|
+
return [
|
|
65
|
+
"babel-plugin-module-resolver",
|
|
66
|
+
{
|
|
67
|
+
root: absoluteBaseUrl,
|
|
68
|
+
alias: mergedPaths,
|
|
69
|
+
resolvePath: isTsPath ? resolvePathFn : void 0,
|
|
70
|
+
extensions: isTsProject ? typescriptExts : void 0
|
|
71
|
+
}
|
|
72
|
+
];
|
|
73
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { getBaseBabelChain } from "@modern-js/babel-preset-base";
|
|
2
|
+
import { aliasPlugin } from "./alias";
|
|
3
|
+
export const getBabelChain = (libPresetOption, syntaxOption) => {
|
|
4
|
+
const { appDirectory, jsxTransformRuntime, enableReactPreset, enableTypescriptPreset, lodashOptions, styledComponentsOptions } = libPresetOption;
|
|
5
|
+
const { syntax, type } = syntaxOption;
|
|
6
|
+
const chain = getBaseBabelChain({
|
|
7
|
+
appDirectory,
|
|
8
|
+
type,
|
|
9
|
+
syntax,
|
|
10
|
+
presets: {
|
|
11
|
+
envOptions: true,
|
|
12
|
+
reactOptions: enableReactPreset,
|
|
13
|
+
typescriptOptions: enableTypescriptPreset ? {
|
|
14
|
+
allowDeclareFields: true,
|
|
15
|
+
allExtensions: true
|
|
16
|
+
} : false
|
|
17
|
+
},
|
|
18
|
+
plugins: {
|
|
19
|
+
transformRuntime: {
|
|
20
|
+
corejs: false,
|
|
21
|
+
// for es5 code need helper functions
|
|
22
|
+
helpers: syntaxOption.syntax === "es5"
|
|
23
|
+
},
|
|
24
|
+
lodashOptions,
|
|
25
|
+
styledComponentsOptions
|
|
26
|
+
},
|
|
27
|
+
jsxTransformRuntime
|
|
28
|
+
});
|
|
29
|
+
if (libPresetOption.alias) {
|
|
30
|
+
const [name, opt] = aliasPlugin(libPresetOption.alias);
|
|
31
|
+
chain.plugin(name).use(require.resolve(name), [
|
|
32
|
+
opt
|
|
33
|
+
]);
|
|
34
|
+
}
|
|
35
|
+
return chain;
|
|
36
|
+
};
|
|
37
|
+
export * from "./types";
|
|
38
|
+
export { applyUserBabelConfig } from "@modern-js/utils";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { ILibPresetOption, ISyntaxOption } from '@modern-js/babel-preset-lib';
|
|
2
|
-
import { TransformOptions } from '@babel/core';
|
|
3
1
|
import { CompileFunc } from '../../common';
|
|
4
2
|
export * from '@babel/core';
|
|
5
3
|
export interface ITsconfig {
|
|
@@ -18,7 +16,6 @@ export interface ITsconfig {
|
|
|
18
16
|
}
|
|
19
17
|
export declare const readTsConfig: <T extends ITsconfig | null>(tsconfigPath: string, noExistReturn?: T) => ITsconfig | T;
|
|
20
18
|
export declare const existTsConfigFile: (tsconfigAbsolutePath: string) => boolean;
|
|
21
|
-
export declare const getBabelConfig: (libPresetOption: ILibPresetOption, syntaxOption: ISyntaxOption) => TransformOptions;
|
|
22
19
|
export interface IPackageModeValue {
|
|
23
20
|
type: 'module' | 'commonjs';
|
|
24
21
|
syntax: 'es5' | 'es6+';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ISyntaxOption, ILibPresetOption } from './types';
|
|
2
|
+
export declare const getBabelChain: (libPresetOption: ILibPresetOption, syntaxOption: ISyntaxOption) => import("@modern-js/babel-preset-base").BabelChain;
|
|
3
|
+
export * from './types';
|
|
4
|
+
export { applyUserBabelConfig } from '@modern-js/utils';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { IStyledComponentOptions } from '@modern-js/babel-preset-base';
|
|
2
|
+
export interface ISyntaxOption {
|
|
3
|
+
syntax: 'es5' | 'es6+';
|
|
4
|
+
type: 'module' | 'commonjs';
|
|
5
|
+
}
|
|
6
|
+
export interface AliasOption {
|
|
7
|
+
absoluteBaseUrl: string;
|
|
8
|
+
paths?: Record<string, string | string[]>;
|
|
9
|
+
isTsPath?: boolean;
|
|
10
|
+
isTsProject?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface ILibPresetOption {
|
|
13
|
+
appDirectory: string;
|
|
14
|
+
enableTypescriptPreset?: boolean;
|
|
15
|
+
enableReactPreset?: boolean;
|
|
16
|
+
lodashOptions?: any;
|
|
17
|
+
alias?: AliasOption;
|
|
18
|
+
jsxTransformRuntime?: JsxTransformRuntimeType;
|
|
19
|
+
styledComponentsOptions?: IStyledComponentOptions;
|
|
20
|
+
}
|
|
21
|
+
export type JsxTransformRuntimeType = 'automatic' | 'classic';
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "2.22.
|
|
18
|
+
"version": "2.22.1",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
"babel-plugin-module-resolver": "^4.1.0",
|
|
40
40
|
"babel-plugin-transform-typescript-metadata": "^0.3.2",
|
|
41
41
|
"@swc/helpers": "0.5.1",
|
|
42
|
-
"@modern-js/babel-compiler": "2.22.
|
|
43
|
-
"@modern-js/babel-preset-
|
|
44
|
-
"@modern-js/utils": "2.22.
|
|
42
|
+
"@modern-js/babel-compiler": "2.22.1",
|
|
43
|
+
"@modern-js/babel-preset-base": "2.22.1",
|
|
44
|
+
"@modern-js/utils": "2.22.1"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/babel__core": "^7.20.0",
|
|
@@ -50,9 +50,9 @@
|
|
|
50
50
|
"jest": "^29",
|
|
51
51
|
"ts-jest": "^29.1.0",
|
|
52
52
|
"typescript": "^5",
|
|
53
|
-
"@modern-js/server-core": "2.22.
|
|
54
|
-
"@scripts/build": "2.22.
|
|
55
|
-
"@scripts/jest-config": "2.22.
|
|
53
|
+
"@modern-js/server-core": "2.22.1",
|
|
54
|
+
"@scripts/build": "2.22.1",
|
|
55
|
+
"@scripts/jest-config": "2.22.1"
|
|
56
56
|
},
|
|
57
57
|
"sideEffects": false,
|
|
58
58
|
"publishConfig": {
|