-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3149c2
commit feaf5f7
Showing
14 changed files
with
204 additions
and
158 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* If the unresolved_id is for the t18s config, this function will resolve it. | ||
* @type {import("./types.js").IDResolver} | ||
*/ | ||
export const resolveConfigModuleId = (unresolved_id) => { | ||
if (unresolved_id !== "t18s-internal:config") return null; | ||
return "\0t18s-internal:config"; | ||
}; | ||
|
||
/** | ||
* @type {import("./types.js").ModuleLoader} | ||
*/ | ||
export const loadConfigModule = async (resolved_id, config, Catalogue) => { | ||
if (resolved_id !== "\0t18s-internal:config") return null; | ||
return generateConfigModule(config); | ||
}; | ||
|
||
/** @type {string | null} */ | ||
let cachedCode = null; | ||
|
||
/** | ||
* @param {import("../types.js").ResolvedPluginConfig} config | ||
*/ | ||
function generateConfigModule(config) { | ||
if (cachedCode !== null) return cachedCode; | ||
let code = ""; | ||
code += `export const verbose = ${config.verbose ? "true" : "false"};\n`; | ||
code += `export const locales = ${JSON.stringify(config.locales)};\n`; | ||
code += `export const fallbackLocale = ${ | ||
config.fallbackLocale ? `"${config.fallbackLocale}"` : "undefined" | ||
};\n`; | ||
code += `export const defaultDomain = "${config.defaultDomain}";\n`; | ||
code += `export const loadingDelay = 200;\n`; | ||
cachedCode = code; | ||
return code; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { MessageCatalogue } from "../MessageCatalogue.js"; | ||
import { generateDictionaryModule } from "../codegen/dictionary.js"; | ||
|
||
/** | ||
* If the unresolved_id is for a t18s dictionary, this function will resolve it. | ||
* Dictionary modules have the format "t18s-dictionary:<locale>:<domain>" | ||
* | ||
* @type {import("./types.js").IDResolver} | ||
*/ | ||
export const resolveDictionaryModuleId = (unresolved_id) => { | ||
if (!unresolved_id.startsWith("t18s-dictionary:")) return null; | ||
|
||
const [_, locale, domain] = unresolved_id.split(":"); | ||
if (!locale || !domain) return null; | ||
|
||
const resolved_id = "\0" + unresolved_id; | ||
return resolved_id; | ||
} | ||
|
||
/** | ||
* Returns the code for the dictionary module if the resolved_id is for the main module. | ||
* @type {import("./types.js").ModuleLoader} | ||
*/ | ||
export const loadDictionaryModule = async (resolved_id, config, Catalogue) => { | ||
if (!resolved_id.startsWith("\0t18s-dictionary:")) return null; | ||
|
||
const [_, locale, domain] = resolved_id.split(":"); | ||
if (!locale || !domain) return null; | ||
|
||
const dictionary = Catalogue.getDictionary(locale, domain); | ||
|
||
return dictionary | ||
? generateDictionaryModule(dictionary) | ||
: "export default {}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { generateLoaderModule } from "../codegen/loaders.js"; | ||
|
||
/** | ||
* @type {import("./types.js").ModuleLoader} | ||
*/ | ||
export const loadLoaderModule = async (resolved_id, config, Catalogue) => { | ||
if (resolved_id !== "\0t18s-internal:loaders") return null; | ||
return generateLoaderModule(config, Catalogue); | ||
}; | ||
|
||
/** | ||
* @type {import("./types.js").IDResolver} | ||
*/ | ||
export const resolveLoaderModuleId = (unresolved_id) => { | ||
if (unresolved_id !== "t18s-internal:loaders") return null; | ||
return "\0t18s-internal:loaders"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { MessageCatalogue } from "../MessageCatalogue.js" | ||
import { ResolvedPluginConfig } from "../types.js" | ||
|
||
export type ModuleLoader = (resolved_id: string, config: ResolvedPluginConfig, catalogue: MessageCatalogue) => Promise<string | null>; | ||
export type IDResolver = (unresolved_id: string) => Promise<string | null> | string | null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { cleanUrl } from "../utils/id.js"; | ||
|
||
/** | ||
* @param {import("./types.js").IDResolver[]} resolvers | ||
* @returns {import("./types.js").IDResolver} | ||
*/ | ||
export function resolveIdSequence(resolvers) { | ||
return async (unresolved_id) => { | ||
const id = cleanUrl(unresolved_id); | ||
|
||
for (const resolver of resolvers) { | ||
const resolved = resolver(id); | ||
if (resolved) return resolved; | ||
} | ||
|
||
return null; | ||
}; | ||
} | ||
|
||
/** | ||
* @param {import("./types.js").ModuleLoader[]} loaders | ||
* @returns {import("./types.js").ModuleLoader} | ||
*/ | ||
export function loadSequence(loaders) { | ||
return async (resolved_id, config, Catalogue) => { | ||
const id = cleanUrl(resolved_id); | ||
|
||
//Attempt to load the module from all loaders | ||
const loadingPromises = loaders.map((loader) => | ||
loader(id, config, Catalogue) | ||
); | ||
const results = await Promise.allSettled(loadingPromises); | ||
|
||
//Pick the fulfilled result. There should only be one, otherwise we have a bug. | ||
for (const result of results) { | ||
if (result.status !== "fulfilled") continue; | ||
if (result.value) return result.value; | ||
} | ||
|
||
//If none of the loaders could load the module, return null. | ||
return null; | ||
}; | ||
} |
Oops, something went wrong.