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:
parent
1965ae4628
commit
116d8835ca
6 changed files with 66 additions and 1 deletions
5
.changeset/pretty-planes-promise.md
Normal file
5
.changeset/pretty-planes-promise.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix for hoisted scripts in project with spaces in the file path
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
8
packages/astro/test/fixtures/space in folder name/app/package.json
vendored
Normal file
8
packages/astro/test/fixtures/space in folder name/app/package.json
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"name": "@test/space-in-folder-name",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"astro": "workspace:*"
|
||||||
|
}
|
||||||
|
}
|
11
packages/astro/test/fixtures/space in folder name/app/src/pages/index.astro
vendored
Normal file
11
packages/astro/test/fixtures/space in folder name/app/src/pages/index.astro
vendored
Normal 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>
|
33
packages/astro/test/space-in-folder-name.test.js
Normal file
33
packages/astro/test/space-in-folder-name.test.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -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:*
|
||||||
|
|
Loading…
Reference in a new issue