@next-box/i18n 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/cjs/constants.cjs.map +1 -0
  2. package/dist/cjs/helpers/injectVariables.cjs.map +1 -0
  3. package/dist/cjs/helpers/loadConfig.cjs.map +1 -0
  4. package/dist/cjs/macros.cjs.map +1 -0
  5. package/dist/cjs/main.cjs +1 -1
  6. package/dist/cjs/main.cjs.map +1 -1
  7. package/dist/esm/constants.mjs.map +1 -0
  8. package/dist/esm/helpers/injectVariables.mjs.map +1 -0
  9. package/dist/esm/helpers/loadConfig.mjs.map +1 -0
  10. package/dist/esm/macros.mjs.map +1 -0
  11. package/dist/esm/main.mjs +1 -1
  12. package/dist/esm/main.mjs.map +1 -1
  13. package/dist/types/cjs/main.d.cts.map +1 -1
  14. package/dist/types/esm/main.d.ts.map +1 -1
  15. package/dist/types/tsconfig.build.tsbuildinfo +1 -1
  16. package/package.json +2 -2
  17. package/rollup.config.mjs +1 -9
  18. package/src/main.ts +12 -11
  19. package/dist/cjs/client/helpers/injectVariables.cjs.map +0 -1
  20. package/dist/cjs/macros/constants.cjs.map +0 -1
  21. package/dist/cjs/macros/helpers/loadConfig.cjs.map +0 -1
  22. package/dist/cjs/macros/macros.cjs.map +0 -1
  23. package/dist/esm/client/helpers/injectVariables.mjs.map +0 -1
  24. package/dist/esm/macros/constants.mjs.map +0 -1
  25. package/dist/esm/macros/helpers/loadConfig.mjs.map +0 -1
  26. package/dist/esm/macros/macros.mjs.map +0 -1
  27. /package/dist/cjs/{client/client.cjs → client.cjs} +0 -0
  28. /package/dist/cjs/{client/client.cjs.map → client.cjs.map} +0 -0
  29. /package/dist/cjs/{client/components → components}/I18nProvider.cjs +0 -0
  30. /package/dist/cjs/{client/components → components}/I18nProvider.cjs.map +0 -0
  31. /package/dist/cjs/{client/components → components}/Md.cjs +0 -0
  32. /package/dist/cjs/{client/components → components}/Md.cjs.map +0 -0
  33. /package/dist/cjs/{macros/constants.cjs → constants.cjs} +0 -0
  34. /package/dist/cjs/{client/helpers → helpers}/injectVariables.cjs +0 -0
  35. /package/dist/cjs/{macros/helpers → helpers}/loadConfig.cjs +0 -0
  36. /package/dist/cjs/{macros/macros.cjs → macros.cjs} +0 -0
  37. /package/dist/esm/{client/client.mjs → client.mjs} +0 -0
  38. /package/dist/esm/{client/client.mjs.map → client.mjs.map} +0 -0
  39. /package/dist/esm/{client/components → components}/I18nProvider.mjs +0 -0
  40. /package/dist/esm/{client/components → components}/I18nProvider.mjs.map +0 -0
  41. /package/dist/esm/{client/components → components}/Md.mjs +0 -0
  42. /package/dist/esm/{client/components → components}/Md.mjs.map +0 -0
  43. /package/dist/esm/{macros/constants.mjs → constants.mjs} +0 -0
  44. /package/dist/esm/{client/helpers → helpers}/injectVariables.mjs +0 -0
  45. /package/dist/esm/{macros/helpers → helpers}/loadConfig.mjs +0 -0
  46. /package/dist/esm/{macros/macros.mjs → macros.mjs} +0 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@next-box/i18n",
3
3
  "description": "A type-safe content resolver with content path auto-complete and value preview.",
4
- "version": "0.1.2",
4
+ "version": "0.1.4",
5
5
  "author": "miami-man",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/badbatch/next-box",
@@ -54,7 +54,7 @@
54
54
  "callsites": "^4.2.0",
55
55
  "markdown-to-jsx": "^9.10.4",
