@umijs/preset-umi 4.0.3 → 4.0.4

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.
@@ -7,6 +7,7 @@ const utils_1 = require("@umijs/utils");
7
7
  const fs_1 = require("fs");
8
8
  const path_1 = require("path");
9
9
  const constants_1 = require("../../constants");
10
+ const AutoUpdateSourceCodeCache_1 = require("../../libs/folderCache/AutoUpdateSourceCodeCache");
10
11
  const lazyImportFromCurrentPkg_1 = require("../../utils/lazyImportFromCurrentPkg");
11
12
  const createRouteMiddleware_1 = require("./createRouteMiddleware");
12
13
  const faviconMiddleware_1 = require("./faviconMiddleware");
@@ -207,7 +208,7 @@ PORT=8888 umi dev
207
208
  const debouncedPrintMemoryUsage = utils_1.lodash.debounce(printMemoryUsage_1.printMemoryUsage, 5000);
208
209
  let srcCodeCache;
209
210
  if (((_b = api.config.mfsu) === null || _b === void 0 ? void 0 : _b.strategy) === 'eager') {
210
- srcCodeCache = new utils_1.AutoUpdateSrcCodeCache({
211
+ srcCodeCache = new AutoUpdateSourceCodeCache_1.AutoUpdateSrcCodeCache({
211
212
  cwd: api.paths.absSrcPath,
212
213
  cachePath: (0, path_1.join)(api.paths.absNodeModulesPath, '.cache', 'mfsu', 'v4'),
213
214
  });
@@ -0,0 +1,32 @@
1
+ declare type AbsPath = string;
2
+ declare type FileContent = string;
3
+ declare type FileContentCache = Record<AbsPath, FileContent>;
4
+ export declare type FileChangeEvent = {
5
+ event: 'unlink' | 'change' | 'add';
6
+ path: string;
7
+ };
8
+ export declare class AutoUpdateFolderCache {
9
+ fileContentCache: FileContentCache;
10
+ private watcher;
11
+ private readonly readyPromise;
12
+ private readonly cwd;
13
+ pendingChanges: FileChangeEvent[];
14
+ private readonly debouchedHandleChanges;
15
+ private readonly onCacheUpdated;
16
+ private readonly filesLoader;
17
+ constructor(opts: {
18
+ cwd: string;
19
+ exts: string[];
20
+ onCacheUpdate: (cache: FileContentCache, events: FileChangeEvent[]) => void;
21
+ debouncedTimeout?: number;
22
+ ignored: string[];
23
+ filesLoader?: (files: string[]) => Promise<Record<string, string>>;
24
+ });
25
+ unwatch(): Promise<void>;
26
+ init(): Promise<void>;
27
+ private watchAll;
28
+ getFileCache(): FileContentCache;
29
+ loadFiles(files: string[]): Promise<void>;
30
+ private _defaultLoader;
31
+ }
32
+ export {};
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AutoUpdateFolderCache = void 0;
4
+ const utils_1 = require("@umijs/utils");
5
+ const fs_1 = require("fs");
6
+ const path_1 = require("path");
7
+ const { watch } = utils_1.chokidar;
8
+ class AutoUpdateFolderCache {
9
+ constructor(opts) {
10
+ this.fileContentCache = {};
11
+ this.pendingChanges = [];
12
+ console.log('the path ', `./**/*.{${opts.exts.join(',')}}`);
13
+ this.cwd = opts.cwd;
14
+ this.onCacheUpdated = opts.onCacheUpdate;
15
+ this.filesLoader = opts.filesLoader || this._defaultLoader;
16
+ this.watcher = watch(`./**/*.{${opts.exts.join(',')}}`, {
17
+ ignored: opts.ignored || [],
18
+ cwd: opts.cwd,
19
+ ignorePermissionErrors: true,
20
+ ignoreInitial: true,
21
+ });
22
+ this.watchAll();
23
+ this.readyPromise = new Promise((resolve) => {
24
+ this.watcher.on('ready', () => {
25
+ resolve();
26
+ });
27
+ });
28
+ this.debouchedHandleChanges = utils_1.lodash.debounce(async () => {
29
+ const modifiedFiles = [];
30
+ const events = this.pendingChanges.slice();
31
+ while (this.pendingChanges.length > 0) {
32
+ const c = this.pendingChanges.pop();
33
+ switch (c.event) {
34
+ case 'unlink':
35
+ delete this.fileContentCache[c.path];
36
+ break;
37
+ case 'change':
38
+ case 'add':
39
+ modifiedFiles.push(c.path);
40
+ break;
41
+ default:
42
+ ((_n) => { })(c.event);
43
+ }
44
+ }
45
+ await this.loadFiles(modifiedFiles);
46
+ await this.onCacheUpdated(this.fileContentCache, events);
47
+ }, opts.debouncedTimeout);
48
+ }
49
+ unwatch() {
50
+ return this.watcher.close();
51
+ }
52
+ async init() {
53
+ await this.readyPromise;
54
+ }
55
+ watchAll() {
56
+ this.watcher.on('all', (eventName, path) => {
57
+ switch (eventName) {
58
+ case 'change':
59
+ this.pendingChanges.push({
60
+ event: 'change',
61
+ path: (0, path_1.join)(this.cwd, path),
62
+ });
63
+ this.debouchedHandleChanges();
64
+ break;
65
+ case 'add':
66
+ this.pendingChanges.push({
67
+ event: 'add',
68
+ path: (0, path_1.join)(this.cwd, path),
69
+ });
70
+ this.debouchedHandleChanges();
71
+ break;
72
+ case 'unlink':
73
+ this.pendingChanges.push({
74
+ event: 'unlink',
75
+ path: (0, path_1.join)(this.cwd, path),
76
+ });
77
+ this.debouchedHandleChanges();
78
+ break;
79
+ default:
80
+ // ignore all others;
81
+ }
82
+ });
83
+ }
84
+ getFileCache() {
85
+ return this.fileContentCache;
86
+ }
87
+ async loadFiles(files) {
88
+ const loaded = await this.filesLoader(files);
89
+ for (const f of Object.keys(loaded)) {
90
+ this.fileContentCache[f] = loaded[f];
91
+ }
92
+ }
93
+ async _defaultLoader(files) {
94
+ const loaded = {};
95
+ for (let file of files) {
96
+ try {
97
+ loaded[file] = (0, fs_1.readFileSync)(file, 'utf-8');
98
+ }
99
+ catch (e) {
100
+ utils_1.logger.error('[fileCache] load file', (0, path_1.relative)(this.cwd, file), 'failed ', e);
101
+ }
102
+ }
103
+ return loaded;
104
+ }
105
+ }
106
+ exports.AutoUpdateFolderCache = AutoUpdateFolderCache;
@@ -0,0 +1,27 @@
1
+ import { ImportSpecifier } from '@umijs/bundler-utils/compiled/es-module-lexer';
2
+ import { AutoUpdateFolderCache, FileChangeEvent } from './AutoUpdateFolderCache';
3
+ export declare type MergedCodeInfo = {
4
+ code: string;
5
+ imports: readonly ImportSpecifier[];
6
+ events: FileChangeEvent[];
7
+ };
8
+ export declare type Listener = (info: MergedCodeInfo) => void;
9
+ export declare class AutoUpdateSrcCodeCache {
10
+ private readonly srcPath;
11
+ private readonly cachePath;
12
+ folderCache: AutoUpdateFolderCache;
13
+ private listeners;
14
+ constructor(opts: {
15
+ cwd: string;
16
+ cachePath: string;
17
+ });
18
+ init(): Promise<void>;
19
+ private initFileList;
20
+ batchProcess(files: string[]): Promise<void>;
21
+ getMergedCode(): {
22
+ code: string;
23
+ imports: readonly ImportSpecifier[];
24
+ };
25
+ register(l: Listener): () => void;
26
+ unwatch(): Promise<void>;
27
+ }
@@ -0,0 +1,124 @@
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.AutoUpdateSrcCodeCache = void 0;
7
+ const es_module_lexer_1 = require("@umijs/bundler-utils/compiled/es-module-lexer");
8
+ const esbuild_1 = require("@umijs/bundler-utils/compiled/esbuild");
9
+ const utils_1 = require("@umijs/utils");
10
+ // @ts-ignore
11
+ const fast_glob_1 = __importDefault(require("fast-glob"));
12
+ const fs_1 = require("fs");
13
+ const path_1 = require("path");
14
+ const AutoUpdateFolderCache_1 = require("./AutoUpdateFolderCache");
15
+ class AutoUpdateSrcCodeCache {
16
+ constructor(opts) {
17
+ this.listeners = [];
18
+ this.srcPath = opts.cwd;
19
+ this.cachePath = opts.cachePath;
20
+ this.folderCache = new AutoUpdateFolderCache_1.AutoUpdateFolderCache({
21
+ cwd: this.srcPath,
22
+ exts: ['ts', 'js', 'jsx', 'tsx'],
23
+ ignored: [
24
+ '**/*.d.ts',
25
+ '**/*.test.{js,ts,jsx,tsx}',
26
+ // fixme respect to environment
27
+ '**/.umi-production/**',
28
+ '**/node_modules/**',
29
+ '**/.git/**',
30
+ ],
31
+ debouncedTimeout: 500,
32
+ filesLoader: async (files) => {
33
+ const loaded = {};
34
+ await this.batchProcess(files);
35
+ for (const f of files) {
36
+ let newFile = (0, path_1.join)(this.cachePath, (0, path_1.relative)(this.srcPath, f));
37
+ // fixme ensure the last one
38
+ newFile = newFile.replace((0, path_1.extname)(newFile), '.js');
39
+ loaded[f] = (0, fs_1.readFileSync)(newFile, 'utf-8');
40
+ }
41
+ return loaded;
42
+ },
43
+ onCacheUpdate: (_cache, events) => {
44
+ const merged = this.getMergedCode();
45
+ const info = { ...merged, events };
46
+ this.listeners.forEach((l) => l(info));
47
+ },
48
+ });
49
+ }
50
+ async init() {
51
+ const [files] = await Promise.all([this.initFileList(), es_module_lexer_1.init]);
52
+ await this.folderCache.loadFiles(files);
53
+ }
54
+ async initFileList() {
55
+ const start = Date.now();
56
+ const files = await (0, fast_glob_1.default)((0, path_1.join)(this.srcPath, '**', '*.{ts,js,jsx,tsx}'), {
57
+ dot: true,
58
+ ignore: [
59
+ '**/*.d.ts',
60
+ '**/*.test.{js,ts,jsx,tsx}',
61
+ // fixme respect to environment
62
+ '**/.umi-production/**',
63
+ '**/node_modules/**',
64
+ '**/.git/**',
65
+ ],
66
+ });
67
+ utils_1.logger.debug('[MFSU][eager] fast-glob costs', Date.now() - start);
68
+ return files;
69
+ }
70
+ async batchProcess(files) {
71
+ var _a, _b, _c, _d;
72
+ try {
73
+ await (0, esbuild_1.build)({
74
+ entryPoints: files,
75
+ bundle: false,
76
+ outdir: this.cachePath,
77
+ outbase: this.srcPath,
78
+ loader: {
79
+ // in case some js using some feature, eg: decorator
80
+ '.jsx': 'tsx',
81
+ },
82
+ logLevel: 'silent',
83
+ });
84
+ }
85
+ catch (e) {
86
+ // error ignored due to user have to update code to fix then trigger another batchProcess;
87
+ // @ts-ignore
88
+ if (((_a = e.errors) === null || _a === void 0 ? void 0 : _a.length) || ((_b = e.warnings) === null || _b === void 0 ? void 0 : _b.length)) {
89
+ utils_1.logger.warn('transpile code with esbuild got ',
90
+ // @ts-ignore
91
+ ((_c = e.errors) === null || _c === void 0 ? void 0 : _c.lenght) || 0, 'errors,',
92
+ // @ts-ignore
93
+ ((_d = e.warnings) === null || _d === void 0 ? void 0 : _d.length) || 0, 'warnings');
94
+ utils_1.logger.debug('esbuild transpile code with error', e);
95
+ }
96
+ else {
97
+ utils_1.logger.warn('transpile code with esbuild error', e);
98
+ }
99
+ }
100
+ }
101
+ getMergedCode() {
102
+ const fileContentCache = this.folderCache.getFileCache();
103
+ const code = Object.values(fileContentCache).join('\n');
104
+ const [imports] = (0, es_module_lexer_1.parse)(code);
105
+ const merged = {
106
+ code,
107
+ imports,
108
+ };
109
+ return merged;
110
+ }
111
+ register(l) {
112
+ if (this.listeners.indexOf(l) < 0) {
113
+ this.listeners.push(l);
114
+ }
115
+ return () => {
116
+ const i = this.listeners.indexOf(l);
117
+ this.listeners.splice(i, 1);
118
+ };
119
+ }
120
+ unwatch() {
121
+ return this.folderCache.unwatch();
122
+ }
123
+ }
124
+ exports.AutoUpdateSrcCodeCache = AutoUpdateSrcCodeCache;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/preset-umi",
3
- "version": "4.0.3",
3
+ "version": "4.0.4",
4
4
  "description": "@umijs/preset-umi",
5
5
  "homepage": "https://github.com/umijs/umi/tree/master/packages/preset-umi#readme",
6
6
  "bugs": "https://github.com/umijs/umi/issues",
@@ -26,18 +26,19 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@types/multer": "1.4.7",
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",
29
+ "@umijs/ast": "4.0.4",
30
+ "@umijs/babel-preset-umi": "4.0.4",
31
+ "@umijs/bundler-utils": "4.0.4",
32
+ "@umijs/bundler-vite": "4.0.4",
33
+ "@umijs/bundler-webpack": "4.0.4",
34
+ "@umijs/core": "4.0.4",
35
+ "@umijs/renderer-react": "4.0.4",
36
+ "@umijs/server": "4.0.4",
37
+ "@umijs/utils": "4.0.4",
38
38
  "core-js": "3.22.4",
39
39
  "current-script-polyfill": "1.0.0",
40
40
  "enhanced-resolve": "5.9.3",
41
+ "fast-glob": "^3.2.11",
41
42
  "magic-string": "0.26.2",
42
43
  "path-to-regexp": "1.7.0",
43
44
  "react": "18.1.0",