From 4728b2820d42458673ef35e87f70ec44242f69cf Mon Sep 17 00:00:00 2001 From: weareoutman Date: Tue, 8 Oct 2024 09:59:20 +0800 Subject: [PATCH] fix: use `require.resolve` for generated js, fixes #459 --- .../src/server/utils/generate.spec.ts | 12 +++++++++--- docusaurus-search-local/src/server/utils/generate.ts | 6 +++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/docusaurus-search-local/src/server/utils/generate.spec.ts b/docusaurus-search-local/src/server/utils/generate.spec.ts index b0e2971d..d2b20e50 100644 --- a/docusaurus-search-local/src/server/utils/generate.spec.ts +++ b/docusaurus-search-local/src/server/utils/generate.spec.ts @@ -44,7 +44,9 @@ describe("generate", () => { expect.stringMatching( /^require\(".+\/|\\lunr-languages\/|\\lunr\.stemmer\.support\.js"\)\(lunr\);$/ ), - 'require("@easyops-cn/docusaurus-search-local/dist/client/shared/lunrLanguageZh").lunrLanguageZh(lunr);', + expect.stringMatching( + /^require\(".+\/lunrLanguageZh\.js"\)\.lunrLanguageZh\(lunr\);$/ + ), 'export const language = ["zh"];', "export const removeDefaultStopWordFilter = false;", "export const removeDefaultStemmer = false;", @@ -129,7 +131,9 @@ describe("generate", () => { expect.stringMatching( /^require\(".+\/|\\lunr-languages\/|\\lunr\.stemmer\.support\.js"\)\(lunr\);$/ ), - 'require("@easyops-cn/docusaurus-search-local/dist/client/shared/lunrLanguageZh").lunrLanguageZh(lunr);', + expect.stringMatching( + /^require\(".+\/lunrLanguageZh\.js"\)\.lunrLanguageZh\(lunr\);$/ + ), expect.stringMatching( /^require\(".+\/|\\lunr-languages\/|\\lunr\.multi\.js"\)\(lunr\);$/ ), @@ -161,7 +165,9 @@ describe("generate", () => { expect.stringMatching( /^require\(".+\/|\\lunr-languages\/|\\lunr\.es\.js"\)\(lunr\);$/ ), - 'require("@easyops-cn/docusaurus-search-local/dist/client/shared/lunrLanguageZh").lunrLanguageZh(lunr);', + expect.stringMatching( + /^require\(".+\/lunrLanguageZh\.js"\)\.lunrLanguageZh\(lunr\);$/ + ), expect.stringMatching( /^require\(".+\/|\\lunr-languages\/|\\lunr\.multi\.js"\)\(lunr\);$/ ), diff --git a/docusaurus-search-local/src/server/utils/generate.ts b/docusaurus-search-local/src/server/utils/generate.ts index 72624e66..70bdaee7 100644 --- a/docusaurus-search-local/src/server/utils/generate.ts +++ b/docusaurus-search-local/src/server/utils/generate.ts @@ -50,7 +50,11 @@ export function generate(config: ProcessedPluginOptions, dir: string): string { } if (language.includes("zh")) { contents.push( - 'require("@easyops-cn/docusaurus-search-local/dist/client/shared/lunrLanguageZh").lunrLanguageZh(lunr);' + `require(${JSON.stringify( + require.resolve( + "@easyops-cn/docusaurus-search-local/dist/client/shared/lunrLanguageZh" + ) + )}).lunrLanguageZh(lunr);` ); } if (language.length > 1) {