-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add html package * feat: support assets in HTML * feat(html): upgrade html integration * feat(html): add `@astrojs/html` integration * feat(html): add html support to astro core * test(html): update html tests with package.json files * chore: add changeset * fix: remove import cycle * chore: fix types * refactor: remove @astrojs/html, add to core * chore: update types for `*.html` * fix: move *.html to astro/env Co-authored-by: Nate Moore <nate@astro.build>
- Loading branch information
1 parent
8b468cc
commit 7250e4e
Showing
32 changed files
with
534 additions
and
12 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 | ||
--- | ||
|
||
Add support for `.html` components and pages |
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
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
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,14 @@ | ||
import { transform } from './transform/index.js'; | ||
|
||
export default function html() { | ||
return { | ||
name: 'astro:html', | ||
options(options: any) { | ||
options.plugins = options.plugins?.filter((p: any) => p.name !== 'vite:build-html'); | ||
}, | ||
async transform(source: string, id: string) { | ||
if (!id.endsWith('.html')) return; | ||
return await transform(source, id); | ||
} | ||
} | ||
} |
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 type { Plugin } from 'unified'; | ||
import type { Root, RootContent } from 'hast'; | ||
import type MagicString from 'magic-string'; | ||
import { visit } from 'unist-util-visit'; | ||
|
||
import { replaceAttribute, needsEscape, escape } from './utils.js'; | ||
|
||
const rehypeEscape: Plugin<[{ s: MagicString }], Root> = ({ s }) => { | ||
return (tree, file) => { | ||
visit(tree, (node: Root | RootContent, index, parent) => { | ||
if (node.type === 'text' || node.type === 'comment') { | ||
if (needsEscape(node.value)) { | ||
s.overwrite(node.position!.start.offset!, node.position!.end.offset!, escape(node.value)); | ||
} | ||
} else if (node.type === 'element') { | ||
for (const [key, value] of Object.entries(node.properties ?? {})) { | ||
const newKey = needsEscape(key) ? escape(key) : key; | ||
const newValue = needsEscape(value) ? escape(value) : value; | ||
if (newKey === key && newValue === value) continue; | ||
replaceAttribute(s, node, key, (value === '') ? newKey : `${newKey}="${newValue}"`); | ||
} | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
export default rehypeEscape; |
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,32 @@ | ||
import MagicString from 'magic-string'; | ||
import { rehype } from 'rehype'; | ||
import { VFile } from 'vfile'; | ||
import escape from './escape.js'; | ||
import slots, { SLOT_PREFIX } from './slots.js'; | ||
|
||
export async function transform(code: string, id: string) { | ||
const s = new MagicString(code, { filename: id }); | ||
const imports = new Map(); | ||
const parser = rehype() | ||
.data('settings', { fragment: true }) | ||
.use(escape, { s }) | ||
.use(slots, { s }); | ||
|
||
const vfile = new VFile({ value: code, path: id }) | ||
await parser.process(vfile) | ||
s.prepend(`export default {\n\t"astro:html": true,\n\trender({ slots: ${SLOT_PREFIX} }) {\n\t\treturn \``); | ||
s.append('`\n\t}\n}'); | ||
|
||
if (imports.size > 0) { | ||
let importText = '' | ||
for (const [path, importName] of imports.entries()) { | ||
importText += `import ${importName} from "${path}";\n` | ||
} | ||
s.prepend(importText); | ||
} | ||
|
||
return { | ||
code: s.toString(), | ||
map: s.generateMap() | ||
} | ||
} |
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 type { Plugin } from 'unified'; | ||
import type { Root, RootContent } from 'hast'; | ||
|
||
import { visit } from 'unist-util-visit'; | ||
import MagicString from 'magic-string'; | ||
import { escape } from './utils.js'; | ||
|
||
const rehypeSlots: Plugin<[{ s: MagicString }], Root> = ({ s }) => { | ||
return (tree, file) => { | ||
visit(tree, (node: Root | RootContent, index, parent) => { | ||
if (node.type === 'element' && node.tagName === 'slot') { | ||
if (typeof node.properties?.['is:inline'] !== 'undefined') return; | ||
const name = node.properties?.['name'] ?? 'default'; | ||
const start = node.position?.start.offset ?? 0; | ||
const end = node.position?.end.offset ?? 0; | ||
const first = node.children.at(0) ?? node; | ||
const last = node.children.at(-1) ?? node; | ||
const text = file.value.slice(first.position?.start.offset ?? 0, last.position?.end.offset ?? 0).toString(); | ||
s.overwrite(start, end, `\${${SLOT_PREFIX}["${name}"] ?? \`${escape(text).trim()}\`}`) | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default rehypeSlots; | ||
|
||
export const SLOT_PREFIX = `___SLOTS___`; |
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 type { Element } from 'hast'; | ||
import MagicString from 'magic-string'; | ||
|
||
const splitAttrsTokenizer = /([\$\{\}\@a-z0-9_\:\-]*)\s*?=\s*?(['"]?)(.*?)\2\s+/gim; | ||
|
||
export function replaceAttribute(s: MagicString, node: Element, key: string, newValue: string) { | ||
splitAttrsTokenizer.lastIndex = 0; | ||
const text = s.original.slice(node.position?.start.offset ?? 0, node.position?.end.offset ?? 0).toString(); | ||
const offset = text.indexOf(key); | ||
if (offset === -1) return; | ||
const start = node.position!.start.offset! + offset; | ||
const tokens = text.slice(offset).split(splitAttrsTokenizer); | ||
const token = tokens[0].replace(/([^>])(\>[\s\S]*$)/gmi, '$1'); | ||
if (token.trim() === key) { | ||
const end = start + key.length; | ||
s.overwrite(start, end, newValue) | ||
} else { | ||
const end = start + `${key}=${tokens[2]}${tokens[3]}${tokens[2]}`.length; | ||
s.overwrite(start, end, newValue) | ||
} | ||
} | ||
export function needsEscape(value: any): value is string { | ||
return typeof value === 'string' && (value.includes('`') || value.includes('${')); | ||
} | ||
export function escape(value: string) { | ||
return value.replace(/`/g, '\\`').replace(/\$\{/g, '\\${'); | ||
} |
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/html-component", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/html-component/src/components/Test.html
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,3 @@ | ||
<h1>Hello component!</h1> | ||
|
||
<div id="foo">bar</div> |
5 changes: 5 additions & 0 deletions
5
packages/astro/test/fixtures/html-component/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,5 @@ | ||
--- | ||
import Test from '../components/Test.html'; | ||
--- | ||
|
||
<Test /> |
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/html-escape", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/astro/test/fixtures/html-escape/src/components/Test.html
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,4 @@ | ||
<div>${foo}</div> | ||
<span ${attr}></span> | ||
<custom-element x-data="`${test}`"></custom-element> | ||
<script>console.log(`hello ${"world"}!`)</script> |
5 changes: 5 additions & 0 deletions
5
packages/astro/test/fixtures/html-escape/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,5 @@ | ||
--- | ||
import Test from '../components/Test.html'; | ||
--- | ||
|
||
<Test /> |
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/html-page", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
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 @@ | ||
<h1>Hello page!</h1> |
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/html-slots", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/html-slots/src/components/Default.html
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 @@ | ||
<div id="default"><slot></slot></div> |
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/html-slots/src/components/Inline.html
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 @@ | ||
<div id="inline"><slot is:inline></slot></div> |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/html-slots/src/components/Named.html
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,3 @@ | ||
<div id="a"><slot name="a"></slot></div> | ||
<div id="b"><slot name="b"></slot></div> | ||
<div id="c"><slot name="c"></slot></div> |
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/html-slots/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,13 @@ | ||
--- | ||
import Default from '../components/Default.html'; | ||
import Named from '../components/Named.html'; | ||
import Inline from '../components/Inline.html'; | ||
--- | ||
|
||
<Default>Default</Default> | ||
<Named> | ||
<span slot="a">A</span> | ||
<span slot="b">B</span> | ||
<span slot="c">C</span> | ||
</Named> | ||
<Inline></Inline> |
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,57 @@ | ||
import { expect } from 'chai'; | ||
import * as cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('HTML Component', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/html-component/', | ||
}); | ||
}); | ||
|
||
describe('build', () => { | ||
before(async () => { | ||
await fixture.build(); | ||
}); | ||
|
||
it('works', async () => { | ||
const html = await fixture.readFile('/index.html'); | ||
const $ = cheerio.load(html); | ||
|
||
const h1 = $('h1'); | ||
const foo = $('#foo'); | ||
|
||
expect(h1.text()).to.equal('Hello component!'); | ||
expect(foo.text()).to.equal('bar'); | ||
}); | ||
}); | ||
|
||
describe('dev', () => { | ||
let devServer; | ||
|
||
before(async () => { | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
it('works', async () => { | ||
const res = await fixture.fetch('/'); | ||
|
||
expect(res.status).to.equal(200); | ||
|
||
const html = await res.text(); | ||
const $ = cheerio.load(html); | ||
|
||
const h1 = $('h1'); | ||
const foo = $('#foo'); | ||
|
||
expect(h1.text()).to.equal('Hello component!'); | ||
expect(foo.text()).to.equal('bar'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.