@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.
Files changed (113) hide show
  1. package/README.md +3 -0
  2. package/assets/umi.png +0 -0
  3. package/compiled/body-parser/LICENSE +23 -0
  4. package/compiled/body-parser/connect/index.d.ts +93 -0
  5. package/compiled/body-parser/index.d.ts +107 -0
  6. package/compiled/body-parser/index.js +129 -0
  7. package/compiled/body-parser/package.json +1 -0
  8. package/compiled/multer/LICENSE +17 -0
  9. package/compiled/multer/body-parser/index.d.ts +104 -0
  10. package/compiled/multer/connect/index.d.ts +93 -0
  11. package/compiled/multer/express/index.d.ts +133 -0
  12. package/compiled/multer/express-serve-static-core/index.d.ts +1252 -0
  13. package/compiled/multer/index.d.ts +321 -0
  14. package/compiled/multer/index.js +41 -0
  15. package/compiled/multer/mime/index.d.ts +35 -0
  16. package/compiled/multer/package.json +1 -0
  17. package/compiled/multer/qs/index.d.ts +62 -0
  18. package/compiled/multer/range-parser/index.d.ts +35 -0
  19. package/compiled/multer/serve-static/index.d.ts +108 -0
  20. package/dist/commands/build.d.ts +3 -0
  21. package/dist/commands/build.js +120 -0
  22. package/dist/commands/config/config.d.ts +3 -0
  23. package/dist/commands/config/config.js +61 -0
  24. package/dist/commands/config/list.d.ts +2 -0
  25. package/dist/commands/config/list.js +34 -0
  26. package/dist/commands/config/remove.d.ts +1 -0
  27. package/dist/commands/config/remove.js +21 -0
  28. package/dist/commands/config/set.d.ts +1 -0
  29. package/dist/commands/config/set.js +21 -0
  30. package/dist/commands/dev/createRouteMiddleware.d.ts +5 -0
  31. package/dist/commands/dev/createRouteMiddleware.js +35 -0
  32. package/dist/commands/dev/dev.d.ts +3 -0
  33. package/dist/commands/dev/dev.js +269 -0
  34. package/dist/commands/dev/faviconMiddleware.d.ts +2 -0
  35. package/dist/commands/dev/faviconMiddleware.js +13 -0
  36. package/dist/commands/dev/getBabelOpts.d.ts +20 -0
  37. package/dist/commands/dev/getBabelOpts.js +57 -0
  38. package/dist/commands/dev/getMarkupArgs.d.ts +13 -0
  39. package/dist/commands/dev/getMarkupArgs.js +73 -0
  40. package/dist/commands/dev/watch.d.ts +19 -0
  41. package/dist/commands/dev/watch.js +55 -0
  42. package/dist/commands/generate/page.d.ts +3 -0
  43. package/dist/commands/generate/page.js +44 -0
  44. package/dist/commands/generate/prettier.d.ts +3 -0
  45. package/dist/commands/generate/prettier.js +31 -0
  46. package/dist/commands/help.d.ts +3 -0
  47. package/dist/commands/help.js +61 -0
  48. package/dist/commands/plugin.d.ts +3 -0
  49. package/dist/commands/plugin.js +38 -0
  50. package/dist/commands/setup.d.ts +3 -0
  51. package/dist/commands/setup.js +20 -0
  52. package/dist/commands/version.d.ts +3 -0
  53. package/dist/commands/version.js +16 -0
  54. package/dist/constants.d.ts +4 -0
  55. package/dist/constants.js +8 -0
  56. package/dist/features/__sample.d.ts +3 -0
  57. package/dist/features/__sample.js +5 -0
  58. package/dist/features/appData/appData.d.ts +3 -0
  59. package/dist/features/appData/appData.js +30 -0
  60. package/dist/features/check/check.d.ts +3 -0
  61. package/dist/features/check/check.js +34 -0
  62. package/dist/features/configPlugins/configPlugins.d.ts +3 -0
  63. package/dist/features/configPlugins/configPlugins.js +61 -0
  64. package/dist/features/configPlugins/schema.d.ts +2 -0
  65. package/dist/features/configPlugins/schema.js +16 -0
  66. package/dist/features/esmi/esmi.d.ts +3 -0
  67. package/dist/features/esmi/esmi.js +44 -0
  68. package/dist/features/favicon/favicon.d.ts +3 -0
  69. package/dist/features/favicon/favicon.js +58 -0
  70. package/dist/features/lowImport/babelPlugin.d.ts +17 -0
  71. package/dist/features/lowImport/babelPlugin.js +68 -0
  72. package/dist/features/lowImport/lowImport.d.ts +9 -0
  73. package/dist/features/lowImport/lowImport.js +108 -0
  74. package/dist/features/mock/constants.d.ts +3 -0
  75. package/dist/features/mock/constants.js +14 -0
  76. package/dist/features/mock/createMockMiddleware.d.ts +7 -0
  77. package/dist/features/mock/createMockMiddleware.js +82 -0
  78. package/dist/features/mock/getMockData.d.ts +9 -0
  79. package/dist/features/mock/getMockData.js +61 -0
  80. package/dist/features/mock/mock.d.ts +2 -0
  81. package/dist/features/mock/mock.js +50 -0
  82. package/dist/features/polyfill/polyfill.d.ts +3 -0
  83. package/dist/features/polyfill/polyfill.js +14 -0
  84. package/dist/features/tmpFiles/importsToStr.d.ts +4 -0
  85. package/dist/features/tmpFiles/importsToStr.js +16 -0
  86. package/dist/features/tmpFiles/routes.d.ts +8 -0
  87. package/dist/features/tmpFiles/routes.js +74 -0
  88. package/dist/features/tmpFiles/tmpFiles.d.ts +3 -0
  89. package/dist/features/tmpFiles/tmpFiles.js +124 -0
  90. package/dist/features/transform/babelPlugin.d.ts +11 -0
  91. package/dist/features/transform/babelPlugin.js +118 -0
  92. package/dist/features/transform/transform.d.ts +3 -0
  93. package/dist/features/transform/transform.js +29 -0
  94. package/dist/index.d.ts +5 -0
  95. package/dist/index.js +30 -0
  96. package/dist/libs/moduleGraph.d.ts +2 -0
  97. package/dist/libs/moduleGraph.js +6 -0
  98. package/dist/libs/scan.d.ts +28 -0
  99. package/dist/libs/scan.js +110 -0
  100. package/dist/registerMethods.d.ts +3 -0
  101. package/dist/registerMethods.js +89 -0
  102. package/dist/types.d.ts +147 -0
  103. package/dist/types.js +2 -0
  104. package/dist/utils/clearTmp.d.ts +1 -0
  105. package/dist/utils/clearTmp.js +17 -0
  106. package/dist/utils/isTypeScriptFile.d.ts +1 -0
  107. package/dist/utils/isTypeScriptFile.js +7 -0
  108. package/package.json +62 -0
  109. package/templates/generate/page/index.less.tpl +4 -0
  110. package/templates/generate/page/index.tsx.tpl +10 -0
  111. package/templates/plugin.tpl +18 -0
  112. package/templates/route.tpl +7 -0
  113. package/templates/umi.tpl +22 -0
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@umijs/utils");
4
+ exports.default = (api) => {
5
+ api.registerCommand({
6
+ name: 'help',
7
+ description: 'show commands help',
8
+ details: `
9
+ umi help build
10
+ umi help dev
11
+ `,
12
+ fn() {
13
+ const subCommand = api.args._[0];
14
+ if (subCommand) {
15
+ if (subCommand in api.service.commands) {
16
+ showHelp(api.service.commands[subCommand]);
17
+ }
18
+ else {
19
+ utils_1.logger.error(`Invalid sub command ${subCommand}.`);
20
+ }
21
+ }
22
+ else {
23
+ showHelps(api.service.commands);
24
+ }
25
+ },
26
+ });
27
+ function showHelp(command) {
28
+ console.log(`
29
+ Usage: umi ${command.name} [options]
30
+ ${command.description ? `${utils_1.chalk.gray(command.description)}.\n` : ''}
31
+ ${command.options ? `Options:\n${padLeft(command.options)}\n` : ''}
32
+ ${command.details ? `Details:\n${padLeft(command.details)}` : ''}
33
+ `);
34
+ }
35
+ function showHelps(commands) {
36
+ console.log(`
37
+ Usage: umi <command> [options]
38
+
39
+ Commands:
40
+
41
+ ${getDeps(commands)}
42
+ `);
43
+ console.log(`Run \`${utils_1.chalk.bold('umi help <command>')}\` for more information of specific commands.`);
44
+ console.log(`Visit ${utils_1.chalk.bold('https://umijs.org/')} to learn more about Umi.`);
45
+ console.log();
46
+ }
47
+ function getDeps(commands) {
48
+ return Object.keys(commands)
49
+ .map((key) => {
50
+ return ` ${utils_1.chalk.green(utils_1.lodash.padEnd(key, 10))}${commands[key].description || ''}`;
51
+ })
52
+ .join('\n');
53
+ }
54
+ function padLeft(str) {
55
+ return str
56
+ .trim()
57
+ .split('\n')
58
+ .map((line) => ` ${line}`)
59
+ .join('\n');
60
+ }
61
+ };
@@ -0,0 +1,3 @@
1
+ import { IApi } from '../types';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@umijs/utils");
4
+ exports.default = (api) => {
5
+ api.registerCommand({
6
+ name: 'plugin',
7
+ description: 'inspect umi plugins',
8
+ fn({ args }) {
9
+ const command = args._[0];
10
+ console.log('command', command);
11
+ if (!command) {
12
+ throw new Error(`
13
+ Sub command not found: umi plugin
14
+ Did you mean:
15
+ umi plugin list
16
+ `);
17
+ }
18
+ switch (command) {
19
+ case 'list':
20
+ getPluginList();
21
+ break;
22
+ default:
23
+ throw new Error(`Unsupported sub command ${command} for umi plugin.`);
24
+ }
25
+ function getPluginList() {
26
+ const localPlugins = ['./plugin.ts', './plugin.js'];
27
+ Object.keys(api.service.plugins).forEach((pluginId) => {
28
+ const plugin = api.service.plugins[pluginId];
29
+ if (localPlugins.includes(plugin.id))
30
+ return console.info(`- ${plugin.id} ${utils_1.chalk.greenBright('(from local)')}`);
31
+ if (plugin.id.startsWith('@umijs/preset'))
32
+ return console.info(`- ${plugin.id} ${utils_1.chalk.cyanBright('(from preset)')}`);
33
+ console.info(`- ${plugin.id}`);
34
+ });
35
+ }
36
+ },
37
+ });
38
+ };
@@ -0,0 +1,3 @@
1
+ import { IApi } from '../types';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@umijs/utils");
4
+ exports.default = (api) => {
5
+ api.registerCommand({
6
+ name: 'setup',
7
+ description: 'setup project',
8
+ fn() {
9
+ // generate tmp files
10
+ utils_1.logger.info('generate files');
11
+ api.applyPlugins({
12
+ key: 'onGenerateFiles',
13
+ args: {
14
+ files: null,
15
+ isFirstTime: false,
16
+ },
17
+ });
18
+ },
19
+ });
20
+ };
@@ -0,0 +1,3 @@
1
+ import { IApi } from '../types';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (api) => {
4
+ api.registerCommand({
5
+ name: 'version',
6
+ alias: 'v',
7
+ description: 'show umi version',
8
+ fn({ args }) {
9
+ const version = require('../../package.json').version;
10
+ if (!args.quiet) {
11
+ console.log(`umi@${version}`);
12
+ }
13
+ return version;
14
+ },
15
+ });
16
+ };
@@ -0,0 +1,4 @@
1
+ export declare const DEFAULT_PORT = "8000";
2
+ export declare const DEFAULT_HOST = "0.0.0.0";
3
+ export declare const CACHE_DIR_NAME = ".cache";
4
+ export declare const TEMPLATES_DIR: string;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TEMPLATES_DIR = exports.CACHE_DIR_NAME = exports.DEFAULT_HOST = exports.DEFAULT_PORT = void 0;
4
+ const path_1 = require("path");
5
+ exports.DEFAULT_PORT = '8000';
6
+ exports.DEFAULT_HOST = '0.0.0.0';
7
+ exports.CACHE_DIR_NAME = '.cache';
8
+ exports.TEMPLATES_DIR = (0, path_1.join)(__dirname, '../templates');
@@ -0,0 +1,3 @@
1
+ import { IApi } from '../types';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (api) => {
4
+ api;
5
+ };
@@ -0,0 +1,3 @@
1
+ import { IApi } from '../../types';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,30 @@
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 path_1 = require("path");
14
+ const routes_1 = require("../tmpFiles/routes");
15
+ exports.default = (api) => {
16
+ api.modifyAppData((memo) => __awaiter(void 0, void 0, void 0, function* () {
17
+ memo.routes = yield (0, routes_1.getRoutes)({
18
+ api,
19
+ });
20
+ memo.hasSrcDir = api.paths.absSrcPath.endsWith('/src');
21
+ memo.npmClient = api.userConfig.npmClient || (0, utils_1.getNpmClient)();
22
+ memo.umi = {
23
+ version: require('../../../package.json').version,
24
+ };
25
+ memo.react = {
26
+ version: require((0, path_1.join)(api.config.alias.react, 'package.json')).version,
27
+ };
28
+ return memo;
29
+ }));
30
+ };
@@ -0,0 +1,3 @@
1
+ import { IApi } from '../../types';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,34 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const assert_1 = __importDefault(require("assert"));
16
+ exports.default = (api) => {
17
+ api.onCheck(() => __awaiter(void 0, void 0, void 0, function* () {
18
+ // routes
19
+ (0, assert_1.default)(api.appData.routes, `routes not found, you may be run umi on the wrong directory.`);
20
+ yield api.applyPlugins({
21
+ key: 'onChangePkgJSON',
22
+ args: {
23
+ current: api.appData.pkg,
24
+ },
25
+ });
26
+ yield api.applyPlugins({
27
+ key: 'onCheckConfig',
28
+ args: {
29
+ config: api.config,
30
+ userConfig: api.userConfig,
31
+ },
32
+ });
33
+ }));
34
+ };
@@ -0,0 +1,3 @@
1
+ import { IApi } from '../../types';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const schema_1 = require("@umijs/bundler-webpack/dist/schema");
4
+ const utils_1 = require("@umijs/utils");
5
+ const path_1 = require("path");
6
+ const schema_2 = require("./schema");
7
+ function resolveProjectDep(opts) {
8
+ var _a, _b;
9
+ if (((_a = opts.pkg.dependencies) === null || _a === void 0 ? void 0 : _a[opts.dep]) ||
10
+ ((_b = opts.pkg.devDependencies) === null || _b === void 0 ? void 0 : _b[opts.dep])) {
11
+ return (0, path_1.dirname)(utils_1.resolve.sync(`${opts.dep}/package`, {
12
+ basedir: opts.cwd,
13
+ }));
14
+ }
15
+ }
16
+ exports.default = (api) => {
17
+ const configDefaults = {
18
+ alias: {
19
+ umi: process.env.UMI_DIR,
20
+ '@umijs/renderer-react': (0, path_1.dirname)(require.resolve('@umijs/renderer-react/package.json')),
21
+ react: resolveProjectDep({
22
+ pkg: api.pkg,
23
+ cwd: api.cwd,
24
+ dep: 'react',
25
+ }) || (0, path_1.dirname)(require.resolve('react/package.json')),
26
+ 'react-dom': resolveProjectDep({
27
+ pkg: api.pkg,
28
+ cwd: api.cwd,
29
+ dep: 'react-dom',
30
+ }) || (0, path_1.dirname)(require.resolve('react-dom/package.json')),
31
+ 'react-router': (0, path_1.dirname)(require.resolve('react-router/package.json')),
32
+ 'react-router-dom': (0, path_1.dirname)(require.resolve('react-router-dom/package.json')),
33
+ },
34
+ externals: {},
35
+ autoCSSModules: true,
36
+ publicPath: '/',
37
+ };
38
+ const bundleSchemas = (0, schema_1.getSchemas)();
39
+ const extraSchemas = (0, schema_2.getSchemas)();
40
+ const schemas = Object.assign(Object.assign({}, bundleSchemas), extraSchemas);
41
+ for (const key of Object.keys(schemas)) {
42
+ const config = {
43
+ schema: schemas[key] || ((joi) => joi.any()),
44
+ };
45
+ if (key in configDefaults) {
46
+ config.default = configDefaults[key];
47
+ }
48
+ api.registerPlugins([
49
+ {
50
+ id: `virtual: config-${key}`,
51
+ key: key,
52
+ config,
53
+ },
54
+ ]);
55
+ }
56
+ // api.paths is ready after register
57
+ api.modifyConfig((memo, args) => {
58
+ memo.alias = Object.assign(Object.assign({}, memo.alias), { '@': args.paths.absSrcPath, '@@': args.paths.absTmpPath });
59
+ return memo;
60
+ });
61
+ };
@@ -0,0 +1,2 @@
1
+ import type { Root } from '@umijs/core/compiled/@hapi/joi';
2
+ export declare function getSchemas(): Record<string, (Joi: Root) => any>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSchemas = void 0;
4
+ const utils_1 = require("@umijs/utils");
5
+ function getSchemas() {
6
+ return {
7
+ plugins: (Joi) => Joi.array().items(Joi.string()),
8
+ publicPath: (Joi) => Joi.string().regex(/\/$/).error(new Error('publicPath must end with /')),
9
+ favicon: (Joi) => Joi.string(),
10
+ headScripts: (Joi) => Joi.array().items(Joi.alternatives(Joi.string())),
11
+ scripts: (Joi) => Joi.array().items(Joi.alternatives(Joi.string())),
12
+ npmClient: (Joi) => Joi.string().valid(utils_1.NpmClientEnum.pnpm, utils_1.NpmClientEnum.tnpm, utils_1.NpmClientEnum.cnpm, utils_1.NpmClientEnum.yarn, utils_1.NpmClientEnum.npm),
13
+ routes: (Joi) => Joi.array().items(Joi.object()),
14
+ };
15
+ }
16
+ exports.getSchemas = getSchemas;
@@ -0,0 +1,3 @@
1
+ import { IApi } from '../../types';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,44 @@
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 path_1 = require("path");
13
+ const scan_1 = require("../../libs/scan");
14
+ exports.default = (api) => {
15
+ api.describe({
16
+ key: 'esmi',
17
+ config: {
18
+ schema(Joi) {
19
+ return Joi.object();
20
+ },
21
+ },
22
+ enableBy: api.EnableBy.config,
23
+ });
24
+ api.onStart(() => {
25
+ if (!api.args.vite) {
26
+ throw new Error(`esmi can only be used in vite mode.`);
27
+ }
28
+ });
29
+ // 需要在 generate 之后是因为需要先等临时文件生成
30
+ api.onBeforeCompiler(() => __awaiter(void 0, void 0, void 0, function* () {
31
+ // scan and module graph
32
+ // TODO: module graph
33
+ if (api.args.vite) {
34
+ const resolver = (0, scan_1.createResolver)({
35
+ alias: api.config.alias,
36
+ });
37
+ api.appData.deps = yield (0, scan_1.scan)({
38
+ entry: (0, path_1.join)(api.paths.absTmpPath, 'umi.ts'),
39
+ externals: api.config.externals,
40
+ resolver,
41
+ });
42
+ }
43
+ }));
44
+ };
@@ -0,0 +1,3 @@
1
+ import { IApi } from '../../types';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,58 @@
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 fs_1 = require("fs");
13
+ const path_1 = require("path");
14
+ const FAVICON_FILES = [
15
+ 'favicon.ico',
16
+ 'favicon.gif',
17
+ 'favicon.png',
18
+ 'favicon.jpg',
19
+ 'favicon.jpeg',
20
+ 'favicon.svg',
21
+ 'favicon.avif',
22
+ 'favicon.webp',
23
+ ];
24
+ function getFaviconFile(p) {
25
+ return FAVICON_FILES.find((f) => (0, fs_1.existsSync)((0, path_1.join)(p, f)));
26
+ }
27
+ exports.default = (api) => {
28
+ api.modifyAppData((memo) => __awaiter(void 0, void 0, void 0, function* () {
29
+ if (api.config.favicon)
30
+ return memo;
31
+ const faviconFile = getFaviconFile(api.paths.absSrcPath);
32
+ if (faviconFile) {
33
+ memo.faviconFile = faviconFile;
34
+ }
35
+ return memo;
36
+ }));
37
+ api.addMiddlewares(() => [
38
+ (req, res, next) => {
39
+ if (api.appData.faviconFile &&
40
+ req.path === `/${api.appData.faviconFile}`) {
41
+ res.sendFile((0, path_1.join)(api.paths.absSrcPath, api.appData.faviconFile));
42
+ }
43
+ else {
44
+ next();
45
+ }
46
+ },
47
+ ]);
48
+ api.onBuildComplete(({ err }) => {
49
+ if (err)
50
+ return;
51
+ if (api.appData.faviconFile) {
52
+ (0, fs_1.copyFileSync)((0, path_1.join)(api.paths.absSrcPath, api.appData.faviconFile), (0, path_1.join)(api.paths.absOutputPath, api.appData.faviconFile));
53
+ }
54
+ });
55
+ api.modifyHTMLFavicon((memo) => {
56
+ return `${api.config.publicPath}${api.appData.faviconFile}` || memo;
57
+ });
58
+ };
@@ -0,0 +1,17 @@
1
+ import * as Babel from '@umijs/bundler-utils/compiled/babel/core';
2
+ import * as t from '@umijs/bundler-utils/compiled/babel/types';
3
+ import { IOpts } from './lowImport';
4
+ interface IPluginOpts {
5
+ opts: IOpts;
6
+ }
7
+ export default function (): {
8
+ visitor: {
9
+ Identifier(path: Babel.NodePath<t.Identifier>, state: {
10
+ opts: IPluginOpts;
11
+ }): void;
12
+ MemberExpression(path: Babel.NodePath<t.MemberExpression>, state: {
13
+ opts: IPluginOpts;
14
+ }): void;
15
+ };
16
+ };
17
+ export {};
@@ -0,0 +1,68 @@
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 helper_module_imports_1 = require("@umijs/bundler-utils/compiled/babel/helper-module-imports");
23
+ const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
24
+ function default_1() {
25
+ return {
26
+ visitor: {
27
+ Identifier(path, state) {
28
+ var _a, _b;
29
+ const { name } = path.node;
30
+ if (path.scope.hasBinding(path.node.name)) {
31
+ return;
32
+ }
33
+ const parentNode = path.parentPath.node;
34
+ if (t.isImportSpecifier(parentNode) ||
35
+ t.isImportDefaultSpecifier(parentNode) ||
36
+ t.isImportNamespaceSpecifier(parentNode)) {
37
+ return;
38
+ }
39
+ if ((_a = state.opts.opts.identifierToLib) === null || _a === void 0 ? void 0 : _a[name]) {
40
+ path.replaceWith((0, helper_module_imports_1.addNamed)(path, name, state.opts.opts.identifierToLib[name]));
41
+ }
42
+ else if ((_b = state.opts.opts.defaultToLib) === null || _b === void 0 ? void 0 : _b[name]) {
43
+ path.replaceWith((0, helper_module_imports_1.addDefault)(path, state.opts.opts.defaultToLib[name], {
44
+ nameHint: name,
45
+ }));
46
+ }
47
+ // TODO: state.opts.opts.namespaceToLib
48
+ },
49
+ MemberExpression(path, state) {
50
+ const { object, property } = path.node;
51
+ if (path.scope.hasBinding(object.name)) {
52
+ return;
53
+ }
54
+ const parentNode = path.parentPath.node;
55
+ if (t.isImportSpecifier(parentNode) ||
56
+ t.isImportDefaultSpecifier(parentNode) ||
57
+ t.isImportNamespaceSpecifier(parentNode)) {
58
+ return;
59
+ }
60
+ if (state.opts.opts.withObjs[object.name] &&
61
+ (state.opts.opts.withObjs[object.name].members || []).includes(property.name)) {
62
+ path.replaceWith((0, helper_module_imports_1.addNamed)(path, property.name, state.opts.opts.withObjs[object.name].importFrom));
63
+ }
64
+ },
65
+ },
66
+ };
67
+ }
68
+ exports.default = default_1;
@@ -0,0 +1,9 @@
1
+ import { IApi } from '../../types';
2
+ export interface IOpts {
3
+ withObjs: Record<string, any>;
4
+ identifierToLib: Record<string, string>;
5
+ defaultToLib: Record<string, string>;
6
+ namespaceToLib: Record<string, string>;
7
+ }
8
+ declare const _default: (api: IApi) => void;
9
+ export default _default;
@@ -0,0 +1,108 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ // Inspired by
16
+ // - https://github.com/google/zx
17
+ // - https://github.com/antfu/unplugin-auto-import
18
+ const fs_1 = require("fs");
19
+ const path_1 = require("path");
20
+ const babelPlugin_1 = __importDefault(require("./babelPlugin"));
21
+ exports.default = (api) => {
22
+ api.describe({
23
+ key: 'lowImport',
24
+ config: {
25
+ schema(Joi) {
26
+ return Joi.object({
27
+ libs: Joi.array(),
28
+ });
29
+ },
30
+ },
31
+ enableBy: api.EnableBy.config,
32
+ });
33
+ api.modifyAppData((memo) => __awaiter(void 0, void 0, void 0, function* () {
34
+ memo.lowImport = [
35
+ yield api.applyPlugins({
36
+ key: 'addLowImportLibs',
37
+ initialValue: [],
38
+ }),
39
+ ...(api.config.lowImport.libs || []),
40
+ ];
41
+ }));
42
+ api.onStart(() => {
43
+ // generate dts
44
+ const dts = api.appData.lowImport.map((lib) => {
45
+ if (lib.withObj) {
46
+ const memberDts = (lib.members || [])
47
+ .map((member) => `${member}: typeof import('${lib.importFrom}')['${member}'],`)
48
+ .join('\n');
49
+ return `const ${lib.withObj} = {\n${memberDts}\n};`;
50
+ }
51
+ else if (lib.namespaceImport) {
52
+ return `const ${lib.namespaceImport}: typeof import('${lib.importFrom}');`;
53
+ }
54
+ else if (lib.defaultImport) {
55
+ return `const ${lib.defaultImport}: typeof import('${lib.importFrom}')['default'];`;
56
+ }
57
+ else {
58
+ return (lib.members || [])
59
+ .map((member) => `const ${member}: typeof import('${lib.importFrom}')['${member}'];`)
60
+ .join('\n');
61
+ }
62
+ });
63
+ const content = `
64
+ // generated by umi
65
+ declare global {
66
+ ${dts}
67
+ }
68
+ export {}
69
+ `.trim() + `\n`;
70
+ (0, fs_1.writeFileSync)((0, path_1.join)(api.paths.cwd, 'lowImport.d.ts'), content, 'utf-8');
71
+ });
72
+ api.addBeforeBabelPresets(() => {
73
+ const opts = normalizeLibs(api.appData.lowImport);
74
+ return [
75
+ {
76
+ plugins: [[babelPlugin_1.default, { opts }]],
77
+ },
78
+ ];
79
+ });
80
+ };
81
+ function normalizeLibs(libs) {
82
+ const withObjs = {};
83
+ const identifierToLib = {};
84
+ const defaultToLib = {};
85
+ const namespaceToLib = {};
86
+ for (const lib of libs) {
87
+ if (lib.withObj) {
88
+ withObjs[lib.withObj] = lib;
89
+ }
90
+ else if (lib.namespaceImport) {
91
+ namespaceToLib[lib.namespaceImport] = lib.importFrom;
92
+ }
93
+ else if (lib.defaultImport) {
94
+ defaultToLib[lib.defaultImport] = lib.importFrom;
95
+ }
96
+ else {
97
+ for (const member of lib.members || []) {
98
+ identifierToLib[member] = lib.importFrom;
99
+ }
100
+ }
101
+ }
102
+ return {
103
+ withObjs,
104
+ identifierToLib,
105
+ defaultToLib,
106
+ namespaceToLib,
107
+ };
108
+ }