diff --git a/src/dev-externals-mixin.ts b/src/dev-externals-mixin.ts index a55b7f4..b6ab026 100644 --- a/src/dev-externals-mixin.ts +++ b/src/dev-externals-mixin.ts @@ -16,10 +16,16 @@ export const devExternalsMixin = { }, configResolved(resolvedConfig) { const VALID_ID_PREFIX = `/@id/`; - const reg = new RegExp(`${VALID_ID_PREFIX}(${federationBuilder.externals.join('|')})`, 'g'); + const reg = new RegExp( + `(?["\'])[^\'"]*?${VALID_ID_PREFIX}(${federationBuilder.externals.join( + '|' + )})\\k`, + 'g' + ); resolvedConfig.plugins.push({ name: 'vite-plugin-ignore-static-import-replace-idprefix', - transform: (code) => (reg.test(code) ? code.replace(reg, (m, s1) => s1) : code), + transform: (code) => + reg.test(code) ? code.replace(reg, (_m, quote, libName) => quote + libName + quote) : code, }); }, resolveId: (id) => { diff --git a/src/index.ts b/src/index.ts index 4236509..83592e5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,17 +38,17 @@ const configureDevServer = async (server: ViteDevServer, params: BuildHelperPara const op = params.options; const dist = path.join(op.workspaceRoot, op.outputPath); - server.middlewares.use(serveFromDist(dist)); + server.middlewares.use(serveFromDist(dist, server.config.base)); }; -const serveFromDist = (dist: string): Connect.NextHandleFunction => { +const serveFromDist = (dist: string, baseUrl: string): Connect.NextHandleFunction => { return (req, res, next) => { if (!req.url || req.url.endsWith('/index.html')) { next(); return; } - const file = path.join(dist, req.url); + const file = path.join(dist, req.url.replace(baseUrl, '')); if (fs.existsSync(file) && fs.lstatSync(file).isFile()) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Content-Type', mime.lookup(req.url));