Skip to content

Commit

Permalink
feat: add rspack support
Browse files Browse the repository at this point in the history
  • Loading branch information
ceopaludetto committed May 30, 2024
1 parent e3380d3 commit bf25582
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 46 deletions.
10 changes: 10 additions & 0 deletions .changeset/eight-games-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@diacritic/runtime": patch
"@diacritic/core": patch
"@diacritic/detector": patch
"@diacritic/parser": patch
"@diacritic/react": patch
"@diacritic/utilities": patch
---

Add rspack support
10 changes: 10 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
"default": "./dist/rollup.cjs"
}
},
"./rspack": {
"import": {
"types": "./dist/rspack.d.ts",
"default": "./dist/rspack.js"
},
"require": {
"types": "./dist/rspack.d.cts",
"default": "./dist/rspack.cjs"
}
},
"./vite": {
"import": {
"types": "./dist/vite.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/rspack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { diacritic } from "./index";

export default diacritic.rspack;
83 changes: 37 additions & 46 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,6 @@ export type Namespace = Registry["namespaces"][number];

function emptyFn() {}

function createProxy(language: Language, modules: Record<Language, Record<Namespace, any>>) {
const proxy: Proxy = new DeepProxy({}, ({ trapName, path, args, DEFAULT, PROXY }) => {
if (trapName === "set") throw new TypeError("Cannot set properties on a Diacritic proxy object");

if (trapName === "get") {
if (path.length === 0 && !modules[language]) console.warn(`[Diacritic] language ${language} is not loaded`);

const [namespace] = path;
if (namespace && !modules[language]?.[namespace as Namespace])
console.warn(`[Diacritic] namespace ${namespace} is not loaded in language ${language}`);

return PROXY(emptyFn);
}

if (trapName === "apply") {
const [namespace, ...rest] = path;

if (!namespace) throw new Error("Namespace is not specified");

if (!modules[language])
throw new Error(`Language ${language} is not loaded`);

if (!modules[language]![namespace as Language])
throw new Error(`Namespace ${namespace} is not loaded`);

const name = toCamelCase(rest.join("-"));
const fn: any = modules[language]![namespace as Namespace][name];

if (typeof fn !== "function")
throw new Error(`Function ${name} is not defined in namespace ${namespace}`);

return fn(proxy, ...args);
}

return DEFAULT;
});

return proxy;
}

/**
* The Diacritic class is used to manage the translations and the current language.
* It also provides a method to change the language and a method to listen to language changes.
Expand All @@ -76,17 +36,13 @@ export class Diacritic {
this.#current = initialLanguage;
}

public t!: Proxy;

public get language(): Language {
return this.#current;
};

public setLanguage = (language: Language) => {
this.#current = language;
this.#listeners.forEach(listener => listener(language));

this.t = createProxy(this.#current, this.#modules);
};

public onChange = (listener: (language: Language) => void) => {
Expand All @@ -104,9 +60,7 @@ export class Diacritic {
}

if (promises.length === 0) return;

await Promise.all(promises);
this.t = createProxy(this.#current, this.#modules);
};

public needToLoadModules = (languages: Language[], namespaces: Namespace[]) => {
Expand All @@ -127,6 +81,43 @@ export class Diacritic {
if (!this.#modules[language]) this.#modules[language] = {} as any;
this.#modules[language]![namespace] = module;
}

public t: Proxy = new DeepProxy({}, ({ trapName, path, args, DEFAULT, PROXY }) => {
if (trapName === "set") throw new TypeError("Cannot set properties on a Diacritic proxy object");

if (trapName === "get") {
if (path.length === 0 && !this.#modules[this.#current])
console.warn(`[Diacritic] language ${this.#current} is not loaded`);

const [namespace] = path;
if (namespace && !this.#modules[this.#current]?.[namespace as Namespace])
console.warn(`[Diacritic] namespace ${namespace} is not loaded in language ${this.#current}`);

return PROXY(emptyFn);
}

if (trapName === "apply") {
const [namespace, ...rest] = path;

if (!namespace) throw new Error("Namespace is not specified");

if (!this.#modules[this.#current])
throw new Error(`Language ${this.#current} is not loaded`);

if (!this.#modules[this.#current]![namespace as Language])
throw new Error(`Namespace ${namespace} is not loaded`);

const name = toCamelCase(rest.join("-"));
const fn: any = this.#modules[this.#current]![namespace as Namespace][name];

if (typeof fn !== "function")
throw new Error(`Function ${name} is not defined in namespace ${namespace}`);

return fn(this.t, ...args);
}

return DEFAULT;
});
}

export type { Proxy };
1 change: 1 addition & 0 deletions packages/runtime/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default defineConfig({
entry: ["./src/index.ts"],
format: ["cjs", "esm"],
external: ["~translations/registry"],
noExternal: ["@qiwi/deep-proxy"],
splitting: true,
clean: true,
dts: true,
Expand Down

0 comments on commit bf25582

Please sign in to comment.