@umijs/preset-umi 4.0.0-beta.13 → 4.0.0-beta.14

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.
@@ -2,6 +2,8 @@ import { IApi } from '../../types';
2
2
  export declare function getMarkupArgs(opts: {
3
3
  api: IApi;
4
4
  }): Promise<{
5
+ mountElementId: any;
6
+ base: any;
5
7
  routes: any;
6
8
  favicon: any;
7
9
  headScripts: any;
@@ -41,6 +41,8 @@ function getMarkupArgs(opts) {
41
41
  initialValue: opts.api.config.favicon,
42
42
  });
43
43
  return {
44
+ mountElementId: opts.api.config.mountElementId,
45
+ base: opts.api.config.base,
44
46
  routes: opts.api.appData.routes,
45
47
  favicon,
46
48
  headScripts,
@@ -34,6 +34,8 @@ exports.default = (api) => {
34
34
  externals: {},
35
35
  autoCSSModules: true,
36
36
  publicPath: '/',
37
+ mountElementId: 'root',
38
+ base: '/',
37
39
  };
38
40
  const bundleSchemas = (0, schema_1.getSchemas)();
39
41
  const extraSchemas = (0, schema_2.getSchemas)();
@@ -55,7 +57,9 @@ exports.default = (api) => {
55
57
  }
56
58
  // api.paths is ready after register
57
59
  api.modifyConfig((memo, args) => {
58
- memo.alias = Object.assign(Object.assign({}, memo.alias), { '@': args.paths.absSrcPath, '@@': args.paths.absTmpPath });
60
+ memo.alias = Object.assign(Object.assign({}, memo.alias), { '@': args.paths.absSrcPath, '@@': args.paths.absTmpPath,
61
+ // like vite, use to pre-bundling dependencies in vite mode
62
+ '@fs': '/' });
59
63
  return memo;
60
64
  });
61
65
  };
@@ -4,11 +4,13 @@ exports.getSchemas = void 0;
4
4
  const utils_1 = require("@umijs/utils");
5
5
  function getSchemas() {
6
6
  return {
7
+ base: (Joi) => Joi.string(),
7
8
  plugins: (Joi) => Joi.array().items(Joi.string()),
8
9
  publicPath: (Joi) => Joi.string().regex(/\/$/).error(new Error('publicPath must end with /')),
9
10
  favicon: (Joi) => Joi.string(),
10
11
  headScripts: (Joi) => Joi.array().items(Joi.alternatives(Joi.string())),
11
12
  scripts: (Joi) => Joi.array().items(Joi.alternatives(Joi.string())),
13
+ mountElementId: (Joi) => Joi.string(),
12
14
  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
15
  routes: (Joi) => Joi.array().items(Joi.object()),
14
16
  };
@@ -53,6 +53,8 @@ exports.default = (api) => {
53
53
  }
54
54
  });
55
55
  api.modifyHTMLFavicon((memo) => {
56
- return `${api.config.publicPath}${api.appData.faviconFile}` || memo;
56
+ return api.appData.faviconFile
57
+ ? `${api.config.publicPath}${api.appData.faviconFile}`
58
+ : memo;
57
59
  });
58
60
  };
@@ -3,11 +3,13 @@ import * as t from '@umijs/bundler-utils/compiled/babel/types';
3
3
  import { IOpts } from './lowImport';
4
4
  interface IPluginOpts {
5
5
  opts: IOpts;
6
+ css: string;
6
7
  }
