Skip to content

Commit

Permalink
Allow file URLs to be used as import specifiers (#9407)
Browse files Browse the repository at this point in the history
* Allow file URLs to be used as import specifiers

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

Co-authored-by: Nate Moore <[email protected]>

---------

Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
matthewp and natemoo-re authored Dec 12, 2023
1 parent 63b3cd1 commit 546d92c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
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
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';
---

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

0 comments on commit 546d92c

Please sign in to comment.