@umijs/mfsu 4.0.2 → 4.0.5
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/babelPlugins/awaitImport/MFImport.d.ts +24 -0
- package/dist/babelPlugins/awaitImport/MFImport.js +68 -0
- package/dist/babelPlugins/awaitImport/checkMatch.d.ts +1 -0
- package/dist/babelPlugins/awaitImport/checkMatch.js +1 -0
- package/dist/babelPlugins/awaitImport/getAliasedPath.d.ts +5 -2
- package/dist/babelPlugins/awaitImport/getAliasedPath.js +16 -2
- package/dist/dep/dep.d.ts +1 -1
- package/dist/dep/dep.js +1 -1
- package/dist/depBuilder/depBuilder.d.ts +1 -1
- package/dist/depInfo.d.ts +18 -4
- package/dist/depInfo.js +7 -0
- package/dist/esbuildHandlers/awaitImport/index.d.ts +3 -0
- package/dist/esbuildHandlers/awaitImport/index.js +30 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/{mfsu.d.ts → mfsu/mfsu.d.ts} +21 -25
- package/dist/{mfsu.js → mfsu/mfsu.js} +69 -75
- package/dist/mfsu/strategyCompileTime.d.ts +20 -0
- package/dist/mfsu/strategyCompileTime.js +100 -0
- package/dist/mfsu/strategyStaticAnalyze.d.ts +23 -0
- package/dist/mfsu/strategyStaticAnalyze.js +122 -0
- package/dist/moduleGraph.d.ts +3 -8
- package/dist/staticDepInfo/importParser.d.ts +4 -0
- package/dist/staticDepInfo/importParser.js +8 -0
- package/dist/staticDepInfo/simulations/babel-plugin-import.d.ts +15 -0
- package/dist/staticDepInfo/simulations/babel-plugin-import.js +99 -0
- package/dist/staticDepInfo/staticDepInfo.d.ts +56 -0
- package/dist/staticDepInfo/staticDepInfo.js +198 -0
- package/dist/webpackPlugins/buildDepPlugin.d.ts +4 -3
- package/dist/webpackPlugins/buildDepPlugin.js +8 -0
- package/package.json +11 -9
- package/vendors/importParser/_importParser.js +683 -0
- package/vendors/importParser/importParser.jison +105 -0
|
@@ -0,0 +1,100 @@
|
|
|
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.StrategyCompileTime = void 0;
|
|
7
|
+
const utils_1 = require("@umijs/utils");
|
|
8
|
+
const awaitImport_1 = __importDefault(require("../babelPlugins/awaitImport/awaitImport"));
|
|
9
|
+
const getRealPath_1 = require("../babelPlugins/awaitImport/getRealPath");
|
|
10
|
+
const dep_1 = require("../dep/dep");
|
|
11
|
+
const depInfo_1 = require("../depInfo");
|
|
12
|
+
class StrategyCompileTime {
|
|
13
|
+
constructor({ mfsu }) {
|
|
14
|
+
this.mfsu = mfsu;
|
|
15
|
+
this.depInfo = new depInfo_1.DepInfo({ mfsu });
|
|
16
|
+
}
|
|
17
|
+
getDepModules() {
|
|
18
|
+
return this.depInfo.getDepModules();
|
|
19
|
+
}
|
|
20
|
+
getCacheFilePath() {
|
|
21
|
+
return this.depInfo.getCacheFilePath();
|
|
22
|
+
}
|
|
23
|
+
init() { }
|
|
24
|
+
shouldBuild() {
|
|
25
|
+
return this.depInfo.shouldBuild();
|
|
26
|
+
}
|
|
27
|
+
loadCache() {
|
|
28
|
+
this.depInfo.loadCache();
|
|
29
|
+
}
|
|
30
|
+
writeCache() {
|
|
31
|
+
this.depInfo.writeCache();
|
|
32
|
+
}
|
|
33
|
+
refresh() {
|
|
34
|
+
this.depInfo.snapshot();
|
|
35
|
+
}
|
|
36
|
+
getBabelPlugin() {
|
|
37
|
+
return [awaitImport_1.default, this.getAwaitImportCollectOpts()];
|
|
38
|
+
}
|
|
39
|
+
getBuildDepPlugConfig() {
|
|
40
|
+
const mfsu = this.mfsu;
|
|
41
|
+
return {
|
|
42
|
+
onCompileDone: () => {
|
|
43
|
+
if (mfsu.depBuilder.isBuilding) {
|
|
44
|
+
mfsu.buildDepsAgain = true;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
mfsu
|
|
48
|
+
.buildDeps()
|
|
49
|
+
.then(() => {
|
|
50
|
+
mfsu.onProgress({
|
|
51
|
+
done: true,
|
|
52
|
+
});
|
|
53
|
+
})
|
|
54
|
+
.catch((e) => {
|
|
55
|
+
utils_1.logger.error(e);
|
|
56
|
+
mfsu.onProgress({
|
|
57
|
+
done: true,
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
getAwaitImportCollectOpts() {
|
|
65
|
+
const mfsuOpts = this.mfsu.opts;
|
|
66
|
+
const mfsu = this.mfsu;
|
|
67
|
+
return {
|
|
68
|
+
onTransformDeps: () => { },
|
|
69
|
+
onCollect: ({ file, data, }) => {
|
|
70
|
+
this.depInfo.moduleGraph.onFileChange({
|
|
71
|
+
file,
|
|
72
|
+
// @ts-ignore
|
|
73
|
+
deps: [
|
|
74
|
+
...Array.from(data.matched).map((item) => ({
|
|
75
|
+
file: item.sourceValue,
|
|
76
|
+
isDependency: true,
|
|
77
|
+
version: dep_1.Dep.getDepVersion({
|
|
78
|
+
dep: item.sourceValue,
|
|
79
|
+
cwd: mfsuOpts.cwd,
|
|
80
|
+
}),
|
|
81
|
+
})),
|
|
82
|
+
...Array.from(data.unMatched).map((item) => ({
|
|
83
|
+
file: (0, getRealPath_1.getRealPath)({
|
|
84
|
+
file,
|
|
85
|
+
dep: item.sourceValue,
|
|
86
|
+
}),
|
|
87
|
+
isDependency: false,
|
|
88
|
+
})),
|
|
89
|
+
],
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
exportAllMembers: mfsuOpts.exportAllMembers,
|
|
93
|
+
unMatchLibs: mfsuOpts.unMatchLibs,
|
|
94
|
+
remoteName: mfsuOpts.mfName,
|
|
95
|
+
alias: mfsu.alias,
|
|
96
|
+
externals: mfsu.externals,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.StrategyCompileTime = StrategyCompileTime;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { IBuildDepPluginOpts } from '../webpackPlugins/buildDepPlugin';
|
|
2
|
+
import type { IMFSUStrategy, MFSU } from './mfsu';
|
|
3
|
+
export declare class StaticAnalyzeStrategy implements IMFSUStrategy {
|
|
4
|
+
private readonly mfsu;
|
|
5
|
+
private staticDepInfo;
|
|
6
|
+
constructor({ mfsu, srcCodeCache }: {
|
|
7
|
+
mfsu: MFSU;
|
|
8
|
+
srcCodeCache: any;
|
|
9
|
+
});
|
|
10
|
+
init(): void;
|
|
11
|
+
getDepModules(): Record<string, {
|
|
12
|
+
file: string;
|
|
13
|
+
version: string;
|
|
14
|
+
}>;
|
|
15
|
+
getCacheFilePath(): string;
|
|
16
|
+
shouldBuild(): false | "dependencies changed";
|
|
17
|
+
writeCache(): void;
|
|
18
|
+
getBabelPlugin(): any[];
|
|
19
|
+
private getMfImportOpts;
|
|
20
|
+
getBuildDepPlugConfig(): IBuildDepPluginOpts;
|
|
21
|
+
loadCache(): void;
|
|
22
|
+
refresh(): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
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.StaticAnalyzeStrategy = void 0;
|
|
7
|
+
const utils_1 = require("@umijs/utils");
|
|
8
|
+
const getAliasedPath_1 = require("../babelPlugins/awaitImport/getAliasedPath");
|
|
9
|
+
const MFImport_1 = __importDefault(require("../babelPlugins/awaitImport/MFImport"));
|
|
10
|
+
const staticDepInfo_1 = require("../staticDepInfo/staticDepInfo");
|
|
11
|
+
class StaticAnalyzeStrategy {
|
|
12
|
+
constructor({ mfsu, srcCodeCache }) {
|
|
13
|
+
this.mfsu = mfsu;
|
|
14
|
+
this.staticDepInfo = new staticDepInfo_1.StaticDepInfo({
|
|
15
|
+
mfsu,
|
|
16
|
+
srcCodeCache,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
init() {
|
|
20
|
+
this.staticDepInfo.init();
|
|
21
|
+
}
|
|
22
|
+
getDepModules() {
|
|
23
|
+
return this.staticDepInfo.getDepModules();
|
|
24
|
+
}
|
|
25
|
+
getCacheFilePath() {
|
|
26
|
+
return this.staticDepInfo.getCacheFilePath();
|
|
27
|
+
}
|
|
28
|
+
shouldBuild() {
|
|
29
|
+
return this.staticDepInfo.shouldBuild();
|
|
30
|
+
}
|
|
31
|
+
writeCache() {
|
|
32
|
+
this.staticDepInfo.writeCache();
|
|
33
|
+
}
|
|
34
|
+
getBabelPlugin() {
|
|
35
|
+
return [MFImport_1.default, this.getMfImportOpts()];
|
|
36
|
+
}
|
|
37
|
+
getMfImportOpts() {
|
|
38
|
+
const mfsu = this.mfsu;
|
|
39
|
+
const mfsuOpts = this.mfsu.opts;
|
|
40
|
+
return {
|
|
41
|
+
resolveImportSource: (source) => {
|
|
42
|
+
const depMat = this.staticDepInfo.getDependencies();
|
|
43
|
+
const r = (0, getAliasedPath_1.getAliasedPathWithLoopDetect)({
|
|
44
|
+
value: source,
|
|
45
|
+
alias: mfsu.alias,
|
|
46
|
+
});
|
|
47
|
+
const m = depMat[r];
|
|
48
|
+
if (m) {
|
|
49
|
+
return m.replaceValue;
|
|
50
|
+
}
|
|
51
|
+
return r;
|
|
52
|
+
},
|
|
53
|
+
exportAllMembers: mfsuOpts.exportAllMembers,
|
|
54
|
+
unMatchLibs: mfsuOpts.unMatchLibs,
|
|
55
|
+
remoteName: mfsuOpts.mfName,
|
|
56
|
+
alias: mfsu.alias,
|
|
57
|
+
externals: mfsu.externals,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
getBuildDepPlugConfig() {
|
|
61
|
+
const mfsu = this.mfsu;
|
|
62
|
+
return {
|
|
63
|
+
beforeCompile: async () => {
|
|
64
|
+
utils_1.logger.event(`[MFSU][eager] start build deps`);
|
|
65
|
+
if (mfsu.depBuilder.isBuilding) {
|
|
66
|
+
mfsu.buildDepsAgain = true;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
this.staticDepInfo.consumeAllProducedEvents();
|
|
70
|
+
mfsu
|
|
71
|
+
.buildDeps()
|
|
72
|
+
.then(() => {
|
|
73
|
+
mfsu.onProgress({
|
|
74
|
+
done: true,
|
|
75
|
+
});
|
|
76
|
+
})
|
|
77
|
+
.catch((e) => {
|
|
78
|
+
utils_1.logger.error(e);
|
|
79
|
+
mfsu.onProgress({
|
|
80
|
+
done: true,
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
onFileChange: async (c) => {
|
|
86
|
+
utils_1.logger.debug('webpack found changes modified:', c.modifiedFiles, 'removed:', c.removedFiles);
|
|
87
|
+
// webpack init run
|
|
88
|
+
if (!c.modifiedFiles || c.modifiedFiles.size === 0) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const start = Date.now();
|
|
92
|
+
let event = this.staticDepInfo.getProducedEvent();
|
|
93
|
+
while (event.length === 0) {
|
|
94
|
+
await sleep(200);
|
|
95
|
+
console.log('.');
|
|
96
|
+
event = this.staticDepInfo.getProducedEvent();
|
|
97
|
+
if (Date.now() - start > 5000) {
|
|
98
|
+
utils_1.logger.warn('webpack wait mfsu deps too long');
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
onCompileDone: () => {
|
|
104
|
+
// fixme if mf module finished earlier than src compile
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
loadCache() {
|
|
109
|
+
this.staticDepInfo.loadCache();
|
|
110
|
+
}
|
|
111
|
+
refresh() {
|
|
112
|
+
this.staticDepInfo.snapshot();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.StaticAnalyzeStrategy = StaticAnalyzeStrategy;
|
|
116
|
+
function sleep(ms) {
|
|
117
|
+
return new Promise((resolve) => {
|
|
118
|
+
setTimeout(() => {
|
|
119
|
+
resolve();
|
|
120
|
+
}, ms);
|
|
121
|
+
});
|
|
122
|
+
}
|
package/dist/moduleGraph.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DepModule } from './depInfo';
|
|
1
2
|
declare class ModuleNode {
|
|
2
3
|
file: string;
|
|
3
4
|
importers: Set<ModuleNode>;
|
|
@@ -15,10 +16,7 @@ interface IDep {
|
|
|
15
16
|
export declare class ModuleGraph {
|
|
16
17
|
fileToModules: Map<string, ModuleNode>;
|
|
17
18
|
depToModules: Map<string, ModuleNode>;
|
|
18
|
-
depSnapshotModules: Record<string,
|
|
19
|
-
file: string;
|
|
20
|
-
version: string;
|
|
21
|
-
}>;
|
|
19
|
+
depSnapshotModules: Record<string, DepModule>;
|
|
22
20
|
rootModules: Set<ModuleNode>;
|
|
23
21
|
constructor();
|
|
24
22
|
restore(data: {
|
|
@@ -36,10 +34,7 @@ export declare class ModuleGraph {
|
|
|
36
34
|
depModules: Record<string, {
|
|
37
35
|
version: string | null;
|
|
38
36
|
}>;
|
|
39
|
-
depSnapshotModules: Record<string,
|
|
40
|
-
file: string;
|
|
41
|
-
version: string;
|
|
42
|
-
}>;
|
|
37
|
+
depSnapshotModules: Record<string, DepModule>;
|
|
43
38
|
};
|
|
44
39
|
snapshotDeps(): void;
|
|
45
40
|
getDepsInfo(mods: Map<string, ModuleNode>): Record<string, {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
const _importParser_js_1 = require("../../vendors/importParser/_importParser.js");
|
|
5
|
+
function parse(code) {
|
|
6
|
+
return (0, _importParser_js_1.parse)(code);
|
|
7
|
+
}
|
|
8
|
+
exports.default = parse;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ImportSpecifier } from '@umijs/bundler-utils/compiled/es-module-lexer';
|
|
2
|
+
import type { Match } from '../staticDepInfo';
|
|
3
|
+
export default function createHandle(importOptions: {
|
|
4
|
+
libraryName: string;
|
|
5
|
+
libraryDirectory: string;
|
|
6
|
+
style: boolean | string;
|
|
7
|
+
camel2UnderlineComponentName?: boolean;
|
|
8
|
+
camel2DashComponentName?: boolean;
|
|
9
|
+
}): (opts: {
|
|
10
|
+
rawCode: string;
|
|
11
|
+
imports: ImportSpecifier[];
|
|
12
|
+
mfName: string;
|
|
13
|
+
alias: Record<string, string>;
|
|
14
|
+
pathToVersion(p: string): string;
|
|
15
|
+
}) => Match[];
|
|
@@ -0,0 +1,99 @@
|
|
|
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
|
+
const utils_1 = require("@umijs/utils");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const getAliasedPath_1 = require("../../babelPlugins/awaitImport/getAliasedPath");
|
|
9
|
+
const importParser_1 = __importDefault(require("../importParser"));
|
|
10
|
+
function createHandle(importOptions) {
|
|
11
|
+
var _a;
|
|
12
|
+
const { libraryName, libraryDirectory } = importOptions;
|
|
13
|
+
const useUnderline = importOptions.camel2UnderlineComponentName;
|
|
14
|
+
const useDash = (_a = importOptions.camel2DashComponentName) !== null && _a !== void 0 ? _a : true;
|
|
15
|
+
const transformName = useUnderline
|
|
16
|
+
? (n) => transCamel(n, '_')
|
|
17
|
+
: useDash
|
|
18
|
+
? (n) => transCamel(n, '-')
|
|
19
|
+
: (n) => n;
|
|
20
|
+
/*
|
|
21
|
+
todo support other config values
|
|
22
|
+
[x] boolean true
|
|
23
|
+
[x] string css
|
|
24
|
+
[ ] function
|
|
25
|
+
*/
|
|
26
|
+
const stylePathFromCompPath = importOptions.style === 'css'
|
|
27
|
+
? (compFsPath) => (0, utils_1.winPath)((0, path_1.join)(compFsPath, 'style/css'))
|
|
28
|
+
: (compFsPath) => (0, utils_1.winPath)((0, path_1.join)(compFsPath, 'style'));
|
|
29
|
+
return function handleImports(opts) {
|
|
30
|
+
const { imports, rawCode } = opts;
|
|
31
|
+
if ((imports === null || imports === void 0 ? void 0 : imports.length) > 0) {
|
|
32
|
+
const version = opts.pathToVersion(libraryName);
|
|
33
|
+
function addToMatches(moduleFsPath) {
|
|
34
|
+
const unAliasedModulePath = (0, getAliasedPath_1.getAliasedPathWithLoopDetect)({
|
|
35
|
+
value: (0, utils_1.winPath)(moduleFsPath),
|
|
36
|
+
alias: opts.alias,
|
|
37
|
+
});
|
|
38
|
+
retMatched.push({
|
|
39
|
+
isMatch: true,
|
|
40
|
+
value: unAliasedModulePath,
|
|
41
|
+
replaceValue: `${mfName}/${unAliasedModulePath}`,
|
|
42
|
+
version,
|
|
43
|
+
});
|
|
44
|
+
const unAliasedStylePath = (0, getAliasedPath_1.getAliasedPathWithLoopDetect)({
|
|
45
|
+
value: stylePathFromCompPath(moduleFsPath),
|
|
46
|
+
alias: opts.alias,
|
|
47
|
+
});
|
|
48
|
+
retMatched.push({
|
|
49
|
+
isMatch: true,
|
|
50
|
+
value: unAliasedStylePath,
|
|
51
|
+
replaceValue: `${mfName}/${unAliasedStylePath}`,
|
|
52
|
+
version,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const importSnippets = imports
|
|
56
|
+
.map(({ ss, se }) => {
|
|
57
|
+
return rawCode.slice(ss, se + 1);
|
|
58
|
+
})
|
|
59
|
+
.join('\n');
|
|
60
|
+
const retMatched = [];
|
|
61
|
+
const parsedImports = (0, importParser_1.default)(importSnippets);
|
|
62
|
+
const importedVariable = new Set();
|
|
63
|
+
for (const i of parsedImports) {
|
|
64
|
+
i.imports.forEach((v) => {
|
|
65
|
+
if (v === '*') {
|
|
66
|
+
errorLogForSpaceImport(libraryName);
|
|
67
|
+
}
|
|
68
|
+
importedVariable.add(v);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const mfName = opts.mfName;
|
|
72
|
+
for (const v of importedVariable.entries()) {
|
|
73
|
+
const importVariableName = v[0];
|
|
74
|
+
if (importVariableName === 'default') {
|
|
75
|
+
addToMatches((0, path_1.join)(libraryName, libraryDirectory));
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const transformedName = transformName(importVariableName);
|
|
79
|
+
const importFsPath = (0, path_1.join)(libraryName, libraryDirectory, transformedName);
|
|
80
|
+
addToMatches(importFsPath);
|
|
81
|
+
}
|
|
82
|
+
return retMatched;
|
|
83
|
+
}
|
|
84
|
+
return [];
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
exports.default = createHandle;
|
|
88
|
+
function transCamel(_str, symbol) {
|
|
89
|
+
const str = _str[0].toLowerCase() + _str.substr(1);
|
|
90
|
+
return str.replace(/([A-Z])/g, ($1) => `${symbol}${$1.toLowerCase()}`);
|
|
91
|
+
}
|
|
92
|
+
function errorLogForSpaceImport(libraryName) {
|
|
93
|
+
utils_1.logger.error(`"import * as ant from 'antd'" or "export * from '${libraryName}'" are not allowed in mfsu#version=v4`);
|
|
94
|
+
utils_1.logger.error(`solutions:`);
|
|
95
|
+
utils_1.logger.error(` change to "import { Xxx } from '${libraryName}'" or`);
|
|
96
|
+
utils_1.logger.error(` "export { Xxx } from '${libraryName}'" syntax`);
|
|
97
|
+
utils_1.logger.error(` or use mfsu#version=v3 configuration`);
|
|
98
|
+
throw Error(`"import * as ant from 'antd'" not allowed in mfsu#version=4`);
|
|
99
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { ImportSpecifier } from '@umijs/bundler-utils/compiled/es-module-lexer';
|
|
2
|
+
import { checkMatch } from '../babelPlugins/awaitImport/checkMatch';
|
|
3
|
+
import { MFSU } from '../mfsu/mfsu';
|
|
4
|
+
declare type FileChangeEvent = {
|
|
5
|
+
event: 'unlink' | 'change' | 'add';
|
|
6
|
+
path: string;
|
|
7
|
+
};
|
|
8
|
+
declare type MergedCodeInfo = {
|
|
9
|
+
imports: readonly ImportSpecifier[];
|
|
10
|
+
code: string;
|
|
11
|
+
events: FileChangeEvent[];
|
|
12
|
+
};
|
|
13
|
+
declare type AutoUpdateSrcCodeCache = {
|
|
14
|
+
register(listener: (info: MergedCodeInfo) => void): void;
|
|
15
|
+
getMergedCode(): MergedCodeInfo;
|
|
16
|
+
};
|
|
17
|
+
interface IOpts {
|
|
18
|
+
mfsu: MFSU;
|
|
19
|
+
srcCodeCache: AutoUpdateSrcCodeCache;
|
|
20
|
+
safeList?: string[];
|
|
21
|
+
}
|
|
22
|
+
export declare type Match = ReturnType<typeof checkMatch> & {
|
|
23
|
+
version: string;
|
|
24
|
+
};
|
|
25
|
+
export declare class StaticDepInfo {
|
|
26
|
+
private opts;
|
|
27
|
+
private readonly cacheFilePath;
|
|
28
|
+
private mfsu;
|
|
29
|
+
private readonly include;
|
|
30
|
+
private currentDep;
|
|
31
|
+
private builtWithDep;
|
|
32
|
+
private produced;
|
|
33
|
+
private readonly cwd;
|
|
34
|
+
private readonly runtimeSimulations;
|
|
35
|
+
constructor(opts: IOpts);
|
|
36
|
+
getProducedEvent(): {
|
|
37
|
+
changes: unknown[];
|
|
38
|
+
}[];
|
|
39
|
+
consumeAllProducedEvents(): void;
|
|
40
|
+
shouldBuild(): false | "dependencies changed";
|
|
41
|
+
getDepModules(): Record<string, {
|
|
42
|
+
file: string;
|
|
43
|
+
version: string;
|
|
44
|
+
}>;
|
|
45
|
+
snapshot(): void;
|
|
46
|
+
loadCache(): void;
|
|
47
|
+
writeCache(): void;
|
|
48
|
+
getCacheFilePath(): string;
|
|
49
|
+
getDependencies(): Record<string, Match>;
|
|
50
|
+
init(): void;
|
|
51
|
+
private _getDependencies;
|
|
52
|
+
private simulateRuntimeTransform;
|
|
53
|
+
private appendIncludeList;
|
|
54
|
+
allRuntimeHelpers(): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,198 @@
|
|
|
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.StaticDepInfo = void 0;
|
|
7
|
+
const utils_1 = require("@umijs/utils");
|
|
8
|
+
const fs_1 = require("fs");
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
const why_1 = __importDefault(require("is-equal/why"));
|
|
11
|
+
const path_1 = require("path");
|
|
12
|
+
const checkMatch_1 = require("../babelPlugins/awaitImport/checkMatch");
|
|
13
|
+
const dep_1 = require("../dep/dep");
|
|
14
|
+
const babel_plugin_import_1 = __importDefault(require("./simulations/babel-plugin-import"));
|
|
15
|
+
class StaticDepInfo {
|
|
16
|
+
constructor(opts) {
|
|
17
|
+
this.currentDep = {};
|
|
18
|
+
this.builtWithDep = {};
|
|
19
|
+
this.produced = [];
|
|
20
|
+
this.mfsu = opts.mfsu;
|
|
21
|
+
this.include = this.mfsu.opts.include || [];
|
|
22
|
+
this.opts = opts;
|
|
23
|
+
this.cacheFilePath = (0, path_1.join)(this.opts.mfsu.opts.tmpBase, 'MFSU_CACHE_v4.json');
|
|
24
|
+
this.cwd = this.mfsu.opts.cwd;
|
|
25
|
+
opts.srcCodeCache.register((info) => {
|
|
26
|
+
this.produced.push({ changes: info.events });
|
|
27
|
+
this.currentDep = this._getDependencies(info.code, info.imports);
|
|
28
|
+
});
|
|
29
|
+
this.runtimeSimulations = [
|
|
30
|
+
{
|
|
31
|
+
packageName: 'antd',
|
|
32
|
+
handleImports: (0, babel_plugin_import_1.default)({
|
|
33
|
+
libraryName: 'antd',
|
|
34
|
+
style: true,
|
|
35
|
+
libraryDirectory: 'es',
|
|
36
|
+
}),
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
getProducedEvent() {
|
|
41
|
+
return this.produced;
|
|
42
|
+
}
|
|
43
|
+
consumeAllProducedEvents() {
|
|
44
|
+
this.produced = [];
|
|
45
|
+
}
|
|
46
|
+
shouldBuild() {
|
|
47
|
+
if (utils_1.lodash.isEqual(this.builtWithDep, this.currentDep)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
if (process.env.DEBUG_UMI) {
|
|
52
|
+
const reason = (0, why_1.default)(this.builtWithDep, this.currentDep);
|
|
53
|
+
utils_1.logger.info('[MFSU][eager]: isEqual(oldDep,newDep) === false, because ', reason);
|
|
54
|
+
}
|
|
55
|
+
return 'dependencies changed';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
getDepModules() {
|
|
59
|
+
const map = this.getDependencies();
|
|
60
|
+
const staticDeps = {};
|
|
61
|
+
const keys = Object.keys(map);
|
|
62
|
+
for (const k of keys) {
|
|
63
|
+
staticDeps[k] = {
|
|
64
|
+
file: k,
|
|
65
|
+
version: map[k].version,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return staticDeps;
|
|
69
|
+
}
|
|
70
|
+
snapshot() {
|
|
71
|
+
this.builtWithDep = this.currentDep;
|
|
72
|
+
}
|
|
73
|
+
loadCache() {
|
|
74
|
+
if ((0, fs_1.existsSync)(this.cacheFilePath)) {
|
|
75
|
+
this.builtWithDep = JSON.parse((0, fs_1.readFileSync)(this.cacheFilePath, 'utf-8'));
|
|
76
|
+
utils_1.logger.info('[MFSU][eager] restored cache');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
writeCache() {
|
|
80
|
+
utils_1.fsExtra.mkdirpSync((0, path_1.dirname)(this.cacheFilePath));
|
|
81
|
+
const newContent = JSON.stringify(this.builtWithDep, null, 2);
|
|
82
|
+
if ((0, fs_1.existsSync)(this.cacheFilePath) &&
|
|
83
|
+
(0, fs_1.readFileSync)(this.cacheFilePath, 'utf-8') === newContent) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
utils_1.logger.info('[MFSU][eager] write cache');
|
|
87
|
+
(0, fs_1.writeFileSync)(this.cacheFilePath, newContent, 'utf-8');
|
|
88
|
+
}
|
|
89
|
+
getCacheFilePath() {
|
|
90
|
+
return this.cacheFilePath;
|
|
91
|
+
}
|
|
92
|
+
getDependencies() {
|
|
93
|
+
return this.currentDep;
|
|
94
|
+
}
|
|
95
|
+
init() {
|
|
96
|
+
const merged = this.opts.srcCodeCache.getMergedCode();
|
|
97
|
+
this.currentDep = this._getDependencies(merged.code, merged.imports);
|
|
98
|
+
}
|
|
99
|
+
_getDependencies(bigCodeString, imports) {
|
|
100
|
+
const start = Date.now();
|
|
101
|
+
const cwd = this.mfsu.opts.cwd;
|
|
102
|
+
const opts = {
|
|
103
|
+
exportAllMembers: this.mfsu.opts.exportAllMembers,
|
|
104
|
+
unMatchLibs: this.mfsu.opts.unMatchLibs,
|
|
105
|
+
remoteName: this.mfsu.opts.mfName,
|
|
106
|
+
alias: this.mfsu.alias,
|
|
107
|
+
externals: this.mfsu.externals,
|
|
108
|
+
};
|
|
109
|
+
const matched = {};
|
|
110
|
+
const unMatched = new Set();
|
|
111
|
+
const pkgNames = this.runtimeSimulations.map(({ packageName }) => packageName);
|
|
112
|
+
const groupedMockImports = {};
|
|
113
|
+
for (const imp of imports) {
|
|
114
|
+
if (pkgNames.indexOf(imp.n) >= 0) {
|
|
115
|
+
const name = imp.n;
|
|
116
|
+
if (groupedMockImports[name]) {
|
|
117
|
+
groupedMockImports[name].push(imp);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
groupedMockImports[name] = [imp];
|
|
121
|
+
}
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (unMatched.has(imp.n)) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (matched[imp.n]) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const match = (0, checkMatch_1.checkMatch)({
|
|
131
|
+
value: imp.n,
|
|
132
|
+
depth: 1,
|
|
133
|
+
filename: '_.js',
|
|
134
|
+
opts,
|
|
135
|
+
});
|
|
136
|
+
if (match.isMatch) {
|
|
137
|
+
matched[match.value] = {
|
|
138
|
+
...match,
|
|
139
|
+
version: dep_1.Dep.getDepVersion({
|
|
140
|
+
dep: match.value,
|
|
141
|
+
cwd,
|
|
142
|
+
}),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
unMatched.add(imp.n);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
this.simulateRuntimeTransform(matched, groupedMockImports, bigCodeString);
|
|
150
|
+
this.appendIncludeList(matched, opts);
|
|
151
|
+
utils_1.logger.debug('[MFSU][eager] _getDependencies costs', Date.now() - start);
|
|
152
|
+
return matched;
|
|
153
|
+
}
|
|
154
|
+
simulateRuntimeTransform(matched, groupedImports, rawCode) {
|
|
155
|
+
for (const mock of this.runtimeSimulations) {
|
|
156
|
+
const name = mock.packageName;
|
|
157
|
+
const pathToVersion = (dep) => {
|
|
158
|
+
return dep_1.Dep.getDepVersion({
|
|
159
|
+
dep,
|
|
160
|
+
cwd: this.cwd,
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
const ms = mock.handleImports({
|
|
164
|
+
imports: groupedImports[name],
|
|
165
|
+
rawCode,
|
|
166
|
+
alias: this.mfsu.alias,
|
|
167
|
+
mfName: this.mfsu.opts.mfName,
|
|
168
|
+
pathToVersion,
|
|
169
|
+
});
|
|
170
|
+
for (const m of ms) {
|
|
171
|
+
matched[m.value] = m;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
appendIncludeList(matched, opts) {
|
|
176
|
+
for (const p of this.include) {
|
|
177
|
+
const match = (0, checkMatch_1.checkMatch)({
|
|
178
|
+
value: p,
|
|
179
|
+
depth: 1,
|
|
180
|
+
filename: '_.js',
|
|
181
|
+
opts,
|
|
182
|
+
});
|
|
183
|
+
if (match.isMatch) {
|
|
184
|
+
matched[match.value] = {
|
|
185
|
+
...match,
|
|
186
|
+
version: dep_1.Dep.getDepVersion({
|
|
187
|
+
dep: match.value,
|
|
188
|
+
cwd: this.cwd,
|
|
189
|
+
}),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async allRuntimeHelpers() {
|
|
195
|
+
// todo mfsu4
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
exports.StaticDepInfo = StaticDepInfo;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { Compiler } from 'webpack';
|
|
2
|
-
interface
|
|
2
|
+
export interface IBuildDepPluginOpts {
|
|
3
3
|
onCompileDone: Function;
|
|
4
|
+
onFileChange?: (c: Compiler) => Promise<any>;
|
|
5
|
+
beforeCompile?: () => Promise<any>;
|
|
4
6
|
}
|
|
5
7
|
export declare class BuildDepPlugin {
|
|
6
8
|
private opts;
|
|
7
|
-
constructor(opts:
|
|
9
|
+
constructor(opts: IBuildDepPluginOpts);
|
|
8
10
|
apply(compiler: Compiler): void;
|
|
9
11
|
}
|
|
10
|
-
export {};
|
|
@@ -7,6 +7,14 @@ class BuildDepPlugin {
|
|
|
7
7
|
this.opts = opts;
|
|
8
8
|
}
|
|
9
9
|
apply(compiler) {
|
|
10
|
+
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, (c) => {
|
|
11
|
+
var _a, _b;
|
|
12
|
+
return ((_b = (_a = this.opts).onFileChange) === null || _b === void 0 ? void 0 : _b.call(_a, c)) || Promise.resolve();
|
|
13
|
+
});
|
|
14
|
+
compiler.hooks.beforeCompile.tap(PLUGIN_NAME, () => {
|
|
15
|
+
var _a, _b;
|
|
16
|
+
(_b = (_a = this.opts).beforeCompile) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
17
|
+
});
|
|
10
18
|
compiler.hooks.done.tap(PLUGIN_NAME, (stats) => {
|
|
11
19
|
if (!stats.hasErrors()) {
|
|
12
20
|
this.opts.onCompileDone();
|