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

Allow file URLs to be used as import specifiers #9407

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/smart-colts-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allows file URLs as import specifiers
lilnasy marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type { Logger } from './logger/core.js';
import { createViteLogger } from './logger/vite.js';
import { vitePluginMiddleware } from './middleware/vite-plugin.js';
import { joinPaths } from './path.js';
import vitePluginFileURL from '../vite-plugin-fileurl/index.js';

interface CreateViteOptions {
settings: AstroSettings;
Expand Down Expand Up @@ -141,6 +142,7 @@ export async function createVite(
astroPrefetch({ settings }),
astroTransitions({ settings }),
astroDevOverlay({ settings, logger }),
vitePluginFileURL({}),
!!settings.config.i18n && astroInternationalization({ settings }),
],
publicDir: fileURLToPath(settings.config.publicDir),
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/src/vite-plugin-fileurl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Plugin as VitePlugin } from 'vite';

export default function vitePluginFileURL({}): VitePlugin {
return {
name: 'astro:vite-plugin-file-url',
resolveId(source, importer) {
if(source.startsWith('file://')) {
const rest = source.slice(7);
return this.resolve(rest, importer);
}
}
}
}
7 changes: 7 additions & 0 deletions packages/astro/test/astro-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ describe('Astro basics', () => {
expect(content2).to.be.ok;
});


it('allows file:// urls as module specifiers', async () => {
const html = await fixture.readFile('/fileurl/index.html');
const $ = cheerio.load(html);
expect($('h1').text()).to.equal('WORKS');
});

describe('preview', () => {
it('returns 200 for valid URLs', async () => {
const result = await fixture.fetch('/');
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/fixtures/astro-basic/src/pages/fileurl.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
import {capitalize} from 'file://../strings.js';
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
---

<html>
<head><title>Testing</title></head>
<body>
<h1>{capitalize('works')</h1>
</body>
</html>
4 changes: 4 additions & 0 deletions packages/astro/test/fixtures/astro-basic/src/strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function capitalize(str) {
return str.toUpperCase();
}
Loading