@modern-js/server-utils 1.17.0 → 1.18.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 +32 -0
- package/dist/js/modern/common/index.js +35 -0
- package/dist/js/modern/{babel.js → compilers/babel/index.js} +37 -0
- package/dist/js/modern/compilers/typescript/index.js +107 -0
- package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +184 -0
- package/dist/js/modern/compilers/typescript/typescript-loader.js +32 -0
- package/dist/js/modern/index.js +2 -1
- package/dist/js/node/common/index.js +53 -0
- package/dist/js/node/{babel.js → compilers/babel/index.js} +51 -3
- package/dist/js/node/compilers/typescript/index.js +127 -0
- package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +199 -0
- package/dist/js/node/compilers/typescript/typescript-loader.js +41 -0
- package/dist/js/node/index.js +14 -2
- package/dist/js/treeshaking/common/index.js +79 -0
- package/dist/js/treeshaking/compilers/babel/index.js +159 -0
- package/dist/js/treeshaking/compilers/typescript/index.js +180 -0
- package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +202 -0
- package/dist/js/treeshaking/compilers/typescript/typescript-loader.js +39 -0
- package/dist/js/treeshaking/index.js +2 -1
- package/dist/types/common/index.d.ts +14 -0
- package/dist/types/{babel.d.ts → compilers/babel/index.d.ts} +3 -1
- package/dist/types/compilers/typescript/index.d.ts +2 -0
- package/dist/types/compilers/typescript/tsconfig-paths-plugin.d.ts +2 -0
- package/dist/types/compilers/typescript/typescript-loader.d.ts +11 -0
- package/dist/types/index.d.ts +2 -1
- package/package.json +11 -8
- package/dist/js/treeshaking/babel.js +0 -74
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.compileByTs = void 0;
|
|
7
|
+
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
|
+
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
var _typescript = _interopRequireDefault(require("typescript"));
|
|
13
|
+
|
|
14
|
+
var _typescriptLoader = require("./typescript-loader");
|
|
15
|
+
|
|
16
|
+
var _tsconfigPathsPlugin = require("./tsconfig-paths-plugin");
|
|
17
|
+
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
+
|
|
20
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
21
|
+
|
|
22
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
23
|
+
|
|
24
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
25
|
+
|
|
26
|
+
const readTsConfigByFile = tsConfigFile => {
|
|
27
|
+
const parsedCmd = _typescript.default.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, _typescript.default.sys);
|
|
28
|
+
|
|
29
|
+
const {
|
|
30
|
+
options,
|
|
31
|
+
fileNames,
|
|
32
|
+
projectReferences
|
|
33
|
+
} = parsedCmd;
|
|
34
|
+
return {
|
|
35
|
+
options,
|
|
36
|
+
fileNames,
|
|
37
|
+
projectReferences
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const copyFiles = async (from, to, tsconfigPath) => {
|
|
42
|
+
if (await _utils.fs.pathExists(from)) {
|
|
43
|
+
const basename = _path.default.basename(from);
|
|
44
|
+
|
|
45
|
+
const targetDir = _path.default.join(to, basename);
|
|
46
|
+
|
|
47
|
+
await _utils.fs.copy(from, targetDir, {
|
|
48
|
+
filter: src => !['.ts'].includes(_path.default.extname(src)) && src !== tsconfigPath
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const compileByTs = async (appDirectory, modernConfig, compileOptions) => {
|
|
54
|
+
_utils.logger.info(`Running ts compile...`);
|
|
55
|
+
|
|
56
|
+
const {
|
|
57
|
+
sourceDirs,
|
|
58
|
+
distDir,
|
|
59
|
+
tsconfigPath
|
|
60
|
+
} = compileOptions;
|
|
61
|
+
|
|
62
|
+
if (!tsconfigPath) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const ts = new _typescriptLoader.TypescriptLoader({
|
|
67
|
+
appDirectory
|
|
68
|
+
}).load();
|
|
69
|
+
const createProgram = ts.createIncrementalProgram || ts.createProgram;
|
|
70
|
+
const formatHost = getFormatHost(ts);
|
|
71
|
+
const {
|
|
72
|
+
alias
|
|
73
|
+
} = modernConfig.source;
|
|
74
|
+
const aliasOption = (0, _utils.getAlias)(alias || {}, {
|
|
75
|
+
appDirectory,
|
|
76
|
+
tsconfigPath
|
|
77
|
+
});
|
|
78
|
+
const {
|
|
79
|
+
paths = {},
|
|
80
|
+
absoluteBaseUrl = './'
|
|
81
|
+
} = aliasOption;
|
|
82
|
+
const {
|
|
83
|
+
options,
|
|
84
|
+
fileNames,
|
|
85
|
+
projectReferences
|
|
86
|
+
} = readTsConfigByFile(tsconfigPath);
|
|
87
|
+
const sourcePosixPaths = sourceDirs.map(sourceDir => sourceDir.split(_path.default.sep).join(_path.default.posix.sep));
|
|
88
|
+
const rootNames = fileNames.filter(fileName => {
|
|
89
|
+
return fileName.endsWith('.d.ts') || sourcePosixPaths.some(sourceDir => {
|
|
90
|
+
return fileName.includes(sourceDir);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
const program = createProgram.call(ts, {
|
|
94
|
+
rootNames,
|
|
95
|
+
projectReferences,
|
|
96
|
+
options: _objectSpread({
|
|
97
|
+
rootDir: appDirectory,
|
|
98
|
+
outDir: distDir
|
|
99
|
+
}, options)
|
|
100
|
+
});
|
|
101
|
+
const tsconfigPathsPlugin = (0, _tsconfigPathsPlugin.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths);
|
|
102
|
+
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
103
|
+
before: [tsconfigPathsPlugin]
|
|
104
|
+
});
|
|
105
|
+
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
106
|
+
|
|
107
|
+
if (allDiagnostics.length > 0) {
|
|
108
|
+
_utils.logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)); // eslint-disable-next-line no-process-exit
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
process.exit(1);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
for (const source of sourceDirs) {
|
|
115
|
+
await copyFiles(source, distDir, tsconfigPath);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
exports.compileByTs = compileByTs;
|
|
120
|
+
|
|
121
|
+
const getFormatHost = ts => {
|
|
122
|
+
return {
|
|
123
|
+
getCanonicalFileName: path => path,
|
|
124
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
125
|
+
getNewLine: () => ts.sys.newLine
|
|
126
|
+
};
|
|
127
|
+
};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.tsconfigPathsBeforeHookFactory = tsconfigPathsBeforeHookFactory;
|
|
7
|
+
|
|
8
|
+
var os = _interopRequireWildcard(require("os"));
|
|
9
|
+
|
|
10
|
+
var _path = _interopRequireWildcard(require("path"));
|
|
11
|
+
|
|
12
|
+
var ts = _interopRequireWildcard(require("typescript"));
|
|
13
|
+
|
|
14
|
+
var _tsconfigPaths = require("tsconfig-paths");
|
|
15
|
+
|
|
16
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
+
|
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
+
|
|
20
|
+
const isRegExpKey = str => {
|
|
21
|
+
return str.startsWith('^') || str.endsWith('$');
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const resolveAliasPath = (baseUrl, filePath) => {
|
|
25
|
+
// exclude absolute path and alias
|
|
26
|
+
if (filePath.startsWith('.') || filePath.startsWith('..')) {
|
|
27
|
+
return _path.default.resolve(baseUrl, filePath);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return filePath;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const createAliasMatcher = (baseUrl, alias) => {
|
|
34
|
+
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
35
|
+
if (isRegExpKey(key)) {
|
|
36
|
+
const regexp = new RegExp(key);
|
|
37
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
38
|
+
o.push([regexp, aliasPath]);
|
|
39
|
+
} else {
|
|
40
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
41
|
+
o.push([key, aliasPath]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return o;
|
|
45
|
+
}, []);
|
|
46
|
+
const cacheMap = new Map(); // eslint-disable-next-line consistent-return
|
|
47
|
+
|
|
48
|
+
return requestedModule => {
|
|
49
|
+
if (cacheMap.has(requestedModule)) {
|
|
50
|
+
return cacheMap.get(requestedModule);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
for (const [key, value] of aliasPairs) {
|
|
54
|
+
if (key instanceof RegExp) {
|
|
55
|
+
if (key.test(requestedModule)) {
|
|
56
|
+
cacheMap.set(requestedModule, value);
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (requestedModule === key) {
|
|
62
|
+
cacheMap.set(requestedModule, value);
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const isDynamicImport = (tsBinary, node) => {
|
|
70
|
+
return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
74
|
+
const tsPaths = {};
|
|
75
|
+
const alias = {};
|
|
76
|
+
Object.keys(paths).forEach(key => {
|
|
77
|
+
if (Array.isArray(paths[key])) {
|
|
78
|
+
tsPaths[key] = paths[key];
|
|
79
|
+
} else {
|
|
80
|
+
alias[key] = paths[key];
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
84
|
+
const matchTsPath = (0, _tsconfigPaths.createMatchPath)(baseUrl, tsPaths, ['main']);
|
|
85
|
+
|
|
86
|
+
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
87
|
+
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
88
|
+
|
|
89
|
+
if (result) {
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return matchAliasPath(requestedModule);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
if (Object.keys(paths).length === 0) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return ctx => {
|
|
101
|
+
return sf => {
|
|
102
|
+
const visitNode = node => {
|
|
103
|
+
if (isDynamicImport(tsBinary, node)) {
|
|
104
|
+
const importPathWithQuotes = node.arguments[0].getText(sf);
|
|
105
|
+
const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
|
|
106
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
107
|
+
|
|
108
|
+
if (!result) {
|
|
109
|
+
return node;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([tsBinary.factory.createStringLiteral(result)]));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
116
|
+
try {
|
|
117
|
+
var _node$moduleSpecifier;
|
|
118
|
+
|
|
119
|
+
const importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
|
|
120
|
+
|
|
121
|
+
if (!importPathWithQuotes) {
|
|
122
|
+
return node;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
126
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
127
|
+
|
|
128
|
+
if (!result) {
|
|
129
|
+
return node;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
133
|
+
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
134
|
+
|
|
135
|
+
if (tsBinary.isImportDeclaration(node)) {
|
|
136
|
+
return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
137
|
+
} else {
|
|
138
|
+
return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
139
|
+
}
|
|
140
|
+
} catch (_unused) {
|
|
141
|
+
return node;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
return tsBinary.visitNode(sf, visitNode);
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
} // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
function getNotAliasedPath(sf, matcher, text) {
|
|
155
|
+
let result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
|
|
156
|
+
|
|
157
|
+
if (!result) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (os.platform() === 'win32') {
|
|
162
|
+
result = result.replace(/\\/g, '/');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!_path.default.isAbsolute(result)) {
|
|
166
|
+
// handle alias to alias
|
|
167
|
+
if (!result.startsWith('.') && !result.startsWith('..')) {
|
|
168
|
+
try {
|
|
169
|
+
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
170
|
+
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
171
|
+
const packagePath = require.resolve(result, {
|
|
172
|
+
paths: [process.cwd(), ...module.paths]
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
if (packagePath) {
|
|
176
|
+
// eslint-disable-next-line consistent-return
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
} catch (_unused2) {}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
184
|
+
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
185
|
+
const packagePath = require.resolve(text, {
|
|
186
|
+
paths: [process.cwd(), ...module.paths]
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
if (packagePath) {
|
|
190
|
+
// eslint-disable-next-line consistent-return
|
|
191
|
+
return text;
|
|
192
|
+
}
|
|
193
|
+
} catch (_unused3) {}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const resolvedPath = _path.posix.relative((0, _path.dirname)(sf.fileName), result) || './'; // eslint-disable-next-line consistent-return
|
|
197
|
+
|
|
198
|
+
return resolvedPath[0] === '.' ? resolvedPath : `./${resolvedPath}`;
|
|
199
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.TypescriptLoader = void 0;
|
|
7
|
+
|
|
8
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
9
|
+
|
|
10
|
+
class TypescriptLoader {
|
|
11
|
+
constructor({
|
|
12
|
+
appDirectory
|
|
13
|
+
}) {
|
|
14
|
+
_defineProperty(this, "tsBinary", void 0);
|
|
15
|
+
|
|
16
|
+
_defineProperty(this, "appDirectory", void 0);
|
|
17
|
+
|
|
18
|
+
this.appDirectory = appDirectory;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
load() {
|
|
22
|
+
if (this.tsBinary) {
|
|
23
|
+
return this.tsBinary;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const tsPath = require.resolve('typescript', {
|
|
28
|
+
paths: [this.appDirectory || process.cwd()]
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const ts = require(tsPath);
|
|
32
|
+
|
|
33
|
+
return ts;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
throw new Error('TypeScript could not be found! Please, install "typescript" package.');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
exports.TypescriptLoader = TypescriptLoader;
|
package/dist/js/node/index.js
CHANGED
|
@@ -3,11 +3,21 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
compile: true
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "compile", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () {
|
|
12
|
+
return _common.compile;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
6
15
|
|
|
7
|
-
var _babel = require("./babel");
|
|
16
|
+
var _babel = require("./compilers/babel");
|
|
8
17
|
|
|
9
18
|
Object.keys(_babel).forEach(function (key) {
|
|
10
19
|
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
11
21
|
if (key in exports && exports[key] === _babel[key]) return;
|
|
12
22
|
Object.defineProperty(exports, key, {
|
|
13
23
|
enumerable: true,
|
|
@@ -15,4 +25,6 @@ Object.keys(_babel).forEach(function (key) {
|
|
|
15
25
|
return _babel[key];
|
|
16
26
|
}
|
|
17
27
|
});
|
|
18
|
-
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
var _common = require("./common");
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
2
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { fs } from '@modern-js/utils';
|
|
5
|
+
import { compileByTs } from "../compilers/typescript";
|
|
6
|
+
import { compileByBabel } from "../compilers/babel";
|
|
7
|
+
export var FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
8
|
+
|
|
9
|
+
var validateAbsolutePath = function validateAbsolutePath(filename, message) {
|
|
10
|
+
if (!path.isAbsolute(filename)) {
|
|
11
|
+
throw new Error(message);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
var validateAbsolutePaths = function validateAbsolutePaths(filenames, messageFunc) {
|
|
16
|
+
filenames.forEach(function (filename) {
|
|
17
|
+
return validateAbsolutePath(filename, messageFunc(filename));
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export var compile = /*#__PURE__*/function () {
|
|
22
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(appDirectory, modernConfig, compileOptions) {
|
|
23
|
+
var _modernConfig$server;
|
|
24
|
+
|
|
25
|
+
var sourceDirs, distDir, tsconfigPath, compiler, isTsProject;
|
|
26
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
27
|
+
while (1) {
|
|
28
|
+
switch (_context.prev = _context.next) {
|
|
29
|
+
case 0:
|
|
30
|
+
sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
|
|
31
|
+
validateAbsolutePaths(sourceDirs, function (dir) {
|
|
32
|
+
return "source dir ".concat(dir, " is not an absolute path.");
|
|
33
|
+
});
|
|
34
|
+
validateAbsolutePath(distDir, "dist dir ".concat(distDir, " is not an absolute path."));
|
|
35
|
+
compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
|
|
36
|
+
_context.t0 = tsconfigPath;
|
|
37
|
+
|
|
38
|
+
if (!_context.t0) {
|
|
39
|
+
_context.next = 9;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_context.next = 8;
|
|
44
|
+
return fs.pathExists(tsconfigPath);
|
|
45
|
+
|
|
46
|
+
case 8:
|
|
47
|
+
_context.t0 = _context.sent;
|
|
48
|
+
|
|
49
|
+
case 9:
|
|
50
|
+
isTsProject = _context.t0;
|
|
51
|
+
|
|
52
|
+
if (!(!isTsProject || compiler === 'babel')) {
|
|
53
|
+
_context.next = 15;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_context.next = 13;
|
|
58
|
+
return compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
59
|
+
|
|
60
|
+
case 13:
|
|
61
|
+
_context.next = 17;
|
|
62
|
+
break;
|
|
63
|
+
|
|
64
|
+
case 15:
|
|
65
|
+
_context.next = 17;
|
|
66
|
+
return compileByTs(appDirectory, modernConfig, compileOptions);
|
|
67
|
+
|
|
68
|
+
case 17:
|
|
69
|
+
case "end":
|
|
70
|
+
return _context.stop();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}, _callee);
|
|
74
|
+
}));
|
|
75
|
+
|
|
76
|
+
return function compile(_x, _x2, _x3) {
|
|
77
|
+
return _ref.apply(this, arguments);
|
|
78
|
+
};
|
|
79
|
+
}();
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
2
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import { getBabelChain, applyUserBabelConfig } from '@modern-js/babel-preset-lib';
|
|
6
|
+
import { fs, json5, getAlias, applyOptionsChain } from '@modern-js/utils';
|
|
7
|
+
import { compiler } from '@modern-js/babel-compiler';
|
|
8
|
+
import { FILE_EXTENSIONS } from "../../common";
|
|
9
|
+
export * from '@babel/core';
|
|
10
|
+
export var readTsConfig = function readTsConfig(tsconfigPath) {
|
|
11
|
+
var noExistReturn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
12
|
+
|
|
13
|
+
// 如果不存在,则返回 noExistReturn
|
|
14
|
+
if (!fs.existsSync(tsconfigPath)) {
|
|
15
|
+
return noExistReturn;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
var content = fs.readFileSync(tsconfigPath, 'utf-8');
|
|
19
|
+
return json5.parse(content);
|
|
20
|
+
};
|
|
21
|
+
export var existTsConfigFile = function existTsConfigFile(tsconfigAbsolutePath) {
|
|
22
|
+
var tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
23
|
+
return Boolean(tsconfig);
|
|
24
|
+
};
|
|
25
|
+
export var getBabelConfig = function getBabelConfig(libPresetOption, syntaxOption) {
|
|
26
|
+
var chain = getBabelChain(libPresetOption, syntaxOption);
|
|
27
|
+
return _objectSpread({
|
|
28
|
+
sourceType: 'unambiguous'
|
|
29
|
+
}, chain.toJSON());
|
|
30
|
+
};
|
|
31
|
+
export var resolveBabelConfig = function resolveBabelConfig(appDirectory, modernConfig, option // FIXME: babel type can't pass type checking
|
|
32
|
+
) {
|
|
33
|
+
var _modernConfig$source = modernConfig.source,
|
|
34
|
+
envVars = _modernConfig$source.envVars,
|
|
35
|
+
globalVars = _modernConfig$source.globalVars,
|
|
36
|
+
_modernConfig$source$ = _modernConfig$source.jsxTransformRuntime,
|
|
37
|
+
jsxTransformRuntime = _modernConfig$source$ === void 0 ? 'automatic' : _modernConfig$source$,
|
|
38
|
+
userLodashOption = modernConfig.tools.lodash; // alias config
|
|
39
|
+
|
|
40
|
+
var aliasConfig = getAlias(modernConfig.source.alias, _objectSpread({
|
|
41
|
+
appDirectory: appDirectory
|
|
42
|
+
}, option)); // lodash config
|
|
43
|
+
|
|
44
|
+
var lodashOptions = applyOptionsChain({
|
|
45
|
+
id: ['lodash', 'ramda']
|
|
46
|
+
}, // TODO: 需要处理类型问题
|
|
47
|
+
userLodashOption); // babel config
|
|
48
|
+
|
|
49
|
+
var babelChain = getBabelChain({
|
|
50
|
+
appDirectory: appDirectory,
|
|
51
|
+
enableReactPreset: true,
|
|
52
|
+
enableTypescriptPreset: true,
|
|
53
|
+
alias: aliasConfig,
|
|
54
|
+
envVars: envVars,
|
|
55
|
+
globalVars: globalVars,
|
|
56
|
+
lodashOptions: lodashOptions,
|
|
57
|
+
jsxTransformRuntime: jsxTransformRuntime
|
|
58
|
+
}, {
|
|
59
|
+
type: option.type,
|
|
60
|
+
syntax: option.syntax
|
|
61
|
+
});
|
|
62
|
+
var envOptions = babelChain.preset('@babel/preset-env').options();
|
|
63
|
+
babelChain.preset('@babel/preset-env').use(require.resolve('@babel/preset-env'), [_objectSpread(_objectSpread({}, envOptions[0]), {}, {
|
|
64
|
+
loose: true
|
|
65
|
+
})]);
|
|
66
|
+
babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
|
|
67
|
+
babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
|
|
68
|
+
legacy: true
|
|
69
|
+
}]); // resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
|
|
70
|
+
|
|
71
|
+
babelChain.plugin('@babel/plugin-proposal-class-properties').use(require.resolve('@babel/plugin-proposal-class-properties'), [{
|
|
72
|
+
loose: true
|
|
73
|
+
}]);
|
|
74
|
+
|
|
75
|
+
var internalBabelConfig = _objectSpread({}, babelChain.toJSON());
|
|
76
|
+
|
|
77
|
+
var userBabelConfig = modernConfig.tools.babel;
|
|
78
|
+
return applyUserBabelConfig(internalBabelConfig, userBabelConfig);
|
|
79
|
+
};
|
|
80
|
+
export var compileByBabel = /*#__PURE__*/function () {
|
|
81
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, modernConfig, compileOptions) {
|
|
82
|
+
var sourceDirs, distDir, tsconfigPath, results;
|
|
83
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
84
|
+
while (1) {
|
|
85
|
+
switch (_context2.prev = _context2.next) {
|
|
86
|
+
case 0:
|
|
87
|
+
sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
|
|
88
|
+
_context2.next = 3;
|
|
89
|
+
return Promise.all(sourceDirs.map( /*#__PURE__*/function () {
|
|
90
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(sourceDir) {
|
|
91
|
+
var babelConfig, basename, targetDir;
|
|
92
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
93
|
+
while (1) {
|
|
94
|
+
switch (_context.prev = _context.next) {
|
|
95
|
+
case 0:
|
|
96
|
+
babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
97
|
+
tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
98
|
+
syntax: 'es6+',
|
|
99
|
+
type: 'commonjs'
|
|
100
|
+
});
|
|
101
|
+
_context.next = 3;
|
|
102
|
+
return fs.pathExists(sourceDir);
|
|
103
|
+
|
|
104
|
+
case 3:
|
|
105
|
+
if (!_context.sent) {
|
|
106
|
+
_context.next = 8;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
basename = path.basename(sourceDir);
|
|
111
|
+
targetDir = path.join(distDir, basename);
|
|
112
|
+
_context.next = 8;
|
|
113
|
+
return fs.copy(sourceDir, targetDir, {
|
|
114
|
+
filter: function filter(src) {
|
|
115
|
+
return !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
case 8:
|
|
120
|
+
return _context.abrupt("return", compiler({
|
|
121
|
+
rootDir: appDirectory,
|
|
122
|
+
distDir: distDir,
|
|
123
|
+
sourceDir: sourceDir,
|
|
124
|
+
extensions: FILE_EXTENSIONS
|
|
125
|
+
}, babelConfig));
|
|
126
|
+
|
|
127
|
+
case 9:
|
|
128
|
+
case "end":
|
|
129
|
+
return _context.stop();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}, _callee);
|
|
133
|
+
}));
|
|
134
|
+
|
|
135
|
+
return function (_x4) {
|
|
136
|
+
return _ref2.apply(this, arguments);
|
|
137
|
+
};
|
|
138
|
+
}()));
|
|
139
|
+
|
|
140
|
+
case 3:
|
|
141
|
+
results = _context2.sent;
|
|
142
|
+
results.forEach(function (result) {
|
|
143
|
+
if (result.code === 1) {
|
|
144
|
+
throw new Error(result.message);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
case 5:
|
|
149
|
+
case "end":
|
|
150
|
+
return _context2.stop();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}, _callee2);
|
|
154
|
+
}));
|
|
155
|
+
|
|
156
|
+
return function compileByBabel(_x, _x2, _x3) {
|
|
157
|
+
return _ref.apply(this, arguments);
|
|
158
|
+
};
|
|
159
|
+
}();
|