Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading of styles in static build #2605

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-drinks-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes Astro style resolution in the static build
4 changes: 4 additions & 0 deletions packages/astro/src/core/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export function removeEndingForwardSlash(path: string) {
return path.endsWith('/') ? path.slice(0, path.length - 1) : path;
}

export function startsWithForwardSlash(path: string) {
return path[0] === '/';
}

export function startsWithDotDotSlash(path: string) {
const c1 = path[0];
const c2 = path[1];
Expand Down
15 changes: 10 additions & 5 deletions packages/astro/src/vite-plugin-astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { parseAstroRequest } from './query.js';
import { cachedCompilation } from './compile.js';
import ancestor from 'common-ancestor-path';
import { trackCSSDependencies, handleHotUpdate } from './hmr.js';
import { isRelativePath } from '../core/path.js';
import { isRelativePath, startsWithForwardSlash } from '../core/path.js';

const FRONTMATTER_PARSE_REGEXP = /^\-\-\-(.*)^\-\-\-/ms;
interface AstroPluginOptions {
Expand All @@ -29,6 +29,11 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
}
return filename;
}
function relativeToRoot(pathname: string) {
const arg = startsWithForwardSlash(pathname) ? '.' + pathname : pathname;
const url = new URL(arg, config.projectRoot);
return slash(fileURLToPath(url)) + url.search;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q. Is there a difference between slash(fileURLToPath(url)) and url.pathname? My understand is that URL would be the same as the slash’d version of the possibly-windows-style path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, url.pathname will contain %20 for a space whereas fileURLToPath removes the encoding and puts a space in. We ran into this bug before and its why the packages/astro/test/fixtures/static build project contains a space.

}

let viteTransform: TransformHook;
let viteDevServer: vite.ViteDevServer | null = null;
Expand All @@ -52,13 +57,13 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
// we need to resolve relative paths ourselves.
if (from) {
const { query: fromQuery, filename } = parseAstroRequest(from);
if (fromQuery.astro && isRelativePath(id)) {
if (fromQuery.astro && isRelativePath(id) && fromQuery.type === 'script') {
const resolvedURL = new URL(id, `file://${filename}`);
const resolved = resolvedURL.pathname;
if (isBrowserPath(resolved)) {
return slash(fileURLToPath(new URL('.' + resolved, config.projectRoot)));
return relativeToRoot(resolved + resolvedURL.search);
}
return slash(fileURLToPath(resolvedURL));
return slash(fileURLToPath(resolvedURL)) + resolvedURL.search;
}
}

Expand All @@ -69,7 +74,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
// Because this needs to be the id for the Vite CSS plugin to property resolve
// relative @imports.
if (query.type === 'style' && isBrowserPath(id)) {
return slash(fileURLToPath(new URL('.' + id, config.projectRoot)));
return relativeToRoot(id);
}

return id;
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/test/static-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ describe('Static build', () => {
};
}

describe('Page CSS', () => {
const findEvidence = createFindEvidence(/height:( )*45vw/);

it('Page level CSS is added', async () => {
const found = await findEvidence('/subpath/index.html');
expect(found).to.equal(true, 'Did not find page-level CSS on this page');
});
});

describe('Shared CSS', () => {
const findEvidence = createFindEvidence(/var\(--c\)/);

Expand Down