@umijs/mfsu 4.0.0-beta.12 → 4.0.0-beta.16

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.
@@ -12,6 +12,9 @@ const isExternals_1 = require("./isExternals");
12
12
  // const UNMATCH_LIBS = ['umi', 'dumi', '@alipay/bigfish'];
13
13
  const RE_NODE_MODULES = /node_modules/;
14
14
  const RE_UMI_LOCAL_DEV = /umi(-next)?\/packages\//;
15
+ function isUmiLocalDev(path) {
16
+ return RE_UMI_LOCAL_DEV.test(path);
17
+ }
15
18
  function checkMatch({ value, path, opts, isExportAll, depth, cache, }) {
16
19
  var _a, _b;
17
20
  let isMatch;
@@ -20,6 +23,8 @@ function checkMatch({ value, path, opts, isExportAll, depth, cache, }) {
20
23
  (0, assert_1.default)(depth <= 10, `endless loop detected in checkMatch, please check your alias config.`);
21
24
  opts = opts || {};
22
25
  const remoteName = opts.remoteName || 'mf';
26
+ // FIXME: hard code for vite mode
27
+ value = value.replace(/^@fs\//, '/');
23
28
  if (
24
29
  // unMatch specified libs
25
30
  ((_a = opts.unMatchLibs) === null || _a === void 0 ? void 0 : _a.includes(value)) ||
@@ -38,7 +43,7 @@ function checkMatch({ value, path, opts, isExportAll, depth, cache, }) {
38
43
  isMatch = false;
39
44
  }
40
45
  else if ((0, path_1.isAbsolute)(value)) {
41
- isMatch = RE_NODE_MODULES.test(value) || RE_UMI_LOCAL_DEV.test(value);
46
+ isMatch = RE_NODE_MODULES.test(value) || isUmiLocalDev(value);
42
47
  }
43
48
  else {
44
49
  const aliasedPath = (0, getAliasedPath_1.getAliasedPath)({
package/dist/dep/dep.js CHANGED
@@ -40,7 +40,7 @@ class Dep {
40
40
  this.file = opts.file;
41
41
  this.version = opts.version;
42
42
  this.cwd = opts.cwd;
43
- this.shortFile = this.file.replace(this.cwd, '$CWD$');
43
+ this.shortFile = this.file;
44
44
  this.normalizedFile = this.shortFile.replace(/\//g, '_').replace(/:/g, '_');
45
45
  this.filePath = `${constants_1.MF_VA_PREFIX}${this.normalizedFile}.js`;
46
46
  this.mfsu = opts.mfsu;
@@ -63,11 +63,7 @@ class DepBuilder {
63
63
  entry: {
64
64
  [`${constants_1.MF_VA_PREFIX}remoteEntry`]: entryPath,
65
65
  },
66
- config: {
67
- outputPath: tmpDir,
68
- alias: this.opts.mfsu.alias,
69
- externals: this.opts.mfsu.externals,
70
- },
66
+ config: Object.assign(Object.assign({}, this.opts.mfsu.opts.depBuildConfig), { outputPath: tmpDir, alias: this.opts.mfsu.alias, externals: this.opts.mfsu.externals }),
71
67
  inlineStyle: true,
72
68
  });
73
69
  utils_1.logger.event(`[mfsu] compiled with esbuild successfully in ${+new Date() - date} ms`);
package/dist/mfsu.d.ts CHANGED
@@ -14,8 +14,10 @@ interface IOpts {
14
14
  mode?: Mode;
15
15
  tmpBase?: string;
16
16
  unMatchLibs?: string[];
17
+ runtimePublicPath?: boolean | string;
17
18
  implementor: typeof webpack;
18
19
  buildDepWithESBuild?: boolean;
20
+ depBuildConfig: any;
19
21
  }
20
22
  export declare class MFSU {
21
23
  opts: IOpts;
@@ -25,10 +27,11 @@ export declare class MFSU {
25
27
  depBuilder: DepBuilder;
26
28
  depConfig: Configuration | null;
27
29
  constructor(opts: IOpts);
30
+ asyncImport(content: string): string;
28
31
  setWebpackConfig(opts: {
29
32
  config: Configuration;
30
33
  depConfig: Configuration;
31
- }): void;
34
+ }): Promise<void>;
32
35
  buildDeps(): Promise<void>;
33
36
  getMiddlewares(): ((req: Request, res: Response, next: NextFunction) => void)[];
34
37
  getBabelPlugins(): (typeof autoExport | (typeof awaitImport | {
package/dist/mfsu.js CHANGED
@@ -13,6 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.MFSU = void 0;
16
+ const bundler_utils_1 = require("@umijs/bundler-utils");
16
17
  const utils_1 = require("@umijs/utils");
17
18
  const fs_1 = require("fs");
18
19
  const path_1 = require("path");
@@ -46,56 +47,118 @@ class MFSU {
46
47
  this.depBuilder = new depBuilder_1.DepBuilder({ mfsu: this });
47
48
  this.depInfo.loadCache();
48
49
  }
50
+ // swc don't support top-level await
51
+ // ref: https://github.com/vercel/next.js/issues/31054
52
+ asyncImport(content) {
53
+ return `await import('${content}');`;
54
+ // return `(async () => await import('${content}'))();`;
55
+ }
49
56
  setWebpackConfig(opts) {
50
57
  var _a;
51
- const { mfName } = this.opts;
52
- /**
53
- * config
54
- */
55
- // set alias and externals with reference for babel plugin
56
- Object.assign(this.alias, ((_a = opts.config.resolve) === null || _a === void 0 ? void 0 : _a.alias) || {});
57
- this.externals.push(...(0, makeArray_1.makeArray)(opts.config.externals || []));
58
- // entry
59
- const entry = {};
60
- const virtualModules = {};
61
- Object.keys(opts.config.entry).forEach((key) => {
62
- const virtualPath = `./mfsu-virtual-entry/${key}.js`;
63
- virtualModules[virtualPath] =
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ const { mfName } = this.opts;
60
+ /**
61
+ * config
62
+ */
63
+ // set alias and externals with reference for babel plugin
64
+ Object.assign(this.alias, ((_a = opts.config.resolve) === null || _a === void 0 ? void 0 : _a.alias) || {});
65
+ this.externals.push(...(0, makeArray_1.makeArray)(opts.config.externals || []));
66
+ // entry
67
+ const entry = {};
68
+ const virtualModules = {};
69
+ for (const key of Object.keys(opts.config.entry)) {
70
+ const virtualPath = `./mfsu-virtual-entry/${key}.js`;
71
+ const virtualContent = [];
72
+ let index = 1;
73
+ let hasDefaultExport = false;
64
74
  // @ts-ignore
65
- opts.config
66
- .entry[key].map((entry) => `await import('${entry}')`)
67
- .join('\n') + `\nexport default 1;`;
68
- entry[key] = virtualPath;
75
+ for (const entry of opts.config.entry[key]) {
76
+ const content = (0, fs_1.readFileSync)(entry, 'utf-8');
77
+ const [_imports, exports] = yield (0, bundler_utils_1.parseModule)({ content, path: entry });
78
+ if (exports.length) {
79
+ virtualContent.push(`const k${index} = ${this.asyncImport(entry)}`);
80
+ for (const exportName of exports) {
81
+ if (exportName === 'default') {
82
+ hasDefaultExport = true;
83
+ virtualContent.push(`export default k${index}.${exportName}`);
84
+ }
85
+ else {
86
+ virtualContent.push(`export const ${exportName} = k${index}.${exportName}`);
87
+ }
88
+ }
89
+ }
90
+ else {
91
+ virtualContent.push(this.asyncImport(entry));
92
+ }
93
+ index += 1;
94
+ }
95
+ if (!hasDefaultExport) {
96
+ virtualContent.push(`export default 1;`);
97
+ }
98
+ virtualModules[virtualPath] = virtualContent.join('\n');
99
+ entry[key] = virtualPath;
100
+ }
101
+ opts.config.entry = entry;
102
+ // plugins
103
+ opts.config.plugins = opts.config.plugins || [];
104
+ // support publicPath auto
105
+ let publicPath = opts.config.output.publicPath;
106
+ if (publicPath === 'auto') {
107
+ publicPath = '/';
108
+ }
109
+ opts.config.plugins.push(...[
110
+ new webpack_virtual_modules_1.default(virtualModules),
111
+ new this.opts.implementor.container.ModuleFederationPlugin({
112
+ name: '__',
113
+ remotes: {
114
+ [mfName]: this.opts.runtimePublicPath
115
+ ? // ref:
116
+ // https://webpack.js.org/concepts/module-federation/#promise-based-dynamic-remotes
117
+ `
118
+ promise new Promise(resolve => {
119
+ const remoteUrlWithVersion = window.publicPath + '${constants_1.REMOTE_FILE_FULL}';
120
+ const script = document.createElement('script');
121
+ script.src = remoteUrlWithVersion;
122
+ script.onload = () => {
123
+ // the injected script has loaded and is available on window
124
+ // we can now resolve this Promise
125
+ const proxy = {
126
+ get: (request) => window['${mfName}'].get(request),
127
+ init: (arg) => {
128
+ try {
129
+ return window['${mfName}'].init(arg);
130
+ } catch(e) {
131
+ console.log('remote container already initialized');
132
+ }
133
+ }
134
+ }
135
+ resolve(proxy);
136
+ }
137
+ // inject this script with the src set to the versioned remoteEntry.js
138
+ document.head.appendChild(script);
139
+ })
140
+ `.trimLeft()
141
+ : `${mfName}@${publicPath}${constants_1.REMOTE_FILE_FULL}`,
142
+ },
143
+ }),
144
+ new buildDepPlugin_1.BuildDepPlugin({
145
+ onCompileDone: () => {
146
+ this.buildDeps().catch((e) => {
147
+ utils_1.logger.error(e);
148
+ });
149
+ },
150
+ }),
151
+ new writeCachePlugin_1.WriteCachePlugin({
152
+ onWriteCache: () => {
153
+ this.depInfo.writeCache();
154
+ },
155
+ }),
156
+ ]);
157
+ /**
158
+ * depConfig
159
+ */
160
+ this.depConfig = opts.depConfig;
69
161
  });
70
- opts.config.entry = entry;
71
- // plugins
72
- opts.config.plugins = opts.config.plugins || [];
73
- opts.config.plugins.push(...[
74
- new webpack_virtual_modules_1.default(virtualModules),
75
- new this.opts.implementor.container.ModuleFederationPlugin({
76
- name: '__',
77
- remotes: {
78
- // TODO: support runtime public path
79
- [mfName]: `${mfName}@${opts.config.output.publicPath}${constants_1.REMOTE_FILE_FULL}`,
80
- },
81
- }),
82
- new buildDepPlugin_1.BuildDepPlugin({
83
- onCompileDone: () => {
84
- this.buildDeps().catch((e) => {
85
- utils_1.logger.error(e);
86
- });
87
- },
88
- }),
89
- new writeCachePlugin_1.WriteCachePlugin({
90
- onWriteCache: () => {
91
- this.depInfo.writeCache();
92
- },
93
- }),
94
- ]);
95
- /**
96
- * depConfig
97
- */
98
- this.depConfig = opts.depConfig;
99
162
  }
100
163
  buildDeps() {
101
164
  return __awaiter(this, void 0, void 0, function* () {
@@ -126,7 +189,7 @@ class MFSU {
126
189
  }
127
190
  res.setHeader('content-type', (0, mrmime_1.lookup)((0, path_1.extname)(req.path)) || 'text/plain');
128
191
  const relativePath = req.path.replace(new RegExp(`^${publicPath}`), '/');
129
- const content = (0, fs_1.readFileSync)((0, path_1.join)(this.opts.tmpBase, relativePath), 'utf-8');
192
+ const content = (0, fs_1.readFileSync)((0, path_1.join)(this.opts.tmpBase, relativePath));
130
193
  res.send(content);
131
194
  });
132
195
  }
@@ -20,7 +20,12 @@ class ModuleGraph {
20
20
  this.rootModules = new Set();
21
21
  }
22
22
  restore(data) {
23
+ let fileMap = new Map();
23
24
  const addNode = ({ file, importer }) => {
25
+ // fix circular dependency
26
+ if (fileMap.has(file))
27
+ return;
28
+ fileMap.set(file, true);
24
29
  const mod = new ModuleNode(file);
25
30
  let isDependency = false;
26
31
  let info;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/mfsu",
3
- "version": "4.0.0-beta.12",
3
+ "version": "4.0.0-beta.16",
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.12",
25
- "@umijs/bundler-utils": "4.0.0-beta.12",
26
- "@umijs/utils": "4.0.0-beta.12"
24
+ "@umijs/bundler-esbuild": "4.0.0-beta.16",
25
+ "@umijs/bundler-utils": "4.0.0-beta.16",
26
+ "@umijs/utils": "4.0.0-beta.16"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/express": "4.17.13",