7
8
  export default function (): {
8
9
  visitor: {
9
10
  Identifier(path: Babel.NodePath<t.Identifier>, state: {
10
11
  opts: IPluginOpts;
12
+ file: any;
11
13
  }): void;
12
14
  MemberExpression(path: Babel.NodePath<t.MemberExpression>, state: {
13
15
  opts: IPluginOpts;
@@ -21,11 +21,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
22
  const helper_module_imports_1 = require("@umijs/bundler-utils/compiled/babel/helper-module-imports");
23
23
  const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
24
+ const path_1 = require("path");
24
25
  function default_1() {
25
26
  return {
26
27
  visitor: {
27
28
  Identifier(path, state) {
28
- var _a, _b;
29
+ var _a, _b, _c;
29
30
  const { name } = path.node;
30
31
  if (path.scope.hasBinding(path.node.name)) {
31
32
  return;
@@ -44,7 +45,15 @@ function default_1() {
44
45
  nameHint: name,
45
46
  }));
46
47
  }
47
- // TODO: state.opts.opts.namespaceToLib
48
+ else if ((_c = state.opts.opts.namespaceToLib) === null || _c === void 0 ? void 0 : _c[name]) {
49
+ path.replaceWith((0, helper_module_imports_1.addNamespace)(path, state.opts.opts.namespaceToLib[name]));
50
+ }
51
+ // import css
52
+ if (name === 'styles' && state.opts.css) {
53
+ const { filename } = state.file.opts;
54
+ const cssFilename = (0, path_1.basename)(filename, (0, path_1.extname)(filename)) + '.' + state.opts.css;
55
+ path.replaceWith((0, helper_module_imports_1.addDefault)(path, './' + cssFilename, { nameHint: name }));
56
+ }
48
57
  },
49
58
  MemberExpression(path, state) {
50
59
  const { object, property } = path.node;
@@ -25,6 +25,7 @@ exports.default = (api) => {
25
25
  schema(Joi) {
26
26
  return Joi.object({
27
27
  libs: Joi.array(),
28
+ css: Joi.string(),
28
29
  });
29
30
  },
30
31
  },
@@ -46,7 +47,7 @@ exports.default = (api) => {
46
47
  const memberDts = (lib.members || [])
47
48
  .map((member) => `${member}: typeof import('${lib.importFrom}')['${member}'],`)
48
49
  .join('\n');
49
- return `const ${lib.withObj} = {\n${memberDts}\n};`;
50
+ return `const ${lib.withObj} : {\n${memberDts}\n};`;
50
51
  }
51
52
  else if (lib.namespaceImport) {
52
53
  return `const ${lib.namespaceImport}: typeof import('${lib.importFrom}');`;
@@ -60,20 +61,24 @@ exports.default = (api) => {
60
61
  .join('\n');
61
62
  }
62
63
  });
64
+ // TODO: styles 的类型提示
63
65
  const content = `
64
66
  // generated by umi
65
67
  declare global {
66
- ${dts}
68
+ ${dts.join('\n')}
69
+ const styles: any;
67
70
  }
68
71
  export {}
69
72
  `.trim() + `\n`;
70
73
  (0, fs_1.writeFileSync)((0, path_1.join)(api.paths.cwd, 'lowImport.d.ts'), content, 'utf-8');
71
74
  });
72
75
  api.addBeforeBabelPresets(() => {
76
+ var _a;
73
77
  const opts = normalizeLibs(api.appData.lowImport);
78
+ const css = ((_a = api.config.lowImport) === null || _a === void 0 ? void 0 : _a.css) || 'less';
74
79
  return [
75
80
  {
76
- plugins: [[babelPlugin_1.default, { opts }]],
81
+ plugins: [[babelPlugin_1.default, { opts, css }]],
77
82
  },
78
83
  ];
79
84
  });
@@ -51,6 +51,10 @@ function getRoutes(opts) {
51
51
  routes,
52
52
  });
53
53
  }
54
+ routes = yield opts.api.applyPlugins({
55
+ key: 'modifyRoutes',
56
+ initialValue: routes,
57
+ });
54
58
  return routes;
55
59
  });
56
60
  }
