@umijs/preset-umi 4.0.2 → 4.0.3

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.
@@ -16,6 +16,12 @@ const printMemoryUsage_1 = require("./printMemoryUsage");
16
16
  const watch_1 = require("./watch");
17
17
  const bundlerWebpack = (0, lazyImportFromCurrentPkg_1.lazyImportFromCurrentPkg)('@umijs/bundler-webpack');
18
18
  const bundlerVite = (0, lazyImportFromCurrentPkg_1.lazyImportFromCurrentPkg)('@umijs/bundler-vite');
19
+ const MFSU_EAGER_DEFAULT_INCLUDE = [
20
+ 'react',
21
+ 'react-error-overlay',
22
+ 'react/jsx-dev-runtime',
23
+ '@umijs/utils/compiled/strip-ansi',
24
+ ];
19
25
  exports.default = (api) => {
20
26
  api.describe({
21
27
  enableBy() {
@@ -32,7 +38,7 @@ umi dev
32
38
  PORT=8888 umi dev
33
39
  `,
34
40
  async fn() {
35
- var _a, _b;
41
+ var _a, _b, _c, _d, _e;
36
42
  utils_1.logger.info(utils_1.chalk.cyan.bold(`Umi v${api.appData.umi.version}`));
37
43
  const enableVite = !!api.config.vite;
38
44
  // clear tmp
@@ -199,6 +205,17 @@ PORT=8888 umi dev
199
205
  });
200
206
  };
201
207
  const debouncedPrintMemoryUsage = utils_1.lodash.debounce(printMemoryUsage_1.printMemoryUsage, 5000);
208
+ let srcCodeCache;
209
+ if (((_b = api.config.mfsu) === null || _b === void 0 ? void 0 : _b.strategy) === 'eager') {
210
+ srcCodeCache = new utils_1.AutoUpdateSrcCodeCache({
211
+ cwd: api.paths.absSrcPath,
212
+ cachePath: (0, path_1.join)(api.paths.absNodeModulesPath, '.cache', 'mfsu', 'v4'),
213
+ });
214
+ await srcCodeCache.init();
215
+ (0, watch_1.addUnWatch)(() => {
216
+ srcCodeCache.unwatch();
217
+ });
218
+ }
202
219
  const opts = {
203
220
  config: api.config,
204
221
  cwd: api.cwd,
@@ -241,13 +258,19 @@ PORT=8888 umi dev
241
258
  ...opts,
242
259
  };
243
260
  },
244
- mfsuWithESBuild: (_b = api.config.mfsu) === null || _b === void 0 ? void 0 : _b.esbuild,
261
+ mfsuWithESBuild: (_c = api.config.mfsu) === null || _c === void 0 ? void 0 : _c.esbuild,
262
+ mfsuStrategy: (_d = api.config.mfsu) === null || _d === void 0 ? void 0 : _d.strategy,
245
263
  cache: {
246
264
  buildDependencies: [
247
265
  api.pkgPath,
248
266
  api.service.configManager.mainConfigFile || '',
249
267
  ].filter(Boolean),
250
268
  },
269
+ srcCodeCache,
270
+ mfsuInclude: utils_1.lodash.union([
271
+ ...MFSU_EAGER_DEFAULT_INCLUDE,
272
+ ...(((_e = api.config.mfsu) === null || _e === void 0 ? void 0 : _e.include) || []),
273
+ ]),
251
274
  };
252
275
  if (enableVite) {
253
276
  await bundlerVite.dev(opts);
@@ -37,11 +37,18 @@ exports.default = (api) => {
37
37
  const packageToInstall = res.willUseTLR
38
38
  ? {
39
39
  ...basicDeps,
40
- '@testing-library/react': '^12',
40
+ '@testing-library/react': '^13',
41
+ '@testing-library/jest-dom': '^5.16.4',
42
+ '@types/testing-library__jest-dom': '^5.14.5',
41
43
  }
42
44
  : basicDeps;
43
45
  h.addDevDeps(packageToInstall);
44
46
  h.addScript('test', 'jest');
47
+ if (res.willUseTLR) {
48
+ (0, fs_1.writeFileSync)((0, path_1.join)(api.cwd, 'jest-setup.ts'), `import '@testing-library/jest-dom';
49
+ `.trimLeft());
50
+ utils_1.logger.info('Write jest-setup.ts');
51
+ }
45
52
  const importSource = api.appData.umi.importSource;
46
53
  (0, fs_1.writeFileSync)((0, path_1.join)(api.cwd, 'jest.config.ts'), `
47
54
  import { Config, configUmiAlias, createConfig } from '${importSource}/test';
@@ -51,6 +58,7 @@ export default async () => {
51
58
  ...createConfig({
52
59
  target: 'browser',
53
60
  }),
61
+ ${res.willUseTLR ? `setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],` : ''}
54
62
  // if you require some es-module npm package, please uncomment below line and insert your package name
55
63
  // transformIgnorePatterns: ['node_modules/(?!.*(lodash-es|your-es-pkg-name)/)']
56
64
  })) as Config.InitialOptions;
@@ -25,24 +25,9 @@ exports.default = (api) => {
25
25
  });
26
26
  (0, fs_1.writeFileSync)((0, path_1.join)(api.cwd, 'tsconfig.json'), `
27
27
  {
28
- "compilerOptions": {
29
- "target": "esnext",
30
- "module": "esnext",
31
- "moduleResolution": "node",
32
- "importHelpers": true,
33
- "jsx": "react",
34
- "esModuleInterop": true,
35
- "sourceMap": true,
36
- "baseUrl": ".",
37
- "strict": true,
38
- "paths": {
39
- "@/*": ["*"],
40
- "@@/*": [".umi/*"]
41
- },
42
- "allowSyntheticDefaultImports": true
43
- }
28
+ "extends": "./${api.appData.hasSrcDir ? 'src/' : ''}.umi/tsconfig.json"
44
29
  }
45
- `.trimLeft());
30
+ `.trimStart());
46
31
  utils_1.logger.info('Write tsconfig.json');
47
32
  h.installDeps();
48
33
  },
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@umijs/utils");
3
4
  exports.default = (api) => {
4
5
  api.registerCommand({
5
6
  name: 'lint',
@@ -18,17 +19,21 @@ umi lint --stylelint-only
18
19
  umi lint --fix
19
20
  `,
20
21
  fn: async function () {
22
+ // re-parse cli args to process boolean flags, for get the lint-staged args
23
+ const args = (0, utils_1.yParser)(process.argv.slice(3), {
24
+ boolean: ['fix', 'eslint-only', 'stylelint-only'],
25
+ });
21
26
  try {
22
27
  require.resolve('@umijs/lint/package.json');
23
28
  }
24
29
  catch (err) {
25
30
  throw new Error('@umijs/lint is not built-in, please install it manually before run umi lint.');
26
31
  }
27
- if (api.args._.length === 0) {
28
- api.args._.unshift('{src,test}/**/*.{js,jsx,ts,tsx,less,css}');
32
+ if (args._.length === 0) {
33
+ args._.unshift('{src,test}/**/*.{js,jsx,ts,tsx,less,css}');
29
34
  }
30
35
  // lazy require for CLI performance
31
- require('@umijs/lint').default({ cwd: api.cwd }, api.args);
36
+ require('@umijs/lint').default({ cwd: api.cwd }, args);
32
37
  },
33
38
  });
