0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00

Allow file URLs to be used as import specifiers (#9407)

* Allow file URLs to be used as import specifiers

* Update packages/astro/src/vite-plugin-fileurl/index.ts

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
Matthew Phillips 2023-12-12 13:28:48 -05:00 committed by GitHub
parent 63b3cd19ee
commit 546d92c862
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Allows file URLs as import specifiers

View file

@ -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;
@ -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),

View file

@ -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);
}
}
}
}

View file

@ -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('/');

View file

@ -0,0 +1,10 @@
---
import {capitalize} from 'file://../strings.js';
---
<html>
<head><title>Testing</title></head>
<body>
<h1>{capitalize('works')</h1>
</body>
</html>

View file

@ -0,0 +1,4 @@
export function capitalize(str) {
return str.toUpperCase();
}