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:
parent
63b3cd19ee
commit
546d92c862
6 changed files with 41 additions and 0 deletions
5
.changeset/smart-colts-think.md
Normal file
5
.changeset/smart-colts-think.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Allows file URLs as import specifiers
|
|
@ -33,6 +33,7 @@ import type { Logger } from './logger/core.js';
|
||||||
import { createViteLogger } from './logger/vite.js';
|
import { createViteLogger } from './logger/vite.js';
|
||||||
import { vitePluginMiddleware } from './middleware/vite-plugin.js';
|
import { vitePluginMiddleware } from './middleware/vite-plugin.js';
|
||||||
import { joinPaths } from './path.js';
|
import { joinPaths } from './path.js';
|
||||||
|
import vitePluginFileURL from '../vite-plugin-fileurl/index.js';
|
||||||
|
|
||||||
interface CreateViteOptions {
|
interface CreateViteOptions {
|
||||||
settings: AstroSettings;
|
settings: AstroSettings;
|
||||||
|
@ -141,6 +142,7 @@ export async function createVite(
|
||||||
astroPrefetch({ settings }),
|
astroPrefetch({ settings }),
|
||||||
astroTransitions({ settings }),
|
astroTransitions({ settings }),
|
||||||
astroDevOverlay({ settings, logger }),
|
astroDevOverlay({ settings, logger }),
|
||||||
|
vitePluginFileURL({}),
|
||||||
!!settings.config.i18n && astroInternationalization({ settings }),
|
!!settings.config.i18n && astroInternationalization({ settings }),
|
||||||
],
|
],
|
||||||
publicDir: fileURLToPath(settings.config.publicDir),
|
publicDir: fileURLToPath(settings.config.publicDir),
|
||||||
|
|
13
packages/astro/src/vite-plugin-fileurl/index.ts
Normal file
13
packages/astro/src/vite-plugin-fileurl/index.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -158,6 +158,13 @@ describe('Astro basics', () => {
|
||||||
expect(content2).to.be.ok;
|
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', () => {
|
describe('preview', () => {
|
||||||
it('returns 200 for valid URLs', async () => {
|
it('returns 200 for valid URLs', async () => {
|
||||||
const result = await fixture.fetch('/');
|
const result = await fixture.fetch('/');
|
||||||
|
|
10
packages/astro/test/fixtures/astro-basic/src/pages/fileurl.astro
vendored
Normal file
10
packages/astro/test/fixtures/astro-basic/src/pages/fileurl.astro
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
---
|
||||||
|
import {capitalize} from 'file://../strings.js';
|
||||||
|
---
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head><title>Testing</title></head>
|
||||||
|
<body>
|
||||||
|
<h1>{capitalize('works')</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
4
packages/astro/test/fixtures/astro-basic/src/strings.js
vendored
Normal file
4
packages/astro/test/fixtures/astro-basic/src/strings.js
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
export function capitalize(str) {
|
||||||
|
return str.toUpperCase();
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue