Skip to content

Commit

Permalink
fix: manifest publicPath (#91)
Browse files Browse the repository at this point in the history
* fix: manifest publicPath

* fix: manifest publicPath

---------

Co-authored-by: 张洪恩 <zhanghongen@bwcj.com>
  • Loading branch information
zhangHongEn and 张洪恩 authored Sep 18, 2024
1 parent 28cb5a8 commit b246f36
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 103 deletions.
7 changes: 1 addition & 6 deletions examples/vite-vite/vite-remote/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ export default defineConfig({
preview: {
port: 5176,
},
// base: 'http://localhost:5176',
experimental: {
renderBuiltUrl() {
return { relative: true };
},
},
base: 'http://localhost:5176',
plugins: [
react({ jsxImportSource: '@emotion/react' }),
federation({
Expand Down
74 changes: 48 additions & 26 deletions lib/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,18 @@ var addEntry = function addEntry(_ref) {
}
},
buildStart: function buildStart() {
var _this$emitFile;
if (_command === "serve") return;
var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
this.emitFile((_this$emitFile = {
name: entryName
}, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
var emitFileOptions = {
name: entryName,
type: 'chunk',
id: entryPath,
preserveSignature: 'strict'
};
if (!hasHash) {
emitFileOptions.fileName = fileName;
}
this.emitFile(emitFileOptions);
if (htmlFilePath) {
var htmlContent = fs__namespace.readFileSync(htmlFilePath, 'utf-8');
var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
Expand Down Expand Up @@ -628,6 +634,8 @@ var Manifest = function Manifest() {
var extensions;
var root;
var remoteEntryFile;
var publicPath;
var _command;
return [{
name: 'moddule-federation-manifest',
apply: 'serve',
Expand Down Expand Up @@ -666,7 +674,7 @@ var Manifest = function Manifest() {
},
globalName: name,
pluginVersion: '0.2.5',
publicPath: 'auto'
publicPath: publicPath
},
shared: Array.from(getUsedShares()).map(function (shareKey) {
var shareItem = getNormalizeShareItem(shareKey);
Expand Down Expand Up @@ -730,13 +738,20 @@ var Manifest = function Manifest() {
}, {
name: 'moddule-federation-manifest',
enforce: 'post',
config: function config(_config) {
config: function config(_config, _ref) {
var command = _ref.command;
if (!_config.build) _config.build = {};
if (!_config.build.manifest) _config.build.manifest = _config.build.manifest || !!manifestOptions;
_command = command;
},
configResolved: function configResolved(config) {
root = config.root;
extensions = config.resolve.extensions || ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'];
publicPath = config.base ? config.base.replace(/\/?$/, "/") : "auto";
if (_command === "serve") {
var origin = config.server.origin;
publicPath = origin ? origin.replace(/\/?$/, "/") : "auto";
}
},
generateBundle: function generateBundle(options, bundle) {
try {
Expand Down Expand Up @@ -777,7 +792,7 @@ var Manifest = function Manifest() {
var _Object$entries$_i = _Object$entries[_i],
fileName = _Object$entries$_i[0],
fileData = _Object$entries$_i[1];
if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name) {
if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name || fileData.name === "remoteEntry") {
remoteEntryFile = fileData.fileName;
}
if (fileData.type === 'chunk') {
Expand Down Expand Up @@ -949,7 +964,7 @@ var Manifest = function Manifest() {
},
globalName: name,
pluginVersion: '0.2.5',
publicPath: 'auto'
publicPath: publicPath
},
shared: shared,
remotes: remotes,
Expand Down Expand Up @@ -1097,6 +1112,7 @@ var PromiseStore = /*#__PURE__*/function () {
function proxySharedModule(options) {
var _options$shared = options.shared,
shared = _options$shared === void 0 ? {} : _options$shared;
var _config;
return [{
name: "generateLocalSharedImportMap",
enforce: "post",
Expand All @@ -1117,16 +1133,20 @@ function proxySharedModule(options) {
}, {
name: 'proxyPreBuildShared',
enforce: 'post',
config: function config(_config, _ref) {
var _config$resolve$alias, _config$resolve$alias2;
configResolved: function configResolved(config) {
_config = config;
},
config: function config(_config2, _ref) {
var _config2$resolve$alia, _config2$resolve$alia2;
var command = _ref.command;
(_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(shared).map(function (key) {
(_config2$resolve$alia = _config2.resolve.alias).push.apply(_config2$resolve$alia, Object.keys(shared).map(function (key) {
var pattern = key.endsWith("/") ? "(^" + key.replace(/\/$/, "") + "(/.+)?$)" : "(^" + key + "$)";
return {
// Intercept all shared requests and proxy them to loadShare
find: new RegExp(pattern),
replacement: "$1",
customResolver: function customResolver(source, importer) {
if (/\.css$/.test(source)) return;
var loadSharePath = getLoadShareModulePath(source);
writeLoadShareModule(source, shared[key], command);
writePreBuildLibPath(source);
Expand All @@ -1137,7 +1157,7 @@ function proxySharedModule(options) {
};
}));
var savePrebuild = new PromiseStore();
(_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
(_config2$resolve$alia2 = _config2.resolve.alias).push.apply(_config2$resolve$alia2, Object.keys(shared).map(function (key) {
return command === "build" ? {
find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
replacement: function replacement($1) {
Expand All @@ -1151,16 +1171,18 @@ function proxySharedModule(options) {
try {
var _this = this;
var pkgName = VirtualModule.findModule(PREBUILD_TAG, source).name;
if (importer.includes(LOAD_SHARE_TAG)) {
// save pre-bunding module id
savePrebuild.set(pkgName, _this.resolve(pkgName).then(function (item) {
return item.id;
}));
}
// Fix localSharedImportMap import id
var _resolve = _this.resolve;
return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
return Promise.resolve(_this.resolve(pkgName).then(function (item) {
return item.id;
})).then(function (result) {
if (!result.includes(_config.cacheDir)) {
// save pre-bunding module id
savePrebuild.set(pkgName, Promise.resolve(result));
}
// Fix localSharedImportMap import id
var _resolve = _this.resolve;
return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
});
});
} catch (e) {
return Promise.reject(e);
Expand All @@ -1172,16 +1194,16 @@ function proxySharedModule(options) {
}, {
name: "watchLocalSharedImportMap",
apply: "serve",
config: function config(_config2) {
_config2.optimizeDeps = defu.defu(_config2.optimizeDeps, {
config: function config(_config3) {
_config3.optimizeDeps = defu.defu(_config3.optimizeDeps, {
exclude: [getLocalSharedImportMapPath()]
});
_config2.server = defu.defu(_config2.server, {
_config3.server = defu.defu(_config3.server, {
watch: {
ignored: []
}
});
var watch = _config2.server.watch;
var watch = _config3.server.watch;
watch.ignored = [].concat(watch.ignored);
watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
}
Expand Down
74 changes: 48 additions & 26 deletions lib/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@ var addEntry = function addEntry(_ref) {
}
},
buildStart: function buildStart() {
var _this$emitFile;
if (_command === "serve") return;
var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
this.emitFile((_this$emitFile = {
name: entryName
}, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
var emitFileOptions = {
name: entryName,
type: 'chunk',
id: entryPath,
preserveSignature: 'strict'
};
if (!hasHash) {
emitFileOptions.fileName = fileName;
}
this.emitFile(emitFileOptions);
if (htmlFilePath) {
var htmlContent = fs.readFileSync(htmlFilePath, 'utf-8');
var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
Expand Down Expand Up @@ -605,6 +611,8 @@ var Manifest = function Manifest() {
var extensions;
var root;
var remoteEntryFile;
var publicPath;
var _command;
return [{
name: 'moddule-federation-manifest',
apply: 'serve',
Expand Down Expand Up @@ -643,7 +651,7 @@ var Manifest = function Manifest() {
},
globalName: name,
pluginVersion: '0.2.5',
publicPath: 'auto'
publicPath: publicPath
},
shared: Array.from(getUsedShares()).map(function (shareKey) {
var shareItem = getNormalizeShareItem(shareKey);
Expand Down Expand Up @@ -707,13 +715,20 @@ var Manifest = function Manifest() {
}, {
name: 'moddule-federation-manifest',
enforce: 'post',
config: function config(_config) {
config: function config(_config, _ref) {
var command = _ref.command;
if (!_config.build) _config.build = {};
if (!_config.build.manifest) _config.build.manifest = _config.build.manifest || !!manifestOptions;
_command = command;
},
configResolved: function configResolved(config) {
root = config.root;
extensions = config.resolve.extensions || ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'];
publicPath = config.base ? config.base.replace(/\/?$/, "/") : "auto";
if (_command === "serve") {
var origin = config.server.origin;
publicPath = origin ? origin.replace(/\/?$/, "/") : "auto";
}
},
generateBundle: function generateBundle(options, bundle) {
try {
Expand Down Expand Up @@ -754,7 +769,7 @@ var Manifest = function Manifest() {
var _Object$entries$_i = _Object$entries[_i],
fileName = _Object$entries$_i[0],
fileData = _Object$entries$_i[1];
if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name) {
if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name || fileData.name === "remoteEntry") {
remoteEntryFile = fileData.fileName;
}
if (fileData.type === 'chunk') {
Expand Down Expand Up @@ -926,7 +941,7 @@ var Manifest = function Manifest() {
},
globalName: name,
pluginVersion: '0.2.5',
publicPath: 'auto'
publicPath: publicPath
},
shared: shared,
remotes: remotes,
Expand Down Expand Up @@ -1074,6 +1089,7 @@ var PromiseStore = /*#__PURE__*/function () {
function proxySharedModule(options) {
var _options$shared = options.shared,
shared = _options$shared === void 0 ? {} : _options$shared;
var _config;
return [{
name: "generateLocalSharedImportMap",
enforce: "post",
Expand All @@ -1094,16 +1110,20 @@ function proxySharedModule(options) {
}, {
name: 'proxyPreBuildShared',
enforce: 'post',
config: function config(_config, _ref) {
var _config$resolve$alias, _config$resolve$alias2;
configResolved: function configResolved(config) {
_config = config;
},
config: function config(_config2, _ref) {
var _config2$resolve$alia, _config2$resolve$alia2;
var command = _ref.command;
(_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(shared).map(function (key) {
(_config2$resolve$alia = _config2.resolve.alias).push.apply(_config2$resolve$alia, Object.keys(shared).map(function (key) {
var pattern = key.endsWith("/") ? "(^" + key.replace(/\/$/, "") + "(/.+)?$)" : "(^" + key + "$)";
return {
// Intercept all shared requests and proxy them to loadShare
find: new RegExp(pattern),
replacement: "$1",
customResolver: function customResolver(source, importer) {
if (/\.css$/.test(source)) return;
var loadSharePath = getLoadShareModulePath(source);
writeLoadShareModule(source, shared[key], command);
writePreBuildLibPath(source);
Expand All @@ -1114,7 +1134,7 @@ function proxySharedModule(options) {
};
}));
var savePrebuild = new PromiseStore();
(_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
(_config2$resolve$alia2 = _config2.resolve.alias).push.apply(_config2$resolve$alia2, Object.keys(shared).map(function (key) {
return command === "build" ? {
find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
replacement: function replacement($1) {
Expand All @@ -1128,16 +1148,18 @@ function proxySharedModule(options) {
try {
var _this = this;
var pkgName = VirtualModule.findModule(PREBUILD_TAG, source).name;
if (importer.includes(LOAD_SHARE_TAG)) {
// save pre-bunding module id
savePrebuild.set(pkgName, _this.resolve(pkgName).then(function (item) {
return item.id;
}));
}
// Fix localSharedImportMap import id
var _resolve = _this.resolve;
return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
return Promise.resolve(_this.resolve(pkgName).then(function (item) {
return item.id;
})).then(function (result) {
if (!result.includes(_config.cacheDir)) {
// save pre-bunding module id
savePrebuild.set(pkgName, Promise.resolve(result));
}
// Fix localSharedImportMap import id
var _resolve = _this.resolve;
return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
});
});
} catch (e) {
return Promise.reject(e);
Expand All @@ -1149,16 +1171,16 @@ function proxySharedModule(options) {
}, {
name: "watchLocalSharedImportMap",
apply: "serve",
config: function config(_config2) {
_config2.optimizeDeps = defu(_config2.optimizeDeps, {
config: function config(_config3) {
_config3.optimizeDeps = defu(_config3.optimizeDeps, {
exclude: [getLocalSharedImportMapPath()]
});
_config2.server = defu(_config2.server, {
_config3.server = defu(_config3.server, {
watch: {
ignored: []
}
});
var watch = _config2.server.watch;
var watch = _config3.server.watch;
watch.ignored = [].concat(watch.ignored);
watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
}
Expand Down
Loading

0 comments on commit b246f36

Please sign in to comment.