diff options
author | 2021-03-24 11:45:38 -0400 | |
---|---|---|
committer | 2021-03-24 11:45:38 -0400 | |
commit | 3c24faa8cab428b17ba2f8e083f5296b1b931fe1 (patch) | |
tree | 940e2b6b68c2d7e895802fd451c7777f60c56eb6 /src | |
parent | 5c1cd5b1da446f51e535030072756126f7b038d7 (diff) | |
download | astro-3c24faa8cab428b17ba2f8e083f5296b1b931fe1.tar.gz astro-3c24faa8cab428b17ba2f8e083f5296b1b931fe1.tar.zst astro-3c24faa8cab428b17ba2f8e083f5296b1b931fe1.zip |
hmx ☞ astro (#22)
This changes all hmx files to astro files and updates all code to not reference hmx any more.
Diffstat (limited to 'src')
-rw-r--r-- | src/@types/astro.ts | 6 | ||||
-rw-r--r-- | src/codegen/index.ts | 8 | ||||
-rw-r--r-- | src/compiler/README.md | 2 | ||||
-rw-r--r-- | src/compiler/parse/index.ts | 10 | ||||
-rw-r--r-- | src/config.ts | 2 | ||||
-rw-r--r-- | src/dev.ts | 2 | ||||
-rw-r--r-- | src/frontend/h.ts | 6 | ||||
-rw-r--r-- | src/generate.ts | 10 | ||||
-rw-r--r-- | src/runtime.ts | 22 | ||||
-rw-r--r-- | src/transform2.ts | 22 |
10 files changed, 46 insertions, 44 deletions
diff --git a/src/@types/astro.ts b/src/@types/astro.ts index 8d5979eec..9ec2aec53 100644 --- a/src/@types/astro.ts +++ b/src/@types/astro.ts @@ -1,16 +1,16 @@ export interface AstroConfigRaw { dist: string; projectRoot: string; - hmxRoot: string; + astroRoot: string; jsx?: string; } -export type ValidExtensionPlugins = 'hmx' | 'react' | 'preact' | 'svelte' | 'vue'; +export type ValidExtensionPlugins = 'astro' | 'react' | 'preact' | 'svelte' | 'vue'; export interface AstroConfig { dist: string; projectRoot: URL; - hmxRoot: URL; + astroRoot: URL; extensions?: Record<string, ValidExtensionPlugins> } diff --git a/src/codegen/index.ts b/src/codegen/index.ts index a9fc433f4..d248b2a60 100644 --- a/src/codegen/index.ts +++ b/src/codegen/index.ts @@ -25,7 +25,7 @@ interface CodeGenOptions { } function internalImport(internalPath: string) { - return `/__hmx_internal__/${internalPath}`; + return `/_astro_internal/${internalPath}`; } function getAttributes(attrs: Attribute[]): Record<string, string> { @@ -96,7 +96,7 @@ interface ComponentInfo { } const defaultExtensions: Readonly<Record<string, ValidExtensionPlugins>> = { - '.hmx': 'hmx', + '.astro': 'astro', '.jsx': 'react', '.vue': 'vue', '.svelte': 'svelte' @@ -117,9 +117,9 @@ function getComponentWrapper(_name: string, { type, url }: ComponentInfo, compil } switch (plugin) { - case 'hmx': { + case 'astro': { if (kind) { - throw new Error(`HMX does not support :${kind}`); + throw new Error(`Astro does not support :${kind}`); } return { wrapper: name, diff --git a/src/compiler/README.md b/src/compiler/README.md index bef440860..f44d45ecf 100644 --- a/src/compiler/README.md +++ b/src/compiler/README.md @@ -1,3 +1,3 @@ -# `hmx/compiler` +# `astro/compiler` This directory is a fork of `svelte/compiler`. It is meant to stay as close to the original source as possible, so that upstream changes are easy to integrate. Everything svelte-specific and unrelated to parsing (compiler, preprocess, etc) has been removed.
\ No newline at end of file diff --git a/src/compiler/parse/index.ts b/src/compiler/parse/index.ts index 00eccdb7d..052cf0317 100644 --- a/src/compiler/parse/index.ts +++ b/src/compiler/parse/index.ts @@ -226,7 +226,7 @@ export default function parse(template: string, options: ParserOptions = {}): As parser.error( { code: 'duplicate-style', - message: 'You can only have one <style> tag per HMX file', + message: 'You can only have one <style> tag per Astro file', }, parser.css[1].start ); @@ -234,15 +234,15 @@ export default function parse(template: string, options: ParserOptions = {}): As // const instance_scripts = parser.js.filter((script) => script.context === 'default'); // const module_scripts = parser.js.filter((script) => script.context === 'module'); - const hmx_scripts = parser.js.filter((script) => script.context === 'setup'); + const astro_scripts = parser.js.filter((script) => script.context === 'setup'); - if (hmx_scripts.length > 1) { + if (astro_scripts.length > 1) { parser.error( { code: 'invalid-script', message: 'A component can only have one frontmatter (---) script', }, - hmx_scripts[1].start + astro_scripts[1].start ); } @@ -260,6 +260,6 @@ export default function parse(template: string, options: ParserOptions = {}): As html: parser.html, css: parser.css[0], // instance: instance_scripts[0], - module: hmx_scripts[0], + module: astro_scripts[0], }; } diff --git a/src/config.ts b/src/config.ts index da58b7a03..1a80d4a15 100644 --- a/src/config.ts +++ b/src/config.ts @@ -17,6 +17,6 @@ export async function loadConfig(rawRoot: string | undefined): Promise<AstroConf const astroConfig: AstroConfig = (await import(astroConfigPath)).default; astroConfig.projectRoot = new URL(astroConfig.projectRoot + '/', fileProtocolRoot); - astroConfig.hmxRoot = new URL(astroConfig.hmxRoot + '/', fileProtocolRoot); + astroConfig.astroRoot = new URL(astroConfig.astroRoot + '/', fileProtocolRoot); return astroConfig; } diff --git a/src/dev.ts b/src/dev.ts index c6ad9ff7c..5c80133a7 100644 --- a/src/dev.ts +++ b/src/dev.ts @@ -53,7 +53,7 @@ export default async function (astroConfig: AstroConfig) { break; } default: { - error(logging, 'executing hmx', result.error); + error(logging, 'executing astro', result.error); break; } } diff --git a/src/frontend/h.ts b/src/frontend/h.ts index 77ecf857b..cd94583f8 100644 --- a/src/frontend/h.ts +++ b/src/frontend/h.ts @@ -1,7 +1,7 @@ export type HProps = Record<string, string> | null | undefined; export type HChild = string | undefined | (() => string); -export type HMXComponent = (props: HProps, ...children: Array<HChild>) => string; -export type HTag = string | HMXComponent; +export type AstroComponent = (props: HProps, ...children: Array<HChild>) => string; +export type HTag = string | AstroComponent; const voidTags = new Set(['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr']); @@ -41,7 +41,7 @@ function* _h(tag: string, attrs: HProps, children: Array<HChild>) { export async function h(tag: HTag, attrs: HProps, ...pChildren: Array<Promise<HChild>>) { const children = await Promise.all(pChildren.flat(Infinity)); if (typeof tag === 'function') { - // We assume it's an hmx component + // We assume it's an astro component return tag(attrs, ...children); } diff --git a/src/generate.ts b/src/generate.ts index 225bf4fde..4a31cc291 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -19,8 +19,8 @@ async function* allPages(root: URL): AsyncGenerator<URL, void, unknown> { } export default async function (astroConfig: AstroConfig) { - const { projectRoot, hmxRoot } = astroConfig; - const pageRoot = new URL('./pages/', hmxRoot); + const { projectRoot, astroRoot } = astroConfig; + const pageRoot = new URL('./pages/', astroRoot); const dist = new URL(astroConfig.dist + '/', projectRoot); const configPath = new URL('./snowpack.config.js', projectRoot).pathname; @@ -39,11 +39,11 @@ export default async function (astroConfig: AstroConfig) { const runtime = snowpack.getServerRuntime(); for await (const filepath of allPages(pageRoot)) { - const rel = pathRelative(hmxRoot.pathname, filepath.pathname); // pages/index.hmx - const pagePath = `/_hmx/${rel.replace(/\.(hmx|md)/, '.js')}`; + const rel = pathRelative(astroRoot.pathname, filepath.pathname); // pages/index.astro + const pagePath = `/_astro/${rel.replace(/\.(astro|md)/, '.js')}`; try { - const outPath = new URL('./' + rel.replace(/\.(hmx|md)/, '.html'), dist); + const outPath = new URL('./' + rel.replace(/\.(astro|md)/, '.html'), dist); const outFolder = new URL('./', outPath); const mod = await runtime.importModule(pagePath); const html = await mod.exports.default({}); diff --git a/src/runtime.ts b/src/runtime.ts index ed498c331..889224548 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -28,18 +28,18 @@ export type LoadResult = LoadResultSuccess | LoadResultNotFound | LoadResultErro async function load(config: RuntimeConfig, rawPathname: string | undefined): Promise<LoadResult> { const { logging, snowpack, snowpackRuntime } = config; - const { hmxRoot } = config.astroConfig; + const { astroRoot } = config.astroConfig; const fullurl = new URL(rawPathname || '/', 'https://example.org/'); const reqPath = decodeURI(fullurl.pathname); const selectedPage = reqPath.substr(1) || 'index'; info(logging, 'access', reqPath); - const selectedPageLoc = new URL(`./pages/${selectedPage}.hmx`, hmxRoot); - const selectedPageMdLoc = new URL(`./pages/${selectedPage}.md`, hmxRoot); - const selectedPageUrl = `/_hmx/pages/${selectedPage}.js`; + const selectedPageLoc = new URL(`./pages/${selectedPage}.astro`, astroRoot); + const selectedPageMdLoc = new URL(`./pages/${selectedPage}.md`, astroRoot); + const selectedPageUrl = `/_astro/pages/${selectedPage}.js`; - // Non-hmx pages (file resources) + // Non-Astro pages (file resources) if (!existsSync(selectedPageLoc) && !existsSync(selectedPageMdLoc)) { try { const result = await snowpack.loadUrl(reqPath); @@ -96,19 +96,19 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro } export async function createRuntime(astroConfig: AstroConfig, logging: LogOptions) { - const { projectRoot, hmxRoot, extensions } = astroConfig; + const { projectRoot, astroRoot, extensions } = astroConfig; const internalPath = new URL('./frontend/', import.meta.url); // Workaround for SKY-251 - const hmxPlugOptions: { + const astroPlugOptions: { resolve?: (s: string) => string; extensions?: Record<string, string> } = { extensions }; if (existsSync(new URL('./package-lock.json', projectRoot))) { const pkgLockStr = await readFile(new URL('./package-lock.json', projectRoot), 'utf-8'); const pkgLock = JSON.parse(pkgLockStr); - hmxPlugOptions.resolve = (pkgName: string) => { + astroPlugOptions.resolve = (pkgName: string) => { const ver = pkgLock.dependencies[pkgName].version; return `/_snowpack/pkg/${pkgName}.v${ver}.js`; }; @@ -117,10 +117,10 @@ export async function createRuntime(astroConfig: AstroConfig, logging: LogOption const snowpackConfig = await loadConfiguration({ root: projectRoot.pathname, mount: { - [hmxRoot.pathname]: '/_hmx', - [internalPath.pathname]: '/__hmx_internal__', + [astroRoot.pathname]: '/_astro', + [internalPath.pathname]: '/_astro_internal', }, - plugins: [[new URL('../snowpack-plugin.cjs', import.meta.url).pathname, hmxPlugOptions]], + plugins: [[new URL('../snowpack-plugin.cjs', import.meta.url).pathname, astroPlugOptions]], devOptions: { open: 'none', output: 'stream', diff --git a/src/transform2.ts b/src/transform2.ts index c58502f2f..27956fb9f 100644 --- a/src/transform2.ts +++ b/src/transform2.ts @@ -24,16 +24,16 @@ const defaultCompileOptions: CompileOptions = { }; function internalImport(internalPath: string) { - return `/__hmx_internal__/${internalPath}`; + return `/_astro_internal/${internalPath}`; } -interface ConvertHmxOptions { +interface ConvertAstroOptions { compileOptions: CompileOptions; filename: string; fileID: string; } -async function convertHmxToJsx(template: string, opts: ConvertHmxOptions): Promise<TransformResult> { +async function convertAstroToJsx(template: string, opts: ConvertAstroOptions): Promise<TransformResult> { const { filename } = opts; // 1. Parse @@ -90,17 +90,19 @@ async function convertMdToJsx( const convertOptions = { compileOptions, filename, fileID }; - return convertHmxToJsx(raw, convertOptions); + return convertAstroToJsx(raw, convertOptions); } +type SupportedExtensions = '.astro' | '.md'; + async function transformFromSource( contents: string, { compileOptions, filename, projectRoot }: { compileOptions: CompileOptions; filename: string; projectRoot: string } ): Promise<TransformResult> { const fileID = path.relative(projectRoot, filename); - switch (path.extname(filename)) { - case '.hmx': - return convertHmxToJsx(contents, { compileOptions, filename, fileID }); + switch (path.extname(filename) as SupportedExtensions) { + case '.astro': + return convertAstroToJsx(contents, { compileOptions, filename, fileID }); case '.md': return convertMdToJsx(contents, { compileOptions, filename, fileID }); default: @@ -125,7 +127,7 @@ export async function compileComponent( // <script astro></script> ${sourceJsx.script} -// \`__render()\`: Render the contents of the HMX module. "<slot:*>" elements are not +// \`__render()\`: Render the contents of the Astro module. "<slot:*>" elements are not // included (see below). import { h, Fragment } from '${internalImport('h.js')}'; export function __slothead(children, context) { return h(Fragment, null, ${headItemJsx}); } @@ -135,7 +137,7 @@ export default __render; if (headItemJsx) { modJsx += ` -// \`__renderPage()\`: Render the contents of the HMX module as a page. This is a special flow, +// \`__renderPage()\`: Render the contents of the Astro module as a page. This is a special flow, // triggered by loading a component directly by URL. // If the page exports a defined "layout", then load + render those first. "context", "astro:head", // and "slot:body" should all inherit from parent layouts, merging together in the correct order. @@ -149,7 +151,7 @@ export async function __renderPage({request, children}) { // find all layouts, going up the layout chain. if (currentChild.layout) { - const layoutComponent = (await import('/_hmx/layouts/' + layout.replace(/.*layouts\\//, "").replace(/\.hmx$/, '.js'))); + const layoutComponent = (await import('/_astro/layouts/' + layout.replace(/.*layouts\\//, "").replace(/\.astro$/, '.js'))); return layoutComponent.__renderPage({ request, children: [currentChild, ...children], |