@umijs/mfsu 4.0.0-canary.20220429.3 → 4.0.0-canary.20220429.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.
Files changed (55) hide show
  1. package/dist/babelPlugins/awaitImport/awaitImport.d.ts +27 -0
  2. package/dist/babelPlugins/awaitImport/awaitImport.js +120 -0
  3. package/dist/babelPlugins/awaitImport/checkMatch.d.ts +18 -0
  4. package/dist/babelPlugins/awaitImport/checkMatch.js +127 -0
  5. package/dist/babelPlugins/awaitImport/getAliasedPath.d.ts +4 -0
  6. package/dist/babelPlugins/awaitImport/getAliasedPath.js +30 -0
  7. package/dist/babelPlugins/awaitImport/getRealPath.d.ts +4 -0
  8. package/dist/babelPlugins/awaitImport/getRealPath.js +24 -0
  9. package/dist/babelPlugins/awaitImport/isExternals.d.ts +11 -0
  10. package/dist/babelPlugins/awaitImport/isExternals.js +29 -0
  11. package/dist/babelPlugins/awaitImport/parseSpecifiers.d.ts +1 -0
  12. package/dist/babelPlugins/awaitImport/parseSpecifiers.js +55 -0
  13. package/dist/constants.d.ts +7 -0
  14. package/dist/constants.js +10 -0
  15. package/dist/dep/dep.d.ts +30 -0
  16. package/dist/dep/dep.js +107 -0
  17. package/dist/dep/getCJSExports.d.ts +3 -0
  18. package/dist/dep/getCJSExports.js +58 -0
  19. package/dist/dep/getExposeFromContent.d.ts +6 -0
  20. package/dist/dep/getExposeFromContent.js +80 -0
  21. package/dist/dep/getModuleExports.d.ts +7 -0
  22. package/dist/dep/getModuleExports.js +45 -0
  23. package/dist/depBuilder/depBuilder.d.ts +30 -0
  24. package/dist/depBuilder/depBuilder.js +173 -0
  25. package/dist/depBuilder/getESBuildEntry.d.ts +4 -0
  26. package/dist/depBuilder/getESBuildEntry.js +328 -0
  27. package/dist/depInfo.d.ts +17 -0
  28. package/dist/depInfo.js +50 -0
  29. package/dist/esbuildHandlers/autoCssModules.d.ts +2 -0
  30. package/dist/esbuildHandlers/autoCssModules.js +24 -0
  31. package/dist/esbuildHandlers/awaitImport/index.d.ts +12 -0
  32. package/dist/esbuildHandlers/awaitImport/index.js +44 -0
  33. package/dist/index.d.ts +4 -0
  34. package/dist/index.js +23 -0
  35. package/dist/loader/esbuild.d.ts +5 -0
  36. package/dist/loader/esbuild.js +54 -0
  37. package/dist/mfsu.d.ts +63 -0
  38. package/dist/mfsu.js +304 -0
  39. package/dist/moduleGraph.d.ts +73 -0
  40. package/dist/moduleGraph.js +197 -0
  41. package/dist/types.d.ts +16 -0
  42. package/dist/types.js +8 -0
  43. package/dist/utils/makeArray.d.ts +1 -0
  44. package/dist/utils/makeArray.js +7 -0
  45. package/dist/utils/trimFileContent.d.ts +1 -0
  46. package/dist/utils/trimFileContent.js +7 -0
  47. package/dist/webpackPlugins/buildDepPlugin.d.ts +10 -0
  48. package/dist/webpackPlugins/buildDepPlugin.js +17 -0
  49. package/dist/webpackPlugins/depChunkIdPrefixPlugin.d.ts +5 -0
  50. package/dist/webpackPlugins/depChunkIdPrefixPlugin.js +19 -0
  51. package/dist/webpackPlugins/stripSourceMapUrlPlugin.d.ts +10 -0
  52. package/dist/webpackPlugins/stripSourceMapUrlPlugin.js +28 -0
  53. package/dist/webpackPlugins/writeCachePlugin.d.ts +10 -0
  54. package/dist/webpackPlugins/writeCachePlugin.js +15 -0
  55. package/package.json +4 -4
