summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Bjorn Lu <bjornlu.dev@gmail.com> 2022-09-04 16:43:47 +0800
committerGravatar GitHub <noreply@github.com> 2022-09-04 16:43:47 +0800
commit29a5fdc1535fc389035d8107025f7490bfa976ed (patch)
tree039db7ceba88e42b4aa36757516f1c12919d0b2c
parent893a02a1bbf19a8a17d5d26e98dee9c9b761e29a (diff)
downloadastro-29a5fdc1535fc389035d8107025f7490bfa976ed.tar.gz
astro-29a5fdc1535fc389035d8107025f7490bfa976ed.tar.zst
astro-29a5fdc1535fc389035d8107025f7490bfa976ed.zip
Correctly escape paths in file names (#4584)
-rw-r--r--.changeset/slimy-fireants-carry.md5
-rw-r--r--packages/astro/src/core/build/vite-plugin-analyzer.ts5
-rw-r--r--packages/astro/src/jsx/babel.ts11
-rw-r--r--packages/astro/src/runtime/server/hydration.ts4
-rw-r--r--packages/astro/test/fixtures/special-chars-in-component-imports/src/pages/index.astro8
-rw-r--r--packages/astro/test/fixtures/special-chars-in-component-imports/src/pages/mdx.mdx5
-rw-r--r--packages/astro/test/special-chars-in-component-imports.test.js12
7 files changed, 31 insertions, 19 deletions
diff --git a/.changeset/slimy-fireants-carry.md b/.changeset/slimy-fireants-carry.md
new file mode 100644
index 000000000..7f972759a
--- /dev/null
+++ b/.changeset/slimy-fireants-carry.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Correctly escape paths in file names
diff --git a/packages/astro/src/core/build/vite-plugin-analyzer.ts b/packages/astro/src/core/build/vite-plugin-analyzer.ts
index e4f64b181..e25ee42aa 100644
--- a/packages/astro/src/core/build/vite-plugin-analyzer.ts
+++ b/packages/astro/src/core/build/vite-plugin-analyzer.ts
@@ -4,7 +4,6 @@ import type { BuildInternals } from '../../core/build/internal.js';
import type { PluginMetadata as AstroPluginMetadata } from '../../vite-plugin-astro/types';
import { prependForwardSlash } from '../../core/path.js';
-import { resolveClientDevPath } from '../../core/render/dev/resolve.js';
import { getTopLevelPages } from './graph.js';
import { getPageDataByViteID, trackClientOnlyPageDatas } from './internal.js';
@@ -82,7 +81,7 @@ export function vitePluginAnalyzer(internals: BuildInternals): VitePlugin {
const astro = info.meta.astro as AstroPluginMetadata['astro'];
for (const c of astro.hydratedComponents) {
- const rid = c.resolvedPath ? resolveClientDevPath(c.resolvedPath) : c.specifier;
+ const rid = c.resolvedPath ? decodeURI(c.resolvedPath) : c.specifier;
internals.discoveredHydratedComponents.add(rid);
}
@@ -93,7 +92,7 @@ export function vitePluginAnalyzer(internals: BuildInternals): VitePlugin {
const clientOnlys: string[] = [];
for (const c of astro.clientOnlyComponents) {
- const cid = c.resolvedPath ? resolveClientDevPath(c.resolvedPath) : c.specifier;
+ const cid = c.resolvedPath ? decodeURI(c.resolvedPath) : c.specifier;
internals.discoveredClientOnlyComponents.add(cid);
clientOnlys.push(cid);
}
diff --git a/packages/astro/src/jsx/babel.ts b/packages/astro/src/jsx/babel.ts
index 338464b26..1a5a7664e 100644
--- a/packages/astro/src/jsx/babel.ts
+++ b/packages/astro/src/jsx/babel.ts
@@ -1,6 +1,7 @@
import type { PluginObj } from '@babel/core';
import * as t from '@babel/types';
import { pathToFileURL } from 'node:url';
+import { resolveClientDevPath } from '../core/render/dev/resolve.js';
import { HydrationDirectiveProps } from '../runtime/server/hydration.js';
import type { PluginMetadata } from '../vite-plugin-astro/types';
@@ -217,10 +218,7 @@ export default function astroJSX(): PluginObj {
let resolvedPath: string;
if (meta.path.startsWith('.')) {
const fileURL = pathToFileURL(state.filename!);
- resolvedPath = `/@fs${new URL(meta.path, fileURL).pathname}`;
- if (resolvedPath.endsWith('.jsx')) {
- resolvedPath = resolvedPath.slice(0, -4);
- }
+ resolvedPath = resolveClientDevPath(`/@fs${new URL(meta.path, fileURL).pathname}`);
} else {
resolvedPath = meta.path;
}
@@ -300,10 +298,7 @@ export default function astroJSX(): PluginObj {
let resolvedPath: string;
if (meta.path.startsWith('.')) {
const fileURL = pathToFileURL(state.filename!);
- resolvedPath = `/@fs${new URL(meta.path, fileURL).pathname}`;
- if (resolvedPath.endsWith('.jsx')) {
- resolvedPath = resolvedPath.slice(0, -4);
- }
+ resolvedPath = resolveClientDevPath(`/@fs${new URL(meta.path, fileURL).pathname}`);
} else {
resolvedPath = meta.path;
}
diff --git a/packages/astro/src/runtime/server/hydration.ts b/packages/astro/src/runtime/server/hydration.ts
index d58dc406e..fea251b79 100644
--- a/packages/astro/src/runtime/server/hydration.ts
+++ b/packages/astro/src/runtime/server/hydration.ts
@@ -140,12 +140,12 @@ export async function generateHydrateScript(
}
// Add component url
- island.props['component-url'] = await result.resolve(componentUrl);
+ island.props['component-url'] = await result.resolve(decodeURI(componentUrl));
// Add renderer url
if (renderer.clientEntrypoint) {
island.props['component-export'] = componentExport.value;
- island.props['renderer-url'] = await result.resolve(renderer.clientEntrypoint);
+ island.props['renderer-url'] = await result.resolve(decodeURI(renderer.clientEntrypoint));
island.props['props'] = escapeHTML(serializeProps(props));
}
diff --git a/packages/astro/test/fixtures/special-chars-in-component-imports/src/pages/index.astro b/packages/astro/test/fixtures/special-chars-in-component-imports/src/pages/index.astro
index 5e7ff90cf..9f44b97c5 100644
--- a/packages/astro/test/fixtures/special-chars-in-component-imports/src/pages/index.astro
+++ b/packages/astro/test/fixtures/special-chars-in-component-imports/src/pages/index.astro
@@ -1,17 +1,21 @@
---
import CaretCounter from '../components/^--with-carets/Counter';
import RocketCounter from '../components/and-rockets-🚀/Counter';
-import PercentCounter from '../components/now-100%-better/Counter';
+// Not supported in Vite
+// import PercentCounter from '../components/now-100%-better/Counter';
import SpaceCounter from '../components/with some spaces/Counter';
import RoundBracketCounter from '../components/with-(round-brackets)/Counter';
import SquareBracketCounter from '../components/with-[square-brackets]/Counter';
---
<html>
+<head>
+ <meta charset="utf-8" />
+</head>
<body>
<h1>Special chars in component import paths from an .astro file</h1>
<CaretCounter id="caret" client:visible />
<RocketCounter id="rocket" client:visible />
- <PercentCounter id="percent" client:visible />
+ <!-- <PercentCounter id="percent" client:visible /> -->
<SpaceCounter id="space" client:visible />
<RoundBracketCounter id="round-bracket" client:visible />
<SquareBracketCounter id="square-bracket" client:visible />
diff --git a/packages/astro/test/fixtures/special-chars-in-component-imports/src/pages/mdx.mdx b/packages/astro/test/fixtures/special-chars-in-component-imports/src/pages/mdx.mdx
index f9ccc9e2b..79c1ba180 100644
--- a/packages/astro/test/fixtures/special-chars-in-component-imports/src/pages/mdx.mdx
+++ b/packages/astro/test/fixtures/special-chars-in-component-imports/src/pages/mdx.mdx
@@ -1,6 +1,7 @@
import CaretCounter from '../components/^--with-carets/Counter'
import RocketCounter from '../components/and-rockets-🚀/Counter'
-import PercentCounter from '../components/now-100%-better/Counter'
+// Not supported in Vite
+// import PercentCounter from '../components/now-100%-better/Counter'
import SpaceCounter from '../components/with some spaces/Counter'
import RoundBracketCounter from '../components/with-(round-brackets)/Counter'
import SquareBracketCounter from '../components/with-[square-brackets]/Counter'
@@ -9,7 +10,7 @@ import SquareBracketCounter from '../components/with-[square-brackets]/Counter'
<CaretCounter id="caret" client:visible />
<RocketCounter id="rocket" client:visible />
-<PercentCounter id="percent" client:visible />
+{/* <PercentCounter id="percent" client:visible /> */}
<SpaceCounter id="space" client:visible />
<RoundBracketCounter id="round-bracket" client:visible />
<SquareBracketCounter id="square-bracket" client:visible />
diff --git a/packages/astro/test/special-chars-in-component-imports.test.js b/packages/astro/test/special-chars-in-component-imports.test.js
index 8cd9b2563..21081a156 100644
--- a/packages/astro/test/special-chars-in-component-imports.test.js
+++ b/packages/astro/test/special-chars-in-component-imports.test.js
@@ -2,11 +2,19 @@ import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import { isWindows, loadFixture } from './test-utils.js';
-describe.skip('Special chars in component import paths', () => {
+describe('Special chars in component import paths', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
- const componentIds = ['caret', 'rocket', 'percent', 'space', 'round-bracket', 'square-bracket'];
+ const componentIds = [
+ 'caret',
+ 'rocket',
+ // Not supported as import identifier in Vite
+ // 'percent',
+ 'space',
+ 'round-bracket',
+ 'square-bracket',
+ ];
before(async () => {
fixture = await loadFixture({