@modern-js/server-utils 1.0.0-rc.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/LICENSE +21 -0
- package/README.md +32 -0
- package/dist/js/modern/babel.js +87 -0
- package/dist/js/modern/gather.js +35 -0
- package/dist/js/modern/index.js +3 -0
- package/dist/js/modern/middleware.js +31 -0
- package/dist/js/modern/requireModule.js +24 -0
- package/dist/js/node/babel.js +130 -0
- package/dist/js/node/gather.js +55 -0
- package/dist/js/node/index.js +44 -0
- package/dist/js/node/middleware.js +43 -0
- package/dist/js/node/requireModule.js +35 -0
- package/dist/js/treeshaking/babel.js +83 -0
- package/dist/js/treeshaking/gather.js +35 -0
- package/dist/js/treeshaking/index.js +3 -0
- package/dist/js/treeshaking/middleware.js +35 -0
- package/dist/js/treeshaking/requireModule.js +26 -0
- package/dist/types/babel.d.ts +27 -0
- package/dist/types/gather.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/middleware.d.ts +16 -0
- package/dist/types/requireModule.d.ts +1 -0
- package/modern.config.js +15 -0
- package/package.json +51 -0
- package/src/babel.ts +149 -0
- package/src/gather.ts +30 -0
- package/src/index.ts +3 -0
- package/src/middleware.ts +43 -0
- package/src/requireModule.ts +23 -0
- package/tests/__snapshots__/babel.test.ts.snap +172 -0
- package/tests/babel.test.ts +31 -0
- package/tests/fixtures/api/_app.ts +5 -0
- package/tests/fixtures/api/tsconfig.json +14 -0
- package/tests/fixtures/server/index.ts +5 -0
- package/tests/gather.test.ts +22 -0
- package/tests/helpers.ts +104 -0
- package/tests/tsconfig.json +13 -0
- package/tsconfig.json +14 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Modern.js
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
<p align="center">
|
|
3
|
+
<a href="https://modernjs.dev" target="blank"><img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs.png" width="300" alt="Modern.js Logo" /></a>
|
|
4
|
+
</p>
|
|
5
|
+
|
|
6
|
+
<p align="center">现代 Web 工程体系</p>
|
|
7
|
+
|
|
8
|
+
## 背景
|
|
9
|
+
- [迈入现代 Web 开发](https://zhuanlan.zhihu.com/p/386607009)
|
|
10
|
+
- [现代 Web 开发者问卷调查报告](https://zhuanlan.zhihu.com/p/403206195)
|
|
11
|
+
|
|
12
|
+
## 计划
|
|
13
|
+
|
|
14
|
+
Modern.js 的 1.0.0.rc 版已经发到 npm,目前在做测试改进,README 文档之后统一提供(现阶段加入测试和开发,可以发 [issue](https://github.com/modern-js-dev/modern.js/issues) 留微信联系),完整的文档站计划在10月14日上线
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 { getBabelChain } from '@modern-js/babel-preset-lib';
|
|
8
|
+
import { applyOptionsChain, fs, getAlias } from '@modern-js/utils';
|
|
9
|
+
import json5 from 'json5';
|
|
10
|
+
export * from '@babel/core';
|
|
11
|
+
export const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
12
|
+
// 如果不存在,则返回 noExistReturn
|
|
13
|
+
if (!fs.existsSync(tsconfigPath)) {
|
|
14
|
+
return noExistReturn;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const content = fs.readFileSync(tsconfigPath, 'utf-8');
|
|
18
|
+
return json5.parse(content);
|
|
19
|
+
};
|
|
20
|
+
export const existTsConfigFile = tsconfigAbsolutePath => {
|
|
21
|
+
const tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
22
|
+
return Boolean(tsconfig);
|
|
23
|
+
};
|
|
24
|
+
export const getBabelConfig = (libPresetOption, syntaxOption) => {
|
|
25
|
+
const chain = getBabelChain(libPresetOption, syntaxOption);
|
|
26
|
+
return _objectSpread({
|
|
27
|
+
sourceType: 'unambiguous'
|
|
28
|
+
}, chain.toJSON());
|
|
29
|
+
};
|
|
30
|
+
export const resolveBabelConfig = (appDirectory, modernConfig, option // FIXME: babel type can't pass type checking
|
|
31
|
+
) => {
|
|
32
|
+
const {
|
|
33
|
+
source: {
|
|
34
|
+
envVars,
|
|
35
|
+
globalVars,
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
37
|
+
// @ts-expect-error
|
|
38
|
+
jsxTransformRuntime = 'automatic'
|
|
39
|
+
},
|
|
40
|
+
tools: {
|
|
41
|
+
lodash: userLodashOption
|
|
42
|
+
}
|
|
43
|
+
} = modernConfig; // alias config
|
|
44
|
+
|
|
45
|
+
const aliasConfig = getAlias(modernConfig.source.alias, _objectSpread({
|
|
46
|
+
appDirectory
|
|
47
|
+
}, option)); // lodash config
|
|
48
|
+
|
|
49
|
+
const lodashOptions = applyOptionsChain({
|
|
50
|
+
id: ['lodash', 'ramda']
|
|
51
|
+
}, // TODO: 需要处理类型问题
|
|
52
|
+
userLodashOption); // babel config
|
|
53
|
+
|
|
54
|
+
const babelChain = getBabelChain({
|
|
55
|
+
appDirectory,
|
|
56
|
+
enableReactPreset: true,
|
|
57
|
+
enableTypescriptPreset: true,
|
|
58
|
+
alias: aliasConfig,
|
|
59
|
+
envVars,
|
|
60
|
+
globalVars,
|
|
61
|
+
lodashOptions,
|
|
62
|
+
jsxTransformRuntime
|
|
63
|
+
}, {
|
|
64
|
+
type: option.type,
|
|
65
|
+
syntax: option.syntax
|
|
66
|
+
});
|
|
67
|
+
const envOptions = babelChain.preset('@babel/preset-env').options();
|
|
68
|
+
babelChain.preset('@babel/preset-env').use(require.resolve('@babel/preset-env'), [_objectSpread(_objectSpread({}, envOptions[0]), {}, {
|
|
69
|
+
loose: true
|
|
70
|
+
})]);
|
|
71
|
+
babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
|
|
72
|
+
babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
|
|
73
|
+
legacy: true
|
|
74
|
+
}]);
|
|
75
|
+
babelChain.plugin('@babel/plugin-proposal-class-properties').use(require.resolve('@babel/plugin-proposal-class-properties'), [{
|
|
76
|
+
loose: true
|
|
77
|
+
}]);
|
|
78
|
+
|
|
79
|
+
const internalBabelConfig = _objectSpread({}, babelChain.toJSON());
|
|
80
|
+
|
|
81
|
+
const userBabelConfig = modernConfig.tools.babel;
|
|
82
|
+
applyOptionsChain(internalBabelConfig, // TODO: 感觉 userBabelConfig 的类型应该是TransformOptions
|
|
83
|
+
userBabelConfig, {
|
|
84
|
+
chain: babelChain
|
|
85
|
+
});
|
|
86
|
+
return internalBabelConfig;
|
|
87
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { createMiddlewareCollecter } from "./middleware";
|
|
3
|
+
import { requireModule } from "./requireModule";
|
|
4
|
+
const API_DIR_PATH = 'api';
|
|
5
|
+
const API_APP_NAME = '_app';
|
|
6
|
+
const SERVER_DIR_PATH = 'server';
|
|
7
|
+
const WEB_APP_NAME = 'index';
|
|
8
|
+
export const gather = pwd => {
|
|
9
|
+
const {
|
|
10
|
+
getMiddlewares,
|
|
11
|
+
addAPIMiddleware,
|
|
12
|
+
addWebMiddleware
|
|
13
|
+
} = createMiddlewareCollecter();
|
|
14
|
+
const apiPath = path.resolve(pwd, API_DIR_PATH);
|
|
15
|
+
const apiAppPath = path.resolve(apiPath, API_APP_NAME);
|
|
16
|
+
const serverPath = path.resolve(pwd, SERVER_DIR_PATH);
|
|
17
|
+
const webAppPath = path.resolve(serverPath, WEB_APP_NAME);
|
|
18
|
+
const apiAttacher = requireModule(apiAppPath);
|
|
19
|
+
|
|
20
|
+
if (apiAttacher) {
|
|
21
|
+
apiAttacher({
|
|
22
|
+
addMiddleware: addAPIMiddleware
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const webAttacher = requireModule(webAppPath);
|
|
27
|
+
|
|
28
|
+
if (webAttacher) {
|
|
29
|
+
webAttacher({
|
|
30
|
+
addMiddleware: addWebMiddleware
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return getMiddlewares();
|
|
35
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const createMiddlewareCollecter = () => {
|
|
2
|
+
const webMiddlewares = [];
|
|
3
|
+
const apiMiddlewares = [];
|
|
4
|
+
const ssrMiddlewares = [];
|
|
5
|
+
|
|
6
|
+
const addWebMiddleware = input => {
|
|
7
|
+
webMiddlewares.push(input);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const addAPIMiddleware = input => {
|
|
11
|
+
apiMiddlewares.push(input);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const addSSRMiddleware = input => {
|
|
15
|
+
ssrMiddlewares.push(input);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const getMiddlewares = () => ({
|
|
19
|
+
web: webMiddlewares,
|
|
20
|
+
api: apiMiddlewares,
|
|
21
|
+
ssr: ssrMiddlewares
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
getMiddlewares,
|
|
26
|
+
addWebMiddleware,
|
|
27
|
+
addAPIMiddleware,
|
|
28
|
+
addSSRMiddleware
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export const hook = attacher => attacher;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { findExists } from '@modern-js/utils';
|
|
2
|
+
const FILE_EXTENSIONS = ['.ts', '.js'];
|
|
3
|
+
export const requireModule = filename => {
|
|
4
|
+
const exist = findExists(FILE_EXTENSIONS.map(ext => `${filename}${ext}`));
|
|
5
|
+
|
|
6
|
+
if (!exist) {
|
|
7
|
+
return null;
|
|
8
|
+
} // throw errors directly if require fail
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const mod = require(exist);
|
|
12
|
+
|
|
13
|
+
return interopRequire(mod);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function interopRequire(obj) {
|
|
17
|
+
return interopRequireDefault(obj).default;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function interopRequireDefault(obj) {
|
|
21
|
+
return obj !== null && obj !== void 0 && obj.__esModule ? obj : {
|
|
22
|
+
default: obj
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
readTsConfig: true,
|
|
8
|
+
existTsConfigFile: true,
|
|
9
|
+
getBabelConfig: true,
|
|
10
|
+
resolveBabelConfig: true
|
|
11
|
+
};
|
|
12
|
+
exports.resolveBabelConfig = exports.getBabelConfig = exports.existTsConfigFile = exports.readTsConfig = void 0;
|
|
13
|
+
|
|
14
|
+
var _babelPresetLib = require("@modern-js/babel-preset-lib");
|
|
15
|
+
|
|
16
|
+
var _utils = require("@modern-js/utils");
|
|
17
|
+
|
|
18
|
+
var _json = _interopRequireDefault(require("json5"));
|
|
19
|
+
|
|
20
|
+
var _core = require("@babel/core");
|
|
21
|
+
|
|
22
|
+
Object.keys(_core).forEach(function (key) {
|
|
23
|
+
if (key === "default" || key === "__esModule") return;
|
|
24
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
25
|
+
if (key in exports && exports[key] === _core[key]) return;
|
|
26
|
+
Object.defineProperty(exports, key, {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () {
|
|
29
|
+
return _core[key];
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
35
|
+
|
|
36
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
37
|
+
|
|
38
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
39
|
+
|
|
40
|
+
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; }
|
|
41
|
+
|
|
42
|
+
const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
43
|
+
// 如果不存在,则返回 noExistReturn
|
|
44
|
+
if (!_utils.fs.existsSync(tsconfigPath)) {
|
|
45
|
+
return noExistReturn;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const content = _utils.fs.readFileSync(tsconfigPath, 'utf-8');
|
|
49
|
+
|
|
50
|
+
return _json.default.parse(content);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
exports.readTsConfig = readTsConfig;
|
|
54
|
+
|
|
55
|
+
const existTsConfigFile = tsconfigAbsolutePath => {
|
|
56
|
+
const tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
57
|
+
return Boolean(tsconfig);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
exports.existTsConfigFile = existTsConfigFile;
|
|
61
|
+
|
|
62
|
+
const getBabelConfig = (libPresetOption, syntaxOption) => {
|
|
63
|
+
const chain = (0, _babelPresetLib.getBabelChain)(libPresetOption, syntaxOption);
|
|
64
|
+
return _objectSpread({
|
|
65
|
+
sourceType: 'unambiguous'
|
|
66
|
+
}, chain.toJSON());
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
exports.getBabelConfig = getBabelConfig;
|
|
70
|
+
|
|
71
|
+
const resolveBabelConfig = (appDirectory, modernConfig, option // FIXME: babel type can't pass type checking
|
|
72
|
+
) => {
|
|
73
|
+
const {
|
|
74
|
+
source: {
|
|
75
|
+
envVars,
|
|
76
|
+
globalVars,
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
78
|
+
// @ts-expect-error
|
|
79
|
+
jsxTransformRuntime = 'automatic'
|
|
80
|
+
},
|
|
81
|
+
tools: {
|
|
82
|
+
lodash: userLodashOption
|
|
83
|
+
}
|
|
84
|
+
} = modernConfig; // alias config
|
|
85
|
+
|
|
86
|
+
const aliasConfig = (0, _utils.getAlias)(modernConfig.source.alias, _objectSpread({
|
|
87
|
+
appDirectory
|
|
88
|
+
}, option)); // lodash config
|
|
89
|
+
|
|
90
|
+
const lodashOptions = (0, _utils.applyOptionsChain)({
|
|
91
|
+
id: ['lodash', 'ramda']
|
|
92
|
+
}, // TODO: 需要处理类型问题
|
|
93
|
+
userLodashOption); // babel config
|
|
94
|
+
|
|
95
|
+
const babelChain = (0, _babelPresetLib.getBabelChain)({
|
|
96
|
+
appDirectory,
|
|
97
|
+
enableReactPreset: true,
|
|
98
|
+
enableTypescriptPreset: true,
|
|
99
|
+
alias: aliasConfig,
|
|
100
|
+
envVars,
|
|
101
|
+
globalVars,
|
|
102
|
+
lodashOptions,
|
|
103
|
+
jsxTransformRuntime
|
|
104
|
+
}, {
|
|
105
|
+
type: option.type,
|
|
106
|
+
syntax: option.syntax
|
|
107
|
+
});
|
|
108
|
+
const envOptions = babelChain.preset('@babel/preset-env').options();
|
|
109
|
+
babelChain.preset('@babel/preset-env').use(require.resolve('@babel/preset-env'), [_objectSpread(_objectSpread({}, envOptions[0]), {}, {
|
|
110
|
+
loose: true
|
|
111
|
+
})]);
|
|
112
|
+
babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
|
|
113
|
+
babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
|
|
114
|
+
legacy: true
|
|
115
|
+
}]);
|
|
116
|
+
babelChain.plugin('@babel/plugin-proposal-class-properties').use(require.resolve('@babel/plugin-proposal-class-properties'), [{
|
|
117
|
+
loose: true
|
|
118
|
+
}]);
|
|
119
|
+
|
|
120
|
+
const internalBabelConfig = _objectSpread({}, babelChain.toJSON());
|
|
121
|
+
|
|
122
|
+
const userBabelConfig = modernConfig.tools.babel;
|
|
123
|
+
(0, _utils.applyOptionsChain)(internalBabelConfig, // TODO: 感觉 userBabelConfig 的类型应该是TransformOptions
|
|
124
|
+
userBabelConfig, {
|
|
125
|
+
chain: babelChain
|
|
126
|
+
});
|
|
127
|
+
return internalBabelConfig;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
exports.resolveBabelConfig = resolveBabelConfig;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.gather = void 0;
|
|
7
|
+
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
|
+
|
|
10
|
+
var _middleware = require("./middleware");
|
|
11
|
+
|
|
12
|
+
var _requireModule = require("./requireModule");
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
16
|
+
const API_DIR_PATH = 'api';
|
|
17
|
+
const API_APP_NAME = '_app';
|
|
18
|
+
const SERVER_DIR_PATH = 'server';
|
|
19
|
+
const WEB_APP_NAME = 'index';
|
|
20
|
+
|
|
21
|
+
const gather = pwd => {
|
|
22
|
+
const {
|
|
23
|
+
getMiddlewares,
|
|
24
|
+
addAPIMiddleware,
|
|
25
|
+
addWebMiddleware
|
|
26
|
+
} = (0, _middleware.createMiddlewareCollecter)();
|
|
27
|
+
|
|
28
|
+
const apiPath = _path.default.resolve(pwd, API_DIR_PATH);
|
|
29
|
+
|
|
30
|
+
const apiAppPath = _path.default.resolve(apiPath, API_APP_NAME);
|
|
31
|
+
|
|
32
|
+
const serverPath = _path.default.resolve(pwd, SERVER_DIR_PATH);
|
|
33
|
+
|
|
34
|
+
const webAppPath = _path.default.resolve(serverPath, WEB_APP_NAME);
|
|
35
|
+
|
|
36
|
+
const apiAttacher = (0, _requireModule.requireModule)(apiAppPath);
|
|
37
|
+
|
|
38
|
+
if (apiAttacher) {
|
|
39
|
+
apiAttacher({
|
|
40
|
+
addMiddleware: addAPIMiddleware
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const webAttacher = (0, _requireModule.requireModule)(webAppPath);
|
|
45
|
+
|
|
46
|
+
if (webAttacher) {
|
|
47
|
+
webAttacher({
|
|
48
|
+
addMiddleware: addWebMiddleware
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return getMiddlewares();
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
exports.gather = gather;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _middleware = require("./middleware");
|
|
8
|
+
|
|
9
|
+
Object.keys(_middleware).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _middleware[key]) return;
|
|
12
|
+
Object.defineProperty(exports, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _middleware[key];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
var _gather = require("./gather");
|
|
21
|
+
|
|
22
|
+
Object.keys(_gather).forEach(function (key) {
|
|
23
|
+
if (key === "default" || key === "__esModule") return;
|
|
24
|
+
if (key in exports && exports[key] === _gather[key]) return;
|
|
25
|
+
Object.defineProperty(exports, key, {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _gather[key];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
var _babel = require("./babel");
|
|
34
|
+
|
|
35
|
+
Object.keys(_babel).forEach(function (key) {
|
|
36
|
+
if (key === "default" || key === "__esModule") return;
|
|
37
|
+
if (key in exports && exports[key] === _babel[key]) return;
|
|
38
|
+
Object.defineProperty(exports, key, {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function () {
|
|
41
|
+
return _babel[key];
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.hook = exports.createMiddlewareCollecter = void 0;
|
|
7
|
+
|
|
8
|
+
const createMiddlewareCollecter = () => {
|
|
9
|
+
const webMiddlewares = [];
|
|
10
|
+
const apiMiddlewares = [];
|
|
11
|
+
const ssrMiddlewares = [];
|
|
12
|
+
|
|
13
|
+
const addWebMiddleware = input => {
|
|
14
|
+
webMiddlewares.push(input);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const addAPIMiddleware = input => {
|
|
18
|
+
apiMiddlewares.push(input);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const addSSRMiddleware = input => {
|
|
22
|
+
ssrMiddlewares.push(input);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const getMiddlewares = () => ({
|
|
26
|
+
web: webMiddlewares,
|
|
27
|
+
api: apiMiddlewares,
|
|
28
|
+
ssr: ssrMiddlewares
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
getMiddlewares,
|
|
33
|
+
addWebMiddleware,
|
|
34
|
+
addAPIMiddleware,
|
|
35
|
+
addSSRMiddleware
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
exports.createMiddlewareCollecter = createMiddlewareCollecter;
|
|
40
|
+
|
|
41
|
+
const hook = attacher => attacher;
|
|
42
|
+
|
|
43
|
+
exports.hook = hook;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.requireModule = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = require("@modern-js/utils");
|
|
9
|
+
|
|
10
|
+
const FILE_EXTENSIONS = ['.ts', '.js'];
|
|
11
|
+
|
|
12
|
+
const requireModule = filename => {
|
|
13
|
+
const exist = (0, _utils.findExists)(FILE_EXTENSIONS.map(ext => `${filename}${ext}`));
|
|
14
|
+
|
|
15
|
+
if (!exist) {
|
|
16
|
+
return null;
|
|
17
|
+
} // throw errors directly if require fail
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
const mod = require(exist);
|
|
21
|
+
|
|
22
|
+
return interopRequire(mod);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
exports.requireModule = requireModule;
|
|
26
|
+
|
|
27
|
+
function interopRequire(obj) {
|
|
28
|
+
return interopRequireDefault(obj).default;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function interopRequireDefault(obj) {
|
|
32
|
+
return obj !== null && obj !== void 0 && obj.__esModule ? obj : {
|
|
33
|
+
default: obj
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 { getBabelChain } from '@modern-js/babel-preset-lib';
|
|
8
|
+
import { applyOptionsChain, fs, getAlias } from '@modern-js/utils';
|
|
9
|
+
import json5 from 'json5';
|
|
10
|
+
export * from '@babel/core';
|
|
11
|
+
export var readTsConfig = function readTsConfig(tsconfigPath) {
|
|
12
|
+
var noExistReturn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
13
|
+
|
|
14
|
+
// 如果不存在,则返回 noExistReturn
|
|
15
|
+
if (!fs.existsSync(tsconfigPath)) {
|
|
16
|
+
return noExistReturn;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
var content = fs.readFileSync(tsconfigPath, 'utf-8');
|
|
20
|
+
return json5.parse(content);
|
|
21
|
+
};
|
|
22
|
+
export var existTsConfigFile = function existTsConfigFile(tsconfigAbsolutePath) {
|
|
23
|
+
var tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
24
|
+
return Boolean(tsconfig);
|
|
25
|
+
};
|
|
26
|
+
export var getBabelConfig = function getBabelConfig(libPresetOption, syntaxOption) {
|
|
27
|
+
var chain = getBabelChain(libPresetOption, syntaxOption);
|
|
28
|
+
return _objectSpread({
|
|
29
|
+
sourceType: 'unambiguous'
|
|
30
|
+
}, chain.toJSON());
|
|
31
|
+
};
|
|
32
|
+
export var resolveBabelConfig = function resolveBabelConfig(appDirectory, modernConfig, option // FIXME: babel type can't pass type checking
|
|
33
|
+
) {
|
|
34
|
+
var _modernConfig$source = modernConfig.source,
|
|
35
|
+
envVars = _modernConfig$source.envVars,
|
|
36
|
+
globalVars = _modernConfig$source.globalVars,
|
|
37
|
+
_modernConfig$source$ = _modernConfig$source.jsxTransformRuntime,
|
|
38
|
+
jsxTransformRuntime = _modernConfig$source$ === void 0 ? 'automatic' : _modernConfig$source$,
|
|
39
|
+
userLodashOption = modernConfig.tools.lodash; // alias config
|
|
40
|
+
|
|
41
|
+
var aliasConfig = getAlias(modernConfig.source.alias, _objectSpread({
|
|
42
|
+
appDirectory: appDirectory
|
|
43
|
+
}, option)); // lodash config
|
|
44
|
+
|
|
45
|
+
var lodashOptions = applyOptionsChain({
|
|
46
|
+
id: ['lodash', 'ramda']
|
|
47
|
+
}, // TODO: 需要处理类型问题
|
|
48
|
+
userLodashOption); // babel config
|
|
49
|
+
|
|
50
|
+
var babelChain = getBabelChain({
|
|
51
|
+
appDirectory: appDirectory,
|
|
52
|
+
enableReactPreset: true,
|
|
53
|
+
enableTypescriptPreset: true,
|
|
54
|
+
alias: aliasConfig,
|
|
55
|
+
envVars: envVars,
|
|
56
|
+
globalVars: globalVars,
|
|
57
|
+
lodashOptions: lodashOptions,
|
|
58
|
+
jsxTransformRuntime: jsxTransformRuntime
|
|
59
|
+
}, {
|
|
60
|
+
type: option.type,
|
|
61
|
+
syntax: option.syntax
|
|
62
|
+
});
|
|
63
|
+
var envOptions = babelChain.preset('@babel/preset-env').options();
|
|
64
|
+
babelChain.preset('@babel/preset-env').use(require.resolve('@babel/preset-env'), [_objectSpread(_objectSpread({}, envOptions[0]), {}, {
|
|
65
|
+
loose: true
|
|
66
|
+
})]);
|
|
67
|
+
babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
|
|
68
|
+
babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
|
|
69
|
+
legacy: true
|
|
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
|
+
applyOptionsChain(internalBabelConfig, // TODO: 感觉 userBabelConfig 的类型应该是TransformOptions
|
|
79
|
+
userBabelConfig, {
|
|
80
|
+
chain: babelChain
|
|
81
|
+
});
|
|
82
|
+
return internalBabelConfig;
|
|
83
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { createMiddlewareCollecter } from "./middleware";
|
|
3
|
+
import { requireModule } from "./requireModule";
|
|
4
|
+
var API_DIR_PATH = 'api';
|
|
5
|
+
var API_APP_NAME = '_app';
|
|
6
|
+
var SERVER_DIR_PATH = 'server';
|
|
7
|
+
var WEB_APP_NAME = 'index';
|
|
8
|
+
export var gather = function gather(pwd) {
|
|
9
|
+
var _createMiddlewareColl = createMiddlewareCollecter(),
|
|
10
|
+
getMiddlewares = _createMiddlewareColl.getMiddlewares,
|
|
11
|
+
addAPIMiddleware = _createMiddlewareColl.addAPIMiddleware,
|
|
12
|
+
addWebMiddleware = _createMiddlewareColl.addWebMiddleware;
|
|
13
|
+
|
|
14
|
+
var apiPath = path.resolve(pwd, API_DIR_PATH);
|
|
15
|
+
var apiAppPath = path.resolve(apiPath, API_APP_NAME);
|
|
16
|
+
var serverPath = path.resolve(pwd, SERVER_DIR_PATH);
|
|
17
|
+
var webAppPath = path.resolve(serverPath, WEB_APP_NAME);
|
|
18
|
+
var apiAttacher = requireModule(apiAppPath);
|
|
19
|
+
|
|
20
|
+
if (apiAttacher) {
|
|
21
|
+
apiAttacher({
|
|
22
|
+
addMiddleware: addAPIMiddleware
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var webAttacher = requireModule(webAppPath);
|
|
27
|
+
|
|
28
|
+
if (webAttacher) {
|
|
29
|
+
webAttacher({
|
|
30
|
+
addMiddleware: addWebMiddleware
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return getMiddlewares();
|
|
35
|
+
};
|