@umijs/utils 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.
- package/dist/folderCache/AutoUpdateFolderCache.d.ts +32 -0
- package/dist/folderCache/AutoUpdateFolderCache.js +133 -0
- package/dist/folderCache/AutoUpdateSourceCodeCache.d.ts +27 -0
- package/dist/folderCache/AutoUpdateSourceCodeCache.js +147 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +5 -4
|
@@ -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,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.AutoUpdateFolderCache = void 0;
|
|
30
|
+
const chokidar_1 = require("chokidar");
|
|
31
|
+
const fs_1 = require("fs");
|
|
32
|
+
const path_1 = require("path");
|
|
33
|
+
const lodash_1 = __importDefault(require("../../compiled/lodash"));
|
|
34
|
+
const logger = __importStar(require("../logger"));
|
|
35
|
+
class AutoUpdateFolderCache {
|
|
36
|
+
constructor(opts) {
|
|
37
|
+
this.fileContentCache = {};
|
|
38
|
+
this.pendingChanges = [];
|
|
39
|
+
console.log('the path ', `./**/*.{${opts.exts.join(',')}}`);
|
|
40
|
+
this.cwd = opts.cwd;
|
|
41
|
+
this.onCacheUpdated = opts.onCacheUpdate;
|
|
42
|
+
this.filesLoader = opts.filesLoader || this._defaultLoader;
|
|
43
|
+
this.watcher = (0, chokidar_1.watch)(`./**/*.{${opts.exts.join(',')}}`, {
|
|
44
|
+
ignored: opts.ignored || [],
|
|
45
|
+
cwd: opts.cwd,
|
|
46
|
+
ignorePermissionErrors: true,
|
|
47
|
+
ignoreInitial: true,
|
|
48
|
+
});
|
|
49
|
+
this.watchAll();
|
|
50
|
+
this.readyPromise = new Promise((resolve) => {
|
|
51
|
+
this.watcher.on('ready', () => {
|
|
52
|
+
resolve();
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
this.debouchedHandleChanges = lodash_1.default.debounce(async () => {
|
|
56
|
+
const modifiedFiles = [];
|
|
57
|
+
const events = this.pendingChanges.slice();
|
|
58
|
+
while (this.pendingChanges.length > 0) {
|
|
59
|
+
const c = this.pendingChanges.pop();
|
|
60
|
+
switch (c.event) {
|
|
61
|
+
case 'unlink':
|
|
62
|
+
delete this.fileContentCache[c.path];
|
|
63
|
+
break;
|
|
64
|
+
case 'change':
|
|
65
|
+
case 'add':
|
|
66
|
+
modifiedFiles.push(c.path);
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
((_n) => { })(c.event);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
await this.loadFiles(modifiedFiles);
|
|
73
|
+
await this.onCacheUpdated(this.fileContentCache, events);
|
|
74
|
+
}, opts.debouncedTimeout);
|
|
75
|
+
}
|
|
76
|
+
unwatch() {
|
|
77
|
+
return this.watcher.close();
|
|
78
|
+
}
|
|
79
|
+
async init() {
|
|
80
|
+
await this.readyPromise;
|
|
81
|
+
}
|
|
82
|
+
watchAll() {
|
|
83
|
+
this.watcher.on('all', (eventName, path) => {
|
|
84
|
+
switch (eventName) {
|
|
85
|
+
case 'change':
|
|
86
|
+
this.pendingChanges.push({
|
|
87
|
+
event: 'change',
|
|
88
|
+
path: (0, path_1.join)(this.cwd, path),
|
|
89
|
+
});
|
|
90
|
+
this.debouchedHandleChanges();
|
|
91
|
+
break;
|
|
92
|
+
case 'add':
|
|
93
|
+
this.pendingChanges.push({
|
|
94
|
+
event: 'add',
|
|
95
|
+
path: (0, path_1.join)(this.cwd, path),
|
|
96
|
+
});
|
|
97
|
+
this.debouchedHandleChanges();
|
|
98
|
+
break;
|
|
99
|
+
case 'unlink':
|
|
100
|
+
this.pendingChanges.push({
|
|
101
|
+
event: 'unlink',
|
|
102
|
+
path: (0, path_1.join)(this.cwd, path),
|
|
103
|
+
});
|
|
104
|
+
this.debouchedHandleChanges();
|
|
105
|
+
break;
|
|
106
|
+
default:
|
|
107
|
+
// ignore all others;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
getFileCache() {
|
|
112
|
+
return this.fileContentCache;
|
|
113
|
+
}
|
|
114
|
+
async loadFiles(files) {
|
|
115
|
+
const loaded = await this.filesLoader(files);
|
|
116
|
+
for (const f of Object.keys(loaded)) {
|
|
117
|
+
this.fileContentCache[f] = loaded[f];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async _defaultLoader(files) {
|
|
121
|
+
const loaded = {};
|
|
122
|
+
for (let file of files) {
|
|
123
|
+
try {
|
|
124
|
+
loaded[file] = (0, fs_1.readFileSync)(file, 'utf-8');
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
logger.error('[fileCache] load file', (0, path_1.relative)(this.cwd, file), 'failed ', e);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return loaded;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
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,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.AutoUpdateSrcCodeCache = void 0;
|
|
30
|
+
const es_module_lexer_1 = require("@umijs/bundler-utils/compiled/es-module-lexer");
|
|
31
|
+
const esbuild_1 = require("@umijs/bundler-utils/compiled/esbuild");
|
|
32
|
+
const logger = __importStar(require("../logger"));
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
35
|
+
const fs_1 = require("fs");
|
|
36
|
+
const path_1 = require("path");
|
|
37
|
+
const AutoUpdateFolderCache_1 = require("./AutoUpdateFolderCache");
|
|
38
|
+
class AutoUpdateSrcCodeCache {
|
|
39
|
+
constructor(opts) {
|
|
40
|
+
this.listeners = [];
|
|
41
|
+
this.srcPath = opts.cwd;
|
|
42
|
+
this.cachePath = opts.cachePath;
|
|
43
|
+
this.folderCache = new AutoUpdateFolderCache_1.AutoUpdateFolderCache({
|
|
44
|
+
cwd: this.srcPath,
|
|
45
|
+
exts: ['ts', 'js', 'jsx', 'tsx'],
|
|
46
|
+
ignored: [
|
|
47
|
+
'**/*.d.ts',
|
|
48
|
+
'**/*.test.{js,ts,jsx,tsx}',
|
|
49
|
+
// fixme respect to environment
|
|
50
|
+
'**/.umi-production/**',
|
|
51
|
+
'**/node_modules/**',
|
|
52
|
+
'**/.git/**',
|
|
53
|
+
],
|
|
54
|
+
debouncedTimeout: 500,
|
|
55
|
+
filesLoader: async (files) => {
|
|
56
|
+
const loaded = {};
|
|
57
|
+
await this.batchProcess(files);
|
|
58
|
+
for (const f of files) {
|
|
59
|
+
let newFile = (0, path_1.join)(this.cachePath, (0, path_1.relative)(this.srcPath, f));
|
|
60
|
+
// fixme ensure the last one
|
|
61
|
+
newFile = newFile.replace((0, path_1.extname)(newFile), '.js');
|
|
62
|
+
loaded[f] = (0, fs_1.readFileSync)(newFile, 'utf-8');
|
|
63
|
+
}
|
|
64
|
+
return loaded;
|
|
65
|
+
},
|
|
66
|
+
onCacheUpdate: (_cache, events) => {
|
|
67
|
+
const merged = this.getMergedCode();
|
|
68
|
+
const info = { ...merged, events };
|
|
69
|
+
this.listeners.forEach((l) => l(info));
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
async init() {
|
|
74
|
+
const [files] = await Promise.all([this.initFileList(), es_module_lexer_1.init]);
|
|
75
|
+
await this.folderCache.loadFiles(files);
|
|
76
|
+
}
|
|
77
|
+
async initFileList() {
|
|
78
|
+
const start = Date.now();
|
|
79
|
+
const files = await (0, fast_glob_1.default)((0, path_1.join)(this.srcPath, '**', '*.{ts,js,jsx,tsx}'), {
|
|
80
|
+
dot: true,
|
|
81
|
+
ignore: [
|
|
82
|
+
'**/*.d.ts',
|
|
83
|
+
'**/*.test.{js,ts,jsx,tsx}',
|
|
84
|
+
// fixme respect to environment
|
|
85
|
+
'**/.umi-production/**',
|
|
86
|
+
'**/node_modules/**',
|
|
87
|
+
'**/.git/**',
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
logger.debug('[MFSU][eager] fast-glob costs', Date.now() - start);
|
|
91
|
+
return files;
|
|
92
|
+
}
|
|
93
|
+
async batchProcess(files) {
|
|
94
|
+
var _a, _b, _c, _d;
|
|
95
|
+
try {
|
|
96
|
+
await (0, esbuild_1.build)({
|
|
97
|
+
entryPoints: files,
|
|
98
|
+
bundle: false,
|
|
99
|
+
outdir: this.cachePath,
|
|
100
|
+
outbase: this.srcPath,
|
|
101
|
+
loader: {
|
|
102
|
+
// in case some js using some feature, eg: decorator
|
|
103
|
+
'.jsx': 'tsx',
|
|
104
|
+
},
|
|
105
|
+
logLevel: 'silent',
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
// error ignored due to user have to update code to fix then trigger another batchProcess;
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
if (((_a = e.errors) === null || _a === void 0 ? void 0 : _a.length) || ((_b = e.warnings) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
112
|
+
logger.warn('transpile code with esbuild got ',
|
|
113
|
+
// @ts-ignore
|
|
114
|
+
((_c = e.errors) === null || _c === void 0 ? void 0 : _c.lenght) || 0, 'errors,',
|
|
115
|
+
// @ts-ignore
|
|
116
|
+
((_d = e.warnings) === null || _d === void 0 ? void 0 : _d.length) || 0, 'warnings');
|
|
117
|
+
logger.debug('esbuild transpile code with error', e);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
logger.warn('transpile code with esbuild error', e);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
getMergedCode() {
|
|
125
|
+
const fileContentCache = this.folderCache.getFileCache();
|
|
126
|
+
const code = Object.values(fileContentCache).join('\n');
|
|
127
|
+
const [imports] = (0, es_module_lexer_1.parse)(code);
|
|
128
|
+
const merged = {
|
|
129
|
+
code,
|
|
130
|
+
imports,
|
|
131
|
+
};
|
|
132
|
+
return merged;
|
|
133
|
+
}
|
|
134
|
+
register(l) {
|
|
135
|
+
if (this.listeners.indexOf(l) < 0) {
|
|
136
|
+
this.listeners.push(l);
|
|
137
|
+
}
|
|
138
|
+
return () => {
|
|
139
|
+
const i = this.listeners.indexOf(l);
|
|
140
|
+
this.listeners.splice(i, 1);
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
unwatch() {
|
|
144
|
+
return this.folderCache.unwatch();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
exports.AutoUpdateSrcCodeCache = AutoUpdateSrcCodeCache;
|
package/dist/index.d.ts
CHANGED
|
@@ -25,6 +25,8 @@ import Generator from './Generator/Generator';
|
|
|
25
25
|
import installDeps from './installDeps';
|
|
26
26
|
import * as logger from './logger';
|
|
27
27
|
import updatePackageJSON from './updatePackageJSON';
|
|
28
|
+
export * from './folderCache/AutoUpdateFolderCache';
|
|
29
|
+
export * from './folderCache/AutoUpdateSourceCodeCache';
|
|
28
30
|
export * from './getCorejsVersion';
|
|
29
31
|
export * from './importLazy';
|
|
30
32
|
export * from './isLocalDev';
|
package/dist/index.js
CHANGED
|
@@ -85,6 +85,8 @@ const logger = __importStar(require("./logger"));
|
|
|
85
85
|
exports.logger = logger;
|
|
86
86
|
const updatePackageJSON_1 = __importDefault(require("./updatePackageJSON"));
|
|
87
87
|
exports.updatePackageJSON = updatePackageJSON_1.default;
|
|
88
|
+
__exportStar(require("./folderCache/AutoUpdateFolderCache"), exports);
|
|
89
|
+
__exportStar(require("./folderCache/AutoUpdateSourceCodeCache"), exports);
|
|
88
90
|
__exportStar(require("./getCorejsVersion"), exports);
|
|
89
91
|
__exportStar(require("./importLazy"), exports);
|
|
90
92
|
__exportStar(require("./isLocalDev"), exports);
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umijs/utils",
|
|
3
|
-
"version": "4.0.
|
|
4
|
-
"homepage": "https://github.com/umijs/umi
|
|
5
|
-
"bugs": "https://github.com/umijs/umi
|
|
3
|
+
"version": "4.0.3",
|
|
4
|
+
"homepage": "https://github.com/umijs/umi/tree/master/packages/utils#readme",
|
|
5
|
+
"bugs": "https://github.com/umijs/umi/issues",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/umijs/umi
|
|
8
|
+
"url": "https://github.com/umijs/umi"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"main": "dist/index.js",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"chokidar": "3.5.3",
|
|
25
|
+
"fast-glob": "^3.2.11",
|
|
25
26
|
"pino": "7.11.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|