56
56
  "type-fest": "^5.10.0",
57
- "zcb": "^0.3.10"
57
+ "zcb": "^0.3.13"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "react": "<20",
package/rollup.config.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import config from '../../rollup.config.mjs';
2
2
 
3
- const { input, output, ...otherConfig } = config;
3
+ const { input, ...otherConfig } = config;
4
4
 
5
5
  // rollup requires default export
6
6
  // eslint-disable-next-line import-x/no-default-export
@@ -9,17 +9,9 @@ export default [
9
9
  {
10
10
  ...otherConfig,
11
11
  input: input.replace('index', 'client'),
12
- output: {
13
- ...output,
14
- dir: `${output.dir}/client`,
15
- },
16
12
  },
17
13
  {
18
14
  ...otherConfig,
19
15
  input: input.replace('index', 'macros'),
20
- output: {
21
- ...output,
22
- dir: `${output.dir}/macros`,
23
- },
24
16
  },
25
17
  ];
package/src/main.ts CHANGED
@@ -1,22 +1,23 @@
1
- import { type Get } from 'type-fest';
2
- import { type AnyRecord, type Path, createConfigReader } from 'zcb';
1
+ import { type AnyRecord, createConfigReader } from 'zcb';
3
2
  import { type CreateI18nResult } from '#types.ts';
4
3
 
5
4
  export const createI18n = <U extends object>(i18n: U): CreateI18nResult<U> => {
6
5
  const reader = createConfigReader<U>(i18n);
7
6
 
7
+ const tt: CreateI18nResult<U>['tt'] = (path, ...variables) => {
8
+ const result = reader.read(path, ...variables);
9
+
10
+ if (result === undefined) {
11
+ throw new Error(`Path "${String(path)}" did not resolve to a value`);
12
+ }
13
+
14
+ return result;
15
+ };
16
+
8
17
  return {
9
18
  // @ts-expect-error intentionally ignoring the type checking here as
10
19
  // function is supposed to be untyped.
11
20
  t: (key: string, variables?: AnyRecord): string | undefined => reader.read(key, variables),
12
- tt: <P extends Path<U>>(path: P, variables?: AnyRecord): Get<U, P> => {
13
- const result = reader.read(path, variables);
14
-
15
- if (result === undefined) {
16
- throw new Error(`Path "${String(path)}" did not resolve to a value`);
17
- }
18
-
19
- return result;
20
- },
21
+ tt,
21
22
  };
22
23
  };