34
39
  };
@@ -52,10 +52,11 @@ umi preview --port [port]
52
52
  app.use(key, (0, http_proxy_middleware_1.createProxyMiddleware)(key, {
53
53
  ...proxy[key],
54
54
  // Add x-real-url in response header
55
- onProxyRes(proxyRes, req) {
56
- var _a;
55
+ onProxyRes(proxyRes, req, res) {
56
+ var _a, _b;
57
57
  proxyRes.headers['x-real-url'] =
58
58
  ((_a = new URL(req.url || '', target)) === null || _a === void 0 ? void 0 : _a.href) || '';
59
+ (_b = proxyConfig.onProxyRes) === null || _b === void 0 ? void 0 : _b.call(proxyConfig, proxyRes, req, res);
59
60
  },
60
61
  }));
61
62
  }
@@ -4,7 +4,7 @@ export declare function getApiRoutes(opts: {
4
4
  }): Promise<any>;
5
5
  export declare function getRoutes(opts: {
6
6
  api: IApi;
7
- }): Promise<any>;
7
+ }): Promise<Record<string, any>>;
8
8
  export declare function getRouteComponents(opts: {
9
9
  routes: Record<string, any>;
10
10
  prefix: string;
@@ -50,7 +50,7 @@ async function getRoutes(opts) {
50
50
  basedir: opts.api.paths.absPagesPath,
51
51
  extensions: ['.js', '.jsx', '.tsx', '.ts', '.vue'],
52
52
  }));
