@umijs/preset-umi 4.0.0-beta.10
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/README.md +3 -0
- package/assets/umi.png +0 -0
- package/compiled/body-parser/LICENSE +23 -0
- package/compiled/body-parser/connect/index.d.ts +93 -0
- package/compiled/body-parser/index.d.ts +107 -0
- package/compiled/body-parser/index.js +129 -0
- package/compiled/body-parser/package.json +1 -0
- package/compiled/multer/LICENSE +17 -0
- package/compiled/multer/body-parser/index.d.ts +104 -0
- package/compiled/multer/connect/index.d.ts +93 -0
- package/compiled/multer/express/index.d.ts +133 -0
- package/compiled/multer/express-serve-static-core/index.d.ts +1252 -0
- package/compiled/multer/index.d.ts +321 -0
- package/compiled/multer/index.js +41 -0
- package/compiled/multer/mime/index.d.ts +35 -0
- package/compiled/multer/package.json +1 -0
- package/compiled/multer/qs/index.d.ts +62 -0
- package/compiled/multer/range-parser/index.d.ts +35 -0
- package/compiled/multer/serve-static/index.d.ts +108 -0
- package/dist/commands/build.d.ts +3 -0
- package/dist/commands/build.js +120 -0
- package/dist/commands/config/config.d.ts +3 -0
- package/dist/commands/config/config.js +61 -0
- package/dist/commands/config/list.d.ts +2 -0
- package/dist/commands/config/list.js +34 -0
- package/dist/commands/config/remove.d.ts +1 -0
- package/dist/commands/config/remove.js +21 -0
- package/dist/commands/config/set.d.ts +1 -0
- package/dist/commands/config/set.js +21 -0
- package/dist/commands/dev/createRouteMiddleware.d.ts +5 -0
- package/dist/commands/dev/createRouteMiddleware.js +35 -0
- package/dist/commands/dev/dev.d.ts +3 -0
- package/dist/commands/dev/dev.js +269 -0
- package/dist/commands/dev/faviconMiddleware.d.ts +2 -0
- package/dist/commands/dev/faviconMiddleware.js +13 -0
- package/dist/commands/dev/getBabelOpts.d.ts +20 -0
- package/dist/commands/dev/getBabelOpts.js +57 -0
- package/dist/commands/dev/getMarkupArgs.d.ts +13 -0
- package/dist/commands/dev/getMarkupArgs.js +73 -0
- package/dist/commands/dev/watch.d.ts +19 -0
- package/dist/commands/dev/watch.js +55 -0
- package/dist/commands/generate/page.d.ts +3 -0
- package/dist/commands/generate/page.js +44 -0
- package/dist/commands/generate/prettier.d.ts +3 -0
- package/dist/commands/generate/prettier.js +31 -0
- package/dist/commands/help.d.ts +3 -0
- package/dist/commands/help.js +61 -0
- package/dist/commands/plugin.d.ts +3 -0
- package/dist/commands/plugin.js +38 -0
- package/dist/commands/setup.d.ts +3 -0
- package/dist/commands/setup.js +20 -0
- package/dist/commands/version.d.ts +3 -0
- package/dist/commands/version.js +16 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +8 -0
- package/dist/features/__sample.d.ts +3 -0
- package/dist/features/__sample.js +5 -0
- package/dist/features/appData/appData.d.ts +3 -0
- package/dist/features/appData/appData.js +30 -0
- package/dist/features/check/check.d.ts +3 -0
- package/dist/features/check/check.js +34 -0
- package/dist/features/configPlugins/configPlugins.d.ts +3 -0
- package/dist/features/configPlugins/configPlugins.js +61 -0
- package/dist/features/configPlugins/schema.d.ts +2 -0
- package/dist/features/configPlugins/schema.js +16 -0
- package/dist/features/esmi/esmi.d.ts +3 -0
- package/dist/features/esmi/esmi.js +44 -0
- package/dist/features/favicon/favicon.d.ts +3 -0
- package/dist/features/favicon/favicon.js +58 -0
- package/dist/features/lowImport/babelPlugin.d.ts +17 -0
- package/dist/features/lowImport/babelPlugin.js +68 -0
- package/dist/features/lowImport/lowImport.d.ts +9 -0
- package/dist/features/lowImport/lowImport.js +108 -0
- package/dist/features/mock/constants.d.ts +3 -0
- package/dist/features/mock/constants.js +14 -0
- package/dist/features/mock/createMockMiddleware.d.ts +7 -0
- package/dist/features/mock/createMockMiddleware.js +82 -0
- package/dist/features/mock/getMockData.d.ts +9 -0
- package/dist/features/mock/getMockData.js +61 -0
- package/dist/features/mock/mock.d.ts +2 -0
- package/dist/features/mock/mock.js +50 -0
- package/dist/features/polyfill/polyfill.d.ts +3 -0
- package/dist/features/polyfill/polyfill.js +14 -0
- package/dist/features/tmpFiles/importsToStr.d.ts +4 -0
- package/dist/features/tmpFiles/importsToStr.js +16 -0
- package/dist/features/tmpFiles/routes.d.ts +8 -0
- package/dist/features/tmpFiles/routes.js +74 -0
- package/dist/features/tmpFiles/tmpFiles.d.ts +3 -0
- package/dist/features/tmpFiles/tmpFiles.js +124 -0
- package/dist/features/transform/babelPlugin.d.ts +11 -0
- package/dist/features/transform/babelPlugin.js +118 -0
- package/dist/features/transform/transform.d.ts +3 -0
- package/dist/features/transform/transform.js +29 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +30 -0
- package/dist/libs/moduleGraph.d.ts +2 -0
- package/dist/libs/moduleGraph.js +6 -0
- package/dist/libs/scan.d.ts +28 -0
- package/dist/libs/scan.js +110 -0
- package/dist/registerMethods.d.ts +3 -0
- package/dist/registerMethods.js +89 -0
- package/dist/types.d.ts +147 -0
- package/dist/types.js +2 -0
- package/dist/utils/clearTmp.d.ts +1 -0
- package/dist/utils/clearTmp.js +17 -0
- package/dist/utils/isTypeScriptFile.d.ts +1 -0
- package/dist/utils/isTypeScriptFile.js +7 -0
- package/package.json +62 -0
- package/templates/generate/page/index.less.tpl +4 -0
- package/templates/generate/page/index.tsx.tpl +10 -0
- package/templates/plugin.tpl +18 -0
- package/templates/route.tpl +7 -0
- package/templates/umi.tpl +22 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MOCK_FILE_GLOB = exports.DEFAULT_METHOD = exports.VALID_METHODS = void 0;
|
|
4
|
+
exports.VALID_METHODS = [
|
|
5
|
+
'GET',
|
|
6
|
+
'POST',
|
|
7
|
+
'PUT',
|
|
8
|
+
'DELETE',
|
|
9
|
+
'PATCH',
|
|
10
|
+
'HEAD',
|
|
11
|
+
'OPTIONS',
|
|
12
|
+
];
|
|
13
|
+
exports.DEFAULT_METHOD = 'GET';
|
|
14
|
+
exports.MOCK_FILE_GLOB = 'mock/**/*.[jt]s';
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createMockMiddleware = void 0;
|
|
7
|
+
const path_to_regexp_1 = __importDefault(require("path-to-regexp"));
|
|
8
|
+
const body_parser_1 = __importDefault(require("../../../compiled/body-parser"));
|
|
9
|
+
const multer_1 = __importDefault(require("../../../compiled/multer"));
|
|
10
|
+
function createMockMiddleware(opts) {
|
|
11
|
+
return (req, res, next) => {
|
|
12
|
+
const method = req.method.toUpperCase();
|
|
13
|
+
for (const key of Object.keys(opts.context.mockData)) {
|
|
14
|
+
const mock = opts.context.mockData[key];
|
|
15
|
+
if (mock.method !== method)
|
|
16
|
+
continue;
|
|
17
|
+
const { keys, re } = getPathReAndKeys(mock.path);
|
|
18
|
+
const m = re.exec(req.path);
|
|
19
|
+
if (m) {
|
|
20
|
+
if (typeof mock.handler === 'function') {
|
|
21
|
+
// add params
|
|
22
|
+
const params = {};
|
|
23
|
+
for (let i = 1; i < m.length; i += 1) {
|
|
24
|
+
const key = keys[i - 1];
|
|
25
|
+
const prop = key.name;
|
|
26
|
+
const val = decodeParam(m[i]);
|
|
27
|
+
if (val !== undefined) {
|
|
28
|
+
params[prop] = val;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
req.params = params;
|
|
32
|
+
// handler
|
|
33
|
+
if (method === 'GET') {
|
|
34
|
+
mock.handler(req, res, next);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const jsonOpts = { limit: '5mb', strict: false };
|
|
38
|
+
const urlEncodedOpts = { limit: '5mb', extended: true };
|
|
39
|
+
// body parser + multer
|
|
40
|
+
body_parser_1.default.json(jsonOpts)(req, res, () => {
|
|
41
|
+
body_parser_1.default.urlencoded(urlEncodedOpts)(req, res, () => {
|
|
42
|
+
(0, multer_1.default)().any()(req, res, () => {
|
|
43
|
+
mock.handler(req, res, next);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
res.status(200).json(mock.handler);
|
|
51
|
+
}
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
next();
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
exports.createMockMiddleware = createMockMiddleware;
|
|
59
|
+
// TODO: cache
|
|
60
|
+
function getPathReAndKeys(path) {
|
|
61
|
+
const keys = [];
|
|
62
|
+
const re = (0, path_to_regexp_1.default)(path, keys);
|
|
63
|
+
return { re, keys };
|
|
64
|
+
}
|
|
65
|
+
function decodeParam(val) {
|
|
66
|
+
if (typeof val !== 'string' || val.length === 0) {
|
|
67
|
+
return val;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
return decodeURIComponent(val);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
if (err instanceof URIError) {
|
|
74
|
+
err.message = `Failed to decode param ' ${val} '`;
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
err.status = 400;
|
|
77
|
+
// @ts-ignore
|
|
78
|
+
err.statusCode = 400;
|
|
79
|
+
}
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getMockData = void 0;
|
|
7
|
+
const esbuild_1 = __importDefault(require("@umijs/bundler-utils/compiled/esbuild"));
|
|
8
|
+
const utils_1 = require("@umijs/utils");
|
|
9
|
+
const assert_1 = __importDefault(require("assert"));
|
|
10
|
+
const constants_1 = require("./constants");
|
|
11
|
+
function getMockData(opts) {
|
|
12
|
+
utils_1.register.register({
|
|
13
|
+
implementor: esbuild_1.default,
|
|
14
|
+
});
|
|
15
|
+
utils_1.register.clearFiles();
|
|
16
|
+
const ret = utils_1.glob
|
|
17
|
+
.sync(constants_1.MOCK_FILE_GLOB, { cwd: opts.cwd })
|
|
18
|
+
.reduce((memo, file) => {
|
|
19
|
+
const mockFile = `${opts.cwd}/${file}`;
|
|
20
|
+
const m = require(mockFile);
|
|
21
|
+
const obj = m.default;
|
|
22
|
+
for (const key of Object.keys(obj)) {
|
|
23
|
+
const mock = getMock({ key, obj });
|
|
24
|
+
mock.file = mockFile;
|
|
25
|
+
// check conflict
|
|
26
|
+
const id = `${mock.method} ${mock.path}`;
|
|
27
|
+
(0, assert_1.default)(utils_1.lodash.isArray(mock.handler) ||
|
|
28
|
+
utils_1.lodash.isPlainObject(mock.handler) ||
|
|
29
|
+
typeof mock.handler === 'function', `Mock handler must be function or array or object, but got ${typeof mock.handler} for ${mock.method} in ${mock.file}`);
|
|
30
|
+
if (memo[id]) {
|
|
31
|
+
throw new Error(`${id} is duplicated in ${mockFile} and ${memo[id].file}`);
|
|
32
|
+
}
|
|
33
|
+
memo[id] = mock;
|
|
34
|
+
}
|
|
35
|
+
return memo;
|
|
36
|
+
}, {});
|
|
37
|
+
for (const file of utils_1.register.getFiles()) {
|
|
38
|
+
delete require.cache[file];
|
|
39
|
+
}
|
|
40
|
+
utils_1.register.restore();
|
|
41
|
+
return ret;
|
|
42
|
+
}
|
|
43
|
+
exports.getMockData = getMockData;
|
|
44
|
+
function getMock(opts) {
|
|
45
|
+
const { method, path } = parseKey(opts.key);
|
|
46
|
+
const handler = opts.obj[opts.key];
|
|
47
|
+
return { method, path, handler };
|
|
48
|
+
}
|
|
49
|
+
function parseKey(key) {
|
|
50
|
+
const spliced = key.split(' ');
|
|
51
|
+
const len = spliced.length;
|
|
52
|
+
if (len === 1) {
|
|
53
|
+
return { method: constants_1.DEFAULT_METHOD, path: key };
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const [method, path] = spliced;
|
|
57
|
+
const upperCaseMethod = method.toUpperCase();
|
|
58
|
+
(0, assert_1.default)(constants_1.VALID_METHODS.includes(upperCaseMethod), `method ${method} is not supported`);
|
|
59
|
+
return { method: upperCaseMethod, path };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const watch_1 = require("../../commands/dev/watch");
|
|
13
|
+
const createMockMiddleware_1 = require("./createMockMiddleware");
|
|
14
|
+
const getMockData_1 = require("./getMockData");
|
|
15
|
+
function default_1(api) {
|
|
16
|
+
api.describe({
|
|
17
|
+
key: 'mock',
|
|
18
|
+
config: {
|
|
19
|
+
schema(Joi) {
|
|
20
|
+
return Joi.object().keys({
|
|
21
|
+
exclude: Joi.array()
|
|
22
|
+
.items(Joi.string())
|
|
23
|
+
.description('exclude files not parse mock'),
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
// context for update mockData
|
|
29
|
+
const context = {
|
|
30
|
+
mockData: null,
|
|
31
|
+
};
|
|
32
|
+
api.onStart(() => {
|
|
33
|
+
(0, watch_1.watch)({
|
|
34
|
+
path: `${api.cwd}/mock`,
|
|
35
|
+
addToUnWatches: true,
|
|
36
|
+
onChange: () => {
|
|
37
|
+
context.mockData = (0, getMockData_1.getMockData)({ cwd: api.cwd });
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
api.addBeforeMiddlewares(() => __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
context.mockData = (0, getMockData_1.getMockData)({ cwd: api.cwd });
|
|
43
|
+
return [
|
|
44
|
+
(0, createMockMiddleware_1.createMockMiddleware)({
|
|
45
|
+
context,
|
|
46
|
+
}),
|
|
47
|
+
];
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = (api) => {
|
|
4
|
+
api.addPolyfillImports(() => {
|
|
5
|
+
var _a;
|
|
6
|
+
return api.config.publicPath === 'auto' && ((_a = api.config.targets) === null || _a === void 0 ? void 0 : _a.ie)
|
|
7
|
+
? [
|
|
8
|
+
{
|
|
9
|
+
source: require.resolve('current-script-polyfill'),
|
|
10
|
+
},
|
|
11
|
+
]
|
|
12
|
+
: [];
|
|
13
|
+
});
|
|
14
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.importsToStr = void 0;
|
|
4
|
+
const winPath_1 = require("@umijs/utils/dist/winPath");
|
|
5
|
+
function importsToStr(imports) {
|
|
6
|
+
return imports.map((imp) => {
|
|
7
|
+
const { source, specifier } = imp;
|
|
8
|
+
if (specifier) {
|
|
9
|
+
return `import ${specifier} from '${(0, winPath_1.winPath)(source)}';`;
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
return `import '${(0, winPath_1.winPath)(source)}';`;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
exports.importsToStr = importsToStr;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getRouteComponents = exports.getRoutes = void 0;
|
|
13
|
+
const core_1 = require("@umijs/core");
|
|
14
|
+
const utils_1 = require("@umijs/utils");
|
|
15
|
+
const fs_1 = require("fs");
|
|
16
|
+
const path_1 = require("path");
|
|
17
|
+
// get route config
|
|
18
|
+
function getRoutes(opts) {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
let routes = null;
|
|
21
|
+
if (opts.api.config.routes) {
|
|
22
|
+
routes = (0, core_1.getConfigRoutes)({
|
|
23
|
+
routes: opts.api.config.routes,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
routes = (0, core_1.getConventionRoutes)({
|
|
28
|
+
base: opts.api.paths.absPagesPath,
|
|
29
|
+
prefix: '',
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const absLayoutPath = (0, path_1.join)(opts.api.paths.absSrcPath, 'layouts/index.tsx');
|
|
33
|
+
const layouts = yield opts.api.applyPlugins({
|
|
34
|
+
key: 'addLayouts',
|
|
35
|
+
initialValue: [
|
|
36
|
+
(0, fs_1.existsSync)(absLayoutPath) && {
|
|
37
|
+
id: '@@/global-layout',
|
|
38
|
+
file: absLayoutPath,
|
|
39
|
+
},
|
|
40
|
+
].filter(Boolean),
|
|
41
|
+
});
|
|
42
|
+
for (const layout of layouts) {
|
|
43
|
+
(0, core_1.addParentRoute)({
|
|
44
|
+
addToAll: true,
|
|
45
|
+
target: {
|
|
46
|
+
id: layout.id,
|
|
47
|
+
path: '/',
|
|
48
|
+
file: layout.file,
|
|
49
|
+
parentId: undefined,
|
|
50
|
+
},
|
|
51
|
+
routes,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return routes;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
exports.getRoutes = getRoutes;
|
|
58
|
+
function getRouteComponents(opts) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const imports = Object.keys(opts.routes)
|
|
61
|
+
.map((key) => {
|
|
62
|
+
const route = opts.routes[key];
|
|
63
|
+
if (!route.file)
|
|
64
|
+
return `// ${key}: no file to import`;
|
|
65
|
+
const path = (0, path_1.isAbsolute)(route.file) || route.file.startsWith('@/')
|
|
66
|
+
? route.file
|
|
67
|
+
: `${opts.prefix}${route.file}`;
|
|
68
|
+
return `'${key}': () => import('${(0, utils_1.winPath)(path)}'),`;
|
|
69
|
+
})
|
|
70
|
+
.join('\n');
|
|
71
|
+
return `{\n${imports}\n}`;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
exports.getRouteComponents = getRouteComponents;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const utils_1 = require("@umijs/utils");
|
|
13
|
+
const fs_1 = require("fs");
|
|
14
|
+
const path_1 = require("path");
|
|
15
|
+
const constants_1 = require("../../constants");
|
|
16
|
+
const importsToStr_1 = require("./importsToStr");
|
|
17
|
+
const routes_1 = require("./routes");
|
|
18
|
+
exports.default = (api) => {
|
|
19
|
+
api.describe({
|
|
20
|
+
key: 'tmpFiles',
|
|
21
|
+
config: {
|
|
22
|
+
schema(Joi) {
|
|
23
|
+
return Joi.boolean();
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
api.onGenerateFiles((opts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
|
+
// umi.ts
|
|
29
|
+
api.writeTmpFile({
|
|
30
|
+
path: 'umi.ts',
|
|
31
|
+
tplPath: (0, path_1.join)(constants_1.TEMPLATES_DIR, 'umi.tpl'),
|
|
32
|
+
context: {
|
|
33
|
+
rendererPath: yield api.applyPlugins({
|
|
34
|
+
key: 'modifyRendererPath',
|
|
35
|
+
initialValue: '@umijs/renderer-react',
|
|
36
|
+
}),
|
|
37
|
+
entryCode: (yield api.applyPlugins({
|
|
38
|
+
key: 'addEntryCode',
|
|
39
|
+
initialValue: [],
|
|
40
|
+
})).join('\n'),
|
|
41
|
+
entryCodeAhead: (yield api.applyPlugins({
|
|
42
|
+
key: 'addEntryCodeAhead',
|
|
43
|
+
initialValue: [],
|
|
44
|
+
})).join('\n'),
|
|
45
|
+
polyfillImports: (0, importsToStr_1.importsToStr)(yield api.applyPlugins({
|
|
46
|
+
key: 'addPolyfillImports',
|
|
47
|
+
initialValue: [],
|
|
48
|
+
})).join('\n'),
|
|
49
|
+
importsAhead: (0, importsToStr_1.importsToStr)(yield api.applyPlugins({
|
|
50
|
+
key: 'addEntryImportsAhead',
|
|
51
|
+
initialValue: [],
|
|
52
|
+
})).join('\n'),
|
|
53
|
+
imports: (0, importsToStr_1.importsToStr)(yield api.applyPlugins({
|
|
54
|
+
key: 'addEntryImports',
|
|
55
|
+
initialValue: [],
|
|
56
|
+
})).join('\n'),
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
// route.ts
|
|
60
|
+
let routes;
|
|
61
|
+
if (opts.isFirstTime) {
|
|
62
|
+
routes = api.appData.routes;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
routes = yield (0, routes_1.getRoutes)({
|
|
66
|
+
api,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const hasSrc = api.appData.hasSrcDir;
|
|
70
|
+
// @/pages/
|
|
71
|
+
const prefix = hasSrc ? '../../../src/pages/' : '../../pages/';
|
|
72
|
+
api.writeTmpFile({
|
|
73
|
+
path: 'core/route.ts',
|
|
74
|
+
tplPath: (0, path_1.join)(constants_1.TEMPLATES_DIR, 'route.tpl'),
|
|
75
|
+
context: {
|
|
76
|
+
routes: JSON.stringify(routes),
|
|
77
|
+
routeComponents: yield (0, routes_1.getRouteComponents)({ routes, prefix }),
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
// plugin.ts
|
|
81
|
+
const plugins = yield api.applyPlugins({
|
|
82
|
+
key: 'addRuntimePlugin',
|
|
83
|
+
initialValue: [
|
|
84
|
+
// TODO: add tryFiles in @umijs/utils
|
|
85
|
+
(0, fs_1.existsSync)((0, path_1.join)(api.paths.absSrcPath, 'app.ts')) &&
|
|
86
|
+
(0, path_1.join)(api.paths.absSrcPath, 'app.ts'),
|
|
87
|
+
(0, fs_1.existsSync)((0, path_1.join)(api.paths.absSrcPath, 'app.tsx')) &&
|
|
88
|
+
(0, path_1.join)(api.paths.absSrcPath, 'app.tsx'),
|
|
89
|
+
(0, fs_1.existsSync)((0, path_1.join)(api.paths.absSrcPath, 'app.jsx')) &&
|
|
90
|
+
(0, path_1.join)(api.paths.absSrcPath, 'app.jsx'),
|
|
91
|
+
(0, fs_1.existsSync)((0, path_1.join)(api.paths.absSrcPath, 'app.js')) &&
|
|
92
|
+
(0, path_1.join)(api.paths.absSrcPath, 'app.js'),
|
|
93
|
+
]
|
|
94
|
+
.filter(Boolean)
|
|
95
|
+
.slice(0, 1),
|
|
96
|
+
});
|
|
97
|
+
const validKeys = yield api.applyPlugins({
|
|
98
|
+
key: 'addRuntimePluginKey',
|
|
99
|
+
initialValue: [
|
|
100
|
+
// TODO: support these methods
|
|
101
|
+
// 'modifyClientRenderOpts',
|
|
102
|
+
// 'patchRoutes',
|
|
103
|
+
'rootContainer',
|
|
104
|
+
'innerProvider',
|
|
105
|
+
'i18nProvider',
|
|
106
|
+
'dataflowProvider',
|
|
107
|
+
'outerProvider',
|
|
108
|
+
// 'render',
|
|
109
|
+
// 'onRouteChange',
|
|
110
|
+
],
|
|
111
|
+
});
|
|
112
|
+
api.writeTmpFile({
|
|
113
|
+
path: 'core/plugin.ts',
|
|
114
|
+
tplPath: (0, path_1.join)(constants_1.TEMPLATES_DIR, 'plugin.tpl'),
|
|
115
|
+
context: {
|
|
116
|
+
plugins: plugins.map((plugin, index) => ({
|
|
117
|
+
index,
|
|
118
|
+
path: (0, utils_1.winPath)(plugin),
|
|
119
|
+
})),
|
|
120
|
+
validKeys: validKeys,
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
}));
|
|
124
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as Babel from '@umijs/bundler-utils/compiled/babel/core';
|
|
2
|
+
import * as t from '@umijs/bundler-utils/compiled/babel/types';
|
|
3
|
+
export default function (): {
|
|
4
|
+
pre(): void;
|
|
5
|
+
post(state: any): void;
|
|
6
|
+
visitor: {
|
|
7
|
+
Program: {
|
|
8
|
+
enter(path: Babel.NodePath<t.Program>, state: any): void;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
|
|
23
|
+
const utils_1 = require("@umijs/utils");
|
|
24
|
+
const path_1 = require("path");
|
|
25
|
+
function isModuleExports(node) {
|
|
26
|
+
return (t.isMemberExpression(node) &&
|
|
27
|
+
t.isIdentifier(node.object, { name: 'module' }) &&
|
|
28
|
+
t.isIdentifier(node.property, { name: 'exports' }));
|
|
29
|
+
}
|
|
30
|
+
function isExportsMemberExpression(node) {
|
|
31
|
+
return (t.isMemberExpression(node) &&
|
|
32
|
+
t.isIdentifier(node.object, { name: 'exports' }));
|
|
33
|
+
}
|
|
34
|
+
function default_1() {
|
|
35
|
+
let opts;
|
|
36
|
+
return {
|
|
37
|
+
pre() {
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
this.cache = new Map();
|
|
40
|
+
},
|
|
41
|
+
post(state) {
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
const { cache } = this;
|
|
44
|
+
const filename = (0, utils_1.winPath)(state.opts.filename);
|
|
45
|
+
if (cache.has(filename) &&
|
|
46
|
+
!filename.includes('bundler-webpack/client') &&
|
|
47
|
+
!filename.startsWith((0, utils_1.winPath)((0, path_1.join)(opts.cwd, 'node_modules')))) {
|
|
48
|
+
opts.onCheckCode({
|
|
49
|
+
args: Object.assign(Object.assign({}, cache.get(state.opts.filename)), { file: state.opts.filename, isFromTmp: state.opts.filename.startsWith(opts.absTmpPath) }),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
visitor: {
|
|
54
|
+
Program: {
|
|
55
|
+
enter(path, state) {
|
|
56
|
+
opts = state.opts;
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
const file = path === null || path === void 0 ? void 0 : path.hub.file.opts.filename;
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
const cache = this.cache;
|
|
61
|
+
// reset cache
|
|
62
|
+
cache.set(file, {
|
|
63
|
+
code: path.hub.getCode(),
|
|
64
|
+
imports: [],
|
|
65
|
+
cjsExports: [],
|
|
66
|
+
});
|
|
67
|
+
path.node.body.forEach((node) => {
|
|
68
|
+
// import x from 'x'; { default: 'x' }
|
|
69
|
+
// import * as x2 from 'x'; { namespace: 'x2' }
|
|
70
|
+
// import x3, * as xx from 'x'; { default: 'x3', namespace: 'xx' }
|
|
71
|
+
// import { x4, a as b } from 'x'; { specifiers: { x4: 'x4', a: 'b' } }
|
|
72
|
+
if (t.isImportDeclaration(node)) {
|
|
73
|
+
const ret = {
|
|
74
|
+
source: node.source.value,
|
|
75
|
+
loc: node.loc,
|
|
76
|
+
};
|
|
77
|
+
node.specifiers.forEach((specifier) => {
|
|
78
|
+
if (t.isImportDefaultSpecifier(specifier)) {
|
|
79
|
+
ret.default = specifier.local.name;
|
|
80
|
+
}
|
|
81
|
+
else if (t.isImportNamespaceSpecifier(specifier)) {
|
|
82
|
+
ret.namespace = specifier.local.name;
|
|
83
|
+
}
|
|
84
|
+
else if (t.isImportSpecifier(specifier)) {
|
|
85
|
+
ret.specifiers || (ret.specifiers = {});
|
|
86
|
+
ret.specifiers[t.isIdentifier(specifier.imported)
|
|
87
|
+
? specifier.imported.name
|
|
88
|
+
: specifier.imported.value] = specifier.local.name;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
cache.get(file).imports.push(ret);
|
|
92
|
+
}
|
|
93
|
+
if (t.isExpressionStatement(node)) {
|
|
94
|
+
const n = node;
|
|
95
|
+
// module.exports = x;
|
|
96
|
+
if (t.isAssignmentExpression(n.expression) &&
|
|
97
|
+
isModuleExports(n.expression.left)) {
|
|
98
|
+
cache.get(file).cjsExports.push('default');
|
|
99
|
+
}
|
|
100
|
+
// exports.x = x;
|
|
101
|
+
if (t.isAssignmentExpression(n.expression) &&
|
|
102
|
+
isExportsMemberExpression(n.expression.left)) {
|
|
103
|
+
cache.get(file).cjsExports.push(
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
n.expression.left.property.name);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// TODO:
|
|
109
|
+
// exports
|
|
110
|
+
// callExpression
|
|
111
|
+
// newExpression
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const babelPlugin_1 = __importDefault(require("./babelPlugin"));
|
|
7
|
+
exports.default = (api) => {
|
|
8
|
+
api.addBeforeBabelPresets(() => {
|
|
9
|
+
return [
|
|
10
|
+
{
|
|
11
|
+
plugins: [
|
|
12
|
+
[
|
|
13
|
+
babelPlugin_1.default,
|
|
14
|
+
{
|
|
15
|
+
cwd: api.cwd,
|
|
16
|
+
absTmpPath: api.paths.absTmpPath,
|
|
17
|
+
onCheckCode({ args }) {
|
|
18
|
+
api.applyPlugins({
|
|
19
|
+
key: 'onCheckCode',
|
|
20
|
+
args: args,
|
|
21
|
+
});
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
];
|
|
28
|
+
});
|
|
29
|
+
};
|