Skip to content

Commit e39a86f

Browse files
fix(css-file-paths-formatting): ensure CSS paths include hostname for remote module styles (#667)
1 parent 8df65ef commit e39a86f

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

packages/lib/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@originjs/vite-plugin-federation",
3-
"version": "1.3.8",
3+
"version": "1.3.9",
44
"description": "A Vite plugin which support Module Federation.",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

packages/lib/src/prod/expose-production.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,26 @@ export function prodExposePlugin(
9191
const assetsDir = __VITE_ASSETS_DIR_PLACEHOLDER__;
9292
9393
cssFilePaths.forEach(cssPath => {
94-
let href = ''
95-
const baseUrl = base || curUrl
96-
if (baseUrl && baseUrl !== '/') {
97-
href = [baseUrl, assetsDir, cssPath].filter(Boolean).map(part => part.endsWith('/') ? part.substring(0, part.length - 1) : part).join('/')
94+
let href = '';
95+
const baseUrl = base || curUrl;
96+
if (baseUrl) {
97+
const trimmer = {
98+
trailing: (path) => (path.endsWith('/') ? path.slice(0, -1) : path),
99+
leading: (path) => (path.startsWith('/') ? path.slice(1) : path)
100+
}
101+
const isAbsoluteUrl = (url) => url.startsWith('http') || url.startsWith('//');
102+
103+
const cleanBaseUrl = trimmer.trailing(baseUrl);
104+
const cleanCssPath = trimmer.leading(cssPath);
105+
const cleanCurUrl = trimmer.trailing(curUrl);
106+
107+
if (isAbsoluteUrl(baseUrl)) {
108+
href = [cleanBaseUrl, cleanCssPath].filter(Boolean).join('/');
109+
} else {
110+
href = [cleanCurUrl + cleanBaseUrl, cleanCssPath].filter(Boolean).join('/');
111+
}
98112
} else {
99-
href = curUrl + cssPath
113+
href = cssPath;
100114
}
101115
102116
if (href in seen) return;
@@ -180,7 +194,7 @@ export function prodExposePlugin(
180194
.replace(
181195
'__VITE_ASSETS_DIR_PLACEHOLDER__',
182196
`'${viteConfigResolved.config?.build?.assetsDir || ''}'`
183-
);
197+
)
184198

185199
const filepathMap = new Map()
186200
const getFilename = (name) => parse(parse(name).name).name

0 commit comments

Comments
 (0)