Skip to content

Commit

Permalink
fix: fix Uncaught ReferenceError: importScripts is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Nov 27, 2024
1 parent 4376e13 commit 139b82b
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 373 deletions.
2 changes: 1 addition & 1 deletion docusaurus-search-local/src/client/theme/searchByWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface RemoteWorker {
let remoteWorkerPromise: Promise<RemoteWorker> | undefined;

function getRemoteWorker() {
if (!remoteWorkerPromise) {
if (process.env.NODE_ENV === "production" && !remoteWorkerPromise) {
remoteWorkerPromise = (async () => {
const Remote = Comlink.wrap(
new Worker(new URL("./worker.js", import.meta.url))
Expand Down
3 changes: 1 addition & 2 deletions docusaurus-search-local/src/client/theme/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Comlink from "comlink";
import lunr from "lunr";
import { searchIndexUrl, searchResultLimits } from "../utils/proxiedGenerated";
import { searchIndexUrl, searchResultLimits, language } from "../utils/proxiedGeneratedConstants";
import { tokenize } from "../utils/tokenize";
import { smartQueries } from "../utils/smartQueries";
import {
Expand All @@ -13,7 +13,6 @@ import {
} from "../../shared/interfaces";
import { sortSearchResults } from "../utils/sortSearchResults";
import { processTreeStatusOfSearchResults } from "../utils/processTreeStatusOfSearchResults";
import { language } from "../utils/proxiedGenerated";

interface SerializedIndex {
documents: SearchDocument[];
Expand Down
129 changes: 0 additions & 129 deletions docusaurus-search-local/src/client/utils/SearchSourceFactory.spec.ts

This file was deleted.

84 changes: 0 additions & 84 deletions docusaurus-search-local/src/client/utils/SearchSourceFactory.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
export let language = ["en", "zh"];
export let removeDefaultStopWordFilter = false;
export let removeDefaultStemmer = false;
export const searchIndexUrl = "search-index{dir}.json?_=abc";
export const searchResultLimits = 8;
export const removeDefaultStemmer = false;
export const searchResultContextMaxLength = 50;
export const explicitSearchResultPath = false;
export const docsPluginIdForPreferredVersion = undefined;
export const indexDocs = true;

export function __setLanguage(value: string[]): void {
language = value;
}

export function __setRemoveDefaultStopWordFilter(value: boolean): void {
removeDefaultStopWordFilter = value;
}

export function __setRemoveDefaultStemmer(value: boolean): void {
removeDefaultStemmer = value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export let language = ["en", "zh"];
export let removeDefaultStopWordFilter = false;
export const searchIndexUrl = "search-index{dir}.json?_=abc";
export const searchResultLimits = 8;

export function __setLanguage(value: string[]): void {
language = value;
}

export function __setRemoveDefaultStopWordFilter(value: boolean): void {
removeDefaultStopWordFilter = value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto generated while building.
export * from "@generated/@easyops-cn/docusaurus-search-local/default/generated-constants.js";
9 changes: 3 additions & 6 deletions docusaurus-search-local/src/client/utils/smartQueries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { smartQueries } from "./smartQueries";
import {
__setLanguage,
__setRemoveDefaultStopWordFilter,
__setRemoveDefaultStemmer,
} from "./proxiedGenerated";
} from "./proxiedGeneratedConstants";
import { SmartQuery } from "../../shared/interfaces";

jest.mock("./proxiedGeneratedConstants");

// eslint-disable-next-line @typescript-eslint/no-var-requires
require("lunr-languages/lunr.stemmer.support")(lunr);
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -16,8 +17,6 @@ require("lunr-languages/lunr.multi")(lunr);

(lunr as any).fake = {};

jest.mock("./proxiedGenerated");

const zhDictionary = ["研究生", "研究", "生命", "科学", "生命科学"];

interface TestQuery {
Expand All @@ -29,7 +28,6 @@ describe("smartQueries", () => {
beforeEach(() => {
__setLanguage(["en", "zh"]);
__setRemoveDefaultStopWordFilter(false);
__setRemoveDefaultStemmer(false);
});

test.each<[string[], TestQuery[]]>([
Expand Down Expand Up @@ -238,7 +236,6 @@ describe("smartQueries with no stop words filter", () => {
beforeEach(() => {
__setLanguage(["en", "fake"]);
__setRemoveDefaultStopWordFilter(true);
__setRemoveDefaultStemmer(false);
});

test.each<[string[], TestQuery[]]>([
Expand Down
2 changes: 1 addition & 1 deletion docusaurus-search-local/src/client/utils/smartQueries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import lunr from "lunr";
import { SmartQuery, SmartTerm } from "../../shared/interfaces";
import { smartTerms } from "./smartTerms";
import { language, removeDefaultStopWordFilter } from "./proxiedGenerated";
import { language, removeDefaultStopWordFilter } from "./proxiedGeneratedConstants";

/**
* Get all possible queries for a list of tokens consists of words mixed English and Chinese,
Expand Down
12 changes: 7 additions & 5 deletions docusaurus-search-local/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ declare module "@easyops-cn/autocomplete.js" {
}

declare module "*/generated.js" {
export const language: string[];
export const removeDefaultStopWordFilter: string[];
export const removeDefaultStemmer: string[];
export class Mark {
constructor(root: HTMLElement);
mark: (terms: string[], options?: Record<string, unknown>) => void;
unmark: () => void;
}
export const searchIndexUrl: string;
export const searchResultLimits: number;
export const searchResultContextMaxLength: number;
export const explicitSearchResultPath: boolean;
export const searchBarShortcut: boolean;
Expand All @@ -27,10 +23,16 @@ declare module "*/generated.js" {
export const hideSearchBarWithNoSearchContext: boolean;
export const useAllContextsWithNoSearchContext: boolean;
export const forceIgnoreNoIndex: boolean;
}

declare module "*/generated-constants.js" {
export const removeDefaultStopWordFilter: string[];
export const language: string[];
export const searchIndexUrl: string;
export const searchResultLimits: number;
// These below are for mocking only.
export const __setLanguage: (value: string[]) => void;
export const __setRemoveDefaultStopWordFilter: (value: boolean) => void;
export const __setRemoveDefaultStemmer: (value: boolean) => void;
}

declare module "@docusaurus/Head";
Loading

0 comments on commit 139b82b

Please sign in to comment.