@umijs/mfsu 4.0.0-beta.10 → 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)({
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`);
@@ -148,7 +144,7 @@ class DepBuilder {
148
144
  webpack: this.opts.mfsu.opts.implementor,
149
145
  }));
150
146
  const exposes = opts.deps.reduce((memo, dep) => {
151
- memo[`./${dep.shortFile}`] = (0, path_1.join)(this.opts.mfsu.opts.tmpBase, dep.filePath);
147
+ memo[`./${dep.file}`] = (0, path_1.join)(this.opts.mfsu.opts.tmpBase, dep.filePath);
152
148
  return memo;
153
149
  }, {});
154
150
  depConfig.plugins.push(new this.opts.mfsu.opts.implementor.container.ModuleFederationPlugin({
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,54 +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;
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');
115
+ }
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;
69
144
  });
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
145
  }
100
146
  buildDeps() {
101
147
  return __awaiter(this, void 0, void 0, function* () {
@@ -126,7 +172,7 @@ class MFSU {
126
172
  }
127
173
  res.setHeader('content-type', (0, mrmime_1.lookup)((0, path_1.extname)(req.path)) || 'text/plain');
128
174
  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');
175
+ const content = (0, fs_1.readFileSync)((0, path_1.join)(this.opts.tmpBase, relativePath));
130
176
  res.send(content);
131
177
  });
132
178
  }
@@ -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.10",
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,15 +21,15 @@
21
21
  "dev": "pnpm build -- --watch"
22
22
  },
23
23
  "dependencies": {
24
- "@umijs/bundler-esbuild": "4.0.0-beta.10",
25
- "@umijs/bundler-utils": "4.0.0-beta.10",
26
- "@umijs/utils": "4.0.0-beta.10"
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",
30
30
  "enhanced-resolve": "5.8.3",
31
31
  "mrmime": "1.0.0",
32
- "webpack": "5.61.0",
32
+ "webpack": "5.64.4",
33
33
  "webpack-virtual-modules": "0.4.3"
34
34
  },
35
35
  "publishConfig": {