@@ -62,6 +66,12 @@ function getRouteComponents(opts) {
62
66
  const route = opts.routes[key];
63
67
  if (!route.file)
64
68
  return `// ${key}: no file to import`;
69
+ // e.g.
70
+ // component: () => <h1>foo</h1>
71
+ // component: (() => () => <h1>foo</h1>)()
72
+ if (route.file.startsWith('(')) {
73
+ return `'${key}': () => Promise.resolve(${route.file}),`;
74
+ }
65
75
  const path = (0, path_1.isAbsolute)(route.file) || route.file.startsWith('@/')
66
76
  ? route.file
67
77
  : `${opts.prefix}${route.file}`;
@@ -31,6 +31,7 @@ exports.default = (api) => {
31
31
  path: 'umi.ts',
32
32
  tplPath: (0, path_1.join)(constants_1.TEMPLATES_DIR, 'umi.tpl'),
33
33
  context: {
34
+ mountElementId: api.config.mountElementId,
34
35
  rendererPath: yield api.applyPlugins({
35
36
  key: 'modifyRendererPath',
36
37
  initialValue: '@umijs/renderer-react',
@@ -55,6 +56,7 @@ exports.default = (api) => {
55
56
  key: 'addEntryImports',
56
57
  initialValue: [],
57
58
  })).join('\n'),
59
+ basename: api.config.base,
58
60
  },
59
61
  });
60
62
  // route.ts