53
- component = component.replace(`${opts.api.paths.absSrcPath}/`, '@/');
53
+ component = component.replace((0, utils_1.winPath)(`${opts.api.paths.absSrcPath}/`), '@/');
54
54
  return component;
55
55
  },
56
56
  });
@@ -112,6 +112,8 @@ async function getRoutes(opts) {
112
112
  const absLayoutPath = (0, utils_1.tryPaths)([
113
113
  (0, path_1.join)(opts.api.paths.absSrcPath, 'layouts/index.tsx'),
114
114
  (0, path_1.join)(opts.api.paths.absSrcPath, 'layouts/index.vue'),
115
+ (0, path_1.join)(opts.api.paths.absSrcPath, 'layouts/index.jsx'),
116
+ (0, path_1.join)(opts.api.paths.absSrcPath, 'layouts/index.js'),
115
117
  ]);
116
118
  const layouts = (await opts.api.applyPlugins({
117
119
  key: 'addLayouts',
@@ -119,11 +121,14 @@ async function getRoutes(opts) {
119
121
  absLayoutPath && {
120
122
  id: '@@/global-layout',
121
123
  file: (0, utils_1.winPath)(absLayoutPath),
124
+ test(route) {
125
+ return route.layout !== false;
126
+ },
122
127
  },
123
128
  ].filter(Boolean),
124
129
  })).map((layout) => {
125
130
  // prune local path prefix, avoid mix in outputs
126
- layout.file = layout.file.replace(new RegExp(`^${absSrcPath}`), '@');
131
+ layout.file = layout.file.replace(new RegExp(`^${(0, utils_1.winPath)(absSrcPath)}`), '@');
127
132
  return layout;
128
133
  });
129
134
  for (const layout of layouts) {
@@ -135,6 +140,7 @@ async function getRoutes(opts) {
135
140
  file: layout.file,
136
141
  parentId: undefined,
137
142
  absPath: '/',
143
+ isLayout: true,
138
144
  },
139
145
  routes,
140
146
  test: layout.test,
@@ -174,7 +180,11 @@ async function getRouteComponents(opts) {
174
180
  // component: (() => () => <h1>foo</h1>)()
175
181
  if (route.file.startsWith('(')) {
176
182
  return useSuspense
177
- ? `'${key}': React.lazy(() => Promise.resolve(${route.file})),`
183
+ ? // Compatible with none default route exports
184
+ // e.g. https://github.com/umijs/umi/blob/0d40a07bf28b0760096cbe2f22da4d639645b937/packages/plugins/src/qiankun/master.ts#L55
185
+ `'${key}': React.lazy(
186
+ () => Promise.resolve(${route.file}).then(e => e?.default ? e : ({ default: e }))
187
+ ),`
178
188
  : `'${key}': () => Promise.resolve(${route.file}),`;
179
189
  }
180
190
  const path = (0, path_1.isAbsolute)(route.file) || route.file.startsWith('@/')
@@ -51,8 +51,6 @@ exports.default = (api) => {
51
51
  strict: true,
52
52
  resolveJsonModule: true,
53
53
  allowSyntheticDefaultImports: true,
54
- // Enforce using `import type` instead of `import` for types
55
- importsNotUsedAsValues: 'error',
56
54
  // Supported by vue only
57
55
  ...(api.appData.framework === 'vue'
58
56
  ? {
@@ -267,6 +265,7 @@ declare module '*.txt' {
267
265
  noPluginDir: true,
268
266
  path: 'core/EmptyRoute.tsx',
269
267
  content: `
268
+ import React from 'react';
270
269
  import { Outlet } from 'umi';
271
270
  export default function EmptyRoute() {
272
271
  return <Outlet />;
@@ -380,14 +379,17 @@ export default function EmptyRoute() {
380
379
  });
381
380
  }
382
381
  // history.ts
383
- api.writeTmpFile({
384
- noPluginDir: true,
385
- path: 'core/history.ts',
386
- tplPath: (0, path_1.join)(constants_1.TEMPLATES_DIR, 'history.tpl'),
387
- context: {
388
- rendererPath,
389
- },
390
- });
382
+ // only react generates because the preset-vue override causes vite hot updates to fail
383
+ if (api.appData.framework === 'react') {
384
+ api.writeTmpFile({
385
+ noPluginDir: true,
386
+ path: 'core/history.ts',
387
+ tplPath: (0, path_1.join)(constants_1.TEMPLATES_DIR, 'history.tpl'),
388
+ context: {
389
+ rendererPath,
390
+ },
391
+ });
392
+ }
391
393
  });
392
394
  function checkMembers(opts) {
393
395
  const conflicts = utils_1.lodash.intersection(opts.exportMembers, opts.members);
@@ -83,17 +83,19 @@ exports.default = (api) => {
83
83
  (0, assert_1.default)(utils_1.lodash.isPlainObject(opts.context), `opts.context must be plain object.`);
84
84
  content = utils_1.Mustache.render(tpl, opts.context);
85
85
  }
86
+ // Only js files generate comments
87
+ const isJsFile = /\.(t|j)sx?$/.test(absPath);
86
88
  content = [
87
89
  (0, isTypeScriptFile_1.isTypeScriptFile)(opts.path) && `// @ts-nocheck`,
88
- '// This file is generated by Umi automatically',
89
- '// DO NOT CHANGE IT MANUALLY!',
90
+ isJsFile && '// This file is generated by Umi automatically',
91
+ isJsFile && '// DO NOT CHANGE IT MANUALLY!',
90
92
  content.trim(),
91
93
  '',
92
94
  ]
93
95
  .filter((text) => text !== false)
94
96
  .join('\n');
95
97
  // transform imports for all javascript-like files only vite mode enable
96
- if (api.appData.vite && /\.(t|j)sx?$/.test(absPath)) {
98
+ if (api.appData.vite && isJsFile) {
97
99
  content = (0, transformIEAR_1.default)({ content, path: absPath }, api);
98
100
  }
99
101
  if (!(0, fs_1.existsSync)(absPath)) {
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@umijs/preset-umi",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "description": "@umijs/preset-umi",
5
- "homepage": "https://github.com/umijs/umi-next/tree/master/packages/preset-umi#readme",
6
- "bugs": "https://github.com/umijs/umi-next/issues",
5
+ "homepage": "https://github.com/umijs/umi/tree/master/packages/preset-umi#readme",
6
+ "bugs": "https://github.com/umijs/umi/issues",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "https://github.com/umijs/umi-next"
9
+ "url": "https://github.com/umijs/umi"
10
10
  },
11
11
  "license": "MIT",
12
12
  "main": "dist/index.js",
@@ -26,15 +26,15 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@types/multer": "1.4.7",
29
- "@umijs/ast": "4.0.2",
30
- "@umijs/babel-preset-umi": "4.0.2",
31
- "@umijs/bundler-utils": "4.0.2",
32
- "@umijs/bundler-vite": "4.0.2",
33
- "@umijs/bundler-webpack": "4.0.2",
34
- "@umijs/core": "4.0.2",
35
- "@umijs/renderer-react": "4.0.2",
36
- "@umijs/server": "4.0.2",
37
- "@umijs/utils": "4.0.2",
29
+ "@umijs/ast": "4.0.3",
30
+ "@umijs/babel-preset-umi": "4.0.3",
31
+ "@umijs/bundler-utils": "4.0.3",
32
+ "@umijs/bundler-vite": "4.0.3",
33
+ "@umijs/bundler-webpack": "4.0.3",
34
+ "@umijs/core": "4.0.3",
35
+ "@umijs/renderer-react": "4.0.3",
36
+ "@umijs/server": "4.0.3",
37
+ "@umijs/utils": "4.0.3",
38
38
  "core-js": "3.22.4",
39
39
  "current-script-polyfill": "1.0.0",
40
40
  "enhanced-resolve": "5.9.3",
@@ -1,4 +1,4 @@
1
- import { UmiApiRequest, UmiApiResponse } from "umi";
1
+ import type { UmiApiRequest, UmiApiResponse } from "umi";
2
2
 
3
3
  export default async function (req: UmiApiRequest, res: UmiApiResponse) {
4
4
  switch (req.method) {