Skip to content

Commit

Permalink
feat(search-pro): improve worker, close #2841
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Feb 28, 2023
1 parent cf4706f commit ef06dff
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
10 changes: 9 additions & 1 deletion packages/search-pro/rollup.config.ts
Expand Up @@ -15,7 +15,7 @@ export default [
copy: [["client/styles", "client"]],
}
),
...bundle("worker/index", {
...bundle("worker/minify", {
resolve: true,
dts: false,
external: [/^@internal\//],
Expand All @@ -24,4 +24,12 @@ export default [
__VUEPRESS_SSR__: false,
},
}),
...bundle("worker/original", {
resolve: true,
dts: false,
replace: {
// eslint-disable-next-line @typescript-eslint/naming-convention
__VUEPRESS_SSR__: false,
},
}),
];
14 changes: 11 additions & 3 deletions packages/search-pro/src/node/generateWorker.ts
Expand Up @@ -4,21 +4,29 @@ import { utoa } from "vuepress-shared/node";

import { getSearchIndex } from "./generateIndex.js";
import { type SearchProOptions } from "./options.js";
import { WORKER_PATH } from "./utils.js";
import { WORKER_FOLDER } from "./utils.js";

const CONTENT_THRESHOLD = 8192;

export const generateWorker = async (
app: App,
options: SearchProOptions
): Promise<void> => {
const workerFilePath = app.dir.dest(options.worker || "search-pro.worker.js");
const workerFileContent = await fs.readFile(WORKER_PATH, "utf8");
const searchIndexContent = JSON.stringify(getSearchIndex(app, options));
const shouldMinify = searchIndexContent.length > CONTENT_THRESHOLD;
const workerPath = `${WORKER_FOLDER}${
shouldMinify ? "minify" : "original"
}.js`;

const workerFileContent = await fs.readFile(workerPath, "utf8");

await fs.ensureDir(path.dirname(workerFilePath));
await fs.writeFile(
workerFilePath,
workerFileContent.replace(
"SEARCH_PRO_INDEX",
`"${utoa(JSON.stringify(getSearchIndex(app, options)))}"`
`"${shouldMinify ? utoa(searchIndexContent) : searchIndexContent}"`
)
);
};
4 changes: 3 additions & 1 deletion packages/search-pro/src/node/utils.ts
Expand Up @@ -8,7 +8,9 @@ export const CLIENT_FOLDER = ensureEndingSlash(
path.resolve(__dirname, "../client/")
);

export const WORKER_PATH = path.resolve(__dirname, "../worker/index.js");
export const WORKER_FOLDER = ensureEndingSlash(
path.resolve(__dirname, "../worker/")
);

export const PLUGIN_NAME = "vuepress-plugin-search-pro";

Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions packages/search-pro/src/worker/original.ts
@@ -0,0 +1,13 @@
import { getResults } from "../client/utils/index.js";
import { type SearchIndex } from "../shared/index.js";

const searchIndex: SearchIndex = <SearchIndex>JSON.parse(
// @ts-ignore
SEARCH_PRO_INDEX as string
);

self.onmessage = ({
data,
}: MessageEvent<{ query: string; routeLocale: string }>): void => {
self.postMessage(getResults(data.query, searchIndex[data.routeLocale]));
};

0 comments on commit ef06dff

Please sign in to comment.