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