From adad7e96680640e1d0a5ec270cd2516f350c7652 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 4 Jan 2023 15:36:44 -0500 Subject: [PATCH] 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 --- .changeset/pretty-planes-promise.md | 5 +++ packages/astro/src/vite-plugin-utils/index.ts | 4 ++- .../space in folder name/app/package.json | 8 +++++ .../app/src/pages/index.astro | 11 +++++++ .../astro/test/space-in-folder-name.test.js | 33 +++++++++++++++++++ pnpm-lock.yaml | 6 ++++ 6 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 .changeset/pretty-planes-promise.md create mode 100644 packages/astro/test/fixtures/space in folder name/app/package.json create mode 100644 packages/astro/test/fixtures/space in folder name/app/src/pages/index.astro create mode 100644 packages/astro/test/space-in-folder-name.test.js diff --git a/.changeset/pretty-planes-promise.md b/.changeset/pretty-planes-promise.md new file mode 100644 index 000000000000..668be49b294d --- /dev/null +++ b/.changeset/pretty-planes-promise.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix for hoisted scripts in project with spaces in the file path diff --git a/packages/astro/src/vite-plugin-utils/index.ts b/packages/astro/src/vite-plugin-utils/index.ts index 51479380eecf..8491343a8946 100644 --- a/packages/astro/src/vite-plugin-utils/index.ts +++ b/packages/astro/src/vite-plugin-utils/index.ts @@ -6,6 +6,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, @@ -75,7 +76,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); } diff --git a/packages/astro/test/fixtures/space in folder name/app/package.json b/packages/astro/test/fixtures/space in folder name/app/package.json new file mode 100644 index 000000000000..daf3a1963d7a --- /dev/null +++ b/packages/astro/test/fixtures/space in folder name/app/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/space-in-folder-name", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/space in folder name/app/src/pages/index.astro b/packages/astro/test/fixtures/space in folder name/app/src/pages/index.astro new file mode 100644 index 000000000000..ada8acc288a6 --- /dev/null +++ b/packages/astro/test/fixtures/space in folder name/app/src/pages/index.astro @@ -0,0 +1,11 @@ + + +Project with spaces in the folder name + + +

Testing

+ + + diff --git a/packages/astro/test/space-in-folder-name.test.js b/packages/astro/test/space-in-folder-name.test.js new file mode 100644 index 000000000000..3920e195715e --- /dev/null +++ b/packages/astro/test/space-in-folder-name.test.js @@ -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); + }); + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 76741d9b1640..cfebc0e0e79a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2280,6 +2280,12 @@ importers: 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: specifiers: '@astrojs/mdx': workspace:*