Skip to content

Commit

Permalink
feat: donwgrade nanoid to enable cjs support again
Browse files Browse the repository at this point in the history
  • Loading branch information
ceopaludetto committed May 30, 2024
1 parent 0905291 commit aa826a1
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 236 deletions.
10 changes: 10 additions & 0 deletions .changeset/honest-pandas-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@diacritic/runtime": patch
"@diacritic/parser": patch
"@diacritic/core": patch
"@diacritic/detector": patch
"@diacritic/react": patch
"@diacritic/utilities": patch
---

Downgrade nanoid to support CJS environments
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@shikijs/vitepress-twoslash": "^1.6.0",
"@total-typescript/tsconfig": "^1.0.4",
"@types/node": "^20.12.12",
"eslint": "^9.3.0",
"eslint": "^8.56.0",
"eslint-plugin-format": "^0.1.1",
"happy-dom": "^14.11.0",
"tsup": "^8.0.2",
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,32 @@ export const diacritic = createUnplugin<DiacriticOptions>(({
languages,
parser,
resources,
}) => {
}, meta) => {
const resourceGraph = new ResourceGraph(languages, resources);
const processedIDs = new Set<string>();

let ran = false;

return {
name: "diacritic",
enforce: "pre",
buildStart() {
for (const file of resourceGraph.allFiles()) this.addWatchFile(file);
if (meta.framework !== "esbuild")
for (const file of resourceGraph.allFiles()) this.addWatchFile(file);

if (meta.framework !== "esbuild" || !ran) {
generateTypes({ defaultLanguage, generation, languages, parser, resourceGraph });

const namespaces = resourceGraph.allNamespaces();
generateTypes({ defaultLanguage, generation, languages, namespaces, parser, resourceGraph });
ran = true;
}
},
watchChange: (id, change) => {
if (!isResource(id, resources)) return;

if (change.event === "create") resourceGraph.addFile(id, resources);
if (change.event === "delete") resourceGraph.removeFile(id, resources);

const namespaces = resourceGraph.allNamespaces();
generateTypes({ defaultLanguage, generation, languages, namespaces, parser, resourceGraph });
generateTypes({ defaultLanguage, generation, languages, parser, resourceGraph });
},
resolveId: (id) => {
if (isTranslationPath(id)) return normalizeTranslationPath(id) + ".ts";
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utilities/generator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type GenerateTypesOptions = {
defaultLanguage: string;
generation: DiacriticGenerationOptions;
languages: string[];
namespaces: string[];
parser: Parser;
resourceGraph: ResourceGraph;
};
Expand All @@ -33,11 +32,12 @@ export function generateTypes({
defaultLanguage,
generation,
languages,
namespaces,
parser,
resourceGraph,
}: GenerateTypesOptions) {
const entries = resourceGraph.allEntriesForLanguage(defaultLanguage);
const namespaces = resourceGraph.allNamespaces();

const declarations: string[] = generation?.banner ?? [];

declarations.push(
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"dependencies": {
"@diacritic/core": "workspace:*",
"@diacritic/utilities": "workspace:*",
"nanoid": "^5.0.7",
"nanoid": "^3.3.7",
"tiny-invariant": "^1.3.3"
}
}
34 changes: 17 additions & 17 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,66 +59,66 @@ export class Diacritic {
public readonly languages!: Language[];
public readonly namespaces!: Namespace[];

private current!: Language;
private modules: Record<Language, Record<Namespace, any>> = {} as any;
#current!: Language;
#modules: Record<Language, Record<Namespace, any>> = {} as any;

private listeners: Set<(language: Language) => void> = new Set();
#listeners: Set<(language: Language) => void> = new Set();

public constructor(registry: Registry, initialLanguage: Language = registry.defaultLanguage) {
this.registry = registry;
this.current = initialLanguage;
this.#current = initialLanguage;
}

public t!: Proxy;

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

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

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

public onChange = (listener: (language: Language) => void) => {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
this.#listeners.add(listener);
return () => this.#listeners.delete(listener);
};

public loadModules = async (languages: Language[], namespaces: Namespace[]) => {
const promises: Promise<void>[] = [];
for (const language of languages) {
for (const namespace of namespaces) {
if (this.modules[language] && this.modules[language]![namespace]) continue;
promises.push(this.loadModule(language, namespace));
if (this.#modules[language] && this.#modules[language]![namespace]) continue;
promises.push(this.#loadModule(language, namespace));
}
}

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

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

public needToLoadModules = (languages: Language[], namespaces: Namespace[]) => {
const missing = [];
for (const language of languages) {
for (const namespace of namespaces) {
if (this.modules[language] && this.modules[language]![namespace]) continue;
if (this.#modules[language] && this.#modules[language]![namespace]) continue;
missing.push({ language, namespace });
}
}

return missing.length > 0;
};

private async loadModule(language: Language, namespace: Namespace) {
async #loadModule(language: Language, namespace: Namespace) {
const module = await this.registry.importTranslationModule(language, namespace);

if (!this.modules[language]) this.modules[language] = {} as any;
this.modules[language]![namespace] = module;
if (!this.#modules[language]) this.#modules[language] = {} as any;
this.#modules[language]![namespace] = module;
}
}

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

0 comments on commit aa826a1

Please sign in to comment.