Skip to content

Commit

Permalink
Fix for hoisted scripts in project with spaces in the file path (#5756)
Browse files Browse the repository at this point in the history
* Fix for hoisted scripts in project with spaces in the file path

* Adding a changeset

* Use viteID instead
  • Loading branch information
matthewp committed Jan 4, 2023
1 parent 1965ae4 commit 116d883
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pretty-planes-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix for hoisted scripts in project with spaces in the file path
4 changes: 3 additions & 1 deletion packages/astro/src/vite-plugin-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
appendForwardSlash,
removeLeadingForwardSlashWindows,
} from '../core/path.js';
import { viteID } from '../core/util.js';

/**
* Converts the first dot in `import.meta.env` to its Unicode escape sequence,
Expand Down Expand Up @@ -47,7 +48,8 @@ export function normalizeFilename(filename: string, config: AstroConfig) {
if (filename.startsWith('/@fs')) {
filename = filename.slice('/@fs'.length);
} 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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/space-in-folder-name",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -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 changes: 33 additions & 0 deletions packages/astro/test/space-in-folder-name.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 116d883

Please sign in to comment.