@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
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
# @modern-js/server-utils
|
|
2
2
|
|
|
3
|
+
## 1.17.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [1b9176f]
|
|
8
|
+
- Updated dependencies [77d3a38]
|
|
9
|
+
- Updated dependencies [151329d]
|
|
10
|
+
- Updated dependencies [5af9472]
|
|
11
|
+
- Updated dependencies [6b6a534]
|
|
12
|
+
- Updated dependencies [6b43a2b]
|
|
13
|
+
- Updated dependencies [a7be124]
|
|
14
|
+
- Updated dependencies [31547b4]
|
|
15
|
+
- @modern-js/utils@1.17.0
|
|
16
|
+
- @modern-js/babel-preset-lib@1.17.0
|
|
17
|
+
- @modern-js/plugin@1.17.0
|
|
18
|
+
|
|
19
|
+
## 1.16.0
|
|
20
|
+
|
|
21
|
+
### Minor Changes
|
|
22
|
+
|
|
23
|
+
- 1100dd58c: chore: support react 18
|
|
24
|
+
|
|
25
|
+
chore: 支持 React 18
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- Updated dependencies [641592f52]
|
|
30
|
+
- Updated dependencies [3904b30a5]
|
|
31
|
+
- Updated dependencies [1100dd58c]
|
|
32
|
+
- Updated dependencies [e04e6e76a]
|
|
33
|
+
- Updated dependencies [81c66e4a4]
|
|
34
|
+
- Updated dependencies [2c305b6f5]
|
|
35
|
+
- @modern-js/utils@1.16.0
|
|
36
|
+
- @modern-js/babel-preset-lib@1.16.0
|
|
37
|
+
- @modern-js/plugin@1.16.0
|
|
38
|
+
|
|
3
39
|
## 1.15.0
|
|
4
40
|
|
|
5
41
|
### Patch Changes
|
|
@@ -4,11 +4,8 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
4
4
|
|
|
5
5
|
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; }
|
|
6
6
|
|
|
7
|
-
import * as path from 'path';
|
|
8
7
|
import { getBabelChain, applyUserBabelConfig } from '@modern-js/babel-preset-lib';
|
|
9
8
|
import { fs, json5, getAlias, applyOptionsChain } from '@modern-js/utils';
|
|
10
|
-
import { compiler } from '@modern-js/babel-compiler';
|
|
11
|
-
import { FILE_EXTENSIONS } from "../../common";
|
|
12
9
|
export * from '@babel/core';
|
|
13
10
|
export const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
14
11
|
// 如果不存在,则返回 noExistReturn
|
|
@@ -83,38 +80,4 @@ export const resolveBabelConfig = (appDirectory, modernConfig, option // FIXME:
|
|
|
83
80
|
|
|
84
81
|
const userBabelConfig = modernConfig.tools.babel;
|
|
85
82
|
return applyUserBabelConfig(internalBabelConfig, userBabelConfig);
|
|
86
|
-
};
|
|
87
|
-
export const compileByBabel = async (appDirectory, modernConfig, compileOptions) => {
|
|
88
|
-
const {
|
|
89
|
-
sourceDirs,
|
|
90
|
-
distDir,
|
|
91
|
-
tsconfigPath
|
|
92
|
-
} = compileOptions;
|
|
93
|
-
const results = await Promise.all(sourceDirs.map(async sourceDir => {
|
|
94
|
-
const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
95
|
-
tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
96
|
-
syntax: 'es6+',
|
|
97
|
-
type: 'commonjs'
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
if (await fs.pathExists(sourceDir)) {
|
|
101
|
-
const basename = path.basename(sourceDir);
|
|
102
|
-
const targetDir = path.join(distDir, basename);
|
|
103
|
-
await fs.copy(sourceDir, targetDir, {
|
|
104
|
-
filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return compiler({
|
|
109
|
-
rootDir: appDirectory,
|
|
110
|
-
distDir: distDir,
|
|
111
|
-
sourceDir: sourceDir,
|
|
112
|
-
extensions: FILE_EXTENSIONS
|
|
113
|
-
}, babelConfig);
|
|
114
|
-
}));
|
|
115
|
-
results.forEach(result => {
|
|
116
|
-
if (result.code === 1) {
|
|
117
|
-
throw new Error(result.message);
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
83
|
};
|
package/dist/js/modern/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export { compile } from "./common";
|
|
1
|
+
export * from "./babel";
|
|
@@ -7,21 +7,14 @@ var _exportNames = {
|
|
|
7
7
|
readTsConfig: true,
|
|
8
8
|
existTsConfigFile: true,
|
|
9
9
|
getBabelConfig: true,
|
|
10
|
-
resolveBabelConfig: true
|
|
11
|
-
compileByBabel: true
|
|
10
|
+
resolveBabelConfig: true
|
|
12
11
|
};
|
|
13
|
-
exports.resolveBabelConfig = exports.readTsConfig = exports.getBabelConfig = exports.existTsConfigFile =
|
|
14
|
-
|
|
15
|
-
var path = _interopRequireWildcard(require("path"));
|
|
12
|
+
exports.resolveBabelConfig = exports.readTsConfig = exports.getBabelConfig = exports.existTsConfigFile = void 0;
|
|
16
13
|
|
|
17
14
|
var _babelPresetLib = require("@modern-js/babel-preset-lib");
|
|
18
15
|
|
|
19
16
|
var _utils = require("@modern-js/utils");
|
|
20
17
|
|
|
21
|
-
var _babelCompiler = require("@modern-js/babel-compiler");
|
|
22
|
-
|
|
23
|
-
var _common = require("../../common");
|
|
24
|
-
|
|
25
18
|
var _core = require("@babel/core");
|
|
26
19
|
|
|
27
20
|
Object.keys(_core).forEach(function (key) {
|
|
@@ -36,10 +29,6 @@ Object.keys(_core).forEach(function (key) {
|
|
|
36
29
|
});
|
|
37
30
|
});
|
|
38
31
|
|
|
39
|
-
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); }
|
|
40
|
-
|
|
41
|
-
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; }
|
|
42
|
-
|
|
43
32
|
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; }
|
|
44
33
|
|
|
45
34
|
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; }
|
|
@@ -131,41 +120,4 @@ const resolveBabelConfig = (appDirectory, modernConfig, option // FIXME: babel t
|
|
|
131
120
|
return (0, _babelPresetLib.applyUserBabelConfig)(internalBabelConfig, userBabelConfig);
|
|
132
121
|
};
|
|
133
122
|
|
|
134
|
-
exports.resolveBabelConfig = resolveBabelConfig;
|
|
135
|
-
|
|
136
|
-
const compileByBabel = async (appDirectory, modernConfig, compileOptions) => {
|
|
137
|
-
const {
|
|
138
|
-
sourceDirs,
|
|
139
|
-
distDir,
|
|
140
|
-
tsconfigPath
|
|
141
|
-
} = compileOptions;
|
|
142
|
-
const results = await Promise.all(sourceDirs.map(async sourceDir => {
|
|
143
|
-
const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
144
|
-
tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
145
|
-
syntax: 'es6+',
|
|
146
|
-
type: 'commonjs'
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
if (await _utils.fs.pathExists(sourceDir)) {
|
|
150
|
-
const basename = path.basename(sourceDir);
|
|
151
|
-
const targetDir = path.join(distDir, basename);
|
|
152
|
-
await _utils.fs.copy(sourceDir, targetDir, {
|
|
153
|
-
filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return (0, _babelCompiler.compiler)({
|
|
158
|
-
rootDir: appDirectory,
|
|
159
|
-
distDir: distDir,
|
|
160
|
-
sourceDir: sourceDir,
|
|
161
|
-
extensions: _common.FILE_EXTENSIONS
|
|
162
|
-
}, babelConfig);
|
|
163
|
-
}));
|
|
164
|
-
results.forEach(result => {
|
|
165
|
-
if (result.code === 1) {
|
|
166
|
-
throw new Error(result.message);
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
exports.compileByBabel = compileByBabel;
|
|
123
|
+
exports.resolveBabelConfig = resolveBabelConfig;
|
package/dist/js/node/index.js
CHANGED
|
@@ -3,21 +3,11 @@
|
|
|
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
|
-
});
|
|
15
6
|
|
|
16
|
-
var _babel = require("./
|
|
7
|
+
var _babel = require("./babel");
|
|
17
8
|
|
|
18
9
|
Object.keys(_babel).forEach(function (key) {
|
|
19
10
|
if (key === "default" || key === "__esModule") return;
|
|
20
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
21
11
|
if (key in exports && exports[key] === _babel[key]) return;
|
|
22
12
|
Object.defineProperty(exports, key, {
|
|
23
13
|
enumerable: true,
|
|
@@ -25,6 +15,4 @@ Object.keys(_babel).forEach(function (key) {
|
|
|
25
15
|
return _babel[key];
|
|
26
16
|
}
|
|
27
17
|
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
var _common = require("./common");
|
|
18
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
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
|
+
};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export { compile } from "./common";
|
|
1
|
+
export * from "./babel";
|
|
@@ -1,7 +1,6 @@
|
|
|
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';
|
|
5
4
|
export * from '@babel/core';
|
|
6
5
|
export interface ITsconfig {
|
|
7
6
|
compilerOptions?: {
|
|
@@ -25,5 +24,4 @@ export interface IPackageModeValue {
|
|
|
25
24
|
syntax: 'es5' | 'es6+';
|
|
26
25
|
tsconfigPath: string;
|
|
27
26
|
}
|
|
28
|
-
export declare const resolveBabelConfig: (appDirectory: string, modernConfig: NormalizedConfig, option: IPackageModeValue) => any;
|
|
29
|
-
export declare const compileByBabel: CompileFunc;
|
|
27
|
+
export declare const resolveBabelConfig: (appDirectory: string, modernConfig: NormalizedConfig, option: IPackageModeValue) => any;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export { compile } from './common';
|
|
1
|
+
export * from './babel';
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.17.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,25 +35,19 @@
|
|
|
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-
|
|
39
|
-
"@modern-js/
|
|
40
|
-
"@modern-js/
|
|
41
|
-
"@modern-js/utils": "1.15.0",
|
|
38
|
+
"@modern-js/babel-preset-lib": "1.17.0",
|
|
39
|
+
"@modern-js/plugin": "1.17.0",
|
|
40
|
+
"@modern-js/utils": "1.17.0",
|
|
42
41
|
"babel-plugin-module-resolver": "^4.1.0",
|
|
43
|
-
"babel-plugin-transform-typescript-metadata": "^0.3.2"
|
|
44
|
-
"recursive-readdir": "2.2.2",
|
|
45
|
-
"tsconfig-paths": "3.14.1"
|
|
42
|
+
"babel-plugin-transform-typescript-metadata": "^0.3.2"
|
|
46
43
|
},
|
|
47
44
|
"devDependencies": {
|
|
48
|
-
"@modern-js/core": "1.
|
|
49
|
-
"@scripts/build": "1.
|
|
50
|
-
"@scripts/jest-config": "1.
|
|
45
|
+
"@modern-js/core": "1.17.0",
|
|
46
|
+
"@scripts/build": "1.17.0",
|
|
47
|
+
"@scripts/jest-config": "1.17.0",
|
|
51
48
|
"@types/babel__core": "^7.1.15",
|
|
52
49
|
"@types/jest": "^27",
|
|
53
50
|
"@types/node": "^14",
|
|
54
|
-
"@types/react": "^17",
|
|
55
|
-
"@types/react-dom": "^17",
|
|
56
|
-
"@types/recursive-readdir": "2.2.0",
|
|
57
51
|
"jest": "^27",
|
|
58
52
|
"ts-jest": "^27.0.4",
|
|
59
53
|
"typescript": "^4"
|
|
@@ -61,8 +55,7 @@
|
|
|
61
55
|
"sideEffects": false,
|
|
62
56
|
"publishConfig": {
|
|
63
57
|
"registry": "https://registry.npmjs.org/",
|
|
64
|
-
"access": "public"
|
|
65
|
-
"types": "./dist/types/index.d.ts"
|
|
58
|
+
"access": "public"
|
|
66
59
|
},
|
|
67
60
|
"wireit": {
|
|
68
61
|
"build": {
|
|
@@ -89,7 +82,6 @@
|
|
|
89
82
|
},
|
|
90
83
|
"scripts": {
|
|
91
84
|
"new": "modern new",
|
|
92
|
-
"dev": "modern build --watch",
|
|
93
85
|
"build": "wireit",
|
|
94
86
|
"test": "wireit"
|
|
95
87
|
},
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import { fs } from '@modern-js/utils';
|
|
3
|
-
import { compileByTs } from "../compilers/typescript";
|
|
4
|
-
import { compileByBabel } from "../compilers/babel";
|
|
5
|
-
export const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
6
|
-
|
|
7
|
-
const validateAbsolutePath = (filename, message) => {
|
|
8
|
-
if (!path.isAbsolute(filename)) {
|
|
9
|
-
throw new Error(message);
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const validateAbsolutePaths = (filenames, messageFunc) => {
|
|
14
|
-
filenames.forEach(filename => validateAbsolutePath(filename, messageFunc(filename)));
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
18
|
-
const {
|
|
19
|
-
sourceDirs,
|
|
20
|
-
distDir,
|
|
21
|
-
tsconfigPath
|
|
22
|
-
} = compileOptions;
|
|
23
|
-
validateAbsolutePaths(sourceDirs, dir => `source dir ${dir} is not an absolute path.`);
|
|
24
|
-
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
25
|
-
const isTsProject = tsconfigPath && (await fs.pathExists(tsconfigPath));
|
|
26
|
-
|
|
27
|
-
if (isTsProject) {
|
|
28
|
-
await compileByTs(appDirectory, modernConfig, compileOptions);
|
|
29
|
-
} else {
|
|
30
|
-
await compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
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; }
|
|
2
|
-
|
|
3
|
-
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; }
|
|
4
|
-
|
|
5
|
-
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; }
|
|
6
|
-
|
|
7
|
-
import path from 'path';
|
|
8
|
-
import { logger, getAlias, fs } from '@modern-js/utils';
|
|
9
|
-
import { TypescriptLoader } from "./typescript-loader";
|
|
10
|
-
import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
|
|
11
|
-
import ts from 'typescript';
|
|
12
|
-
|
|
13
|
-
const readTsConfigByFile = tsConfigFile => {
|
|
14
|
-
const parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
|
|
15
|
-
const {
|
|
16
|
-
options,
|
|
17
|
-
fileNames,
|
|
18
|
-
projectReferences
|
|
19
|
-
} = parsedCmd;
|
|
20
|
-
return {
|
|
21
|
-
options,
|
|
22
|
-
fileNames,
|
|
23
|
-
projectReferences
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const copyFiles = async (from, to, tsconfigPath) => {
|
|
28
|
-
if (await fs.pathExists(from)) {
|
|
29
|
-
const basename = path.basename(from);
|
|
30
|
-
const targetDir = path.join(to, basename);
|
|
31
|
-
await fs.copy(from, targetDir, {
|
|
32
|
-
filter: src => !['.ts'].includes(path.extname(src)) && src !== tsconfigPath
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export const compileByTs = async (appDirectory, modernConfig, compileOptions) => {
|
|
38
|
-
logger.info(`Running ts compile...`);
|
|
39
|
-
const {
|
|
40
|
-
sourceDirs,
|
|
41
|
-
distDir,
|
|
42
|
-
tsconfigPath
|
|
43
|
-
} = compileOptions;
|
|
44
|
-
if (!tsconfigPath) return;
|
|
45
|
-
const ts = new TypescriptLoader({
|
|
46
|
-
appDirectory
|
|
47
|
-
}).load();
|
|
48
|
-
const createProgram = ts.createIncrementalProgram || ts.createProgram;
|
|
49
|
-
const formatHost = getFormatHost(ts);
|
|
50
|
-
const {
|
|
51
|
-
alias
|
|
52
|
-
} = modernConfig.source;
|
|
53
|
-
const aliasOption = getAlias(alias || {}, {
|
|
54
|
-
appDirectory,
|
|
55
|
-
tsconfigPath
|
|
56
|
-
});
|
|
57
|
-
const {
|
|
58
|
-
paths = {},
|
|
59
|
-
absoluteBaseUrl = './'
|
|
60
|
-
} = aliasOption;
|
|
61
|
-
const {
|
|
62
|
-
options,
|
|
63
|
-
fileNames,
|
|
64
|
-
projectReferences
|
|
65
|
-
} = readTsConfigByFile(tsconfigPath);
|
|
66
|
-
const rootNames = fileNames.filter(fileName => {
|
|
67
|
-
return fileName.endsWith('.d.ts') || sourceDirs.some(sourceDir => {
|
|
68
|
-
return fileName.includes(sourceDir);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
const targetDir = path.join(appDirectory, './dist');
|
|
72
|
-
let program = createProgram.call(ts, {
|
|
73
|
-
rootNames: rootNames,
|
|
74
|
-
projectReferences,
|
|
75
|
-
options: _objectSpread(_objectSpread({}, options), {}, {
|
|
76
|
-
outDir: targetDir
|
|
77
|
-
})
|
|
78
|
-
});
|
|
79
|
-
const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
|
|
80
|
-
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
81
|
-
before: [tsconfigPathsPlugin]
|
|
82
|
-
});
|
|
83
|
-
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
84
|
-
|
|
85
|
-
if (allDiagnostics.length > 0) {
|
|
86
|
-
logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
|
|
87
|
-
process.exit(1);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
for (const source of sourceDirs) {
|
|
91
|
-
await copyFiles(source, distDir, tsconfigPath);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const getFormatHost = ts => {
|
|
96
|
-
return {
|
|
97
|
-
getCanonicalFileName: path => path,
|
|
98
|
-
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
99
|
-
getNewLine: () => ts.sys.newLine
|
|
100
|
-
};
|
|
101
|
-
};
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import * as os from 'os';
|
|
2
|
-
import path, { dirname, posix } from 'path';
|
|
3
|
-
import { createMatchPath } from 'tsconfig-paths';
|
|
4
|
-
|
|
5
|
-
const isRegExpKey = str => {
|
|
6
|
-
return str.startsWith('^') || str.endsWith('$');
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const resolveAliasPath = (baseUrl, filePath) => {
|
|
10
|
-
if (path.isAbsolute(filePath)) {
|
|
11
|
-
return filePath;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return path.resolve(baseUrl, filePath);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const createAliasMatcher = (baseUrl, alias) => {
|
|
18
|
-
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
19
|
-
if (isRegExpKey(key)) {
|
|
20
|
-
const regexp = new RegExp(key);
|
|
21
|
-
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
22
|
-
o.push([regexp, aliasPath]);
|
|
23
|
-
} else {
|
|
24
|
-
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
25
|
-
o.push([key, aliasPath]);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return o;
|
|
29
|
-
}, []);
|
|
30
|
-
return requestedModule => {
|
|
31
|
-
for (const [key, value] of aliasPairs) {
|
|
32
|
-
if (key instanceof RegExp) {
|
|
33
|
-
if (key.test(requestedModule)) {
|
|
34
|
-
return value;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (requestedModule === key) {
|
|
39
|
-
return value;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
46
|
-
const tsPaths = {};
|
|
47
|
-
const alias = {};
|
|
48
|
-
Object.keys(paths).forEach(key => {
|
|
49
|
-
if (Array.isArray(paths[key])) {
|
|
50
|
-
tsPaths[key] = paths[key];
|
|
51
|
-
} else {
|
|
52
|
-
alias[key] = paths[key];
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
56
|
-
const matchTsPath = createMatchPath(baseUrl, tsPaths, ['main']);
|
|
57
|
-
|
|
58
|
-
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
59
|
-
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
60
|
-
|
|
61
|
-
if (result) {
|
|
62
|
-
return result;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return matchAliasPath(requestedModule);
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
if (Object.keys(paths).length === 0) {
|
|
69
|
-
return undefined;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return ctx => {
|
|
73
|
-
return sf => {
|
|
74
|
-
const visitNode = node => {
|
|
75
|
-
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
76
|
-
try {
|
|
77
|
-
const importPathWithQuotes = node.moduleSpecifier && node.moduleSpecifier.getText();
|
|
78
|
-
|
|
79
|
-
if (!importPathWithQuotes) {
|
|
80
|
-
return node;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
84
|
-
const result = getNotAliasedPath(sf, matchPath, text);
|
|
85
|
-
|
|
86
|
-
if (!result) {
|
|
87
|
-
return node;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
91
|
-
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
92
|
-
|
|
93
|
-
if (tsBinary.isImportDeclaration(node)) {
|
|
94
|
-
return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
95
|
-
} else {
|
|
96
|
-
return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
97
|
-
}
|
|
98
|
-
} catch (_unused) {
|
|
99
|
-
return node;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
return tsBinary.visitNode(sf, visitNode);
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
} // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
|
|
110
|
-
|
|
111
|
-
function getNotAliasedPath(sf, matcher, text) {
|
|
112
|
-
let result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
|
|
113
|
-
|
|
114
|
-
if (!result) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (os.platform() === 'win32') {
|
|
119
|
-
result = result.replace(/\\/g, '/');
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (!path.isAbsolute(result)) {
|
|
123
|
-
try {
|
|
124
|
-
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
125
|
-
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
126
|
-
const packagePath = require.resolve(text, {
|
|
127
|
-
paths: [process.cwd(), ...module.paths]
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
if (packagePath) {
|
|
131
|
-
return text;
|
|
132
|
-
}
|
|
133
|
-
} catch (_unused2) {}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
|
|
137
|
-
return resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
|
|
138
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
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; }
|
|
2
|
-
|
|
3
|
-
export class TypescriptLoader {
|
|
4
|
-
constructor({
|
|
5
|
-
appDirectory
|
|
6
|
-
}) {
|
|
7
|
-
_defineProperty(this, "tsBinary", void 0);
|
|
8
|
-
|
|
9
|
-
_defineProperty(this, "appDirectory", void 0);
|
|
10
|
-
|
|
11
|
-
this.appDirectory = appDirectory;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
load() {
|
|
15
|
-
if (this.tsBinary) {
|
|
16
|
-
return this.tsBinary;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
const tsPath = require.resolve('typescript', {
|
|
21
|
-
paths: [this.appDirectory || process.cwd()]
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const ts = require(tsPath);
|
|
25
|
-
|
|
26
|
-
return ts;
|
|
27
|
-
} catch (error) {
|
|
28
|
-
throw new Error('TypeScript could not be found! Please, install "typescript" package.');
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
}
|