diff --git a/.changeset/proud-peaches-cheat.md b/.changeset/proud-peaches-cheat.md new file mode 100644 index 0000000..c323561 --- /dev/null +++ b/.changeset/proud-peaches-cheat.md @@ -0,0 +1,8 @@ +--- +"vite-plugin-pagefind": patch +--- + +Add support for the pagefind highlight script, +as well as a new configuration option to support +having the pagefind bundle placed in other places +other than the default `/pagefind/pagefind.js`. diff --git a/README.md b/README.md index c6b9f51..d8a0d1b 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,12 @@ The command to build and index the project. _default_: 'npm run build' +### pagefind_dir + +The URL directory you use to import the pagefind script on your site. For example, if you use `/pagefind/pagefind.js`, the `pagefind_dir` is `pagefind`. If you use `/search/static/pagefind/pagefind.js`, the `pagefind_dir` is `search/static/pagefind`. + +_default_: 'pagefind' + ### dev_strategy The indexing strategy used during development: @@ -98,8 +104,8 @@ const pagefind: Pagefind = await import("/pagefind/pagefind.js"); ## Pagefind -For further questions regarding Pagefind itself you can refer to [the offical docs](https://pagefind.app/). +For further questions regarding Pagefind itself you can refer to [the official docs](https://pagefind.app/). ## License -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. +This project is licensed under the MIT License - see the [LICENSE.txt](LICENSE.txt) file for details. diff --git a/package.json b/package.json index afe2b61..894e646 100644 --- a/package.json +++ b/package.json @@ -12,17 +12,11 @@ "url": "https://github.com/Hugos68/vite-plugin-pagefind" }, "homepage": "https://github.com/Hugos68/vite-plugin-pagefind#vite-plugin-pagefind", - "keywords": [ - "vite", - "vite-plugin", - "pagefind" - ], + "keywords": ["vite", "vite-plugin", "pagefind"], "publishConfig": { "access": "public" }, - "files": [ - "dist" - ], + "files": ["dist"], "main": "./dist/plugins/pagefind.js", "exports": { ".": { diff --git a/src/internal/config.js b/src/internal/config.js index 458cd56..ae4494d 100644 --- a/src/internal/config.js +++ b/src/internal/config.js @@ -8,6 +8,7 @@ const PagefindConfigSchema = v.object({ v.object({ assets_dir: v.optional(v.string(), "public"), build_command: v.optional(v.string(), "npm run build"), + pagefind_dir: v.optional(v.string(), "pagefind"), dev_strategy: v.optional(v.picklist(["eager", "lazy"]), "lazy"), }), {}, @@ -25,5 +26,11 @@ export async function get_pagefind_config(cwd) { "utf-8", ); const pagefind_parsed = JSON.parse(pagefind_raw); - return v.parse(PagefindConfigSchema, pagefind_parsed); + const config = v.parse(PagefindConfigSchema, pagefind_parsed); + const pagefind_dir = config.vite_plugin_pagefind.pagefind_dir; + config.vite_plugin_pagefind.pagefind_dir = pagefind_dir.replace( + /^\/+|\/+$/g, + "", + ); + return config; } diff --git a/src/plugins/pagefind-build.js b/src/plugins/pagefind-build.js index 287f735..aebc6f5 100644 --- a/src/plugins/pagefind-build.js +++ b/src/plugins/pagefind-build.js @@ -1,3 +1,4 @@ +import { get_pagefind_config } from "../internal/config.js"; import { PACKAGE_NAME } from "../internal/constants.js"; /** @@ -8,11 +9,17 @@ export default function build() { return { name: `${PACKAGE_NAME}-build`, apply: "build", - config() { + async config() { + const cwd = process.cwd(); + const pagefind_config = await get_pagefind_config(cwd); + const pagefind_dir = pagefind_config.vite_plugin_pagefind.pagefind_dir; return { build: { rollupOptions: { - external: "/pagefind/pagefind.js", + external: [ + `/${pagefind_dir}/pagefind.js`, + `/${pagefind_dir}/pagefind-highlight.js`, + ], }, }, }; diff --git a/src/plugins/pagefind-dev.js b/src/plugins/pagefind-dev.js index d0cab50..0322f4f 100644 --- a/src/plugins/pagefind-dev.js +++ b/src/plugins/pagefind-dev.js @@ -18,11 +18,17 @@ export default function dev() { apply: "serve", enforce: "post", async config() { + const cwd = process.cwd(); + const pagefind_config = await get_pagefind_config(cwd); + const pagefind_dir = pagefind_config.vite_plugin_pagefind.pagefind_dir; return { - assetsInclude: "**/pagefind.js", + assetsInclude: ["**/pagefind.js", "**/pagefind-highlight.js"], build: { rollupOptions: { - external: "/pagefind/pagefind.js", + external: [ + `/${pagefind_dir}/pagefind.js`, + `/${pagefind_dir}/pagefind-highlight.js`, + ], }, }, };