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 for hoisted scripts in project with spaces in the file path #5756

Merged
merged 3 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/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
5 changes: 4 additions & 1 deletion packages/astro/src/vite-plugin-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import type { AstroConfig } from '../@types/astro';
import {
appendExtension,
appendForwardSlash,
prependForwardSlash,
removeLeadingForwardSlashWindows,
} from '../core/path.js';
import { fileURLToPath } from 'url';

/**
* Converts the first dot in `import.meta.env` to its Unicode escape sequence,
Expand Down Expand Up @@ -47,7 +49,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 = prependForwardSlash(fileURLToPath(url));
Copy link
Member

Choose a reason for hiding this comment

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

Will this work on Windows? I assume pathname was being used here to ensure that we were getting the normalized /path/to/file but fileURLToPath(url) will give a platform-dependent file path. Maybe the result of that needs to be normalized before the forward slash is prepended?

Copy link
Member

Choose a reason for hiding this comment

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

If Windows tests pass, then I digress.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah there's something wrong here. We should be using fileURLToPath though, otherwise you get %20 for the spaces. Something else is wrong.

Copy link
Member

Choose a reason for hiding this comment

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

I think prependForwardSlash(normalizePath(fileURLToPath(url))) would be correct? Using normalizePath from Vite.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

viteID did it. the problem was that fileURLToPath uses back slashes. viteID fixes it.

}
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.