@@ -1 +0,0 @@
1
- {"version":3,"file":"injectVariables.cjs","sources":["../../../../src/helpers/injectVariables.ts"],"sourcesContent":["const extractTemplateVariables = /{{[^\\s{}]+}}/g;\nconst extractVarName = /{{ *([^ }]*) *}}/;\n\nexport const injectVariables = (markdown: string, variables?: Record<string, string | number>): string =>\n variables\n ? markdown.replaceAll(extractTemplateVariables, (match: string) => {\n const varName = match.replace(extractVarName, '$1');\n const replacementValue = variables[varName];\n\n if (replacementValue !== undefined) {\n return String(replacementValue);\n }\n\n throw new Error(`Markdown contains un-replaced variable '${varName}'`);\n })\n : markdown;\n"],"names":["extractTemplateVariables","extractVarName","markdown","variables","replaceAll","match","varName","replace","replacementValue","undefined","String","Error"],"mappings":"aAAA,MAAMA,EAA2B,gBAC3BC,EAAiB,2CAEQ,CAACC,EAAkBC,IAChDA,EACID,EAASE,WAAWJ,EAA2BK,IAC7C,MAAMC,EAAUD,EAAME,QAAQN,EAAgB,MACxCO,EAAmBL,EAAUG,GAEnC,QAAyBG,IAArBD,EACF,OAAOE,OAAOF,GAGhB,MAAM,IAAIG,MAAM,2CAA2CL,QAE7DJ"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.cjs","sources":["../../../src/constants.ts"],"sourcesContent":["export const CONFIG_FILENAME = '.i18n-macro.config.json';\n"],"names":[],"mappings":"qCAA+B"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"loadConfig.cjs","sources":["../../../../src/helpers/loadConfig.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { CONFIG_FILENAME } from '#constants.ts';\nimport { type MacrosConfig } from '#types.ts';\n\nconst cwd = process.cwd();\n\nexport const loadConfig = (): Partial<MacrosConfig> => {\n let config: Partial<MacrosConfig> = {};\n\n try {\n // JSON.parse returns an any time\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n config = JSON.parse(readFileSync(resolve(cwd, CONFIG_FILENAME), { encoding: 'utf8' })) as Partial<MacrosConfig>;\n } catch {\n // no catch\n }\n\n return config;\n};\n"],"names":["cwd","process","config","JSON","parse","readFileSync","resolve","CONFIG_FILENAME","encoding"],"mappings":"2FAKA,MAAMA,EAAMC,QAAQD,yBAEM,KACxB,IAAIE,EAAgC,CAAA,EAEpC,IAGEA,EAASC,KAAKC,MAAMC,EAAAA,aAAaC,EAAAA,QAAQN,EAAKO,EAAAA,iBAAkB,CAAEC,SAAU,SAC9E,CAAE,MAEF,CAEA,OAAON"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"macros.cjs","sources":["../../../src/macros.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { loadConfig } from '#helpers/loadConfig.ts';\n\nconst { NEXT_PUBLIC_LANGUAGE_CODE } = process.env;\n\nexport const md = <P extends string>(path: P): P => {\n if (!path) {\n throw new Error('md expected a path, but received none');\n }\n\n let dir = process.cwd();\n let { markdownDir } = loadConfig();\n\n if (markdownDir) {\n if (NEXT_PUBLIC_LANGUAGE_CODE) {\n markdownDir = markdownDir.replaceAll('{{NEXT_PUBLIC_LANGUAGE_CODE}}', () => NEXT_PUBLIC_LANGUAGE_CODE);\n }\n\n dir = resolve(dir, markdownDir);\n }\n\n const fullPath = resolve(dir, path);\n // @ts-expect-error `md` function runs and build time and its return value\n // replaces the `md` invocation. We are forcing the return type to be the\n // input type to aid type inference when using `createI18n` and its\n // `tt` content resolvers.\n return readFileSync(fullPath, { encoding: 'utf8' }).trim();\n};\n"],"names":["NEXT_PUBLIC_LANGUAGE_CODE","process","env","path","Error","dir","cwd","markdownDir","loadConfig","replaceAll","resolve","fullPath","readFileSync","encoding","trim"],"mappings":"mGAIA,MAAMA,0BAAEA,GAA8BC,QAAQC,eAETC,IACnC,IAAKA,EACH,MAAM,IAAIC,MAAM,yCAGlB,IAAIC,EAAMJ,QAAQK,OACdC,YAAEA,GAAgBC,eAElBD,IACEP,IACFO,EAAcA,EAAYE,WAAW,gCAAiC,IAAMT,IAG9EK,EAAMK,EAAAA,QAAQL,EAAKE,IAGrB,MAAMI,EAAWD,EAAAA,QAAQL,EAAKF,GAK9B,OAAOS,EAAAA,aAAaD,EAAU,CAAEE,SAAU,SAAUC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"injectVariables.mjs","sources":["../../../../src/helpers/injectVariables.ts"],"sourcesContent":["const extractTemplateVariables = /{{[^\\s{}]+}}/g;\nconst extractVarName = /{{ *([^ }]*) *}}/;\n\nexport const injectVariables = (markdown: string, variables?: Record<string, string | number>): string =>\n variables\n ? markdown.replaceAll(extractTemplateVariables, (match: string) => {\n const varName = match.replace(extractVarName, '$1');\n const replacementValue = variables[varName];\n\n if (replacementValue !== undefined) {\n return String(replacementValue);\n }\n\n throw new Error(`Markdown contains un-replaced variable '${varName}'`);\n })\n : markdown;\n"],"names":["extractTemplateVariables","extractVarName","injectVariables","markdown","variables","replaceAll","match","varName","replace","replacementValue","undefined","String","Error"],"mappings":"AAAA,MAAMA,EAA2B,gBAC3BC,EAAiB,mBAEVC,EAAkB,CAACC,EAAkBC,IAChDA,EACID,EAASE,WAAWL,EAA2BM,IAC7C,MAAMC,EAAUD,EAAME,QAAQP,EAAgB,MACxCQ,EAAmBL,EAAUG,GAEnC,QAAyBG,IAArBD,EACF,OAAOE,OAAOF,GAGhB,MAAM,IAAIG,MAAM,2CAA2CL,QAE7DJ"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.mjs","sources":["../../../src/constants.ts"],"sourcesContent":["export const CONFIG_FILENAME = '.i18n-macro.config.json';\n"],"names":["CONFIG_FILENAME"],"mappings":"AAAO,MAAMA,EAAkB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"loadConfig.mjs","sources":["../../../../src/helpers/loadConfig.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { CONFIG_FILENAME } from '#constants.ts';\nimport { type MacrosConfig } from '#types.ts';\n\nconst cwd = process.cwd();\n\nexport const loadConfig = (): Partial<MacrosConfig> => {\n let config: Partial<MacrosConfig> = {};\n\n try {\n // JSON.parse returns an any time\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n config = JSON.parse(readFileSync(resolve(cwd, CONFIG_FILENAME), { encoding: 'utf8' })) as Partial<MacrosConfig>;\n } catch {\n // no catch\n }\n\n return config;\n};\n"],"names":["cwd","process","loadConfig","config","JSON","parse","readFileSync","resolve","CONFIG_FILENAME","encoding"],"mappings":"8HAKA,MAAMA,EAAMC,QAAQD,MAEPE,EAAa,KACxB,IAAIC,EAAgC,CAAA,EAEpC,IAGEA,EAASC,KAAKC,MAAMC,EAAaC,EAAQP,EAAKQ,GAAkB,CAAEC,SAAU,SAC9E,CAAE,MAEF,CAEA,OAAON"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"macros.mjs","sources":["../../../src/macros.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { loadConfig } from '#helpers/loadConfig.ts';\n\nconst { NEXT_PUBLIC_LANGUAGE_CODE } = process.env;\n\nexport const md = <P extends string>(path: P): P => {\n if (!path) {\n throw new Error('md expected a path, but received none');\n }\n\n let dir = process.cwd();\n let { markdownDir } = loadConfig();\n\n if (markdownDir) {\n if (NEXT_PUBLIC_LANGUAGE_CODE) {\n markdownDir = markdownDir.replaceAll('{{NEXT_PUBLIC_LANGUAGE_CODE}}', () => NEXT_PUBLIC_LANGUAGE_CODE);\n }\n\n dir = resolve(dir, markdownDir);\n }\n\n const fullPath = resolve(dir, path);\n // @ts-expect-error `md` function runs and build time and its return value\n // replaces the `md` invocation. We are forcing the return type to be the\n // input type to aid type inference when using `createI18n` and its\n // `tt` content resolvers.\n return readFileSync(fullPath, { encoding: 'utf8' }).trim();\n};\n"],"names":["NEXT_PUBLIC_LANGUAGE_CODE","process","env","md","path","Error","dir","cwd","markdownDir","loadConfig","replaceAll","resolve","fullPath","readFileSync","encoding","trim"],"mappings":"iIAIA,MAAMA,0BAAEA,GAA8BC,QAAQC,IAEjCC,EAAwBC,IACnC,IAAKA,EACH,MAAM,IAAIC,MAAM,yCAGlB,IAAIC,EAAML,QAAQM,OACdC,YAAEA,GAAgBC,IAElBD,IACER,IACFQ,EAAcA,EAAYE,WAAW,gCAAiC,IAAMV,IAG9EM,EAAMK,EAAQL,EAAKE,IAGrB,MAAMI,EAAWD,EAAQL,EAAKF,GAK9B,OAAOS,EAAaD,EAAU,CAAEE,SAAU,SAAUC"}
File without changes
File without changes
File without changes
File without changes