-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow defining Astro components in Vite plugins (#3889)
* Allow defining Astro components in Vite plugins * Adds a changeset * Move non-main compilation into load * Use the cachedCompilation in the markdown plugin * Fix HMR test * Simplify getNormalizedID * Use a windows-friendly virtual module id for the test
- Loading branch information
Showing
11 changed files
with
194 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Allow defining Astro components in Vite plugins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/astro/test/fixtures/virtual-astro-file/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import myPlugin from './src/plugin/my-plugin.mjs'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
vite: { | ||
plugins: [myPlugin()] | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@test/virtual-astro-file", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/astro/test/fixtures/virtual-astro-file/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
import Something from '@my-plugin/virtual.astro'; | ||
--- | ||
<html> | ||
<head><title>Testing</title></head> | ||
<body> | ||
<Something /> | ||
</body> | ||
</html> | ||
|
27 changes: 27 additions & 0 deletions
27
packages/astro/test/fixtures/virtual-astro-file/src/plugin/my-plugin.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
|
||
export default function myPlugin() { | ||
const pluginId = `@my-plugin/virtual.astro`; | ||
return { | ||
enforce: 'pre', | ||
name: 'virtual-astro-plugin', | ||
resolveId(id) { | ||
if (id === pluginId) return id; | ||
}, | ||
load(id) { | ||
if (id === pluginId) { | ||
return `--- | ||
const works = true; | ||
--- | ||
<h1 id="something">This is a virtual module id</h1> | ||
<h2 id="works">{works}</h2> | ||
<style> | ||
h1 { | ||
color: green; | ||
} | ||
</style> | ||
`; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { expect } from 'chai'; | ||
import * as cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('Loading virtual Astro files', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ root: './fixtures/virtual-astro-file/' }); | ||
await fixture.build(); | ||
}); | ||
|
||
it('renders the component', async () => { | ||
const html = await fixture.readFile('/index.html'); | ||
const $ = cheerio.load(html); | ||
expect($('#something')).to.have.a.lengthOf(1); | ||
expect($('#works').text()).to.equal('true'); | ||
}); | ||
|
||
it('builds component CSS', async () => { | ||
const html = await fixture.readFile('/index.html'); | ||
const $ = cheerio.load(html); | ||
const href = $('link').attr('href'); | ||
const css = await fixture.readFile(href); | ||
expect(css).to.match(/green/, 'css bundled from virtual astro module'); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.