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
This commit is contained in:
Matthew Phillips 2023-01-04 15:36:44 -05:00 committed by GitHub
parent 1965ae4628
commit 116d8835ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix for hoisted scripts in project with spaces in the file path

View file

@ -5,6 +5,7 @@ import {
appendForwardSlash, appendForwardSlash,
removeLeadingForwardSlashWindows, removeLeadingForwardSlashWindows,
} from '../core/path.js'; } from '../core/path.js';
import { viteID } from '../core/util.js';
/** /**
* Converts the first dot in `import.meta.env` to its Unicode escape sequence, * Converts the first dot in `import.meta.env` to its Unicode escape sequence,
@ -47,7 +48,8 @@ export function normalizeFilename(filename: string, config: AstroConfig) {
if (filename.startsWith('/@fs')) { if (filename.startsWith('/@fs')) {
filename = filename.slice('/@fs'.length); filename = filename.slice('/@fs'.length);
} else if (filename.startsWith('/') && !ancestor(filename, config.root.pathname)) { } else if (filename.startsWith('/') && !ancestor(filename, config.root.pathname)) {
filename = new URL('.' + filename, config.root).pathname; const url = new URL('.' + filename, config.root);
filename = viteID(url);
} }
return removeLeadingForwardSlashWindows(filename); return removeLeadingForwardSlashWindows(filename);
} }

View file

@ -0,0 +1,8 @@
{
"name": "@test/space-in-folder-name",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,11 @@
<html>
<head>
<title>Project with spaces in the folder name</title>
</head>
<body>
<h1>Testing</h1>
<script>
console.log('hi');
</script>
</body>
</html>

View file

@ -0,0 +1,33 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Projects with a space in the folder name', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/space in folder name/app/',
});
});
describe('dev', () => {
/** @type {import('./test-utils').Fixture} */
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('Work with hoisted scripts', async () => {
const html = await fixture.fetch('/').then(r => r.text());
const $ = cheerio.load(html);
expect($('script[src*="space in folder name"]')).to.have.a.lengthOf(1);
});
});
});

View file

@ -2275,6 +2275,12 @@ importers:
react: 18.2.0 react: 18.2.0
react-dom: 18.2.0_react@18.2.0 react-dom: 18.2.0_react@18.2.0
packages/astro/test/fixtures/space in folder name/app:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../../..
packages/astro/test/fixtures/special-chars-in-component-imports: packages/astro/test/fixtures/special-chars-in-component-imports:
specifiers: specifiers:
'@astrojs/mdx': workspace:* '@astrojs/mdx': workspace:*