package/dist/mfsu.js ADDED
@@ -0,0 +1,304 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.MFSU = void 0;
16
+ const bundler_utils_1 = require("@umijs/bundler-utils");
17
+ const utils_1 = require("@umijs/utils");
18
+ const assert_1 = __importDefault(require("assert"));
19
+ const fs_1 = require("fs");
20
+ const path_1 = require("path");
21
+ const mrmime_1 = require("../compiled/mrmime");
22
+ // @ts-ignore
23
+ const webpack_virtual_modules_1 = __importDefault(require("../compiled/webpack-virtual-modules"));
24
+ const awaitImport_1 = __importDefault(require("./babelPlugins/awaitImport/awaitImport"));
25
+ const getRealPath_1 = require("./babelPlugins/awaitImport/getRealPath");
26
+ const constants_1 = require("./constants");
27
+ const dep_1 = require("./dep/dep");
28
+ const depBuilder_1 = require("./depBuilder/depBuilder");
29
+ const depInfo_1 = require("./depInfo");
30
+ const awaitImport_2 = __importDefault(require("./esbuildHandlers/awaitImport"));
31
+ const types_1 = require("./types");
32
+ const makeArray_1 = require("./utils/makeArray");
33
+ const buildDepPlugin_1 = require("./webpackPlugins/buildDepPlugin");
34
+ class MFSU {
35
+ constructor(opts) {
36
+ this.alias = {};
37
+ this.externals = [];
38
+ this.depConfig = null;
39
+ this.buildDepsAgain = false;
40
+ this.progress = { done: false };
41
+ this.publicPath = '/';
42
+ this.opts = opts;
43
+ this.opts.mfName = this.opts.mfName || constants_1.DEFAULT_MF_NAME;
44
+ this.opts.tmpBase =
45
+ this.opts.tmpBase || (0, path_1.join)(process.cwd(), constants_1.DEFAULT_TMP_DIR_NAME);
46
+ this.opts.mode = this.opts.mode || types_1.Mode.development;
47
+ this.opts.getCacheDependency = this.opts.getCacheDependency || (() => ({}));
48
+ this.onProgress = (progress) => {
49
+ var _a, _b;
50
+ this.progress = Object.assign(Object.assign({}, this.progress), progress);
51
+ (_b = (_a = this.opts).onMFSUProgress) === null || _b === void 0 ? void 0 : _b.call(_a, this.progress);
52
+ };
53
+ this.opts.cwd = this.opts.cwd || process.cwd();
54
+ this.depInfo = new depInfo_1.DepInfo({ mfsu: this });
55
+ this.depBuilder = new depBuilder_1.DepBuilder({ mfsu: this });
56
+ this.depInfo.loadCache();
57
+ }
58
+ // swc don't support top-level await
59
+ // ref: https://github.com/vercel/next.js/issues/31054
60
+ asyncImport(content) {
61
+ return `await import('${(0, utils_1.winPath)(content)}');`;
62
+ // return `(async () => await import('${content}'))();`;
63
+ }
64
+ setWebpackConfig(opts) {
65
+ var _a;
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const { mfName } = this.opts;
68
+ /**
69
+ * config
70
+ */
71
+ // set alias and externals with reference for babel plugin
72
+ Object.assign(this.alias, ((_a = opts.config.resolve) === null || _a === void 0 ? void 0 : _a.alias) || {});
73
+ this.externals.push(...(0, makeArray_1.makeArray)(opts.config.externals || []));
74
+ // entry
75
+ const entry = {};
76
+ const virtualModules = {};
77
+ // ensure entry object type
78
+ const entryObject = utils_1.lodash.isString(opts.config.entry)
79
+ ? { default: [opts.config.entry] }
80
+ : opts.config.entry;
81
+ (0, assert_1.default)(utils_1.lodash.isPlainObject(entryObject), `webpack config 'entry' value must be a string or an object.`);
82
+ for (const key of Object.keys(entryObject)) {
83
+ const virtualPath = `./mfsu-virtual-entry/${key}.js`;
84
+ const virtualContent = [];
85
+ let index = 1;
86
+ let hasDefaultExport = false;
87
+ const entryFiles = utils_1.lodash.isArray(entryObject[key])
88
+ ? entryObject[key]
89
+ : [entryObject[key]];
90
+ for (let entry of entryFiles) {
91
+ // ensure entry is a file
92
+ if ((0, fs_1.statSync)(entry).isDirectory()) {
93
+ const realEntry = (0, utils_1.tryPaths)([
94
+ (0, path_1.join)(entry, 'index.tsx'),
95
+ (0, path_1.join)(entry, 'index.ts'),
96
+ ]);
97
+ (0, assert_1.default)(realEntry, `entry file not found, please configure the specific entry path. (e.g. 'src/index.tsx')`);
98
+ entry = realEntry;
99
+ }
100
+ const content = (0, fs_1.readFileSync)(entry, 'utf-8');
101
+ const [_imports, exports] = yield (0, bundler_utils_1.parseModule)({ content, path: entry });
102
+ if (exports.length) {
103
+ virtualContent.push(`const k${index} = ${this.asyncImport(entry)}`);
104
+ for (const exportName of exports) {
105
+ if (exportName === 'default') {
106
+ hasDefaultExport = true;
107
+ virtualContent.push(`export default k${index}.${exportName}`);
108
+ }
109
+ else {
110
+ virtualContent.push(`export const ${exportName} = k${index}.${exportName}`);
111
+ }
112
+ }
113
+ }
114
+ else {
115
+ virtualContent.push(this.asyncImport(entry));
116
+ }
117
+ index += 1;
118
+ }
119
+ if (!hasDefaultExport) {
120
+ virtualContent.push(`export default 1;`);
121
+ }
122
+ virtualModules[virtualPath] = virtualContent.join('\n');
123
+ entry[key] = virtualPath;
124
+ }
125
+ opts.config.entry = entry;
126
+ // plugins
127
+ opts.config.plugins = opts.config.plugins || [];
128
+ // support publicPath auto
129
+ let publicPath = opts.config.output.publicPath;
130
+ if (publicPath === 'auto') {
131
+ publicPath = '/';
132
+ }
133
+ this.publicPath = publicPath;
134
+ opts.config.plugins.push(...[
135
+ new webpack_virtual_modules_1.default(virtualModules),
136
+ new this.opts.implementor.container.ModuleFederationPlugin({
137
+ name: '__',
138
+ remotes: {
139
+ [mfName]: this.opts.runtimePublicPath
140
+ ? // ref:
141
+ // https://webpack.js.org/concepts/module-federation/#promise-based-dynamic-remotes
142
+ `
143
+ promise new Promise(resolve => {
144
+ const remoteUrlWithVersion = window.publicPath + '${constants_1.REMOTE_FILE_FULL}';
145
+ const script = document.createElement('script');
146
+ script.src = remoteUrlWithVersion;
147
+ script.onload = () => {
148
+ // the injected script has loaded and is available on window
149
+ // we can now resolve this Promise
150
+ const proxy = {
151
+ get: (request) => window['${mfName}'].get(request),
152
+ init: (arg) => {
153
+ try {
154
+ return window['${mfName}'].init(arg);
155
+ } catch(e) {
156
+ console.log('remote container already initialized');
157
+ }
158
+ }
159
+ }
160
+ resolve(proxy);
161
+ }
162
+ // inject this script with the src set to the versioned remoteEntry.js
163
+ document.head.appendChild(script);
164
+ })
165
+ `.trimLeft()
166
+ : `${mfName}@${publicPath}${constants_1.REMOTE_FILE_FULL}`,
167
+ },
168
+ }),
169
+ new buildDepPlugin_1.BuildDepPlugin({
170
+ onCompileDone: () => {
171
+ if (this.depBuilder.isBuilding) {
172
+ this.buildDepsAgain = true;
173
+ }
174
+ else {
175
+ this.buildDeps()
176
+ .then(() => {
177
+ this.onProgress({
178
+ done: true,
179
+ });
180
+ })
181
+ .catch((e) => {
182
+ utils_1.logger.error(e);
183
+ this.onProgress({
184
+ done: true,
185
+ });
186
+ });
187
+ }
188
+ },
189
+ }),
190
+ // new WriteCachePlugin({
191
+ // onWriteCache: lodash.debounce(() => {
192
+ // this.depInfo.writeCache();
193
+ // }, 300),
194
+ // }),
195
+ ]);
196
+ // ensure topLevelAwait enabled
197
+ utils_1.lodash.set(opts.config, 'experiments.topLevelAwait', true);
198
+ /**
199
+ * depConfig
200
+ */
201
+ this.depConfig = opts.depConfig;
202
+ });
203
+ }
204
+ buildDeps() {
205
+ return __awaiter(this, void 0, void 0, function* () {
206
+ const shouldBuild = this.depInfo.shouldBuild();
207
+ if (!shouldBuild) {
208
+ utils_1.logger.info('MFSU skip buildDeps');
209
+ return;
210
+ }
211
+ this.depInfo.snapshot();
212
+ const deps = dep_1.Dep.buildDeps({
213
+ deps: this.depInfo.moduleGraph.depSnapshotModules,
214
+ cwd: this.opts.cwd,
215
+ mfsu: this,
216
+ });
217
+ utils_1.logger.info(`MFSU buildDeps since ${shouldBuild}`);
218
+ utils_1.logger.debug(deps.map((dep) => dep.file).join(', '));
219
+ yield this.depBuilder.build({
220
+ deps,
221
+ });
222
+ // Write cache
223
+ this.depInfo.writeCache();
224
+ if (this.buildDepsAgain) {
225
+ utils_1.logger.info('MFSU buildDepsAgain');
226
+ this.buildDepsAgain = false;
227
+ this.buildDeps().catch((e) => {
228
+ utils_1.logger.error(e);
229
+ });
230
+ }
231
+ });
232
+ }
233
+ getMiddlewares() {
234
+ return [
235
+ (req, res, next) => {
236
+ const publicPath = this.publicPath;
237
+ const isMF = req.path.startsWith(`${publicPath}${constants_1.MF_VA_PREFIX}`) ||
238
+ req.path.startsWith(`${publicPath}${constants_1.MF_DEP_PREFIX}`) ||
239
+ req.path.startsWith(`${publicPath}${constants_1.MF_STATIC_PREFIX}`);
240
+ if (isMF) {
241
+ this.depBuilder.onBuildComplete(() => {
242
+ if (!req.path.includes(constants_1.REMOTE_FILE)) {
243
+ res.setHeader('cache-control', 'max-age=31536000,immutable');
244
+ }
245
+ res.setHeader('content-type', (0, mrmime_1.lookup)((0, path_1.extname)(req.path)) || 'text/plain');
246
+ const relativePath = req.path.replace(new RegExp(`^${publicPath}`), '/');
247
+ const content = (0, fs_1.readFileSync)((0, path_1.join)(this.opts.tmpBase, relativePath));
248
+ res.send(content);
249
+ });
250
+ }
251
+ else {
252
+ next();
253
+ }
254
+ },
255
+ ];
256
+ }
257
+ getAwaitImportCollectOpts() {
258
+ return {
259
+ onTransformDeps: () => { },
260
+ onCollect: ({ file, data, }) => {
261
+ this.depInfo.moduleGraph.onFileChange({
262
+ file,
263
+ // @ts-ignore
264
+ deps: [
265
+ ...Array.from(data.matched).map((item) => ({
266
+ file: item.sourceValue,
267
+ isDependency: true,
268
+ version: dep_1.Dep.getDepVersion({
269
+ dep: item.sourceValue,
270
+ cwd: this.opts.cwd,
271
+ }),
272
+ })),
273
+ ...Array.from(data.unMatched).map((item) => ({
274
+ file: (0, getRealPath_1.getRealPath)({
275
+ file,
276
+ dep: item.sourceValue,
277
+ }),
278
+ isDependency: false,
279
+ })),
280
+ ],
281
+ });
282
+ },
283
+ exportAllMembers: this.opts.exportAllMembers,
284
+ unMatchLibs: this.opts.unMatchLibs,
285
+ remoteName: this.opts.mfName,
286
+ alias: this.alias,
287
+ externals: this.externals,
288
+ };
289
+ }
290
+ getBabelPlugins() {
291
+ return [[awaitImport_1.default, this.getAwaitImportCollectOpts()]];
292
+ }
293
+ getEsbuildLoaderHandler() {
294
+ const cache = new Map();
295
+ const checkOpts = this.getAwaitImportCollectOpts();
296
+ return [
297
+ (0, awaitImport_2.default)({
298
+ cache,
299
+ opts: checkOpts,
300
+ }),
301
+ ];
302
+ }
303
+ }
304
+ exports.MFSU = MFSU;
@@ -0,0 +1,73 @@
1
+ declare class ModuleNode {
2
+ file: string;
3
+ importers: Set<ModuleNode>;
4
+ importedModules: Set<ModuleNode>;
5
+ isDependency: boolean;
6
+ isRoot: boolean;
7
+ version: string | null;
8
+ constructor(file: string);
9
+ }
10
+ interface IDep {
11
+ file: string;
12
+ isDependency: boolean;
13
+ version?: string;
14
+ }
15
+ export declare class ModuleGraph {
16
+ fileToModules: Map<string, ModuleNode>;
17
+ depToModules: Map<string, ModuleNode>;
18
+ depSnapshotModules: Record<string, {
19
+ file: string;
20
+ version: string;
21
+ }>;
22
+ rootModules: Set<ModuleNode>;
23
+ constructor();
24
+ restore(data: {
25
+ roots: any;
26
+ fileModules: any;
27
+ depModules: any;
28
+ depSnapshotModules: any;
29
+ }): void;
30
+ toJSON(): {
31
+ roots: string[];
32
+ fileModules: Record<string, {
33
+ importedModules: string[];
34
+ isRoot?: boolean | undefined;
35
+ }>;
36
+ depModules: Record<string, {
37
+ version: string | null;
38
+ }>;
39
+ depSnapshotModules: Record<string, {
40
+ file: string;
41
+ version: string;
42
+ }>;
43
+ };
44
+ snapshotDeps(): void;
45
+ getDepsInfo(mods: Map<string, ModuleNode>): Record<string, {
46
+ file: string;
47
+ version: string;
48
+ }>;
49
+ getDepInfo(mod: ModuleNode): {
50
+ file: string;
51
+ version: string;
52
+ };
53
+ hasDepChanged(): boolean;
54
+ onFileChange(opts: {
55
+ file: string;
56
+ deps: IDep[];
57
+ }): void;
58
+ updateModule(opts: {
59
+ mod: ModuleNode;
60
+ deps: IDep[];
61
+ }): void;
62
+ addNode(opts: {
63
+ file: string;
64
+ isDependency: boolean;
65
+ importer: ModuleNode;
66
+ version?: string | null;
67
+ }): void;
68
+ deleteNode(opts: {
69
+ mod: ModuleNode;
70
+ importer: ModuleNode;
71
+ }): void;
72
+ }
73
+ export {};
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModuleGraph = void 0;
4
+ const utils_1 = require("@umijs/utils");
5
+ class ModuleNode {
6
+ constructor(file) {
7
+ this.importers = new Set();
8
+ this.importedModules = new Set();
9
+ this.isDependency = false;
10
+ this.isRoot = false;
11
+ this.version = null;
12
+ this.file = file;
13
+ }
14
+ }
15
+ class ModuleGraph {
16
+ constructor() {
17
+ this.fileToModules = new Map();
18
+ this.depToModules = new Map();
19
+ this.depSnapshotModules = {};
20
+ this.rootModules = new Set();
21
+ }
22
+ restore(data) {
23
+ let fileMap = new Map();
24
+ const addNode = ({ file, importer }) => {
25
+ // fix circular dependency
26
+ if (fileMap.has(file))
27
+ return;
28
+ fileMap.set(file, true);
29
+ const mod = new ModuleNode(file);
30
+ let isDependency = false;
31
+ let info;
32
+ if (data.fileModules[file]) {
33
+ info = data.fileModules[file];
34
+ }
35
+ else if (data.depModules[file]) {
36
+ info = data.depModules[file];
37
+ isDependency = true;
38
+ }
39
+ if (info.isRoot)
40
+ mod.isRoot = true;
41
+ if (importer) {
42
+ mod.importers.add(importer);
43
+ importer.importedModules.add(mod);
44
+ }
45
+ mod.isDependency = isDependency;
46
+ if (info.version !== undefined) {
47
+ mod.version = info.version;
48
+ }
49
+ if (isDependency) {
50
+ this.depToModules.set(file, mod);
51
+ }
52
+ else {
53
+ for (const importedModule of info.importedModules) {
54
+ addNode({ file: importedModule, importer: mod });
55
+ }
56
+ this.fileToModules.set(file, mod);
57
+ }
58
+ };
59
+ for (const root of data.roots) {
60
+ addNode({ file: root });
61
+ }
62
+ this.depSnapshotModules = data.depSnapshotModules;
63
+ }
64
+ toJSON() {
65
+ const roots = [];
66
+ const fileModules = {};
67
+ const depModules = {};
68
+ this.depToModules.forEach((value, key) => {
69
+ depModules[key] = {
70
+ version: value.version,
71
+ };
72
+ });
73
+ this.fileToModules.forEach((value, key) => {
74
+ fileModules[key] = {
75
+ importedModules: Array.from(value.importedModules).map((item) => item.file),
76
+ };
77
+ if (value.isRoot) {
78
+ fileModules[key].isRoot = true;
79
+ roots.push(key);
80
+ }
81
+ });
82
+ return {
83
+ roots,
84
+ fileModules,
85
+ depModules,
86
+ depSnapshotModules: this.depSnapshotModules,
87
+ };
88
+ }
89
+ snapshotDeps() {
90
+ this.depSnapshotModules = this.getDepsInfo(this.depToModules);
91
+ }
92
+ getDepsInfo(mods) {
93
+ return Array.from(mods.keys()).reduce((memo, key) => {
94
+ memo[key] = this.getDepInfo(mods.get(key));
95
+ return memo;
96
+ }, {});
97
+ }
98
+ getDepInfo(mod) {
99
+ return {
100
+ file: mod.file,
101
+ version: mod.version,
102
+ };
103
+ }
104
+ hasDepChanged() {
105
+ const depModulesInfo = this.getDepsInfo(this.depToModules);
106
+ return !utils_1.lodash.isEqual(depModulesInfo, this.depSnapshotModules);
107
+ }
108
+ onFileChange(opts) {
109
+ if (this.fileToModules.has(opts.file)) {
110
+ const mod = this.fileToModules.get(opts.file);
111
+ this.updateModule({
112
+ mod,
113
+ deps: opts.deps,
114
+ });
115
+ }
116
+ else {
117
+ const mod = new ModuleNode(opts.file);
118
+ mod.isRoot = true;
119
+ this.fileToModules.set(opts.file, mod);
120
+ this.rootModules.add(mod);
121
+ opts.deps.forEach((dep) => {
122
+ this.addNode({
123
+ file: dep.file,
124
+ isDependency: dep.isDependency,
125
+ version: dep.version || null,
126
+ importer: mod,
127
+ });
128
+ });
129
+ }
130
+ }
131
+ updateModule(opts) {
132
+ const importedModulesMap = Array.from(opts.mod.importedModules).reduce((memo, mod) => {
133
+ memo[mod.file] = mod;
134
+ return memo;
135
+ }, {});
136
+ const newDeps = [];
137
+ for (const dep of opts.deps) {
138
+ // update
139
+ if (importedModulesMap[dep.file]) {
140
+ if (dep.version !== undefined) {
141
+ importedModulesMap[dep.file].version = dep.version;
142
+ }
143
+ delete importedModulesMap[dep.file];
144
+ }
145
+ // add
146
+ else {
147
+ newDeps.push(dep);
148
+ }
149
+ }
150
+ Object.keys(importedModulesMap).forEach((key) => {
151
+ this.deleteNode({ mod: importedModulesMap[key], importer: opts.mod });
152
+ });
153
+ newDeps.forEach((dep) => {
154
+ this.addNode(Object.assign(Object.assign({}, dep), { importer: opts.mod }));
155
+ });
156
+ }
157
+ addNode(opts) {
158
+ const modules = opts.isDependency ? this.depToModules : this.fileToModules;
159
+ let mod;
160
+ if (modules.has(opts.file)) {
161
+ mod = modules.get(opts.file);
162
+ if (opts.version !== undefined)
163
+ mod.version = opts.version;
164
+ }
165
+ else {
166
+ mod = new ModuleNode(opts.file);
167
+ mod.isDependency = opts.isDependency;
168
+ if (opts.version !== undefined)
169
+ mod.version = opts.version;
170
+ modules.set(opts.file, mod);
171
+ }
172
+ if (!mod.importers.has(opts.importer)) {
173
+ mod.importers.add(opts.importer);
174
+ }
175
+ if (!opts.importer.importedModules.has(mod)) {
176
+ opts.importer.importedModules.add(mod);
177
+ }
178
+ }
179
+ deleteNode(opts) {
180
+ const modules = opts.mod.isDependency
181
+ ? this.depToModules
182
+ : this.fileToModules;
183
+ const { mod, importer } = opts;
184
+ mod.importers.delete(opts.importer);
185
+ importer.importedModules.delete(mod);
186
+ if (!mod.importers.size) {
187
+ modules.delete(opts.mod.file);
188
+ mod.importedModules.forEach((importedModule) => {
189
+ this.deleteNode({
190
+ mod: importedModule,
191
+ importer: mod,
192
+ });
193
+ });
194
+ }
195
+ }
196
+ }
197
+ exports.ModuleGraph = ModuleGraph;
@@ -0,0 +1,16 @@
1
+ import type { ImportSpecifier } from '@umijs/bundler-utils/compiled/es-module-lexer';
2
+ import type { TransformOptions } from '@umijs/bundler-utils/compiled/esbuild';
3
+ export declare enum Mode {
4
+ development = "development",
5
+ production = "production"
6
+ }
7
+ export interface IEsbuildLoaderHandlerParams {
8
+ code: string;
9
+ filePath: string;
10
+ imports: readonly ImportSpecifier[];
11
+ exports: readonly string[];
12
+ }
13
+ export interface IEsbuildLoaderOpts extends Partial<TransformOptions> {
14
+ handler?: Array<(opts: IEsbuildLoaderHandlerParams) => string>;
15
+ implementation?: typeof import('@umijs/bundler-utils/compiled/esbuild');
16
+ }
package/dist/types.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Mode = void 0;
4
+ var Mode;
5
+ (function (Mode) {
6
+ Mode["development"] = "development";
7
+ Mode["production"] = "production";
8
+ })(Mode = exports.Mode || (exports.Mode = {}));
@@ -0,0 +1 @@
1
+ export declare function makeArray(item: any): any[];
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeArray = void 0;
4
+ function makeArray(item) {
5
+ return Array.isArray(item) ? item : [item];
6
+ }
7
+ exports.makeArray = makeArray;
@@ -0,0 +1 @@
1
+ export declare function trimFileContent(content: string): string;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.trimFileContent = void 0;
4
+ function trimFileContent(content) {
5
+ return content.trim() + '\n';
6
+ }
7
+ exports.trimFileContent = trimFileContent;
@@ -0,0 +1,10 @@
1
+ import type { Compiler } from 'webpack';
2
+ interface IOpts {
3
+ onCompileDone: Function;
4
+ }
5
+ export declare class BuildDepPlugin {
6
+ private opts;
7
+ constructor(opts: IOpts);
8
+ apply(compiler: Compiler): void;
9
+ }
10
+ export {};
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BuildDepPlugin = void 0;
4
+ const PLUGIN_NAME = 'MFSUBuildDeps';
5
+ class BuildDepPlugin {
6
+ constructor(opts) {
7
+ this.opts = opts;
8
+ }
9
+ apply(compiler) {
10
+ compiler.hooks.done.tap(PLUGIN_NAME, (stats) => {
11
+ if (!stats.hasErrors()) {
12
+ this.opts.onCompileDone();
13
+ }
14
+ });
15
+ }
16
+ }
17
+ exports.BuildDepPlugin = BuildDepPlugin;
@@ -0,0 +1,5 @@
1
+ import { Compiler } from 'webpack';
2
+ export declare class DepChunkIdPrefixPlugin {
3
+ constructor();
4
+ apply(compiler: Compiler): void;
5
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DepChunkIdPrefixPlugin = void 0;
4
+ const constants_1 = require("../constants");
5
+ const pluginId = 'MFSUDepChunkIdPrefix';
6
+ class DepChunkIdPrefixPlugin {
7
+ constructor() { }
8
+ apply(compiler) {
9
+ compiler.hooks.compilation.tap(pluginId, (compilation) => {
10
+ compilation.hooks.afterOptimizeChunkIds.tap(pluginId, (chunks) => {
11
+ for (const chunk of chunks) {
12
+ chunk.id = constants_1.MF_DEP_PREFIX + chunk.id;
13
+ chunk.ids = [chunk.id];
14
+ }
15
+ });
16
+ });
17
+ }
18
+ }
19
+ exports.DepChunkIdPrefixPlugin = DepChunkIdPrefixPlugin;