@modern-js/server-utils 2.4.1-beta.0 → 2.5.1-alpha.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.
- package/CHANGELOG.md +13 -4
- package/dist/cjs/common/index.js +65 -0
- package/dist/cjs/compilers/babel/index.js +147 -0
- package/dist/cjs/compilers/typescript/index.js +117 -0
- package/dist/cjs/compilers/typescript/tsconfig-paths-plugin.js +216 -0
- package/dist/cjs/compilers/typescript/typescript-loader.js +47 -0
- package/dist/cjs/index.js +29 -0
- package/dist/esm/common/index.js +204 -0
- package/dist/esm/compilers/babel/index.js +368 -0
- package/dist/esm/compilers/typescript/index.js +339 -0
- package/dist/esm/compilers/typescript/tsconfig-paths-plugin.js +246 -0
- package/dist/esm/compilers/typescript/typescript-loader.js +65 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm-node/common/index.js +35 -0
- package/dist/esm-node/compilers/babel/index.js +116 -0
- package/dist/esm-node/compilers/typescript/index.js +88 -0
- package/dist/esm-node/compilers/typescript/tsconfig-paths-plugin.js +187 -0
- package/dist/esm-node/compilers/typescript/typescript-loader.js +24 -0
- package/dist/esm-node/index.js +5 -0
- package/package.json +14 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
# @modern-js/server-utils
|
|
2
2
|
|
|
3
|
-
## 2.
|
|
3
|
+
## 2.5.0
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
+
- 89ca6cc: refactor: merge build-config into scripts/build
|
|
8
|
+
|
|
9
|
+
refactor: 把 build-config 合并进 scripts/build
|
|
10
|
+
|
|
11
|
+
- 30614fa: chore: modify package.json entry fields and build config
|
|
12
|
+
chore: 更改 package.json entry 字段以及构建配置
|
|
13
|
+
- Updated dependencies [89ca6cc]
|
|
14
|
+
- Updated dependencies [30614fa]
|
|
15
|
+
- Updated dependencies [1b0ce87]
|
|
7
16
|
- Updated dependencies [11c053b]
|
|
8
|
-
- @modern-js/
|
|
9
|
-
- @modern-js/babel-
|
|
10
|
-
- @modern-js/
|
|
17
|
+
- @modern-js/babel-preset-lib@2.5.0
|
|
18
|
+
- @modern-js/babel-compiler@2.5.0
|
|
19
|
+
- @modern-js/utils@2.5.0
|
|
11
20
|
|
|
12
21
|
## 2.4.0
|
|
13
22
|
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var common_exports = {};
|
|
25
|
+
__export(common_exports, {
|
|
26
|
+
FILE_EXTENSIONS: () => FILE_EXTENSIONS,
|
|
27
|
+
compile: () => compile
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(common_exports);
|
|
30
|
+
var path = __toESM(require("path"));
|
|
31
|
+
var import_utils = require("@modern-js/utils");
|
|
32
|
+
var import_typescript = require("../compilers/typescript");
|
|
33
|
+
var import_babel = require("../compilers/babel");
|
|
34
|
+
const FILE_EXTENSIONS = [".js", ".ts", ".mjs", ".ejs"];
|
|
35
|
+
const validateAbsolutePath = (filename, message) => {
|
|
36
|
+
if (!path.isAbsolute(filename)) {
|
|
37
|
+
throw new Error(message);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const validateAbsolutePaths = (filenames, messageFunc) => {
|
|
41
|
+
filenames.forEach(
|
|
42
|
+
(filename) => validateAbsolutePath(filename, messageFunc(filename))
|
|
43
|
+
);
|
|
44
|
+
};
|
|
45
|
+
const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
46
|
+
var _a;
|
|
47
|
+
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
48
|
+
validateAbsolutePaths(
|
|
49
|
+
sourceDirs,
|
|
50
|
+
(dir) => `source dir ${dir} is not an absolute path.`
|
|
51
|
+
);
|
|
52
|
+
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
53
|
+
const compiler = (_a = modernConfig == null ? void 0 : modernConfig.server) == null ? void 0 : _a.compiler;
|
|
54
|
+
const isTsProject = tsconfigPath && await import_utils.fs.pathExists(tsconfigPath);
|
|
55
|
+
if (!isTsProject || compiler === "babel") {
|
|
56
|
+
await (0, import_babel.compileByBabel)(appDirectory, modernConfig, compileOptions);
|
|
57
|
+
} else {
|
|
58
|
+
await (0, import_typescript.compileByTs)(appDirectory, modernConfig, compileOptions);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
62
|
+
0 && (module.exports = {
|
|
63
|
+
FILE_EXTENSIONS,
|
|
64
|
+
compile
|
|
65
|
+
});
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
var babel_exports = {};
|
|
26
|
+
__export(babel_exports, {
|
|
27
|
+
compileByBabel: () => compileByBabel,
|
|
28
|
+
existTsConfigFile: () => existTsConfigFile,
|
|
29
|
+
getBabelConfig: () => getBabelConfig,
|
|
30
|
+
readTsConfig: () => readTsConfig,
|
|
31
|
+
resolveBabelConfig: () => resolveBabelConfig
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(babel_exports);
|
|
34
|
+
var path = __toESM(require("path"));
|
|
35
|
+
var import_babel_preset_lib = require("@modern-js/babel-preset-lib");
|
|
36
|
+
var import_utils = require("@modern-js/utils");
|
|
37
|
+
var import_babel_compiler = require("@modern-js/babel-compiler");
|
|
38
|
+
var import_common = require("../../common");
|
|
39
|
+
__reExport(babel_exports, require("@babel/core"), module.exports);
|
|
40
|
+
const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
41
|
+
if (!import_utils.fs.existsSync(tsconfigPath)) {
|
|
42
|
+
return noExistReturn;
|
|
43
|
+
}
|
|
44
|
+
const content = import_utils.fs.readFileSync(tsconfigPath, "utf-8");
|
|
45
|
+
return import_utils.json5.parse(content);
|
|
46
|
+
};
|
|
47
|
+
const existTsConfigFile = (tsconfigAbsolutePath) => {
|
|
48
|
+
const tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
49
|
+
return Boolean(tsconfig);
|
|
50
|
+
};
|
|
51
|
+
const getBabelConfig = (libPresetOption, syntaxOption) => {
|
|
52
|
+
const chain = (0, import_babel_preset_lib.getBabelChain)(libPresetOption, syntaxOption);
|
|
53
|
+
return {
|
|
54
|
+
sourceType: "unambiguous",
|
|
55
|
+
...chain.toJSON()
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
const resolveBabelConfig = (appDirectory, config, option) => {
|
|
59
|
+
const { globalVars, alias, babelConfig, define } = config;
|
|
60
|
+
const globalDefineVars = define && Object.entries(define).reduce((object, [key, value]) => {
|
|
61
|
+
object[key] = JSON.stringify(value);
|
|
62
|
+
return object;
|
|
63
|
+
}, {});
|
|
64
|
+
const aliasConfig = (0, import_utils.getAliasConfig)(alias, {
|
|
65
|
+
appDirectory,
|
|
66
|
+
...option
|
|
67
|
+
});
|
|
68
|
+
const babelChain = (0, import_babel_preset_lib.getBabelChain)(
|
|
69
|
+
{
|
|
70
|
+
appDirectory,
|
|
71
|
+
enableReactPreset: true,
|
|
72
|
+
enableTypescriptPreset: true,
|
|
73
|
+
alias: aliasConfig,
|
|
74
|
+
envVars: [],
|
|
75
|
+
globalVars: {
|
|
76
|
+
...globalVars,
|
|
77
|
+
...globalDefineVars
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
type: option.type,
|
|
82
|
+
syntax: option.syntax
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
const envOptions = babelChain.preset("@babel/preset-env").options();
|
|
86
|
+
babelChain.preset("@babel/preset-env").use(require.resolve("@babel/preset-env"), [
|
|
87
|
+
{
|
|
88
|
+
...envOptions[0],
|
|
89
|
+
loose: true
|
|
90
|
+
}
|
|
91
|
+
]);
|
|
92
|
+
babelChain.plugin("babel-plugin-transform-typescript-metadata").use(
|
|
93
|
+
require.resolve("babel-plugin-transform-typescript-metadata"),
|
|
94
|
+
[]
|
|
95
|
+
);
|
|
96
|
+
babelChain.plugin("@babel/plugin-proposal-decorators").use(require.resolve("@babel/plugin-proposal-decorators"), [
|
|
97
|
+
{ legacy: true }
|
|
98
|
+
]);
|
|
99
|
+
babelChain.plugin("@babel/plugin-proposal-class-properties").use(require.resolve("@babel/plugin-proposal-class-properties"), [
|
|
100
|
+
{
|
|
101
|
+
loose: true
|
|
102
|
+
}
|
|
103
|
+
]);
|
|
104
|
+
const internalBabelConfig = { ...babelChain.toJSON() };
|
|
105
|
+
return (0, import_babel_preset_lib.applyUserBabelConfig)(internalBabelConfig, babelConfig);
|
|
106
|
+
};
|
|
107
|
+
const compileByBabel = async (appDirectory, config, compileOptions) => {
|
|
108
|
+
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
109
|
+
const results = await Promise.all(
|
|
110
|
+
sourceDirs.map(async (sourceDir) => {
|
|
111
|
+
const babelConfig = resolveBabelConfig(appDirectory, config, {
|
|
112
|
+
tsconfigPath: tsconfigPath ? tsconfigPath : "",
|
|
113
|
+
syntax: "es6+",
|
|
114
|
+
type: "commonjs"
|
|
115
|
+
});
|
|
116
|
+
if (await import_utils.fs.pathExists(sourceDir)) {
|
|
117
|
+
const basename = path.basename(sourceDir);
|
|
118
|
+
const targetDir = path.join(distDir, basename);
|
|
119
|
+
await import_utils.fs.copy(sourceDir, targetDir, {
|
|
120
|
+
filter: (src) => ![".ts", ".js"].includes(path.extname(src)) && src !== tsconfigPath
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return (0, import_babel_compiler.compiler)(
|
|
124
|
+
{
|
|
125
|
+
rootDir: appDirectory,
|
|
126
|
+
distDir,
|
|
127
|
+
sourceDir,
|
|
128
|
+
extensions: import_common.FILE_EXTENSIONS
|
|
129
|
+
},
|
|
130
|
+
babelConfig
|
|
131
|
+
);
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
results.forEach((result) => {
|
|
135
|
+
if (result.code === 1) {
|
|
136
|
+
throw new Error(result.message);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
141
|
+
0 && (module.exports = {
|
|
142
|
+
compileByBabel,
|
|
143
|
+
existTsConfigFile,
|
|
144
|
+
getBabelConfig,
|
|
145
|
+
readTsConfig,
|
|
146
|
+
resolveBabelConfig
|
|
147
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var typescript_exports = {};
|
|
25
|
+
__export(typescript_exports, {
|
|
26
|
+
compileByTs: () => compileByTs
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(typescript_exports);
|
|
29
|
+
var import_path = __toESM(require("path"));
|
|
30
|
+
var import_utils = require("@modern-js/utils");
|
|
31
|
+
var import_typescript = __toESM(require("typescript"));
|
|
32
|
+
var import_typescript_loader = require("./typescript-loader");
|
|
33
|
+
var import_tsconfig_paths_plugin = require("./tsconfig-paths-plugin");
|
|
34
|
+
const readTsConfigByFile = (tsConfigFile) => {
|
|
35
|
+
const parsedCmd = import_typescript.default.getParsedCommandLineOfConfigFile(
|
|
36
|
+
tsConfigFile,
|
|
37
|
+
void 0,
|
|
38
|
+
import_typescript.default.sys
|
|
39
|
+
);
|
|
40
|
+
const { options, fileNames, projectReferences } = parsedCmd;
|
|
41
|
+
return { options, fileNames, projectReferences };
|
|
42
|
+
};
|
|
43
|
+
const copyFiles = async (from, to, appDirectory, tsconfigPath) => {
|
|
44
|
+
if (await import_utils.fs.pathExists(from)) {
|
|
45
|
+
const relativePath = import_path.default.relative(appDirectory, from);
|
|
46
|
+
const targetDir = import_path.default.join(to, relativePath);
|
|
47
|
+
await import_utils.fs.copy(from, targetDir, {
|
|
48
|
+
filter: (src) => ![".ts"].includes(import_path.default.extname(src)) && src !== tsconfigPath
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const compileByTs = async (appDirectory, config, compileOptions) => {
|
|
53
|
+
import_utils.logger.info(`Running ts compile...`);
|
|
54
|
+
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
55
|
+
if (!tsconfigPath) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const ts2 = new import_typescript_loader.TypescriptLoader({
|
|
59
|
+
appDirectory
|
|
60
|
+
}).load();
|
|
61
|
+
const createProgram = ts2.createIncrementalProgram || ts2.createProgram;
|
|
62
|
+
const formatHost = getFormatHost(ts2);
|
|
63
|
+
const { alias } = config;
|
|
64
|
+
const aliasOption = (0, import_utils.getAliasConfig)(alias, {
|
|
65
|
+
appDirectory,
|
|
66
|
+
tsconfigPath
|
|
67
|
+
});
|
|
68
|
+
const { paths = {}, absoluteBaseUrl = "./" } = aliasOption;
|
|
69
|
+
const { options, fileNames, projectReferences } = readTsConfigByFile(tsconfigPath);
|
|
70
|
+
const sourcePosixPaths = sourceDirs.map(
|
|
71
|
+
(sourceDir) => sourceDir.split(import_path.default.sep).join(import_path.default.posix.sep)
|
|
72
|
+
);
|
|
73
|
+
const rootNames = fileNames.filter((fileName) => {
|
|
74
|
+
return fileName.endsWith(".d.ts") || sourcePosixPaths.some((sourceDir) => {
|
|
75
|
+
return fileName.includes(sourceDir);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
const program = createProgram.call(ts2, {
|
|
79
|
+
rootNames,
|
|
80
|
+
projectReferences,
|
|
81
|
+
options: {
|
|
82
|
+
rootDir: appDirectory,
|
|
83
|
+
outDir: distDir,
|
|
84
|
+
...options
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
const tsconfigPathsPlugin = (0, import_tsconfig_paths_plugin.tsconfigPathsBeforeHookFactory)(
|
|
88
|
+
ts2,
|
|
89
|
+
absoluteBaseUrl,
|
|
90
|
+
paths
|
|
91
|
+
);
|
|
92
|
+
const emitResult = program.emit(void 0, void 0, void 0, void 0, {
|
|
93
|
+
before: [tsconfigPathsPlugin]
|
|
94
|
+
});
|
|
95
|
+
const allDiagnostics = ts2.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
96
|
+
if (allDiagnostics.length > 0) {
|
|
97
|
+
import_utils.logger.error(
|
|
98
|
+
ts2.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)
|
|
99
|
+
);
|
|
100
|
+
process.exit(1);
|
|
101
|
+
}
|
|
102
|
+
for (const source of sourceDirs) {
|
|
103
|
+
await copyFiles(source, distDir, appDirectory, tsconfigPath);
|
|
104
|
+
}
|
|
105
|
+
import_utils.logger.info(`Ts compile succeed`);
|
|
106
|
+
};
|
|
107
|
+
const getFormatHost = (ts2) => {
|
|
108
|
+
return {
|
|
109
|
+
getCanonicalFileName: (path2) => path2,
|
|
110
|
+
getCurrentDirectory: ts2.sys.getCurrentDirectory,
|
|
111
|
+
getNewLine: () => ts2.sys.newLine
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
115
|
+
0 && (module.exports = {
|
|
116
|
+
compileByTs
|
|
117
|
+
});
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var tsconfig_paths_plugin_exports = {};
|
|
25
|
+
__export(tsconfig_paths_plugin_exports, {
|
|
26
|
+
tsconfigPathsBeforeHookFactory: () => tsconfigPathsBeforeHookFactory
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(tsconfig_paths_plugin_exports);
|
|
29
|
+
var os = __toESM(require("os"));
|
|
30
|
+
var import_path = __toESM(require("path"));
|
|
31
|
+
var ts = __toESM(require("typescript"));
|
|
32
|
+
var import_tsconfig_paths = require("@modern-js/utils/tsconfig-paths");
|
|
33
|
+
const isRegExpKey = (str) => {
|
|
34
|
+
return str.startsWith("^") || str.endsWith("$");
|
|
35
|
+
};
|
|
36
|
+
const resolveAliasPath = (baseUrl, filePath) => {
|
|
37
|
+
if (filePath.startsWith(".") || filePath.startsWith("..")) {
|
|
38
|
+
return import_path.default.resolve(baseUrl, filePath);
|
|
39
|
+
}
|
|
40
|
+
return filePath;
|
|
41
|
+
};
|
|
42
|
+
const createAliasMatcher = (baseUrl, alias) => {
|
|
43
|
+
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
44
|
+
if (isRegExpKey(key)) {
|
|
45
|
+
const regexp = new RegExp(key);
|
|
46
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
47
|
+
o.push([regexp, aliasPath]);
|
|
48
|
+
} else {
|
|
49
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
50
|
+
o.push([key, aliasPath]);
|
|
51
|
+
}
|
|
52
|
+
return o;
|
|
53
|
+
}, []);
|
|
54
|
+
const cacheMap = /* @__PURE__ */ new Map();
|
|
55
|
+
return (requestedModule) => {
|
|
56
|
+
if (cacheMap.has(requestedModule)) {
|
|
57
|
+
return cacheMap.get(requestedModule);
|
|
58
|
+
}
|
|
59
|
+
for (const [key, value] of aliasPairs) {
|
|
60
|
+
if (key instanceof RegExp) {
|
|
61
|
+
if (key.test(requestedModule)) {
|
|
62
|
+
cacheMap.set(requestedModule, value);
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (requestedModule === key) {
|
|
67
|
+
cacheMap.set(requestedModule, value);
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
const isDynamicImport = (tsBinary, node) => {
|
|
74
|
+
return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
75
|
+
};
|
|
76
|
+
function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
77
|
+
const tsPaths = {};
|
|
78
|
+
const alias = {};
|
|
79
|
+
Object.keys(paths).forEach((key) => {
|
|
80
|
+
if (Array.isArray(paths[key])) {
|
|
81
|
+
tsPaths[key] = paths[key];
|
|
82
|
+
} else {
|
|
83
|
+
alias[key] = paths[key];
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
87
|
+
const matchTsPath = (0, import_tsconfig_paths.createMatchPath)(baseUrl, tsPaths, ["main"]);
|
|
88
|
+
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
89
|
+
const result = matchTsPath(
|
|
90
|
+
requestedModule,
|
|
91
|
+
readJSONSync,
|
|
92
|
+
fileExists,
|
|
93
|
+
extensions
|
|
94
|
+
);
|
|
95
|
+
if (result) {
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
return matchAliasPath(requestedModule);
|
|
99
|
+
};
|
|
100
|
+
if (Object.keys(paths).length === 0) {
|
|
101
|
+
return void 0;
|
|
102
|
+
}
|
|
103
|
+
return (ctx) => {
|
|
104
|
+
return (sf) => {
|
|
105
|
+
const visitNode = (node) => {
|
|
106
|
+
var _a;
|
|
107
|
+
if (isDynamicImport(tsBinary, node)) {
|
|
108
|
+
const importPathWithQuotes = node.arguments[0].getText(sf);
|
|
109
|
+
const text = importPathWithQuotes.slice(
|
|
110
|
+
1,
|
|
111
|
+
importPathWithQuotes.length - 1
|
|
112
|
+
);
|
|
113
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
114
|
+
if (!result) {
|
|
115
|
+
return node;
|
|
116
|
+
}
|
|
117
|
+
return tsBinary.factory.updateCallExpression(
|
|
118
|
+
node,
|
|
119
|
+
node.expression,
|
|
120
|
+
node.typeArguments,
|
|
121
|
+
tsBinary.factory.createNodeArray([
|
|
122
|
+
tsBinary.factory.createStringLiteral(result)
|
|
123
|
+
])
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
127
|
+
try {
|
|
128
|
+
const importPathWithQuotes = (_a = node == null ? void 0 : node.moduleSpecifier) == null ? void 0 : _a.getText();
|
|
129
|
+
if (!importPathWithQuotes) {
|
|
130
|
+
return node;
|
|
131
|
+
}
|
|
132
|
+
const text = importPathWithQuotes.substring(
|
|
133
|
+
1,
|
|
134
|
+
importPathWithQuotes.length - 1
|
|
135
|
+
);
|
|
136
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
137
|
+
if (!result) {
|
|
138
|
+
return node;
|
|
139
|
+
}
|
|
140
|
+
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
141
|
+
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
142
|
+
let newNode;
|
|
143
|
+
if (tsBinary.isImportDeclaration(node)) {
|
|
144
|
+
newNode = tsBinary.factory.updateImportDeclaration(
|
|
145
|
+
node,
|
|
146
|
+
node.decorators,
|
|
147
|
+
node.modifiers,
|
|
148
|
+
node.importClause,
|
|
149
|
+
moduleSpecifier,
|
|
150
|
+
node.assertClause
|
|
151
|
+
);
|
|
152
|
+
} else {
|
|
153
|
+
newNode = tsBinary.factory.updateExportDeclaration(
|
|
154
|
+
node,
|
|
155
|
+
node.decorators,
|
|
156
|
+
node.modifiers,
|
|
157
|
+
node.isTypeOnly,
|
|
158
|
+
node.exportClause,
|
|
159
|
+
moduleSpecifier,
|
|
160
|
+
node.assertClause
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
newNode.flags = node.flags;
|
|
164
|
+
return newNode;
|
|
165
|
+
} catch {
|
|
166
|
+
return node;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
170
|
+
};
|
|
171
|
+
return tsBinary.visitNode(sf, visitNode);
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function getNotAliasedPath(sf, matcher, text) {
|
|
176
|
+
let result = matcher(text, void 0, void 0, [
|
|
177
|
+
".ts",
|
|
178
|
+
".tsx",
|
|
179
|
+
".js",
|
|
180
|
+
".jsx"
|
|
181
|
+
]);
|
|
182
|
+
if (!result) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (os.platform() === "win32") {
|
|
186
|
+
result = result.replace(/\\/g, "/");
|
|
187
|
+
}
|
|
188
|
+
if (!import_path.default.isAbsolute(result)) {
|
|
189
|
+
if (!result.startsWith(".") && !result.startsWith("..")) {
|
|
190
|
+
try {
|
|
191
|
+
const packagePath = require.resolve(result, {
|
|
192
|
+
paths: [process.cwd(), ...module.paths]
|
|
193
|
+
});
|
|
194
|
+
if (packagePath) {
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
} catch {
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
try {
|
|
201
|
+
const packagePath = require.resolve(text, {
|
|
202
|
+
paths: [process.cwd(), ...module.paths]
|
|
203
|
+
});
|
|
204
|
+
if (packagePath) {
|
|
205
|
+
return text;
|
|
206
|
+
}
|
|
207
|
+
} catch {
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const resolvedPath = import_path.posix.relative((0, import_path.dirname)(sf.fileName), result) || "./";
|
|
211
|
+
return resolvedPath[0] === "." ? resolvedPath : `./${resolvedPath}`;
|
|
212
|
+
}
|
|
213
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
214
|
+
0 && (module.exports = {
|
|
215
|
+
tsconfigPathsBeforeHookFactory
|
|
216
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var typescript_loader_exports = {};
|
|
19
|
+
__export(typescript_loader_exports, {
|
|
20
|
+
TypescriptLoader: () => TypescriptLoader
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(typescript_loader_exports);
|
|
23
|
+
class TypescriptLoader {
|
|
24
|
+
constructor({ appDirectory }) {
|
|
25
|
+
this.appDirectory = appDirectory;
|
|
26
|
+
}
|
|
27
|
+
load() {
|
|
28
|
+
if (this.tsBinary) {
|
|
29
|
+
return this.tsBinary;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const tsPath = require.resolve("typescript", {
|
|
33
|
+
paths: [this.appDirectory || process.cwd()]
|
|
34
|
+
});
|
|
35
|
+
const ts = require(tsPath);
|
|
36
|
+
return ts;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
'TypeScript could not be found! Please, install "typescript" package.'
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
TypescriptLoader
|
|
47
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var src_exports = {};
|
|
20
|
+
__export(src_exports, {
|
|
21
|
+
compile: () => import_common.compile
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(src_exports);
|
|
24
|
+
__reExport(src_exports, require("./compilers/babel"), module.exports);
|
|
25
|
+
var import_common = require("./common");
|
|
26
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
27
|
+
0 && (module.exports = {
|
|
28
|
+
compile
|
|
29
|
+
});
|