Skip to content

Commit 116d883

Browse files
authored
Fix for hoisted scripts in project with spaces in the file path (#5756)
* Fix for hoisted scripts in project with spaces in the file path * Adding a changeset * Use viteID instead
1 parent 1965ae4 commit 116d883

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fix for hoisted scripts in project with spaces in the file path

packages/astro/src/vite-plugin-utils/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
appendForwardSlash,
66
removeLeadingForwardSlashWindows,
77
} from '../core/path.js';
8+
import { viteID } from '../core/util.js';
89

910
/**
1011
* Converts the first dot in `import.meta.env` to its Unicode escape sequence,
@@ -47,7 +48,8 @@ export function normalizeFilename(filename: string, config: AstroConfig) {
4748
if (filename.startsWith('/@fs')) {
4849
filename = filename.slice('/@fs'.length);
4950
} else if (filename.startsWith('/') && !ancestor(filename, config.root.pathname)) {
50-
filename = new URL('.' + filename, config.root).pathname;
51+
const url = new URL('.' + filename, config.root);
52+
filename = viteID(url);
5153
}
5254
return removeLeadingForwardSlashWindows(filename);
5355
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@test/space-in-folder-name",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"astro": "workspace:*"
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
<head>
3+
<title>Project with spaces in the folder name</title>
4+
</head>
5+
<body>
6+
<h1>Testing</h1>
7+
<script>
8+
console.log('hi');
9+
</script>
10+
</body>
11+
</html>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect } from 'chai';
2+
import * as cheerio from 'cheerio';
3+
import { loadFixture } from './test-utils.js';
4+
5+
describe('Projects with a space in the folder name', () => {
6+
let fixture;
7+
8+
before(async () => {
9+
fixture = await loadFixture({
10+
root: './fixtures/space in folder name/app/',
11+
});
12+
});
13+
14+
describe('dev', () => {
15+
/** @type {import('./test-utils').Fixture} */
16+
let devServer;
17+
18+
before(async () => {
19+
devServer = await fixture.startDevServer();
20+
});
21+
22+
after(async () => {
23+
await devServer.stop();
24+
});
25+
26+
it('Work with hoisted scripts', async () => {
27+
const html = await fixture.fetch('/').then(r => r.text());
28+
const $ = cheerio.load(html);
29+
30+
expect($('script[src*="space in folder name"]')).to.have.a.lengthOf(1);
31+
});
32+
});
33+
});

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)