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

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;
@@ -38,7 +41,7 @@ function checkMatch({ value, path, opts, isExportAll, depth, cache, }) {
38
41
  isMatch = false;
39
42
  }
40
43
  else if ((0, path_1.isAbsolute)(value)) {
41
- isMatch = RE_NODE_MODULES.test(value) || RE_UMI_LOCAL_DEV.test(value);
44
+ isMatch = RE_NODE_MODULES.test(value) || isUmiLocalDev(value);
42
45
  }
43
46
  else {
44
47
  const aliasedPath = (0, getAliasedPath_1.getAliasedPath)({
@@ -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;
@@ -28,7 +30,7 @@ export declare class MFSU {
28
30
  setWebpackConfig(opts: {
29
31
  config: Configuration;
30
32
  depConfig: Configuration;
31
- }): void;
33
+ }): Promise<void>;
32
34
  buildDeps(): Promise<void>;
33
35
  getMiddlewares(): ((req: Request, res: Response, next: NextFunction) => void)[];
34
36
  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");
@@ -48,59 +49,99 @@ class MFSU {
48
49
  }
49
50
  setWebpackConfig(opts) {
50
51
  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] =
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ const { mfName } = this.opts;
54
+ /**
55
+ * config
56
+ */
57
+ // set alias and externals with reference for babel plugin
58
+ Object.assign(this.alias, ((_a = opts.config.resolve) === null || _a === void 0 ? void 0 : _a.alias) || {});
59
+ this.externals.push(...(0, makeArray_1.makeArray)(opts.config.externals || []));
60
+ // entry
61
+ const entry = {};
62
+ const virtualModules = {};
63
+ for (const key of Object.keys(opts.config.entry)) {
64
+ const virtualPath = `./mfsu-virtual-entry/${key}.js`;
65
+ const virtualContent = [];
66
+ let index = 1;
64
67
  // @ts-ignore
65
- opts.config
66
- .entry[key].map((entry) => `await import('${entry}')`)
67
- .join('\n') + `\nexport default 1;`;
68
- entry[key] = virtualPath;
69
- });
70
- opts.config.entry = entry;
71
- // plugins
72
- opts.config.plugins = opts.config.plugins || [];
73
- // support publicPath auto
74
- let publicPath = opts.config.output.publicPath;
75
- if (publicPath === 'auto') {
76
- publicPath = '/';
68
+ for (const entry of opts.config.entry[key]) {
69
+ const content = (0, fs_1.readFileSync)(entry, 'utf-8');
70
+ const [_imports, exports] = yield (0, bundler_utils_1.parseModule)({ content, path: entry });
71
+ if (exports.length) {
72
+ virtualContent.push(`const k${index} = await import('${entry}');`);
73
+ for (const exportName of exports) {
74
+ virtualContent.push(`export const ${exportName} = k${index}.${exportName}`);
75
+ }
76
+ }
77
+ else {
78
+ virtualContent.push(`await import('${entry}')`);
79
+ }
80
+ index += 1;
81
+ }
82
+ virtualContent.push(`export default 1;`);
83
+ virtualModules[virtualPath] = virtualContent.join('\n');
84
+ entry[key] = virtualPath;
85
+ }
86
+ opts.config.entry = entry;
87
+ // plugins
88
+ opts.config.plugins = opts.config.plugins || [];
89
+ // support publicPath auto
90
+ let publicPath = opts.config.output.publicPath;
91
+ if (publicPath === 'auto') {
92
+ publicPath = '/';
93
+ }
94
+ opts.config.plugins.push(...[
95
+ new webpack_virtual_modules_1.default(virtualModules),
96
+ new this.opts.implementor.container.ModuleFederationPlugin({
97
+ name: '__',
98
+ remotes: {
99
+ [mfName]: this.opts.runtimePublicPath
100
+ ? `
101
+ promise new Promise(resolve => {
102
+ const remoteUrlWithVersion = window.publicPath + '${constants_1.REMOTE_FILE_FULL}';
103
+ const script = document.createElement('script');
104
+ script.src = remoteUrlWithVersion;
105
+ script.onload = () => {
106
+ // the injected script has loaded and is available on window
107
+ // we can now resolve this Promise
108
+ const proxy = {
109
+ get: (request) => window['${mfName}'].get(request),
110
+ init: (arg) => {
111
+ try {
112
+ return window['${mfName}'].init(arg);
113
+ } catch(e) {
114
+ console.log('remote container already initialized');
77
115
  }
78
- opts.config.plugins.push(...[
79
- new webpack_virtual_modules_1.default(virtualModules),
80
- new this.opts.implementor.container.ModuleFederationPlugin({
81
- name: '__',
82
- remotes: {
83
- // TODO: support runtime public path
84
- [mfName]: `${mfName}@${publicPath}${constants_1.REMOTE_FILE_FULL}`,
85
- },
86
- }),
87
- new buildDepPlugin_1.BuildDepPlugin({
88
- onCompileDone: () => {
89
- this.buildDeps().catch((e) => {
90
- utils_1.logger.error(e);
91
- });
92
- },
93
- }),
94
- new writeCachePlugin_1.WriteCachePlugin({
95
- onWriteCache: () => {
96
- this.depInfo.writeCache();
97
- },
98
- }),
99
- ]);
100
- /**
101
- * depConfig
102
- */
103
- this.depConfig = opts.depConfig;
116
+ }
117
+ }
118
+ resolve(proxy);
119
+ }
120
+ // inject this script with the src set to the versioned remoteEntry.js
121
+ document.head.appendChild(script);
122
+ })
123
+ `.trimLeft()
124
+ : `${mfName}@${publicPath}${constants_1.REMOTE_FILE_FULL}`,
125
+ },
126
+ }),
127
+ new buildDepPlugin_1.BuildDepPlugin({
128
+ onCompileDone: () => {
129
+ this.buildDeps().catch((e) => {
130
+ utils_1.logger.error(e);
131
+ });
132
+ },
133
+ }),
134
+ new writeCachePlugin_1.WriteCachePlugin({
135
+ onWriteCache: () => {
136
+ this.depInfo.writeCache();
137
+ },
138
+ }),
139
+ ]);
140
+ /**
141
+ * depConfig
142
+ */
143
+ this.depConfig = opts.depConfig;
144
+ });
104
145
  }
105
146
  buildDeps() {
106
147
  return __awaiter(this, void 0, void 0, function* () {
@@ -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.13",
3
+ "version": "4.0.0-beta.14",
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.13",
25
- "@umijs/bundler-utils": "4.0.0-beta.13",
26
- "@umijs/utils": "4.0.0-beta.13"
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"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/express": "4.17.13",