diff --git a/.changeset/proud-timers-pretend.md b/.changeset/proud-timers-pretend.md new file mode 100644 index 0000000..3214554 --- /dev/null +++ b/.changeset/proud-timers-pretend.md @@ -0,0 +1,5 @@ +--- +"vite-plugin-pagefind": patch +--- + +Renamed `vite_plugin` to `vite_plugin_pagefind` diff --git a/README.md b/README.md index 37dd8f0..4c693a4 100644 --- a/README.md +++ b/README.md @@ -49,10 +49,10 @@ Note: If your framework does not have a `vite.config` checkout the [examples](#e ```json { "site": "", - "vite_plugin": { + "vite_plugin_pagefind": { "assets_dir": "", - "build_command": "BUILD_COMMAND", - "dev_strategy": "DEV_STRATEGY" + "build_command": "", + "dev_strategy": "" } } ``` diff --git a/src/dev.ts b/src/dev.ts index 7846111..6872384 100644 --- a/src/dev.ts +++ b/src/dev.ts @@ -12,59 +12,51 @@ export default function dev(): PluginOption { apply: 'serve', async config(vite_config) { const pagefind_config = get_pagefind_config(vite_config.root); - const resolved_site_dir = resolve( - vite_config.root, - pagefind_config.site - ); - const resolved_assets_dir = resolve( - vite_config.root, - pagefind_config.vite_plugin.assets_dir - ); - const build_command = pagefind_config.vite_plugin.build_command; - const dev_strategy = pagefind_config.vite_plugin.dev_strategy; - switch (dev_strategy) { + switch (pagefind_config.dev_strategy) { case 'eager': { console_log('Building pagefind...'); - execSync(build_command, { cwd: vite_config.root }); - console_log( - `Copying pagefind bundle to ${pagefind_config.vite_plugin.assets_dir}...` - ); + execSync(pagefind_config.build_command, { + cwd: vite_config.root + }); + console_log(`Copying pagefind bundle to assets dir...`); await promises.cp( - resolve(resolved_site_dir, 'pagefind'), - resolve(resolved_assets_dir, 'pagefind'), + resolve(pagefind_config.site_dir, 'pagefind'), + resolve(pagefind_config.assets_dir, 'pagefind'), { recursive: true } ); break; } case 'lazy': { const pagefind_in_assets = existsSync( - resolve(resolved_assets_dir, 'pagefind') + resolve(pagefind_config.assets_dir, 'pagefind') ); if (!pagefind_in_assets) { const pagefind_in_site = existsSync( - resolve(resolved_site_dir, 'pagefind') + resolve(pagefind_config.site_dir, 'pagefind') ); if (!pagefind_in_site) { console_log('Building pagefind...'); - execSync(build_command, { cwd: vite_config.root }); + execSync(pagefind_config.build_command, { + cwd: vite_config.root + }); } - console_log( - `Copying pagefind bundle to ${pagefind_config.vite_plugin.assets_dir}...` - ); + console_log(`Copying pagefind bundle to assets dir...`); await promises.cp( - resolve(resolved_site_dir, 'pagefind'), - resolve(resolved_assets_dir, 'pagefind'), + resolve(pagefind_config.site_dir, 'pagefind'), + resolve(pagefind_config.assets_dir, 'pagefind'), { recursive: true } ); } break; } default: { - throw new Error(`Invalid dev strategy "${dev_strategy}".`); + throw new Error( + `Invalid dev strategy "${pagefind_config.dev_strategy}".` + ); } } diff --git a/src/index.ts b/src/index.ts index 2042e05..12b2b63 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import dev from './dev.js'; import build from './build.js'; +import { PluginOption } from 'vite'; -export default function pagefind() { +export default function pagefind(): PluginOption { return [dev(), build()]; } diff --git a/src/util/config.ts b/src/util/config.ts index c3803d9..4da375e 100644 --- a/src/util/config.ts +++ b/src/util/config.ts @@ -4,7 +4,7 @@ import { readFileSync } from 'fs'; const PagefindConfigSchema = v.object({ site: v.string(), - vite_plugin: v.object({ + vite_plugin_pagefind: v.object({ assets_dir: v.string(), build_command: v.string(), dev_strategy: v.string() @@ -14,5 +14,11 @@ const PagefindConfigSchema = v.object({ export function get_pagefind_config(cwd: string) { const pagefind_raw = readFileSync(resolve(cwd, 'pagefind.json'), 'utf-8'); const pagefind_parsed = JSON.parse(pagefind_raw); - return v.parse(PagefindConfigSchema, pagefind_parsed); + const config = v.parse(PagefindConfigSchema, pagefind_parsed); + return { + site_dir: resolve(cwd, config.site), + assets_dir: resolve(cwd, config.vite_plugin_pagefind.assets_dir), + build_command: config.vite_plugin_pagefind.build_command, + dev_strategy: config.vite_plugin_pagefind.dev_strategy + }; }