@umijs/preset-umi 4.0.3 → 4.0.6
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/dist/commands/dev/dev.js +8 -3
- package/dist/commands/lint.js +4 -1
- package/dist/commands/preview.js +1 -1
- package/dist/features/apiRoute/apiRoute.js +0 -1
- package/dist/features/clickToComponent/clickToComponent.d.ts +3 -0
- package/dist/features/clickToComponent/clickToComponent.js +58 -0
- package/dist/features/ssr/builder/css-loader.js +2 -2
- package/dist/features/tmpFiles/routes.d.ts +9 -0
- package/dist/features/tmpFiles/routes.js +35 -3
- package/dist/index.js +1 -0
- package/dist/libs/folderCache/AutoUpdateFolderCache.d.ts +32 -0
- package/dist/libs/folderCache/AutoUpdateFolderCache.js +106 -0
- package/dist/libs/folderCache/AutoUpdateSourceCodeCache.d.ts +27 -0
- package/dist/libs/folderCache/AutoUpdateSourceCodeCache.js +124 -0
- package/package.json +12 -10
package/dist/commands/dev/dev.js
CHANGED
|
@@ -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
|
|
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
|
});
|
|
@@ -234,12 +235,16 @@ PORT=8888 umi dev
|
|
|
234
235
|
extraBabelPresets,
|
|
235
236
|
beforeMiddlewares: [].concat([
|
|
236
237
|
...beforeMiddlewares,
|
|
237
|
-
faviconMiddleware_1.faviconMiddleware,
|
|
238
238
|
]),
|
|
239
239
|
// vite 模式使用 ./plugins/ViteHtmlPlugin.ts 处理
|
|
240
240
|
afterMiddlewares: enableVite
|
|
241
241
|
? []
|
|
242
|
-
: middlewares.concat(
|
|
242
|
+
: middlewares.concat([
|
|
243
|
+
(0, createRouteMiddleware_1.createRouteMiddleware)({ api }),
|
|
244
|
+
// 放置 favicon 在 webpack middleware 之后,兼容 public 目录下有 favicon.ico 的场景
|
|
245
|
+
// ref: https://github.com/umijs/umi/issues/8024
|
|
246
|
+
faviconMiddleware_1.faviconMiddleware,
|
|
247
|
+
]),
|
|
243
248
|
onDevCompileDone(opts) {
|
|
244
249
|
debouncedPrintMemoryUsage;
|
|
245
250
|
// debouncedPrintMemoryUsage();
|
package/dist/commands/lint.js
CHANGED
|
@@ -17,11 +17,14 @@ umi lint --stylelint-only
|
|
|
17
17
|
|
|
18
18
|
# automatically fix, where possible
|
|
19
19
|
umi lint --fix
|
|
20
|
+
|
|
21
|
+
# disable reporting on warnings
|
|
22
|
+
umi lint --quiet
|
|
20
23
|
`,
|
|
21
24
|
fn: async function () {
|
|
22
25
|
// re-parse cli args to process boolean flags, for get the lint-staged args
|
|
23
26
|
const args = (0, utils_1.yParser)(process.argv.slice(3), {
|
|
24
|
-
boolean: ['fix', 'eslint-only', 'stylelint-only'],
|
|
27
|
+
boolean: ['quiet', 'fix', 'eslint-only', 'stylelint-only'],
|
|
25
28
|
});
|
|
26
29
|
try {
|
|
27
30
|
require.resolve('@umijs/lint/package.json');
|
package/dist/commands/preview.js
CHANGED
|
@@ -92,7 +92,7 @@ umi preview --port [port]
|
|
|
92
92
|
server.listen(port, () => {
|
|
93
93
|
const host = api.args.host && api.args.host !== '0.0.0.0'
|
|
94
94
|
? api.args.host
|
|
95
|
-
: '
|
|
95
|
+
: 'localhost';
|
|
96
96
|
utils_1.logger.ready(`App listening at ${utils_1.chalk.green(`${protocol}//${host}:${port}`)}`);
|
|
97
97
|
});
|
|
98
98
|
},
|
|
@@ -48,7 +48,6 @@ exports.default = (api) => {
|
|
|
48
48
|
return false;
|
|
49
49
|
const config = api.userConfig.apiRoute;
|
|
50
50
|
if (!config) {
|
|
51
|
-
utils_1.logger.warn('Directory ./src/api exists, but config.apiRoute is not set. API route feature will not be enabled!');
|
|
52
51
|
return false;
|
|
53
52
|
}
|
|
54
53
|
if (!config.platform) {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("@umijs/utils");
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
exports.default = (api) => {
|
|
6
|
+
api.describe({
|
|
7
|
+
key: 'clickToComponent',
|
|
8
|
+
config: {
|
|
9
|
+
schema(joi) {
|
|
10
|
+
return joi.object({
|
|
11
|
+
editor: joi.string(),
|
|
12
|
+
});
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
enableBy: api.EnableBy.config,
|
|
16
|
+
});
|
|
17
|
+
const pkgPath = (0, path_1.dirname)(require.resolve('click-to-react-component'));
|
|
18
|
+
api.modifyConfig((memo) => {
|
|
19
|
+
memo.alias['click-to-react-component'] = pkgPath;
|
|
20
|
+
return memo;
|
|
21
|
+
});
|
|
22
|
+
api.modifyAppData((memo) => {
|
|
23
|
+
memo.clickToComponent = {
|
|
24
|
+
pkgPath,
|
|
25
|
+
version: '1.0.8',
|
|
26
|
+
};
|
|
27
|
+
return memo;
|
|
28
|
+
});
|
|
29
|
+
api.onGenerateFiles({
|
|
30
|
+
name: 'clickToComponent',
|
|
31
|
+
fn: () => {
|
|
32
|
+
api.writeTmpFile({
|
|
33
|
+
path: 'runtime.tsx',
|
|
34
|
+
content: `
|
|
35
|
+
import { ClickToComponent } from 'click-to-react-component';
|
|
36
|
+
import React from 'react';
|
|
37
|
+
export function rootContainer(container, opts) {
|
|
38
|
+
return React.createElement(
|
|
39
|
+
(props) => {
|
|
40
|
+
return (
|
|
41
|
+
<>
|
|
42
|
+
<ClickToComponent editor="${api.config.clickToComponent.editor || 'vscode'}"/>
|
|
43
|
+
{props.children}
|
|
44
|
+
</>
|
|
45
|
+
);
|
|
46
|
+
},
|
|
47
|
+
opts,
|
|
48
|
+
container,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
`,
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
api.addRuntimePlugin(() => [
|
|
56
|
+
(0, utils_1.winPath)((0, path_1.join)(api.paths.absTmpPath, 'plugin-clickToComponent/runtime.tsx')),
|
|
57
|
+
]);
|
|
58
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.cssLoader = exports.getClassNames = exports.hashString = exports.ensureLastSlash = void 0;
|
|
4
|
-
const
|
|
4
|
+
const parcelCSS_1 = require("@umijs/bundler-webpack/dist/parcelCSS");
|
|
5
5
|
const utils_1 = require("@umijs/utils");
|
|
6
6
|
const fs_1 = require("fs");
|
|
7
7
|
const path_1 = require("path");
|
|
@@ -18,7 +18,7 @@ exports.hashString = hashString;
|
|
|
18
18
|
function getClassNames(code, filename) {
|
|
19
19
|
// why use Parcel CSS?
|
|
20
20
|
// ref: https://github.com/indooorsman/esbuild-css-modules-plugin
|
|
21
|
-
const { exports } =
|
|
21
|
+
const { exports } = parcelCSS_1.parcelCSS.transform({
|
|
22
22
|
filename,
|
|
23
23
|
code,
|
|
24
24
|
minify: false,
|
|
@@ -10,3 +10,12 @@ export declare function getRouteComponents(opts: {
|
|
|
10
10
|
prefix: string;
|
|
11
11
|
api: IApi;
|
|
12
12
|
}): Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* transform component into webpack chunkName
|
|
16
|
+
* @export
|
|
17
|
+
* @param {string} component component path
|
|
18
|
+
* @param {string} [cwd] current root path
|
|
19
|
+
* @return {*} {string}
|
|
20
|
+
*/
|
|
21
|
+
export declare function componentToChunkName(component: string, cwd?: string): string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getRouteComponents = exports.getRoutes = exports.getApiRoutes = void 0;
|
|
3
|
+
exports.componentToChunkName = exports.getRouteComponents = exports.getRoutes = exports.getApiRoutes = void 0;
|
|
4
4
|
const core_1 = require("@umijs/core");
|
|
5
5
|
const utils_1 = require("@umijs/utils");
|
|
6
6
|
const fs_1 = require("fs");
|
|
@@ -190,11 +190,43 @@ async function getRouteComponents(opts) {
|
|
|
190
190
|
const path = (0, path_1.isAbsolute)(route.file) || route.file.startsWith('@/')
|
|
191
191
|
? route.file
|
|
192
192
|
: `${opts.prefix}${route.file}`;
|
|
193
|
+
const webpackChunkName = componentToChunkName(path, opts.api.cwd);
|
|
193
194
|
return useSuspense
|
|
194
|
-
? `'${key}': React.lazy(() => import(/* webpackChunkName: "${
|
|
195
|
-
: `'${key}': () => import(/* webpackChunkName: "${
|
|
195
|
+
? `'${key}': React.lazy(() => import(/* webpackChunkName: "${webpackChunkName}" */'${(0, utils_1.winPath)(path)}')),`
|
|
196
|
+
: `'${key}': () => import(/* webpackChunkName: "${webpackChunkName}" */'${(0, utils_1.winPath)(path)}'),`;
|
|
196
197
|
})
|
|
197
198
|
.join('\n');
|
|
198
199
|
return `{\n${imports}\n}`;
|
|
199
200
|
}
|
|
200
201
|
exports.getRouteComponents = getRouteComponents;
|
|
202
|
+
function lastSlash(str) {
|
|
203
|
+
return str[str.length - 1] === '/' ? str : `${str}/`;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
*
|
|
207
|
+
* transform component into webpack chunkName
|
|
208
|
+
* @export
|
|
209
|
+
* @param {string} component component path
|
|
210
|
+
* @param {string} [cwd] current root path
|
|
211
|
+
* @return {*} {string}
|
|
212
|
+
*/
|
|
213
|
+
function componentToChunkName(component, cwd) {
|
|
214
|
+
return typeof component === 'string'
|
|
215
|
+
? component
|
|
216
|
+
.replace(new RegExp(`^${utils_1.lodash.escapeRegExp(lastSlash((0, utils_1.winPath)(cwd || '/')))}`), '')
|
|
217
|
+
.replace(/^.(\/|\\)/, '')
|
|
218
|
+
.replace(/(\/|\\)/g, '__')
|
|
219
|
+
.replace(/\.jsx?$/, '')
|
|
220
|
+
.replace(/\.tsx?$/, '')
|
|
221
|
+
.replace(/\.vue?$/, '')
|
|
222
|
+
.replace(/^src__/, '')
|
|
223
|
+
.replace(/\.\.__/g, '')
|
|
224
|
+
// 约定式路由的 [ 会导致 webpack 的 code splitting 失败
|
|
225
|
+
// ref: https://github.com/umijs/umi/issues/4155
|
|
226
|
+
.replace(/[\[\]]/g, '')
|
|
227
|
+
// 插件层的文件也可能是路由组件,比如 plugin-layout 插件
|
|
228
|
+
.replace(/^.umi-production__/, 't__')
|
|
229
|
+
.replace(/^pages__/, 'p__')
|
|
230
|
+
: '';
|
|
231
|
+
}
|
|
232
|
+
exports.componentToChunkName = componentToChunkName;
|
package/dist/index.js
CHANGED
|
@@ -28,6 +28,7 @@ exports.default = () => {
|
|
|
28
28
|
require.resolve('./features/vite/vite'),
|
|
29
29
|
require.resolve('./features/apiRoute/apiRoute'),
|
|
30
30
|
require.resolve('./features/monorepo/redirect'),
|
|
31
|
+
require.resolve('./features/clickToComponent/clickToComponent'),
|
|
31
32
|
// commands
|
|
32
33
|
require.resolve('./commands/build'),
|
|
33
34
|
require.resolve('./commands/config/config'),
|
|
@@ -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
|
+
"version": "4.0.6",
|
|
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,20 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@types/multer": "1.4.7",
|
|
29
|
-
"@umijs/ast": "4.0.
|
|
30
|
-
"@umijs/babel-preset-umi": "4.0.
|
|
31
|
-
"@umijs/bundler-utils": "4.0.
|
|
32
|
-
"@umijs/bundler-vite": "4.0.
|
|
33
|
-
"@umijs/bundler-webpack": "4.0.
|
|
34
|
-
"@umijs/core": "4.0.
|
|
35
|
-
"@umijs/renderer-react": "4.0.
|
|
36
|
-
"@umijs/server": "4.0.
|
|
37
|
-
"@umijs/utils": "4.0.
|
|
29
|
+
"@umijs/ast": "4.0.6",
|
|
30
|
+
"@umijs/babel-preset-umi": "4.0.6",
|
|
31
|
+
"@umijs/bundler-utils": "4.0.6",
|
|
32
|
+
"@umijs/bundler-vite": "4.0.6",
|
|
33
|
+
"@umijs/bundler-webpack": "4.0.6",
|
|
34
|
+
"@umijs/core": "4.0.6",
|
|
35
|
+
"@umijs/renderer-react": "4.0.6",
|
|
36
|
+
"@umijs/server": "4.0.6",
|
|
37
|
+
"@umijs/utils": "4.0.6",
|
|
38
|
+
"click-to-react-component": "^1.0.8",
|
|
38
39
|
"core-js": "3.22.4",
|
|
39
40
|
"current-script-polyfill": "1.0.0",
|
|
40
41
|
"enhanced-resolve": "5.9.3",
|
|
42
|
+
"fast-glob": "^3.2.11",
|
|
41
43
|
"magic-string": "0.26.2",
|
|
42
44
|
"path-to-regexp": "1.7.0",
|
|
43
45
|
"react": "18.1.0",
|