@astrojs/alpinejs
Advanced tools
Comparing version 0.3.2 to 0.4.0
import type { AstroIntegration } from 'astro'; | ||
export default function createPlugin(): AstroIntegration; | ||
interface Options { | ||
/** | ||
* You can extend Alpine by setting this option to a root-relative import specifier (for example, `entrypoint: "/src/entrypoint"`). | ||
* | ||
* The default export of this file should be a function that accepts an Alpine instance prior to starting, allowing the use of custom directives, plugins and other customizations for advanced use cases. | ||
* | ||
* ```js | ||
* // astro.config.mjs | ||
* import { defineConfig } from 'astro/config'; | ||
* import alpine from '@astrojs/alpinejs'; | ||
* | ||
* export default defineConfig({ | ||
* // ... | ||
* integrations: [alpine({ entrypoint: '/src/entrypoint' })], | ||
* }); | ||
* ``` | ||
* | ||
* ```js | ||
* // src/entrypoint.ts | ||
* import type { Alpine } from 'alpinejs' | ||
* | ||
* export default (Alpine: Alpine) => { | ||
* Alpine.directive('foo', el => { | ||
* el.textContent = 'bar'; | ||
* }) | ||
* } | ||
* ``` | ||
*/ | ||
entrypoint?: string; | ||
} | ||
export default function createPlugin(options?: Options): AstroIntegration; | ||
export {}; |
@@ -1,10 +0,62 @@ | ||
function createPlugin() { | ||
import { resolve } from "node:path"; | ||
function virtualEntrypoint(options) { | ||
const virtualModuleId = "virtual:@astrojs/alpinejs/entrypoint"; | ||
const resolvedVirtualModuleId = "\0" + virtualModuleId; | ||
let isBuild; | ||
let root; | ||
let entrypoint; | ||
return { | ||
name: "@astrojs/alpinejs/virtual-entrypoint", | ||
config(_, { command }) { | ||
isBuild = command === "build"; | ||
}, | ||
configResolved(config) { | ||
root = config.root; | ||
if (options?.entrypoint) { | ||
entrypoint = options.entrypoint.startsWith(".") ? resolve(root, options.entrypoint) : options.entrypoint; | ||
} | ||
}, | ||
resolveId(id) { | ||
if (id === virtualModuleId) { | ||
return resolvedVirtualModuleId; | ||
} | ||
}, | ||
load(id) { | ||
if (id === resolvedVirtualModuleId) { | ||
if (entrypoint) { | ||
return `import * as mod from ${JSON.stringify(entrypoint)}; | ||
export const setup = (Alpine) => { | ||
if ('default' in mod) { | ||
mod.default(Alpine); | ||
} else { | ||
${!isBuild ? `console.warn("[@astrojs/alpinejs] entrypoint \`" + ${JSON.stringify( | ||
entrypoint | ||
)} + "\` does not export a default function. Check out https://docs.astro.build/en/guides/integrations-guide/alpinejs/#entrypoint.");` : ""} | ||
} | ||
}`; | ||
} | ||
return `export const setup = () => {};`; | ||
} | ||
} | ||
}; | ||
} | ||
function createPlugin(options) { | ||
return { | ||
name: "@astrojs/alpinejs", | ||
hooks: { | ||
"astro:config:setup": ({ injectScript }) => { | ||
"astro:config:setup": ({ injectScript, updateConfig }) => { | ||
injectScript( | ||
"page", | ||
`import Alpine from 'alpinejs'; window.Alpine = Alpine; Alpine.start();` | ||
`import Alpine from 'alpinejs'; | ||
import { setup } from 'virtual:@astrojs/alpinejs/entrypoint'; | ||
setup(Alpine); | ||
window.Alpine = Alpine; | ||
Alpine.start();` | ||
); | ||
updateConfig({ | ||
vite: { | ||
plugins: [virtualEntrypoint(options)] | ||
} | ||
}); | ||
} | ||
@@ -11,0 +63,0 @@ } |
{ | ||
"name": "@astrojs/alpinejs", | ||
"description": "Use Alpine within Astro", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"type": "module", | ||
@@ -35,3 +35,5 @@ "types": "./dist/index.d.ts", | ||
"devDependencies": { | ||
"astro": "4.0.8", | ||
"@playwright/test": "1.40.0", | ||
"vite": "^5.0.10", | ||
"astro": "4.2.5", | ||
"astro-scripts": "0.0.14" | ||
@@ -45,4 +47,5 @@ }, | ||
"build:ci": "astro-scripts build \"src/**/*.ts\"", | ||
"dev": "astro-scripts dev \"src/**/*.ts\"" | ||
"dev": "astro-scripts dev \"src/**/*.ts\"", | ||
"test:e2e": "playwright test" | ||
} | ||
} |
9155
100
4