@@ -72,7 +74,7 @@ exports.default = (api) => {
72
74
  const prefix = hasSrc ? '../../../src/pages/' : '../../pages/';
73
75
  api.writeTmpFile({
74
76
  noPluginDir: true,
75
- path: 'core/route.ts',
77
+ path: 'core/route.tsx',
76
78
  tplPath: (0, path_1.join)(constants_1.TEMPLATES_DIR, 'route.tpl'),
77
79
  context: {
78
80
  routes: JSON.stringify(routes),
@@ -108,7 +110,7 @@ exports.default = (api) => {
108
110
  'accessProvider',
109
111
  'dataflowProvider',
110
112
  'outerProvider',
111
- // 'render',
113
+ 'render',
112
114
  // 'onRouteChange',
113
115
  ],
114
116
  });
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ exports.default = () => {
15
15
  require.resolve('./features/polyfill/polyfill'),
16
16
  require.resolve('./features/tmpFiles/tmpFiles'),
17
17
  require.resolve('./features/transform/transform'),
18
+ require.resolve('./features/lowImport/lowImport'),
18
19
  // commands
19
20
  require.resolve('./commands/build'),
20
21
  require.resolve('./commands/config/config'),
@@ -7,6 +7,7 @@ const utils_1 = require("@umijs/utils");
7
7
  const assert_1 = __importDefault(require("assert"));
8
8
  const fs_1 = require("fs");
9
9
  const path_1 = require("path");
10
+ const transformIEAR_1 = __importDefault(require("./utils/transformIEAR"));
10
11
  const isTypeScriptFile_1 = require("./utils/isTypeScriptFile");
11
12
  exports.default = (api) => {
12
13
  [
@@ -52,7 +53,7 @@ exports.default = (api) => {
52
53
  // 'modifyExportRouteMap',
53
54
  // 'modifyPublicPathStr',
54
55
  'modifyRendererPath',
55
- // 'modifyRoutes',
56
+ 'modifyRoutes',
56
57
  ].forEach((name) => {
57
58
  api.registerMethod({ name });
58
59
  });
@@ -84,6 +85,10 @@ exports.default = (api) => {
84
85
  ]
85
86
  .filter((text) => text !== false)
86
87
  .join('\n');
88
+ // transform imports for all javascript-like files
89
+ if (/\.(t|j)sx?$/.test(absPath)) {
90
+ content = (0, transformIEAR_1.default)({ content, path: absPath }, api);
91
+ }
87
92
  if (!(0, fs_1.existsSync)(absPath) || (0, fs_1.readFileSync)(absPath, 'utf-8') !== content) {
88
93
  (0, fs_1.writeFileSync)(absPath, content, 'utf-8');
89
94
  }
package/dist/types.d.ts CHANGED
@@ -131,6 +131,7 @@ export declare type IApi = PluginAPI & IServicePluginAPI & {
131
131
  path: string;
132
132
  }>;
133
133
  modifyRendererPath: IModify<string, {}>;
134
+ modifyRoutes: IModify<Record<string, IRoute>, {}>;
134
135
  modifyWebpackConfig: IModify<webpack.Configuration, {
135
136
  webpack: typeof webpack;
136
137
  env: Env;
@@ -0,0 +1,22 @@
1
+ import type { IApi } from '../types';
2
+ /**
3
+ * import/export/await-import/require match regular expression
4
+ *
5
+ * WHY: REGEXP
6
+ * ref: https://github.com/umijs/umi-next/pull/230
7
+ *
8
+ * TODO: more choices
9
+ * 1. fork es-module-lexer, support jsx
10
+ * 2. use sourcemap of esbuild
11
+ */
12
+ export declare const IEAR_REG_EXP: RegExp;
13
+ /**
14
+ * transform absolute import/export/await-import/require path
15
+ * @note use to vite can deps:
16
+ * transform to relative path for .umi dir imports
17
+ * prefix `@fs` for node_modules imports
18
+ */
19
+ export default function transformIEAR({ content, path }: {
20
+ content: string;
21
+ path: string;
22
+ }, api: IApi): string;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IEAR_REG_EXP = void 0;
4
+ const utils_1 = require("@umijs/utils");
5
+ const path_1 = require("path");
6
+ /**
7
+ * import/export/await-import/require match regular expression
8
+ *
9
+ * WHY: REGEXP
10
+ * ref: https://github.com/umijs/umi-next/pull/230
11
+ *
12
+ * TODO: more choices
13
+ * 1. fork es-module-lexer, support jsx
14
+ * 2. use sourcemap of esbuild
15
+ */
16
+ exports.IEAR_REG_EXP = new RegExp([
17
+ // match content before quote ($1)
18
+ '(',
19
+ [
20
+ // import/export statements
21
+ [
22
+ '(?:',
23
+ // match head
24
+ '(?:^|\\r|\\n|;)\\s*',
25
+ // match identifier
26
+ '(?:import|export)\\s+',
27
+ [
28
+ '(?:',
29
+ '(?:',
30
+ // match body
31
+ [
32
+ // match default & member import
33
+ [
34
+ // match default import
35
+ [
36
+ '(?:',
37
+ // match type import
38
+ '(?:type\\s*)?',
39
+ // match variable name
40
+ '[a-zA-Z_$][\\w_$]*\\s*,?\\s+',
41
+ // optional
42
+ ')?',
43
+ ].join(''),
44
+ // match member import/export (optional)
45
+ '(?:{[^}]+}\\s+)?',
46
+ ].join(''),
47
+ // match contents import/export
48
+ '(?:type\\s*)?\\*\\s+(?:as\\s+[a-zA-Z][\\w_$]*\\s+)?',
49
+ ].join('|'),
50
+ ')',
51
+ // match from
52
+ 'from\\s+',
53
+ // match direct file import
54
+ '|\\s*',
55
+ ')',
56
+ ].join(''),
57
+ ')',
58
+ ].join(''),
59
+ // import/require call
60
+ [
61
+ // match head (must be single function name)
62
+ '(?:^|[^a-zA-Z\\w_$\\.])',
63
+ // match call
64
+ '(?:import|require)\\(\\s*',
65
+ ].join(''),
66
+ ].join('|'),
67
+ ')',
68
+ '(?:',
69
+ // match quotes ($2)
70
+ `('|")`,
71
+ // match absolute file path ($3)
72
+ `(\\/.*[^\\\\])\\2`,
73
+ ')',
74
+ ].join(''),
75
+ // match full-content
76
+ 'g');
77
+ /**
78
+ * transform absolute import/export/await-import/require path
79
+ * @note use to vite can deps:
80
+ * transform to relative path for .umi dir imports
81
+ * prefix `@fs` for node_modules imports
82
+ */
83
+ function transformIEAR({ content, path }, api) {
84
+ return content.replace(exports.IEAR_REG_EXP, (_, prefix, quote, absPath) => {
85
+ if (absPath.startsWith(api.paths.absTmpPath)) {
86
+ // transform .umi absolute imports
87
+ absPath = (0, utils_1.winPath)((0, path_1.relative)((0, path_1.dirname)(path), absPath));
88
+ }
89
+ else if (absPath.includes('node_modules')) {
90
+ // transform node_modules absolute imports
91
+ absPath = `@fs${absPath}`;
92
+ }
93
+ return `${prefix}${quote}${absPath}${quote}`;
94
+ });
95
+ }
96
+ exports.default = transformIEAR;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/preset-umi",
3
- "version": "4.0.0-beta.13",
3
+ "version": "4.0.0-beta.14",
4
4
  "description": "@umijs/preset-umi",
5
5
  "homepage": "https://github.com/umijs/umi-next/tree/master/packages/preset-umi#readme",
6
6
  "bugs": "https://github.com/umijs/umi-next/issues",
@@ -24,15 +24,15 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@types/multer": "1.4.7",
27
- "@umijs/ast": "4.0.0-beta.13",
28
- "@umijs/babel-preset-umi": "4.0.0-beta.13",
29
- "@umijs/bundler-utils": "4.0.0-beta.13",
30
- "@umijs/bundler-vite": "4.0.0-beta.13",
31
- "@umijs/bundler-webpack": "4.0.0-beta.13",
32
- "@umijs/core": "4.0.0-beta.13",
33
- "@umijs/renderer-react": "4.0.0-beta.13",
34
- "@umijs/server": "4.0.0-beta.13",
35
- "@umijs/utils": "4.0.0-beta.13",
27
+ "@umijs/ast": "4.0.0-beta.14",
28
+ "@umijs/babel-preset-umi": "4.0.0-beta.14",
29
+ "@umijs/bundler-utils": "4.0.0-beta.14",
30
+ "@umijs/bundler-vite": "4.0.0-beta.14",
31
+ "@umijs/bundler-webpack": "4.0.0-beta.14",
32
+ "@umijs/core": "4.0.0-beta.14",
33
+ "@umijs/renderer-react": "4.0.0-beta.14",
34
+ "@umijs/server": "4.0.0-beta.14",
35
+ "@umijs/utils": "4.0.0-beta.14",
36
36
  "current-script-polyfill": "1.0.0",
37
37
  "enhanced-resolve": "5.8.3",
38
38
  "magic-string": "0.25.7",
@@ -47,7 +47,7 @@
47
47
  "@types/multer": "1.4.7",
48
48
  "body-parser": "1.19.0",
49
49
  "multer": "1.4.3",
50
- "vite": "2.6.13"
50
+ "vite": "2.7.6"
51
51
  },
52
52
  "publishConfig": {
53
53
  "access": "public"
package/templates/umi.tpl CHANGED
@@ -3,7 +3,7 @@
3
3
  import { renderClient } from '{{{ rendererPath }}}';
4
4
  import { getRoutes } from './core/route';
5
5
  import { createPluginManager } from './core/plugin';
6
- import { PluginManager } from 'umi';
6
+ import { ApplyPluginsType, PluginManager } from 'umi';
7
7
  {{{ imports }}}
8
8
 
9
9
  async function render() {
@@ -23,8 +23,19 @@ async function render() {
23
23
  routes,
24
24
  routeComponents,
25
25
  pluginManager,
26
+ rootElement: document.getElementById('{{{ mountElementId }}}'),
27
+ {{#basename}}
28
+ basename: '{{{ basename }}}',
29
+ {{/basename}}
26
30
  };
27
- return renderClient(context);
31
+
32
+ return (pluginManager.applyPlugins({
33
+ key: 'render',
34
+ type: ApplyPluginsType.compose,
35
+ initialValue() {
36
+ return renderClient(context);
37
+ },
38
+ }))();
28
39
  }
29
40
 
30
41
  {{{ entryCodeAhead }}}
@@ -1,2 +0,0 @@
1
- export declare class ModuleGraph {
2
- }
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModuleGraph = void 0;
4
- class ModuleGraph {
5
- }
6
- exports.ModuleGraph = ModuleGraph;