@umijs/mfsu 4.0.0-beta.14 → 4.0.0-beta.18

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.
@@ -23,6 +23,8 @@ function checkMatch({ value, path, opts, isExportAll, depth, cache, }) {
23
23
  (0, assert_1.default)(depth <= 10, `endless loop detected in checkMatch, please check your alias config.`);
24
24
  opts = opts || {};
25
25
  const remoteName = opts.remoteName || 'mf';
26
+ // FIXME: hard code for vite mode
27
+ value = value.replace(/^@fs\//, '/');
26
28
  if (
27
29
  // unMatch specified libs
28
30
  ((_a = opts.unMatchLibs) === null || _a === void 0 ? void 0 : _a.includes(value)) ||
@@ -98,6 +98,7 @@ class DepBuilder {
98
98
  writeMFFiles(opts) {
99
99
  return __awaiter(this, void 0, void 0, function* () {
100
100
  const tmpBase = this.opts.mfsu.opts.tmpBase;
101
+ utils_1.fsExtra.mkdirpSync(tmpBase);
101
102
  // expose files
102
103
  for (const dep of opts.deps) {
103
104
  const content = yield dep.buildExposeContent();
package/dist/depInfo.js CHANGED
@@ -27,6 +27,7 @@ class DepInfo {
27
27
  }
28
28
  loadCache() {
29
29
  if ((0, fs_1.existsSync)(this.cacheFilePath)) {
30
+ utils_1.logger.info('MFSU restore cache');
30
31
  const { cacheDependency, moduleGraph } = JSON.parse((0, fs_1.readFileSync)(this.cacheFilePath, 'utf-8'));
31
32
  this.cacheDependency = cacheDependency;
32
33
  this.moduleGraph.restore(moduleGraph);
@@ -34,6 +35,7 @@ class DepInfo {
34
35
  }
35
36
  writeCache() {
36
37
  utils_1.fsExtra.mkdirpSync((0, path_1.dirname)(this.cacheFilePath));
38
+ utils_1.logger.info('MFSU write cache');
37
39
  (0, fs_1.writeFileSync)(this.cacheFilePath, JSON.stringify({
38
40
  cacheDependency: this.cacheDependency,
39
41
  moduleGraph: this.moduleGraph.toJSON(),
package/dist/mfsu.d.ts CHANGED
@@ -27,6 +27,7 @@ export declare class MFSU {
27
27
  depBuilder: DepBuilder;
28
28
  depConfig: Configuration | null;
29
29
  constructor(opts: IOpts);
30
+ asyncImport(content: string): string;
30
31
  setWebpackConfig(opts: {
31
32
  config: Configuration;
32
33
  depConfig: Configuration;
package/dist/mfsu.js CHANGED
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.MFSU = void 0;
16
16
  const bundler_utils_1 = require("@umijs/bundler-utils");
17
17
  const utils_1 = require("@umijs/utils");
18
+ const assert_1 = __importDefault(require("assert"));
18
19
  const fs_1 = require("fs");
19
20
  const path_1 = require("path");
20
21
  const mrmime_1 = require("../compiled/mrmime");
@@ -47,6 +48,12 @@ class MFSU {
47
48
  this.depBuilder = new depBuilder_1.DepBuilder({ mfsu: this });
48
49
  this.depInfo.loadCache();
49
50
  }
51
+ // swc don't support top-level await
52
+ // ref: https://github.com/vercel/next.js/issues/31054
53
+ asyncImport(content) {
54
+ return `await import('${content}');`;
55
+ // return `(async () => await import('${content}'))();`;
56
+ }
50
57
  setWebpackConfig(opts) {
51
58
  var _a;
52
59
  return __awaiter(this, void 0, void 0, function* () {
@@ -60,26 +67,51 @@ class MFSU {
60
67
  // entry
61
68
  const entry = {};
62
69
  const virtualModules = {};
63
- for (const key of Object.keys(opts.config.entry)) {
70
+ // ensure entry object type
71
+ const entryObject = utils_1.lodash.isString(opts.config.entry)
72
+ ? { default: [opts.config.entry] }
73
+ : opts.config.entry;
74
+ (0, assert_1.default)(utils_1.lodash.isPlainObject(entryObject), `webpack config 'entry' value must be a string or an object.`);
75
+ for (const key of Object.keys(entryObject)) {
64
76
  const virtualPath = `./mfsu-virtual-entry/${key}.js`;
65
77
  const virtualContent = [];
66
78
  let index = 1;
67
- // @ts-ignore
68
- for (const entry of opts.config.entry[key]) {
79
+ let hasDefaultExport = false;
80
+ const entryFiles = utils_1.lodash.isArray(entryObject[key])
81
+ ? entryObject[key]
82
+ : [entryObject[key]];
83
+ for (let entry of entryFiles) {
84
+ // ensure entry is a file
85
+ if ((0, fs_1.statSync)(entry).isDirectory()) {
86
+ const realEntry = (0, utils_1.tryPaths)([
87
+ (0, path_1.join)(entry, 'index.tsx'),
88
+ (0, path_1.join)(entry, 'index.ts'),
89
+ ]);
90
+ (0, assert_1.default)(realEntry, `entry file not found, please configure the specific entry path. (e.g. 'src/index.tsx')`);
91
+ entry = realEntry;
92
+ }
69
93
  const content = (0, fs_1.readFileSync)(entry, 'utf-8');
70
94
  const [_imports, exports] = yield (0, bundler_utils_1.parseModule)({ content, path: entry });
71
95
  if (exports.length) {
72
- virtualContent.push(`const k${index} = await import('${entry}');`);
96
+ virtualContent.push(`const k${index} = ${this.asyncImport(entry)}`);
73
97
  for (const exportName of exports) {
74
- virtualContent.push(`export const ${exportName} = k${index}.${exportName}`);
98
+ if (exportName === 'default') {
99
+ hasDefaultExport = true;
100
+ virtualContent.push(`export default k${index}.${exportName}`);
101
+ }
102
+ else {
103
+ virtualContent.push(`export const ${exportName} = k${index}.${exportName}`);
104
+ }
75
105
  }
76
106
  }
77
107
  else {
78
- virtualContent.push(`await import('${entry}')`);
108
+ virtualContent.push(this.asyncImport(entry));
79
109
  }
80
110
  index += 1;
81
111
  }
82
- virtualContent.push(`export default 1;`);
112
+ if (!hasDefaultExport) {
113
+ virtualContent.push(`export default 1;`);
114
+ }
83
115
  virtualModules[virtualPath] = virtualContent.join('\n');
84
116
  entry[key] = virtualPath;
85
117
  }
@@ -97,7 +129,9 @@ class MFSU {
97
129
  name: '__',
98
130
  remotes: {
99
131
  [mfName]: this.opts.runtimePublicPath
100
- ? `
132
+ ? // ref:
133
+ // https://webpack.js.org/concepts/module-federation/#promise-based-dynamic-remotes
134
+ `
101
135
  promise new Promise(resolve => {
102
136
  const remoteUrlWithVersion = window.publicPath + '${constants_1.REMOTE_FILE_FULL}';
103
137
  const script = document.createElement('script');
@@ -132,11 +166,13 @@ promise new Promise(resolve => {
132
166
  },
133
167
  }),
134
168
  new writeCachePlugin_1.WriteCachePlugin({
135
- onWriteCache: () => {
169
+ onWriteCache: utils_1.lodash.debounce(() => {
136
170
  this.depInfo.writeCache();
137
- },
171
+ }, 300),
138
172
  }),
139
173
  ]);
174
+ // ensure topLevelAwait enabled
175
+ utils_1.lodash.set(opts.config, 'experiments.topLevelAwait', true);
140
176
  /**
141
177
  * depConfig
142
178
  */
@@ -145,14 +181,18 @@ promise new Promise(resolve => {
145
181
  }
146
182
  buildDeps() {
147
183
  return __awaiter(this, void 0, void 0, function* () {
148
- if (!this.depInfo.shouldBuild())
184
+ if (!this.depInfo.shouldBuild()) {
185
+ utils_1.logger.info('MFSU skip buildDeps');
149
186
  return;
187
+ }
150
188
  this.depInfo.snapshot();
151
189
  const deps = dep_1.Dep.buildDeps({
152
190
  deps: this.depInfo.moduleGraph.depSnapshotModules,
153
191
  cwd: this.opts.cwd,
154
192
  mfsu: this,
155
193
  });
194
+ utils_1.logger.info('MFSU buildDeps');
195
+ utils_1.logger.debug(deps.map((dep) => dep.file).join(', '));
156
196
  yield this.depBuilder.build({
157
197
  deps,
158
198
  });
@@ -31,6 +31,7 @@ export declare class ModuleGraph {
31
31
  roots: string[];
32
32
  fileModules: Record<string, {
33
33
  importedModules: string[];
34
+ isRoot?: boolean | undefined;
34
35
  }>;
35
36
  depModules: Record<string, {
36
37
  version: string | null;
@@ -38,8 +38,10 @@ class ModuleGraph {
38
38
  }
39
39
  if (info.isRoot)
40
40
  mod.isRoot = true;
41
- if (importer)
41
+ if (importer) {
42
42
  mod.importers.add(importer);
43
+ importer.importedModules.add(mod);
44
+ }
43
45
  mod.isDependency = isDependency;
44
46
  if (info.version !== undefined) {
45
47
  mod.version = info.version;
@@ -73,6 +75,7 @@ class ModuleGraph {
73
75
  importedModules: Array.from(value.importedModules).map((item) => item.file),
74
76
  };
75
77
  if (value.isRoot) {
78
+ fileModules[key].isRoot = true;
76
79
  roots.push(key);
77
80
  }
78
81
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/mfsu",
3
- "version": "4.0.0-beta.14",
3
+ "version": "4.0.0-beta.18",
4
4
  "description": "@umijs/mfsu",
5
5
  "homepage": "https://github.com/umijs/umi-next/tree/master/packages/mfsu#readme",
6
6
  "bugs": "https://github.com/umijs/umi-next/issues",
@@ -21,9 +21,9 @@
21
21
  "dev": "pnpm build -- --watch"
22
22
  },
23
23
  "dependencies": {
24
- "@umijs/bundler-esbuild": "4.0.0-beta.14",
25
- "@umijs/bundler-utils": "4.0.0-beta.14",
26
- "@umijs/utils": "4.0.0-beta.14"
24
+ "@umijs/bundler-esbuild": "4.0.0-beta.18",
25
+ "@umijs/bundler-utils": "4.0.0-beta.18",
26
+ "@umijs/utils": "4.0.0-beta.18"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/express": "4.17.13",