@modern-js/server-utils 1.15.0 → 1.15.1-beta.2
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/js/modern/common/index.js +32 -0
- package/dist/js/modern/{babel.js → compilers/babel/index.js} +37 -0
- package/dist/js/modern/compilers/typescript/index.js +106 -0
- package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +167 -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 +50 -0
- package/dist/js/node/{babel.js → compilers/babel/index.js} +51 -3
- package/dist/js/node/compilers/typescript/index.js +126 -0
- package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +181 -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 +76 -0
- package/dist/js/treeshaking/compilers/babel/index.js +159 -0
- package/dist/js/treeshaking/compilers/typescript/index.js +177 -0
- package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +184 -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 +7 -3
- package/dist/js/treeshaking/babel.js +0 -74
|
@@ -0,0 +1,184 @@
|
|
|
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 { createMatchPath } from 'tsconfig-paths';
|
|
7
|
+
|
|
8
|
+
var isRegExpKey = function isRegExpKey(str) {
|
|
9
|
+
return str.startsWith('^') || str.endsWith('$');
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
var resolveAliasPath = function resolveAliasPath(baseUrl, filePath) {
|
|
13
|
+
// exclude absolute path and alias
|
|
14
|
+
if (filePath.startsWith('.') || filePath.startsWith('..')) {
|
|
15
|
+
return path.resolve(baseUrl, filePath);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return filePath;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
|
|
22
|
+
var aliasPairs = Object.keys(alias).reduce(function (o, key) {
|
|
23
|
+
if (isRegExpKey(key)) {
|
|
24
|
+
var regexp = new RegExp(key);
|
|
25
|
+
var aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
26
|
+
o.push([regexp, aliasPath]);
|
|
27
|
+
} else {
|
|
28
|
+
var _aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
29
|
+
|
|
30
|
+
o.push([key, _aliasPath]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return o;
|
|
34
|
+
}, []);
|
|
35
|
+
var cacheMap = new Map(); // eslint-disable-next-line consistent-return
|
|
36
|
+
|
|
37
|
+
return function (requestedModule) {
|
|
38
|
+
if (cacheMap.has(requestedModule)) {
|
|
39
|
+
return cacheMap.get(requestedModule);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
var _iterator = _createForOfIteratorHelper(aliasPairs),
|
|
43
|
+
_step;
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
47
|
+
var _step$value = _slicedToArray(_step.value, 2),
|
|
48
|
+
key = _step$value[0],
|
|
49
|
+
value = _step$value[1];
|
|
50
|
+
|
|
51
|
+
if (key instanceof RegExp) {
|
|
52
|
+
if (key.test(requestedModule)) {
|
|
53
|
+
cacheMap.set(requestedModule, value);
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (requestedModule === key) {
|
|
59
|
+
cacheMap.set(requestedModule, value);
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} catch (err) {
|
|
64
|
+
_iterator.e(err);
|
|
65
|
+
} finally {
|
|
66
|
+
_iterator.f();
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
72
|
+
var tsPaths = {};
|
|
73
|
+
var alias = {};
|
|
74
|
+
Object.keys(paths).forEach(function (key) {
|
|
75
|
+
if (Array.isArray(paths[key])) {
|
|
76
|
+
tsPaths[key] = paths[key];
|
|
77
|
+
} else {
|
|
78
|
+
alias[key] = paths[key];
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
var matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
82
|
+
var matchTsPath = createMatchPath(baseUrl, tsPaths, ['main']);
|
|
83
|
+
|
|
84
|
+
var matchPath = function matchPath(requestedModule, readJSONSync, fileExists, extensions) {
|
|
85
|
+
var result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
86
|
+
|
|
87
|
+
if (result) {
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return matchAliasPath(requestedModule);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
if (Object.keys(paths).length === 0) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return function (ctx) {
|
|
99
|
+
return function (sf) {
|
|
100
|
+
var visitNode = function visitNode(node) {
|
|
101
|
+
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
102
|
+
try {
|
|
103
|
+
var _node$moduleSpecifier;
|
|
104
|
+
|
|
105
|
+
var importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
|
|
106
|
+
|
|
107
|
+
if (!importPathWithQuotes) {
|
|
108
|
+
return node;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
var text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
112
|
+
var result = getNotAliasedPath(sf, matchPath, text);
|
|
113
|
+
|
|
114
|
+
if (!result) {
|
|
115
|
+
return node;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
var moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
119
|
+
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
120
|
+
|
|
121
|
+
if (tsBinary.isImportDeclaration(node)) {
|
|
122
|
+
return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
123
|
+
} else {
|
|
124
|
+
return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
125
|
+
}
|
|
126
|
+
} catch (_unused) {
|
|
127
|
+
return node;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
return tsBinary.visitNode(sf, visitNode);
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
} // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
|
|
138
|
+
|
|
139
|
+
function getNotAliasedPath(sf, matcher, text) {
|
|
140
|
+
var result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
|
|
141
|
+
|
|
142
|
+
if (!result) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (os.platform() === 'win32') {
|
|
147
|
+
result = result.replace(/\\/g, '/');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!path.isAbsolute(result)) {
|
|
151
|
+
try {
|
|
152
|
+
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
153
|
+
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
154
|
+
var packagePath = require.resolve(text, {
|
|
155
|
+
paths: [process.cwd()].concat(_toConsumableArray(module.paths))
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
if (packagePath) {
|
|
159
|
+
// eslint-disable-next-line consistent-return
|
|
160
|
+
return text;
|
|
161
|
+
}
|
|
162
|
+
} catch (_unused2) {} // handle alias to alias
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
if (!result.startsWith('.') && !result.startsWith('..')) {
|
|
166
|
+
try {
|
|
167
|
+
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
168
|
+
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
169
|
+
var _packagePath = require.resolve(result, {
|
|
170
|
+
paths: [process.cwd()].concat(_toConsumableArray(module.paths))
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
if (_packagePath) {
|
|
174
|
+
// eslint-disable-next-line consistent-return
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
} catch (_unused3) {}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
var resolvedPath = posix.relative(dirname(sf.fileName), result) || './'; // eslint-disable-next-line consistent-return
|
|
182
|
+
|
|
183
|
+
return resolvedPath[0] === '.' ? resolvedPath : "./".concat(resolvedPath);
|
|
184
|
+
}
|
|
@@ -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;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -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.15.
|
|
14
|
+
"version": "1.15.1-beta.2",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -35,11 +35,13 @@
|
|
|
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-compiler": "^1.15.0",
|
|
38
39
|
"@modern-js/babel-preset-lib": "1.15.0",
|
|
39
40
|
"@modern-js/plugin": "1.15.0",
|
|
40
41
|
"@modern-js/utils": "1.15.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
47
|
"@modern-js/core": "1.15.0",
|
|
@@ -57,7 +59,8 @@
|
|
|
57
59
|
"sideEffects": false,
|
|
58
60
|
"publishConfig": {
|
|
59
61
|
"registry": "https://registry.npmjs.org/",
|
|
60
|
-
"access": "public"
|
|
62
|
+
"access": "public",
|
|
63
|
+
"types": "./dist/types/index.d.ts"
|
|
61
64
|
},
|
|
62
65
|
"wireit": {
|
|
63
66
|
"build": {
|
|
@@ -84,6 +87,7 @@
|
|
|
84
87
|
},
|
|
85
88
|
"scripts": {
|
|
86
89
|
"new": "modern new",
|
|
90
|
+
"dev": "modern build --watch",
|
|
87
91
|
"build": "wireit",
|
|
88
92
|
"test": "wireit"
|
|
89
93
|
},
|
|
@@ -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
|
-
};
|