Skip to content

Commit

Permalink
Renamed vite_plugin to vite_plugin_pagefind
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugos68 committed May 25, 2024
1 parent a5fdbf4 commit 20c545a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-timers-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vite-plugin-pagefind": patch
---

Renamed `vite_plugin` to `vite_plugin_pagefind`
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ Note: If your framework does not have a `vite.config` checkout the [examples](#e
```json
{
"site": "<BUILD_DIR>",
"vite_plugin": {
"vite_plugin_pagefind": {
"assets_dir": "<ASSETS_DIR>",
"build_command": "BUILD_COMMAND",
"dev_strategy": "DEV_STRATEGY"
"build_command": "<BUILD_COMMAND>",
"dev_strategy": "<DEV_STRATEGY>"
}
}
```
Expand Down
44 changes: 18 additions & 26 deletions src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}".`
);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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()];
}
10 changes: 8 additions & 2 deletions src/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
};
}

0 comments on commit 20c545a

Please sign in to comment.