@modern-js/server-utils 2.11.0 → 2.11.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/dist/cjs/compilers/typescript/index.js +8 -2
- package/dist/esm/compilers/typescript/index.js +31 -3
- package/dist/esm-node/compilers/typescript/index.js +8 -2
- package/dist/js/modern/common/index.js +55 -0
- package/dist/js/modern/compilers/babel/index.js +149 -0
- package/dist/js/modern/compilers/typescript/index.js +123 -0
- package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +187 -0
- package/dist/js/modern/compilers/typescript/typescript-loader.js +24 -0
- package/dist/js/modern/index.js +5 -0
- package/dist/js/node/common/index.js +85 -0
- package/dist/js/node/compilers/babel/index.js +178 -0
- package/dist/js/node/compilers/typescript/index.js +150 -0
- package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +216 -0
- package/dist/js/node/compilers/typescript/typescript-loader.js +47 -0
- package/dist/js/node/index.js +29 -0
- package/dist/js/treeshaking/common/index.js +204 -0
- package/dist/js/treeshaking/compilers/babel/index.js +368 -0
- package/dist/js/treeshaking/compilers/typescript/index.js +339 -0
- package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +246 -0
- package/dist/js/treeshaking/compilers/typescript/typescript-loader.js +65 -0
- package/dist/js/treeshaking/index.js +3 -0
- package/package.json +1 -1
|
@@ -97,11 +97,17 @@ const compileByTs = async (appDirectory, config, compileOptions) => {
|
|
|
97
97
|
before: [tsconfigPathsPlugin]
|
|
98
98
|
});
|
|
99
99
|
const allDiagnostics = ts2.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
100
|
+
const { noEmitOnError } = options;
|
|
100
101
|
if (allDiagnostics.length > 0) {
|
|
101
102
|
import_utils.logger.error(
|
|
102
|
-
ts2.formatDiagnosticsWithColorAndContext(
|
|
103
|
+
ts2.formatDiagnosticsWithColorAndContext(
|
|
104
|
+
[...new Set(allDiagnostics)],
|
|
105
|
+
formatHost
|
|
106
|
+
)
|
|
103
107
|
);
|
|
104
|
-
|
|
108
|
+
if (typeof noEmitOnError === "undefined" || noEmitOnError === false) {
|
|
109
|
+
process.exit(1);
|
|
110
|
+
}
|
|
105
111
|
}
|
|
106
112
|
for (const source of sourceDirs) {
|
|
107
113
|
await copyFiles(source, distDir, appDirectory, tsconfigPath);
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
function _arrayLikeToArray(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
+
return arr2;
|
|
5
|
+
}
|
|
6
|
+
function _arrayWithoutHoles(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
8
|
+
}
|
|
1
9
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
10
|
try {
|
|
3
11
|
var info = gen[key](arg);
|
|
@@ -40,6 +48,12 @@ function _defineProperty(obj, key, value) {
|
|
|
40
48
|
}
|
|
41
49
|
return obj;
|
|
42
50
|
}
|
|
51
|
+
function _iterableToArray(iter) {
|
|
52
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
53
|
+
}
|
|
54
|
+
function _nonIterableSpread() {
|
|
55
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
56
|
+
}
|
|
43
57
|
function _objectSpread(target) {
|
|
44
58
|
for(var i = 1; i < arguments.length; i++){
|
|
45
59
|
var source = arguments[i] != null ? arguments[i] : {};
|
|
@@ -55,6 +69,17 @@ function _objectSpread(target) {
|
|
|
55
69
|
}
|
|
56
70
|
return target;
|
|
57
71
|
}
|
|
72
|
+
function _toConsumableArray(arr) {
|
|
73
|
+
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
74
|
+
}
|
|
75
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
76
|
+
if (!o) return;
|
|
77
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
78
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
79
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
80
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
81
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
82
|
+
}
|
|
58
83
|
var __generator = this && this.__generator || function(thisArg, body) {
|
|
59
84
|
var f, y, t, g, _ = {
|
|
60
85
|
label: 0,
|
|
@@ -207,7 +232,7 @@ var copyFiles = function() {
|
|
|
207
232
|
}();
|
|
208
233
|
var compileByTs = function() {
|
|
209
234
|
var _ref = _asyncToGenerator(function(appDirectory, config, compileOptions) {
|
|
210
|
-
var sourceDirs, distDir, tsconfigPath, ts2, createProgram, formatHost, alias, aliasOption, _aliasOption_paths, paths, _aliasOption_absoluteBaseUrl, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, sourcePosixPaths, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, source, err;
|
|
235
|
+
var sourceDirs, distDir, tsconfigPath, ts2, createProgram, formatHost, alias, aliasOption, _aliasOption_paths, paths, _aliasOption_absoluteBaseUrl, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, sourcePosixPaths, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, noEmitOnError, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, source, err;
|
|
211
236
|
return __generator(this, function(_state) {
|
|
212
237
|
switch(_state.label){
|
|
213
238
|
case 0:
|
|
@@ -253,9 +278,12 @@ var compileByTs = function() {
|
|
|
253
278
|
]
|
|
254
279
|
});
|
|
255
280
|
allDiagnostics = ts2.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
281
|
+
noEmitOnError = options.noEmitOnError;
|
|
256
282
|
if (allDiagnostics.length > 0) {
|
|
257
|
-
logger.error(ts2.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
|
|
258
|
-
|
|
283
|
+
logger.error(ts2.formatDiagnosticsWithColorAndContext(_toConsumableArray(new Set(allDiagnostics)), formatHost));
|
|
284
|
+
if (typeof noEmitOnError === "undefined" || noEmitOnError === false) {
|
|
285
|
+
process.exit(1);
|
|
286
|
+
}
|
|
259
287
|
}
|
|
260
288
|
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
261
289
|
_state.label = 1;
|
|
@@ -65,11 +65,17 @@ const compileByTs = async (appDirectory, config, compileOptions) => {
|
|
|
65
65
|
before: [tsconfigPathsPlugin]
|
|
66
66
|
});
|
|
67
67
|
const allDiagnostics = ts2.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
68
|
+
const { noEmitOnError } = options;
|
|
68
69
|
if (allDiagnostics.length > 0) {
|
|
69
70
|
logger.error(
|
|
70
|
-
ts2.formatDiagnosticsWithColorAndContext(
|
|
71
|
+
ts2.formatDiagnosticsWithColorAndContext(
|
|
72
|
+
[...new Set(allDiagnostics)],
|
|
73
|
+
formatHost
|
|
74
|
+
)
|
|
71
75
|
);
|
|
72
|
-
|
|
76
|
+
if (typeof noEmitOnError === "undefined" || noEmitOnError === false) {
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
73
79
|
}
|
|
74
80
|
for (const source of sourceDirs) {
|
|
75
81
|
await copyFiles(source, distDir, appDirectory, tsconfigPath);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import * as path from "path";
|
|
22
|
+
import { fs } from "@modern-js/utils";
|
|
23
|
+
import { compileByTs } from "../compilers/typescript";
|
|
24
|
+
import { compileByBabel } from "../compilers/babel";
|
|
25
|
+
const FILE_EXTENSIONS = [".js", ".ts", ".mjs", ".ejs"];
|
|
26
|
+
const validateAbsolutePath = (filename, message) => {
|
|
27
|
+
if (!path.isAbsolute(filename)) {
|
|
28
|
+
throw new Error(message);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const validateAbsolutePaths = (filenames, messageFunc) => {
|
|
32
|
+
filenames.forEach(
|
|
33
|
+
(filename) => validateAbsolutePath(filename, messageFunc(filename))
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
const compile = (appDirectory, modernConfig, compileOptions) => __async(void 0, null, function* () {
|
|
37
|
+
var _a;
|
|
38
|
+
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
39
|
+
validateAbsolutePaths(
|
|
40
|
+
sourceDirs,
|
|
41
|
+
(dir) => `source dir ${dir} is not an absolute path.`
|
|
42
|
+
);
|
|
43
|
+
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
44
|
+
const compiler = (_a = modernConfig == null ? void 0 : modernConfig.server) == null ? void 0 : _a.compiler;
|
|
45
|
+
const isTsProject = tsconfigPath && (yield fs.pathExists(tsconfigPath));
|
|
46
|
+
if (!isTsProject || compiler === "babel") {
|
|
47
|
+
yield compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
48
|
+
} else {
|
|
49
|
+
yield compileByTs(appDirectory, modernConfig, compileOptions);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
export {
|
|
53
|
+
FILE_EXTENSIONS,
|
|
54
|
+
compile
|
|
55
|
+
};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
import * as path from "path";
|
|
41
|
+
import {
|
|
42
|
+
getBabelChain,
|
|
43
|
+
applyUserBabelConfig
|
|
44
|
+
} from "@modern-js/babel-preset-lib";
|
|
45
|
+
import { fs, json5, getAliasConfig } from "@modern-js/utils";
|
|
46
|
+
import { compiler } from "@modern-js/babel-compiler";
|
|
47
|
+
import { FILE_EXTENSIONS } from "../../common";
|
|
48
|
+
export * from "@babel/core";
|
|
49
|
+
const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
50
|
+
if (!fs.existsSync(tsconfigPath)) {
|
|
51
|
+
return noExistReturn;
|
|
52
|
+
}
|
|
53
|
+
const content = fs.readFileSync(tsconfigPath, "utf-8");
|
|
54
|
+
return json5.parse(content);
|
|
55
|
+
};
|
|
56
|
+
const existTsConfigFile = (tsconfigAbsolutePath) => {
|
|
57
|
+
const tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
58
|
+
return Boolean(tsconfig);
|
|
59
|
+
};
|
|
60
|
+
const getBabelConfig = (libPresetOption, syntaxOption) => {
|
|
61
|
+
const chain = getBabelChain(libPresetOption, syntaxOption);
|
|
62
|
+
return __spreadValues({
|
|
63
|
+
sourceType: "unambiguous"
|
|
64
|
+
}, chain.toJSON());
|
|
65
|
+
};
|
|
66
|
+
const resolveBabelConfig = (appDirectory, config, option) => {
|
|
67
|
+
const { globalVars, alias, babelConfig, define } = config;
|
|
68
|
+
const globalDefineVars = define && Object.entries(define).reduce((object, [key, value]) => {
|
|
69
|
+
object[key] = JSON.stringify(value);
|
|
70
|
+
return object;
|
|
71
|
+
}, {});
|
|
72
|
+
const aliasConfig = getAliasConfig(alias, __spreadValues({
|
|
73
|
+
appDirectory
|
|
74
|
+
}, option));
|
|
75
|
+
const babelChain = getBabelChain(
|
|
76
|
+
{
|
|
77
|
+
appDirectory,
|
|
78
|
+
enableReactPreset: true,
|
|
79
|
+
enableTypescriptPreset: true,
|
|
80
|
+
alias: aliasConfig,
|
|
81
|
+
envVars: [],
|
|
82
|
+
globalVars: __spreadValues(__spreadValues({}, globalVars), globalDefineVars)
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
type: option.type,
|
|
86
|
+
syntax: option.syntax
|
|
87
|
+
}
|
|
88
|
+
);
|
|
89
|
+
const envOptions = babelChain.preset("@babel/preset-env").options();
|
|
90
|
+
babelChain.preset("@babel/preset-env").use(require.resolve("@babel/preset-env"), [
|
|
91
|
+
__spreadProps(__spreadValues({}, envOptions[0]), {
|
|
92
|
+
loose: true
|
|
93
|
+
})
|
|
94
|
+
]);
|
|
95
|
+
babelChain.plugin("babel-plugin-transform-typescript-metadata").use(
|
|
96
|
+
require.resolve("babel-plugin-transform-typescript-metadata"),
|
|
97
|
+
[]
|
|
98
|
+
);
|
|
99
|
+
babelChain.plugin("@babel/plugin-proposal-decorators").use(require.resolve("@babel/plugin-proposal-decorators"), [
|
|
100
|
+
{ legacy: true }
|
|
101
|
+
]);
|
|
102
|
+
babelChain.plugin("@babel/plugin-proposal-class-properties").use(require.resolve("@babel/plugin-proposal-class-properties"), [
|
|
103
|
+
{
|
|
104
|
+
loose: true
|
|
105
|
+
}
|
|
106
|
+
]);
|
|
107
|
+
const internalBabelConfig = __spreadValues({}, babelChain.toJSON());
|
|
108
|
+
return applyUserBabelConfig(internalBabelConfig, babelConfig);
|
|
109
|
+
};
|
|
110
|
+
const compileByBabel = (appDirectory, config, compileOptions) => __async(void 0, null, function* () {
|
|
111
|
+
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
112
|
+
const results = yield Promise.all(
|
|
113
|
+
sourceDirs.map((sourceDir) => __async(void 0, null, function* () {
|
|
114
|
+
const babelConfig = resolveBabelConfig(appDirectory, config, {
|
|
115
|
+
tsconfigPath: tsconfigPath ? tsconfigPath : "",
|
|
116
|
+
syntax: "es6+",
|
|
117
|
+
type: "commonjs"
|
|
118
|
+
});
|
|
119
|
+
if (yield fs.pathExists(sourceDir)) {
|
|
120
|
+
const basename = path.basename(sourceDir);
|
|
121
|
+
const targetDir = path.join(distDir, basename);
|
|
122
|
+
yield fs.copy(sourceDir, targetDir, {
|
|
123
|
+
filter: (src) => ![".ts", ".js"].includes(path.extname(src)) && src !== tsconfigPath
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return compiler(
|
|
127
|
+
{
|
|
128
|
+
rootDir: appDirectory,
|
|
129
|
+
distDir,
|
|
130
|
+
sourceDir,
|
|
131
|
+
extensions: FILE_EXTENSIONS
|
|
132
|
+
},
|
|
133
|
+
babelConfig
|
|
134
|
+
);
|
|
135
|
+
}))
|
|
136
|
+
);
|
|
137
|
+
results.forEach((result) => {
|
|
138
|
+
if (result.code === 1) {
|
|
139
|
+
throw new Error(result.message);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
export {
|
|
144
|
+
compileByBabel,
|
|
145
|
+
existTsConfigFile,
|
|
146
|
+
getBabelConfig,
|
|
147
|
+
readTsConfig,
|
|
148
|
+
resolveBabelConfig
|
|
149
|
+
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __async = (__this, __arguments, generator) => {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
var fulfilled = (value) => {
|
|
20
|
+
try {
|
|
21
|
+
step(generator.next(value));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var rejected = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.throw(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
34
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
import path from "path";
|
|
38
|
+
import { logger, getAliasConfig, fs } from "@modern-js/utils";
|
|
39
|
+
import ts from "typescript";
|
|
40
|
+
import { TypescriptLoader } from "./typescript-loader";
|
|
41
|
+
import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
|
|
42
|
+
const readTsConfigByFile = (tsConfigFile) => {
|
|
43
|
+
const parsedCmd = ts.getParsedCommandLineOfConfigFile(
|
|
44
|
+
tsConfigFile,
|
|
45
|
+
void 0,
|
|
46
|
+
ts.sys
|
|
47
|
+
);
|
|
48
|
+
const { options, fileNames, projectReferences } = parsedCmd;
|
|
49
|
+
return { options, fileNames, projectReferences };
|
|
50
|
+
};
|
|
51
|
+
const copyFiles = (from, to, tsconfigPath) => __async(void 0, null, function* () {
|
|
52
|
+
if (yield fs.pathExists(from)) {
|
|
53
|
+
const basename = path.basename(from);
|
|
54
|
+
const targetDir = path.join(to, basename);
|
|
55
|
+
yield fs.copy(from, targetDir, {
|
|
56
|
+
filter: (src) => ![".ts"].includes(path.extname(src)) && src !== tsconfigPath
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const compileByTs = (appDirectory, config, compileOptions) => __async(void 0, null, function* () {
|
|
61
|
+
logger.info(`Running ts compile...`);
|
|
62
|
+
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
63
|
+
if (!tsconfigPath) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const ts2 = new TypescriptLoader({
|
|
67
|
+
appDirectory
|
|
68
|
+
}).load();
|
|
69
|
+
const createProgram = ts2.createIncrementalProgram || ts2.createProgram;
|
|
70
|
+
const formatHost = getFormatHost(ts2);
|
|
71
|
+
const { alias } = config;
|
|
72
|
+
const aliasOption = getAliasConfig(alias, {
|
|
73
|
+
appDirectory,
|
|
74
|
+
tsconfigPath
|
|
75
|
+
});
|
|
76
|
+
const { paths = {}, absoluteBaseUrl = "./" } = aliasOption;
|
|
77
|
+
const { options, fileNames, projectReferences } = readTsConfigByFile(tsconfigPath);
|
|
78
|
+
const sourcePosixPaths = sourceDirs.map(
|
|
79
|
+
(sourceDir) => sourceDir.split(path.sep).join(path.posix.sep)
|
|
80
|
+
);
|
|
81
|
+
const rootNames = fileNames.filter((fileName) => {
|
|
82
|
+
return fileName.endsWith(".d.ts") || sourcePosixPaths.some((sourceDir) => {
|
|
83
|
+
return fileName.includes(sourceDir);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
const program = createProgram.call(ts2, {
|
|
87
|
+
rootNames,
|
|
88
|
+
projectReferences,
|
|
89
|
+
options: __spreadValues({
|
|
90
|
+
rootDir: appDirectory,
|
|
91
|
+
outDir: distDir
|
|
92
|
+
}, options)
|
|
93
|
+
});
|
|
94
|
+
const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(
|
|
95
|
+
ts2,
|
|
96
|
+
absoluteBaseUrl,
|
|
97
|
+
paths
|
|
98
|
+
);
|
|
99
|
+
const emitResult = program.emit(void 0, void 0, void 0, void 0, {
|
|
100
|
+
before: [tsconfigPathsPlugin]
|
|
101
|
+
});
|
|
102
|
+
const allDiagnostics = ts2.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
103
|
+
if (allDiagnostics.length > 0) {
|
|
104
|
+
logger.error(
|
|
105
|
+
ts2.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)
|
|
106
|
+
);
|
|
107
|
+
process.exit(1);
|
|
108
|
+
}
|
|
109
|
+
for (const source of sourceDirs) {
|
|
110
|
+
yield copyFiles(source, distDir, tsconfigPath);
|
|
111
|
+
}
|
|
112
|
+
logger.info(`Ts compile succeed`);
|
|
113
|
+
});
|
|
114
|
+
const getFormatHost = (ts2) => {
|
|
115
|
+
return {
|
|
116
|
+
getCanonicalFileName: (path2) => path2,
|
|
117
|
+
getCurrentDirectory: ts2.sys.getCurrentDirectory,
|
|
118
|
+
getNewLine: () => ts2.sys.newLine
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
export {
|
|
122
|
+
compileByTs
|
|
123
|
+
};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import * as os from "os";
|
|
2
|
+
import path, { dirname, posix } from "path";
|
|
3
|
+
import * as ts from "typescript";
|
|
4
|
+
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
5
|
+
const isRegExpKey = (str) => {
|
|
6
|
+
return str.startsWith("^") || str.endsWith("$");
|
|
7
|
+
};
|
|
8
|
+
const resolveAliasPath = (baseUrl, filePath) => {
|
|
9
|
+
if (filePath.startsWith(".") || filePath.startsWith("..")) {
|
|
10
|
+
return path.resolve(baseUrl, filePath);
|
|
11
|
+
}
|
|
12
|
+
return filePath;
|
|
13
|
+
};
|
|
14
|
+
const createAliasMatcher = (baseUrl, alias) => {
|
|
15
|
+
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
16
|
+
if (isRegExpKey(key)) {
|
|
17
|
+
const regexp = new RegExp(key);
|
|
18
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
19
|
+
o.push([regexp, aliasPath]);
|
|
20
|
+
} else {
|
|
21
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
22
|
+
o.push([key, aliasPath]);
|
|
23
|
+
}
|
|
24
|
+
return o;
|
|
25
|
+
}, []);
|
|
26
|
+
const cacheMap = /* @__PURE__ */ new Map();
|
|
27
|
+
return (requestedModule) => {
|
|
28
|
+
if (cacheMap.has(requestedModule)) {
|
|
29
|
+
return cacheMap.get(requestedModule);
|
|
30
|
+
}
|
|
31
|
+
for (const [key, value] of aliasPairs) {
|
|
32
|
+
if (key instanceof RegExp) {
|
|
33
|
+
if (key.test(requestedModule)) {
|
|
34
|
+
cacheMap.set(requestedModule, value);
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (requestedModule === key) {
|
|
39
|
+
cacheMap.set(requestedModule, value);
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
const isDynamicImport = (tsBinary, node) => {
|
|
46
|
+
return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
47
|
+
};
|
|
48
|
+
function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
49
|
+
const tsPaths = {};
|
|
50
|
+
const alias = {};
|
|
51
|
+
Object.keys(paths).forEach((key) => {
|
|
52
|
+
if (Array.isArray(paths[key])) {
|
|
53
|
+
tsPaths[key] = paths[key];
|
|
54
|
+
} else {
|
|
55
|
+
alias[key] = paths[key];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
59
|
+
const matchTsPath = createMatchPath(baseUrl, tsPaths, ["main"]);
|
|
60
|
+
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
61
|
+
const result = matchTsPath(
|
|
62
|
+
requestedModule,
|
|
63
|
+
readJSONSync,
|
|
64
|
+
fileExists,
|
|
65
|
+
extensions
|
|
66
|
+
);
|
|
67
|
+
if (result) {
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
return matchAliasPath(requestedModule);
|
|
71
|
+
};
|
|
72
|
+
if (Object.keys(paths).length === 0) {
|
|
73
|
+
return void 0;
|
|
74
|
+
}
|
|
75
|
+
return (ctx) => {
|
|
76
|
+
return (sf) => {
|
|
77
|
+
const visitNode = (node) => {
|
|
78
|
+
var _a;
|
|
79
|
+
if (isDynamicImport(tsBinary, node)) {
|
|
80
|
+
const importPathWithQuotes = node.arguments[0].getText(sf);
|
|
81
|
+
const text = importPathWithQuotes.slice(
|
|
82
|
+
1,
|
|
83
|
+
importPathWithQuotes.length - 1
|
|
84
|
+
);
|
|
85
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
86
|
+
if (!result) {
|
|
87
|
+
return node;
|
|
88
|
+
}
|
|
89
|
+
return tsBinary.factory.updateCallExpression(
|
|
90
|
+
node,
|
|
91
|
+
node.expression,
|
|
92
|
+
node.typeArguments,
|
|
93
|
+
tsBinary.factory.createNodeArray([
|
|
94
|
+
tsBinary.factory.createStringLiteral(result)
|
|
95
|
+
])
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
99
|
+
try {
|
|
100
|
+
const importPathWithQuotes = (_a = node == null ? void 0 : node.moduleSpecifier) == null ? void 0 : _a.getText();
|
|
101
|
+
if (!importPathWithQuotes) {
|
|
102
|
+
return node;
|
|
103
|
+
}
|
|
104
|
+
const text = importPathWithQuotes.substring(
|
|
105
|
+
1,
|
|
106
|
+
importPathWithQuotes.length - 1
|
|
107
|
+
);
|
|
108
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
109
|
+
if (!result) {
|
|
110
|
+
return node;
|
|
111
|
+
}
|
|
112
|
+
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
113
|
+
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
114
|
+
let newNode;
|
|
115
|
+
if (tsBinary.isImportDeclaration(node)) {
|
|
116
|
+
newNode = tsBinary.factory.updateImportDeclaration(
|
|
117
|
+
node,
|
|
118
|
+
node.decorators,
|
|
119
|
+
node.modifiers,
|
|
120
|
+
node.importClause,
|
|
121
|
+
moduleSpecifier,
|
|
122
|
+
node.assertClause
|
|
123
|
+
);
|
|
124
|
+
} else {
|
|
125
|
+
newNode = tsBinary.factory.updateExportDeclaration(
|
|
126
|
+
node,
|
|
127
|
+
node.decorators,
|
|
128
|
+
node.modifiers,
|
|
129
|
+
node.isTypeOnly,
|
|
130
|
+
node.exportClause,
|
|
131
|
+
moduleSpecifier,
|
|
132
|
+
node.assertClause
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
newNode.flags = node.flags;
|
|
136
|
+
return newNode;
|
|
137
|
+
} catch (e) {
|
|
138
|
+
return node;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
142
|
+
};
|
|
143
|
+
return tsBinary.visitNode(sf, visitNode);
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function getNotAliasedPath(sf, matcher, text) {
|
|
148
|
+
let result = matcher(text, void 0, void 0, [
|
|
149
|
+
".ts",
|
|
150
|
+
".tsx",
|
|
151
|
+
".js",
|
|
152
|
+
".jsx"
|
|
153
|
+
]);
|
|
154
|
+
if (!result) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (os.platform() === "win32") {
|
|
158
|
+
result = result.replace(/\\/g, "/");
|
|
159
|
+
}
|
|
160
|
+
if (!path.isAbsolute(result)) {
|
|
161
|
+
if (!result.startsWith(".") && !result.startsWith("..")) {
|
|
162
|
+
try {
|
|
163
|
+
const packagePath = require.resolve(result, {
|
|
164
|
+
paths: [process.cwd(), ...module.paths]
|
|
165
|
+
});
|
|
166
|
+
if (packagePath) {
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
} catch (e) {
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
const packagePath = require.resolve(text, {
|
|
174
|
+
paths: [process.cwd(), ...module.paths]
|
|
175
|
+
});
|
|
176
|
+
if (packagePath) {
|
|
177
|
+
return text;
|
|
178
|
+
}
|
|
179
|
+
} catch (e) {
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const resolvedPath = posix.relative(dirname(sf.fileName), result) || "./";
|
|
183
|
+
return resolvedPath[0] === "." ? resolvedPath : `./${resolvedPath}`;
|
|
184
|
+
}
|
|
185
|
+
export {
|
|
186
|
+
tsconfigPathsBeforeHookFactory
|
|
187
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class TypescriptLoader {
|
|
2
|
+
constructor({ appDirectory }) {
|
|
3
|
+
this.appDirectory = appDirectory;
|
|
4
|
+
}
|
|
5
|
+
load() {
|
|
6
|
+
if (this.tsBinary) {
|
|
7
|
+
return this.tsBinary;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const tsPath = require.resolve("typescript", {
|
|
11
|
+
paths: [this.appDirectory || process.cwd()]
|
|
12
|
+
});
|
|
13
|
+
const ts = require(tsPath);
|
|
14
|
+
return ts;
|
|
15
|
+
} catch (error) {
|
|
16
|
+
throw new Error(
|
|
17
|
+
'TypeScript could not be found! Please, install "typescript" package.'
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
TypescriptLoader
|
|
24
|
+
};
|