Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Nov 5, 2024
1 parent fbc0bd3 commit 1cc0d82
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/vite/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export function assets(): Plugin {
configureServer(server) {
return () => {
server.middlewares.use((req, res, next) => {
if (req.originalUrl && req.originalUrl.length > 1) {
const assetUrl = findPublicAsset(req.originalUrl.split('?')[0], resolverLoader.resolver);
const originalUrl = req.originalUrl.slice(server.config.base.length - 1);
if (originalUrl && originalUrl.length > 1) {
const assetUrl = findPublicAsset(originalUrl.split('?')[0], resolverLoader.resolver);
if (assetUrl) {
return send(req as Readable, assetUrl).pipe(res);
}
Expand Down
16 changes: 14 additions & 2 deletions packages/vite/src/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ export function scripts(params?: { include?: string[]; exclude?: string[] }): Pl
}
});

let config: any = null;

return {
name: 'embroider-scripts',
enforce: 'pre',

configResolved(resolvedConfig) {
config = resolvedConfig;
optimizer = new ScriptOptimizer(resolvedConfig.root);
},

Expand All @@ -47,7 +50,7 @@ export function scripts(params?: { include?: string[]; exclude?: string[] }): Pl
// we don't do anything in `vite dev`, we only need to work in `vite
// build`
if (!context.server) {
return optimizer.transformHTML(htmlIn);
return optimizer.transformHTML(htmlIn, config.base);
}
},
};
Expand Down Expand Up @@ -123,16 +126,25 @@ class ScriptOptimizer {
return fileParts.join('.');
}

transformHTML(htmlIn: string) {
transformHTML(htmlIn: string, baseUrl: string) {
if (this.transformState?.htmlIn !== htmlIn) {
let parsed = new JSDOM(htmlIn);
let scriptTags = [...parsed.window.document.querySelectorAll('script')] as HTMLScriptElement[];
let linkTags = [...parsed.window.document.querySelectorAll('link')] as HTMLLinkElement[];
for (const linkTag of linkTags) {
if (linkTag.href.startsWith('/') && !linkTag.href.startsWith(baseUrl)) {
linkTag.href = baseUrl + linkTag.href.slice(1);
}
}
for (let scriptTag of scriptTags) {
if (scriptTag.type !== 'module') {
let fingerprinted = this.emitted.get(scriptTag.src);
if (fingerprinted) {
scriptTag.src = fingerprinted;
}
if (scriptTag.src.startsWith('/') && !scriptTag.src.startsWith(baseUrl)) {
scriptTag.src = baseUrl + scriptTag.src.slice(1);
}
}
}
let htmlOut = parsed.serialize();
Expand Down

0 comments on commit 1cc0d82

Please sign in to comment.