@modern-js/server-utils 1.15.1-beta.1 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/dist/js/modern/{compilers/babel/index.js → babel.js} +0 -37
- package/dist/js/modern/index.js +1 -2
- package/dist/js/node/{compilers/babel/index.js → babel.js} +3 -51
- package/dist/js/node/index.js +2 -14
- package/dist/js/treeshaking/babel.js +74 -0
- package/dist/js/treeshaking/index.js +1 -2
- package/dist/types/{compilers/babel/index.d.ts → babel.d.ts} +1 -3
- package/dist/types/index.d.ts +1 -2
- package/package.json +9 -17
- package/dist/js/modern/common/index.js +0 -32
- package/dist/js/modern/compilers/typescript/index.js +0 -101
- package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +0 -138
- package/dist/js/modern/compilers/typescript/typescript-loader.js +0 -32
- package/dist/js/node/common/index.js +0 -50
- package/dist/js/node/compilers/typescript/index.js +0 -123
- package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +0 -152
- package/dist/js/node/compilers/typescript/typescript-loader.js +0 -41
- package/dist/js/treeshaking/common/index.js +0 -76
- package/dist/js/treeshaking/compilers/babel/index.js +0 -159
- package/dist/js/treeshaking/compilers/typescript/index.js +0 -176
- package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +0 -155
- package/dist/js/treeshaking/compilers/typescript/typescript-loader.js +0 -39
- package/dist/types/common/index.d.ts +0 -14
- package/dist/types/compilers/typescript/index.d.ts +0 -2
- package/dist/types/compilers/typescript/tsconfig-paths-plugin.d.ts +0 -2
- package/dist/types/compilers/typescript/typescript-loader.d.ts +0 -11
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.compile = exports.FILE_EXTENSIONS = void 0;
|
|
7
|
-
|
|
8
|
-
var path = _interopRequireWildcard(require("path"));
|
|
9
|
-
|
|
10
|
-
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
|
-
var _typescript = require("../compilers/typescript");
|
|
13
|
-
|
|
14
|
-
var _babel = require("../compilers/babel");
|
|
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 FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
21
|
-
exports.FILE_EXTENSIONS = FILE_EXTENSIONS;
|
|
22
|
-
|
|
23
|
-
const validateAbsolutePath = (filename, message) => {
|
|
24
|
-
if (!path.isAbsolute(filename)) {
|
|
25
|
-
throw new Error(message);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const validateAbsolutePaths = (filenames, messageFunc) => {
|
|
30
|
-
filenames.forEach(filename => validateAbsolutePath(filename, messageFunc(filename)));
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
34
|
-
const {
|
|
35
|
-
sourceDirs,
|
|
36
|
-
distDir,
|
|
37
|
-
tsconfigPath
|
|
38
|
-
} = compileOptions;
|
|
39
|
-
validateAbsolutePaths(sourceDirs, dir => `source dir ${dir} is not an absolute path.`);
|
|
40
|
-
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
41
|
-
const isTsProject = tsconfigPath && (await _utils.fs.pathExists(tsconfigPath));
|
|
42
|
-
|
|
43
|
-
if (isTsProject) {
|
|
44
|
-
await (0, _typescript.compileByTs)(appDirectory, modernConfig, compileOptions);
|
|
45
|
-
} else {
|
|
46
|
-
await (0, _babel.compileByBabel)(appDirectory, modernConfig, compileOptions);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
exports.compile = compile;
|
|
@@ -1,123 +0,0 @@
|
|
|
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 _typescriptLoader = require("./typescript-loader");
|
|
13
|
-
|
|
14
|
-
var _tsconfigPathsPlugin = require("./tsconfig-paths-plugin");
|
|
15
|
-
|
|
16
|
-
var _typescript = _interopRequireDefault(require("typescript"));
|
|
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
|
-
if (!tsconfigPath) return;
|
|
62
|
-
const ts = new _typescriptLoader.TypescriptLoader({
|
|
63
|
-
appDirectory
|
|
64
|
-
}).load();
|
|
65
|
-
const createProgram = ts.createIncrementalProgram || ts.createProgram;
|
|
66
|
-
const formatHost = getFormatHost(ts);
|
|
67
|
-
const {
|
|
68
|
-
alias
|
|
69
|
-
} = modernConfig.source;
|
|
70
|
-
const aliasOption = (0, _utils.getAlias)(alias || {}, {
|
|
71
|
-
appDirectory,
|
|
72
|
-
tsconfigPath
|
|
73
|
-
});
|
|
74
|
-
const {
|
|
75
|
-
paths = {},
|
|
76
|
-
absoluteBaseUrl = './'
|
|
77
|
-
} = aliasOption;
|
|
78
|
-
const {
|
|
79
|
-
options,
|
|
80
|
-
fileNames,
|
|
81
|
-
projectReferences
|
|
82
|
-
} = readTsConfigByFile(tsconfigPath);
|
|
83
|
-
const rootNames = fileNames.filter(fileName => {
|
|
84
|
-
return fileName.endsWith('.d.ts') || sourceDirs.some(sourceDir => {
|
|
85
|
-
return fileName.includes(sourceDir);
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
const targetDir = _path.default.join(appDirectory, './dist');
|
|
90
|
-
|
|
91
|
-
let program = createProgram.call(ts, {
|
|
92
|
-
rootNames: rootNames,
|
|
93
|
-
projectReferences,
|
|
94
|
-
options: _objectSpread(_objectSpread({}, options), {}, {
|
|
95
|
-
outDir: targetDir
|
|
96
|
-
})
|
|
97
|
-
});
|
|
98
|
-
const tsconfigPathsPlugin = (0, _tsconfigPathsPlugin.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths);
|
|
99
|
-
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
100
|
-
before: [tsconfigPathsPlugin]
|
|
101
|
-
});
|
|
102
|
-
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
103
|
-
|
|
104
|
-
if (allDiagnostics.length > 0) {
|
|
105
|
-
_utils.logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
|
|
106
|
-
|
|
107
|
-
process.exit(1);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
for (const source of sourceDirs) {
|
|
111
|
-
await copyFiles(source, distDir, tsconfigPath);
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
exports.compileByTs = compileByTs;
|
|
116
|
-
|
|
117
|
-
const getFormatHost = ts => {
|
|
118
|
-
return {
|
|
119
|
-
getCanonicalFileName: path => path,
|
|
120
|
-
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
121
|
-
getNewLine: () => ts.sys.newLine
|
|
122
|
-
};
|
|
123
|
-
};
|
|
@@ -1,152 +0,0 @@
|
|
|
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 _tsconfigPaths = require("tsconfig-paths");
|
|
13
|
-
|
|
14
|
-
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); }
|
|
15
|
-
|
|
16
|
-
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; }
|
|
17
|
-
|
|
18
|
-
const isRegExpKey = str => {
|
|
19
|
-
return str.startsWith('^') || str.endsWith('$');
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const resolveAliasPath = (baseUrl, filePath) => {
|
|
23
|
-
if (_path.default.isAbsolute(filePath)) {
|
|
24
|
-
return filePath;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return _path.default.resolve(baseUrl, filePath);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const createAliasMatcher = (baseUrl, alias) => {
|
|
31
|
-
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
32
|
-
if (isRegExpKey(key)) {
|
|
33
|
-
const regexp = new RegExp(key);
|
|
34
|
-
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
35
|
-
o.push([regexp, aliasPath]);
|
|
36
|
-
} else {
|
|
37
|
-
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
38
|
-
o.push([key, aliasPath]);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return o;
|
|
42
|
-
}, []);
|
|
43
|
-
return requestedModule => {
|
|
44
|
-
for (const [key, value] of aliasPairs) {
|
|
45
|
-
if (key instanceof RegExp) {
|
|
46
|
-
if (key.test(requestedModule)) {
|
|
47
|
-
return value;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (requestedModule === key) {
|
|
52
|
-
return value;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
59
|
-
const tsPaths = {};
|
|
60
|
-
const alias = {};
|
|
61
|
-
Object.keys(paths).forEach(key => {
|
|
62
|
-
if (Array.isArray(paths[key])) {
|
|
63
|
-
tsPaths[key] = paths[key];
|
|
64
|
-
} else {
|
|
65
|
-
alias[key] = paths[key];
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
69
|
-
const matchTsPath = (0, _tsconfigPaths.createMatchPath)(baseUrl, tsPaths, ['main']);
|
|
70
|
-
|
|
71
|
-
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
72
|
-
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
73
|
-
|
|
74
|
-
if (result) {
|
|
75
|
-
return result;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return matchAliasPath(requestedModule);
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
if (Object.keys(paths).length === 0) {
|
|
82
|
-
return undefined;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return ctx => {
|
|
86
|
-
return sf => {
|
|
87
|
-
const visitNode = node => {
|
|
88
|
-
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
89
|
-
try {
|
|
90
|
-
const importPathWithQuotes = node.moduleSpecifier && node.moduleSpecifier.getText();
|
|
91
|
-
|
|
92
|
-
if (!importPathWithQuotes) {
|
|
93
|
-
return node;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
97
|
-
const result = getNotAliasedPath(sf, matchPath, text);
|
|
98
|
-
|
|
99
|
-
if (!result) {
|
|
100
|
-
return node;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
104
|
-
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
105
|
-
|
|
106
|
-
if (tsBinary.isImportDeclaration(node)) {
|
|
107
|
-
return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
108
|
-
} else {
|
|
109
|
-
return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
110
|
-
}
|
|
111
|
-
} catch (_unused) {
|
|
112
|
-
return node;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
return tsBinary.visitNode(sf, visitNode);
|
|
120
|
-
};
|
|
121
|
-
};
|
|
122
|
-
} // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
function getNotAliasedPath(sf, matcher, text) {
|
|
126
|
-
let result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
|
|
127
|
-
|
|
128
|
-
if (!result) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (os.platform() === 'win32') {
|
|
133
|
-
result = result.replace(/\\/g, '/');
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (!_path.default.isAbsolute(result)) {
|
|
137
|
-
try {
|
|
138
|
-
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
139
|
-
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
140
|
-
const packagePath = require.resolve(text, {
|
|
141
|
-
paths: [process.cwd(), ...module.paths]
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
if (packagePath) {
|
|
145
|
-
return text;
|
|
146
|
-
}
|
|
147
|
-
} catch (_unused2) {}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const resolvedPath = _path.posix.relative((0, _path.dirname)(sf.fileName), result) || './';
|
|
151
|
-
return resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
|
|
152
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
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;
|
|
@@ -1,76 +0,0 @@
|
|
|
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 sourceDirs, distDir, tsconfigPath, isTsProject;
|
|
24
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
25
|
-
while (1) {
|
|
26
|
-
switch (_context.prev = _context.next) {
|
|
27
|
-
case 0:
|
|
28
|
-
sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
|
|
29
|
-
validateAbsolutePaths(sourceDirs, function (dir) {
|
|
30
|
-
return "source dir ".concat(dir, " is not an absolute path.");
|
|
31
|
-
});
|
|
32
|
-
validateAbsolutePath(distDir, "dist dir ".concat(distDir, " is not an absolute path."));
|
|
33
|
-
_context.t0 = tsconfigPath;
|
|
34
|
-
|
|
35
|
-
if (!_context.t0) {
|
|
36
|
-
_context.next = 8;
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
_context.next = 7;
|
|
41
|
-
return fs.pathExists(tsconfigPath);
|
|
42
|
-
|
|
43
|
-
case 7:
|
|
44
|
-
_context.t0 = _context.sent;
|
|
45
|
-
|
|
46
|
-
case 8:
|
|
47
|
-
isTsProject = _context.t0;
|
|
48
|
-
|
|
49
|
-
if (!isTsProject) {
|
|
50
|
-
_context.next = 14;
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
_context.next = 12;
|
|
55
|
-
return compileByTs(appDirectory, modernConfig, compileOptions);
|
|
56
|
-
|
|
57
|
-
case 12:
|
|
58
|
-
_context.next = 16;
|
|
59
|
-
break;
|
|
60
|
-
|
|
61
|
-
case 14:
|
|
62
|
-
_context.next = 16;
|
|
63
|
-
return compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
64
|
-
|
|
65
|
-
case 16:
|
|
66
|
-
case "end":
|
|
67
|
-
return _context.stop();
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}, _callee);
|
|
71
|
-
}));
|
|
72
|
-
|
|
73
|
-
return function compile(_x, _x2, _x3) {
|
|
74
|
-
return _ref.apply(this, arguments);
|
|
75
|
-
};
|
|
76
|
-
}();
|
|
@@ -1,159 +0,0 @@
|
|
|
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
|
-
}();
|