@inlang/paraglide-js 2.0.0-beta.15 → 2.0.0-beta.17

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 (120) hide show
  1. package/dist/bundler-plugins/unplugin.d.ts.map +1 -1
  2. package/dist/bundler-plugins/unplugin.js +10 -3
  3. package/dist/cli/steps/run-compiler.js +1 -1
  4. package/dist/compiler/compile-bundle.d.ts.map +1 -1
  5. package/dist/compiler/compile-bundle.js +9 -4
  6. package/dist/compiler/compile-bundle.test.js +11 -0
  7. package/dist/compiler/compile-message.d.ts +0 -3
  8. package/dist/compiler/compile-message.d.ts.map +1 -1
  9. package/dist/compiler/compile-message.js +14 -9
  10. package/dist/compiler/compile-message.test.js +25 -3
  11. package/dist/compiler/compile-project.d.ts.map +1 -1
  12. package/dist/compiler/compile-project.js +0 -3
  13. package/dist/compiler/compile-project.test.js +59 -66
  14. package/dist/compiler/compile.d.ts +41 -2
  15. package/dist/compiler/compile.d.ts.map +1 -1
  16. package/dist/compiler/compile.js +12 -4
  17. package/dist/compiler/compile.test.js +29 -7
  18. package/dist/compiler/jsdoc-types.d.ts +9 -3
  19. package/dist/compiler/jsdoc-types.d.ts.map +1 -1
  20. package/dist/compiler/jsdoc-types.js +15 -11
  21. package/dist/compiler/output-structure/locale-modules.d.ts.map +1 -1
  22. package/dist/compiler/output-structure/locale-modules.js +5 -8
  23. package/dist/compiler/runtime/assert-is-locale.js +1 -1
  24. package/dist/compiler/runtime/create-runtime.d.ts +3 -4
  25. package/dist/compiler/runtime/create-runtime.d.ts.map +1 -1
  26. package/dist/compiler/runtime/create-runtime.js +28 -11
  27. package/dist/compiler/runtime/de-localize-path.d.ts +2 -2
  28. package/dist/compiler/runtime/de-localize-path.d.ts.map +1 -1
  29. package/dist/compiler/runtime/de-localize-path.js +25 -6
  30. package/dist/compiler/runtime/de-localize-path.test.js +83 -21
  31. package/dist/compiler/runtime/de-localized-path.d.ts +27 -0
  32. package/dist/compiler/runtime/de-localized-path.d.ts.map +1 -0
  33. package/dist/compiler/runtime/de-localized-path.js +32 -0
  34. package/dist/compiler/runtime/de-localized-path.test.d.ts +2 -0
  35. package/dist/compiler/runtime/de-localized-path.test.d.ts.map +1 -0
  36. package/dist/compiler/runtime/de-localized-path.test.js +48 -0
  37. package/dist/compiler/runtime/detect-locale-from-request.d.ts +22 -0
  38. package/dist/compiler/runtime/detect-locale-from-request.d.ts.map +1 -0
  39. package/dist/compiler/runtime/detect-locale-from-request.js +44 -0
  40. package/dist/compiler/runtime/e2e.test.d.ts +2 -0
  41. package/dist/compiler/runtime/e2e.test.d.ts.map +1 -0
  42. package/dist/compiler/runtime/e2e.test.js +33 -0
  43. package/dist/compiler/runtime/extract-locale-from-cookie.js +1 -1
  44. package/dist/compiler/runtime/extract-locale-from-pathname.d.ts.map +1 -1
  45. package/dist/compiler/runtime/extract-locale-from-pathname.js +19 -3
  46. package/dist/compiler/runtime/extract-locale-from-pathname.test.js +83 -0
  47. package/dist/compiler/runtime/extract-locale-from-request.d.ts.map +1 -1
  48. package/dist/compiler/runtime/extract-locale-from-request.js +2 -4
  49. package/dist/compiler/runtime/extract-locale-from-request.test.js +5 -0
  50. package/dist/compiler/runtime/get-locale-from-path.d.ts +13 -0
  51. package/dist/compiler/runtime/get-locale-from-path.d.ts.map +1 -0
  52. package/dist/compiler/runtime/get-locale-from-path.js +17 -0
  53. package/dist/compiler/runtime/get-locale-from-path.test.d.ts +2 -0
  54. package/dist/compiler/runtime/get-locale-from-path.test.d.ts.map +1 -0
  55. package/dist/compiler/runtime/get-locale-from-path.test.js +22 -0
  56. package/dist/compiler/runtime/get-locale.d.ts.map +1 -1
  57. package/dist/compiler/runtime/get-locale.js +7 -5
  58. package/dist/compiler/runtime/get-locale.test.js +2 -2
  59. package/dist/compiler/runtime/is-locale.js +1 -1
  60. package/dist/compiler/runtime/jsdoc-runtime.d.ts +2 -0
  61. package/dist/compiler/runtime/jsdoc-runtime.d.ts.map +1 -0
  62. package/dist/compiler/runtime/jsdoc-runtime.js +131 -0
  63. package/dist/compiler/runtime/locale-in-path.d.ts +13 -0
  64. package/dist/compiler/runtime/locale-in-path.d.ts.map +1 -0
  65. package/dist/compiler/runtime/locale-in-path.js +17 -0
  66. package/dist/compiler/runtime/locale-in-path.test.d.ts +2 -0
  67. package/dist/compiler/runtime/locale-in-path.test.d.ts.map +1 -0
  68. package/dist/compiler/runtime/locale-in-path.test.js +22 -0
  69. package/dist/compiler/runtime/localize-path.d.ts +2 -2
  70. package/dist/compiler/runtime/localize-path.d.ts.map +1 -1
  71. package/dist/compiler/runtime/localize-path.js +61 -13
  72. package/dist/compiler/runtime/localize-path.test.js +192 -20
  73. package/dist/compiler/runtime/localized-path.d.ts +32 -0
  74. package/dist/compiler/runtime/localized-path.d.ts.map +1 -0
  75. package/dist/compiler/runtime/localized-path.js +44 -0
  76. package/dist/compiler/runtime/localized-path.test.d.ts +2 -0
  77. package/dist/compiler/runtime/localized-path.test.d.ts.map +1 -0
  78. package/dist/compiler/runtime/localized-path.test.js +42 -0
  79. package/dist/compiler/runtime/mock-runtime.d.ts +6 -0
  80. package/dist/compiler/runtime/mock-runtime.d.ts.map +1 -0
  81. package/dist/compiler/runtime/mock-runtime.js +31 -0
  82. package/dist/compiler/runtime/path-to-regexp.d.ts +2 -0
  83. package/dist/compiler/runtime/path-to-regexp.d.ts.map +1 -0
  84. package/dist/compiler/runtime/path-to-regexp.js +1 -0
  85. package/dist/compiler/runtime/pathname-pattern.d.ts +30 -0
  86. package/dist/compiler/runtime/pathname-pattern.d.ts.map +1 -0
  87. package/dist/compiler/runtime/pathname-pattern.js +99 -0
  88. package/dist/compiler/runtime/pathname-pattern.test.d.ts +2 -0
  89. package/dist/compiler/runtime/pathname-pattern.test.d.ts.map +1 -0
  90. package/dist/compiler/runtime/pathname-pattern.test.js +103 -0
  91. package/dist/compiler/runtime/set-locale.d.ts.map +1 -1
  92. package/dist/compiler/runtime/set-locale.js +5 -5
  93. package/dist/compiler/runtime/set-locale.test.js +1 -1
  94. package/dist/compiler/runtime/ts-runtime.d.ts +2 -0
  95. package/dist/compiler/runtime/ts-runtime.d.ts.map +1 -0
  96. package/dist/compiler/runtime/ts-runtime.js +119 -0
  97. package/dist/compiler/runtime/type.d.ts +2 -2
  98. package/dist/compiler/runtime/type.d.ts.map +1 -1
  99. package/dist/compiler/runtime/type.test.js +48 -11
  100. package/dist/compiler/runtime/variables.d.ts +46 -0
  101. package/dist/compiler/runtime/variables.d.ts.map +1 -0
  102. package/dist/compiler/runtime/variables.js +45 -0
  103. package/dist/compiler/strategy.d.ts +13 -0
  104. package/dist/compiler/strategy.d.ts.map +1 -0
  105. package/dist/compiler/strategy.js +12 -0
  106. package/dist/compiler/strategy.test.d.ts +2 -0
  107. package/dist/compiler/strategy.test.d.ts.map +1 -0
  108. package/dist/compiler/strategy.test.js +9 -0
  109. package/dist/path-to-regexp/index.d.ts +12 -0
  110. package/dist/path-to-regexp/index.d.ts.map +1 -0
  111. package/dist/path-to-regexp/index.js +11 -0
  112. package/dist/services/env-variables/index.js +1 -1
  113. package/dist/services/file-handling/write-output.d.ts +6 -1
  114. package/dist/services/file-handling/write-output.d.ts.map +1 -1
  115. package/dist/services/file-handling/write-output.js +33 -19
  116. package/dist/services/file-handling/write-output.test.js +60 -11
  117. package/dist/urlpattern-polyfill/index.d.ts +2 -0
  118. package/dist/urlpattern-polyfill/index.d.ts.map +1 -0
  119. package/dist/urlpattern-polyfill/index.js +1 -0
  120. package/package.json +12 -8
@@ -0,0 +1,103 @@
1
+ import { expect, test } from "vitest";
2
+ import { createRuntimeForTesting } from "./create-runtime.js";
3
+ import { match as pathToRegexp } from "path-to-regexp";
4
+ import { matchPathnamePattern2 } from "./pathname-pattern.js";
5
+ test("1234", async () => {
6
+ expect(matchPathnamePattern2("/*", "/en")).toBeDefined();
7
+ expect(matchPathnamePattern2("/*", "/en/hello-world")).toBeDefined();
8
+ expect(matchPathnamePattern2("/*", "/de/hello-world")).toBeDefined();
9
+ expect(matchPathnamePattern2("/de*", "/de/hello-world")).toBeDefined();
10
+ expect(matchPathnamePattern2("/de*", "/de")).toBeDefined();
11
+ });
12
+ test("static pattern", async () => {
13
+ const { matchPathnamePattern } = await createRuntimeForTesting({
14
+ baseLocale: "en",
15
+ locales: ["en"],
16
+ });
17
+ // static pattern
18
+ expect(pathToRegexp("/about")("/about")).toMatchObject(matchPathnamePattern("/about", "/about"));
19
+ // static pattern with trailing slash
20
+ expect(pathToRegexp("/about")("/about/")).toMatchObject(matchPathnamePattern("/about", "/about/"));
21
+ // static pattern with suffix
22
+ expect(pathToRegexp("/about/suffix")("/about/suffix")).toMatchObject(matchPathnamePattern("/about/suffix", "/about/suffix"));
23
+ // both don't match because suffix has leading suffix (peter)
24
+ expect([
25
+ matchPathnamePattern("/about/suffix", "/about/suffix/peter"),
26
+ pathToRegexp("/about/suffix")("/about/suffix/peter"),
27
+ ]).toStrictEqual([undefined, false]);
28
+ });
29
+ test("wildcard pattern", async () => {
30
+ const { matchPathnamePattern } = await createRuntimeForTesting({
31
+ baseLocale: "en",
32
+ locales: ["en"],
33
+ });
34
+ // wildcard pattern
35
+ expect(pathToRegexp("/*path")("/about")).toMatchObject(matchPathnamePattern("/*path", "/about"));
36
+ // wildcard with suffix
37
+ expect(pathToRegexp("/*path/suffix")("/about/xyz/suffix")).toMatchObject(matchPathnamePattern("/*path/suffix", "/about/xyz/suffix"));
38
+ // both don't match because suffix has leading suffix (peter)
39
+ expect([
40
+ matchPathnamePattern("/*path/suffix", "/about/xyz/suffix/peter"),
41
+ pathToRegexp("/*path/suffix")("/about/xyz/suffix/peter"),
42
+ ]).toStrictEqual([undefined, false]);
43
+ });
44
+ test("parameter pattern", async () => {
45
+ const { matchPathnamePattern } = await createRuntimeForTesting({
46
+ baseLocale: "en",
47
+ locales: ["en"],
48
+ });
49
+ // parameter pattern
50
+ expect(pathToRegexp("/:post")("/123")).toMatchObject(matchPathnamePattern("/:post", "/123"));
51
+ // parameter pattern with trailing slash
52
+ expect(pathToRegexp("/:post")("/123/")).toMatchObject(matchPathnamePattern("/:post", "/123/"));
53
+ // parameter with suffix
54
+ expect(matchPathnamePattern("/:post/suffix", "/123/suffix")).toMatchObject(matchPathnamePattern("/:post", "/123"));
55
+ });
56
+ test("optional parameter pattern", async () => {
57
+ const { matchPathnamePattern } = await createRuntimeForTesting({
58
+ baseLocale: "en",
59
+ locales: ["en"],
60
+ });
61
+ expect(pathToRegexp("/users{/:id}/delete")("/users/delete")).toMatchObject(matchPathnamePattern("/users{/:id}/delete", "/users/delete"));
62
+ expect(pathToRegexp("/users{/:id}/delete")("/users/123/delete")).toMatchObject(matchPathnamePattern("/users{/:id}/delete", "/users/123/delete"));
63
+ });
64
+ test("wildcard on root with prefixed slash", async () => {
65
+ const { matchPathnamePattern } = await createRuntimeForTesting({
66
+ baseLocale: "en",
67
+ locales: ["en"],
68
+ });
69
+ expect([
70
+ matchPathnamePattern("/*path", "/"),
71
+ pathToRegexp("/*path")("/"),
72
+ ]).toStrictEqual([undefined, false]);
73
+ });
74
+ test("wildcard on root with no slash", async () => {
75
+ const { matchPathnamePattern } = await createRuntimeForTesting({
76
+ baseLocale: "en",
77
+ locales: ["en"],
78
+ });
79
+ expect(pathToRegexp("*path")("/")).toMatchObject(matchPathnamePattern("*path", "/"));
80
+ });
81
+ test("optional wildcard", async () => {
82
+ const { matchPathnamePattern } = await createRuntimeForTesting({
83
+ baseLocale: "en",
84
+ locales: ["en", "de"],
85
+ });
86
+ expect(pathToRegexp("/{*path}")("/")).toMatchObject(matchPathnamePattern("/{*path}", "/"));
87
+ expect(pathToRegexp("/de{/*path}")("/de")).toMatchObject(matchPathnamePattern("/de{/*path}", "/de"));
88
+ expect(pathToRegexp("/de{/*path}")("/de/etwas")).toMatchObject(matchPathnamePattern("/de{/*path}", "/de/etwas"));
89
+ });
90
+ test("generating paths from patterns", async () => {
91
+ const { compilePathnamePattern } = await createRuntimeForTesting({
92
+ baseLocale: "en",
93
+ locales: ["en"],
94
+ });
95
+ expect(compilePathnamePattern("/:post", { post: "123" })).toBe("/123");
96
+ expect(compilePathnamePattern("/:category/:id", { category: "books", id: "42" })).toBe("/books/42");
97
+ expect(compilePathnamePattern("/users{/:id}/delete", { id: "123" })).toBe("/users/123/delete");
98
+ expect(compilePathnamePattern("/users{/:id}/delete", {})).toBe("/users/delete");
99
+ expect(compilePathnamePattern("/*path/suffix", { path: ["about", "xyz"] })).toBe("/about/xyz/suffix");
100
+ expect(compilePathnamePattern("/*path", { path: ["home"] })).toBe("/home");
101
+ expect(() => compilePathnamePattern("/:post", {})).toThrow('Missing value for parameter ":post"');
102
+ expect(() => compilePathnamePattern("/*path", { path: "home" })).toThrow('Wildcard parameter "*path" must be an array');
103
+ });
@@ -1 +1 @@
1
- {"version":3,"file":"set-locale.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/set-locale.js"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,sBAFU,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAoDnC"}
1
+ {"version":3,"file":"set-locale.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/set-locale.js"],"names":[],"mappings":"AASA;;;;;;;GAOG;AACH,sBAFU,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAuDnC"}
@@ -1,6 +1,5 @@
1
- import { cookieName } from "./cookie-name.js";
1
+ import { cookieName, strategy, TREE_SHAKE_COOKIE_STRATEGY_USED, TREE_SHAKE_PATHNAME_STRATEGY_USED, TREE_SHAKE_GLOBAL_VARIABLE_STRATEGY_USED, } from "./variables.js";
2
2
  import { localizePath } from "./localize-path.js";
3
- import { strategy } from "./strategy.js";
4
3
  /**
5
4
  * Set the locale.
6
5
  *
@@ -12,13 +11,14 @@ import { strategy } from "./strategy.js";
12
11
  export let setLocale = (newLocale) => {
13
12
  let localeHasBeenSet = false;
14
13
  for (const strat of strategy) {
15
- if (strat === "variable") {
14
+ if (TREE_SHAKE_GLOBAL_VARIABLE_STRATEGY_USED &&
15
+ strat === "globalVariable") {
16
16
  // a default for a custom strategy to get started quickly
17
17
  // is likely overwritten by `defineSetLocale()`
18
18
  _locale = newLocale;
19
19
  localeHasBeenSet = true;
20
20
  }
21
- else if (strat === "cookie") {
21
+ else if (TREE_SHAKE_COOKIE_STRATEGY_USED && strat === "cookie") {
22
22
  if (typeof document === "undefined" || !document.cookie) {
23
23
  continue;
24
24
  }
@@ -26,7 +26,7 @@ export let setLocale = (newLocale) => {
26
26
  document.cookie = `${cookieName}=${newLocale}`;
27
27
  localeHasBeenSet = true;
28
28
  }
29
- else if (strat === "pathname") {
29
+ else if (TREE_SHAKE_PATHNAME_STRATEGY_USED && strat === "pathname") {
30
30
  if (typeof window === "undefined" || !window.location) {
31
31
  continue;
32
32
  }
@@ -33,7 +33,7 @@ test("doesn't throw for server unavailable APIs", async () => {
33
33
  locales: ["en", "de"],
34
34
  compilerOptions: {
35
35
  // using browser based strategies first, then variable which is available on the server
36
- strategy: ["pathname", "cookie", "variable", "baseLocale"],
36
+ strategy: ["pathname", "cookie", "globalVariable", "baseLocale"],
37
37
  },
38
38
  });
39
39
  expect(() => runtime.setLocale("de")).not.toThrow();
@@ -0,0 +1,2 @@
1
+ export declare const tsRuntime = "\n// @eslint-disable\n\n/**\n * The project's base locale.\n *\n * @example\n * if (locale === baseLocale) {\n * // do something\n * }\n */\nexport const baseLocale = \"<replace>\";\n\n/**\n * The project's locales that have been specified in the settings.\n *\n * @example\n * if (locales.includes(userSelectedLocale) === false) {\n * throw new Error('Locale is not available');\n * }\n */\nexport const locales = [\"<replace>\"] as const;\n\n/**\n * This is a default implementation that is almost always\n * overwritten by `defineGetLocale()` and `defineSetLocale()`.\n *\n */\nlet _locale: Locale = baseLocale;\n\n/**\n * Define the `getLocale()` function.\n *\n * Use this function to define how the locale is resolved. For example,\n * you can resolve the locale from the browser's preferred language,\n * a cookie, env variable, or a user's preference.\n *\n * @example\n * defineGetLocale(() => {\n * // resolve the locale from a cookie. fallback to the base locale.\n * return Cookies.get('locale') ?? baseLocale\n * }\n *\n */\nexport const defineGetLocale: (fn: () => Locale) => void = (fn) => {\n\tgetLocale = fn;\n};\n\n/**\n * Define the `setLocale()` function.\n *\n * Use this function to define how the locale is set. For example,\n * modify a cookie, env variable, or a user's preference.\n *\n * @example\n * defineSetLocale((newLocale) => {\n * // set the locale in a cookie\n * return Cookies.set('locale', newLocale)\n * });\n *\n */\nexport const defineSetLocale: (fn: (newLocale: Locale) => void) => void = (\n\tfn\n) => {\n\tsetLocale = fn;\n};\n\n/**\n * Get the current locale.\n *\n * @example\n * if (getLocale() === 'de') {\n * console.log('Germany \uD83C\uDDE9\uD83C\uDDEA');\n * } else if (getLocale() === 'nl') {\n * console.log('Netherlands \uD83C\uDDF3\uD83C\uDDF1');\n * }\n *\n */\nexport let getLocale: () => Locale =\n\t/** default implementation likely overwritten by `defineGetLocale()` */ () =>\n\t\t_locale;\n\n/**\n * Set the locale.\n *\n * @example\n * setLocale('en');\n */\nexport let setLocale: (newLocale: Locale) => void = (newLocale) => {\n\t/** default implementation likely overwritten by `defineSetLocale()` */\n\t_locale = newLocale;\n};\n\n/**\n * Check if something is an available locale.\n *\n * @example\n * if (isLocale(params.locale)) {\n * setLocale(params.locale);\n * } else {\n * setLocale('en');\n * }\n */\nexport function isLocale(locale: any): locale is Locale {\n\treturn locales.includes(locale);\n}\n\n// ------ TYPES ------\n\n/**\n * A locale that is available in the project.\n *\n * @example\n * setLocale(request.locale as Locale)\n *\n */\nexport type Locale = (typeof locales)[number];\n";
2
+ //# sourceMappingURL=ts-runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ts-runtime.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/ts-runtime.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,SAAS,yrFAqHrB,CAAC"}
@@ -0,0 +1,119 @@
1
+ // to modify the runtime, outcomment the export const jsdocRuntime line
2
+ export const tsRuntime = `
3
+ // @eslint-disable
4
+
5
+ /**
6
+ * The project's base locale.
7
+ *
8
+ * @example
9
+ * if (locale === baseLocale) {
10
+ * // do something
11
+ * }
12
+ */
13
+ export const baseLocale = "<replace>";
14
+
15
+ /**
16
+ * The project's locales that have been specified in the settings.
17
+ *
18
+ * @example
19
+ * if (locales.includes(userSelectedLocale) === false) {
20
+ * throw new Error('Locale is not available');
21
+ * }
22
+ */
23
+ export const locales = ["<replace>"] as const;
24
+
25
+ /**
26
+ * This is a default implementation that is almost always
27
+ * overwritten by \`defineGetLocale()\` and \`defineSetLocale()\`.
28
+ *
29
+ */
30
+ let _locale: Locale = baseLocale;
31
+
32
+ /**
33
+ * Define the \`getLocale()\` function.
34
+ *
35
+ * Use this function to define how the locale is resolved. For example,
36
+ * you can resolve the locale from the browser's preferred language,
37
+ * a cookie, env variable, or a user's preference.
38
+ *
39
+ * @example
40
+ * defineGetLocale(() => {
41
+ * // resolve the locale from a cookie. fallback to the base locale.
42
+ * return Cookies.get('locale') ?? baseLocale
43
+ * }
44
+ *
45
+ */
46
+ export const defineGetLocale: (fn: () => Locale) => void = (fn) => {
47
+ getLocale = fn;
48
+ };
49
+
50
+ /**
51
+ * Define the \`setLocale()\` function.
52
+ *
53
+ * Use this function to define how the locale is set. For example,
54
+ * modify a cookie, env variable, or a user's preference.
55
+ *
56
+ * @example
57
+ * defineSetLocale((newLocale) => {
58
+ * // set the locale in a cookie
59
+ * return Cookies.set('locale', newLocale)
60
+ * });
61
+ *
62
+ */
63
+ export const defineSetLocale: (fn: (newLocale: Locale) => void) => void = (
64
+ fn
65
+ ) => {
66
+ setLocale = fn;
67
+ };
68
+
69
+ /**
70
+ * Get the current locale.
71
+ *
72
+ * @example
73
+ * if (getLocale() === 'de') {
74
+ * console.log('Germany 🇩🇪');
75
+ * } else if (getLocale() === 'nl') {
76
+ * console.log('Netherlands 🇳🇱');
77
+ * }
78
+ *
79
+ */
80
+ export let getLocale: () => Locale =
81
+ /** default implementation likely overwritten by \`defineGetLocale()\` */ () =>
82
+ _locale;
83
+
84
+ /**
85
+ * Set the locale.
86
+ *
87
+ * @example
88
+ * setLocale('en');
89
+ */
90
+ export let setLocale: (newLocale: Locale) => void = (newLocale) => {
91
+ /** default implementation likely overwritten by \`defineSetLocale()\` */
92
+ _locale = newLocale;
93
+ };
94
+
95
+ /**
96
+ * Check if something is an available locale.
97
+ *
98
+ * @example
99
+ * if (isLocale(params.locale)) {
100
+ * setLocale(params.locale);
101
+ * } else {
102
+ * setLocale('en');
103
+ * }
104
+ */
105
+ export function isLocale(locale: any): locale is Locale {
106
+ return locales.includes(locale);
107
+ }
108
+
109
+ // ------ TYPES ------
110
+
111
+ /**
112
+ * A locale that is available in the project.
113
+ *
114
+ * @example
115
+ * setLocale(request.locale as Locale)
116
+ *
117
+ */
118
+ export type Locale = (typeof locales)[number];
119
+ `;
@@ -4,8 +4,8 @@
4
4
  export type Runtime = {
5
5
  baseLocale: Locale;
6
6
  locales: Readonly<Locale[]>;
7
- strategy: typeof import("./strategy.js").strategy;
8
- cookieName: typeof import("./cookie-name.js").cookieName;
7
+ strategy: typeof import("./variables.js").strategy;
8
+ cookieName: typeof import("./variables.js").cookieName;
9
9
  getLocale: typeof import("./get-locale.js").getLocale;
10
10
  setLocale: typeof import("./set-locale.js").setLocale;
11
11
  defineGetLocale: (fn: () => Locale) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/type.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IAC5B,QAAQ,EAAE,cAAc,eAAe,EAAE,QAAQ,CAAC;IAClD,UAAU,EAAE,cAAc,kBAAkB,EAAE,UAAU,CAAC;IACzD,SAAS,EAAE,cAAc,iBAAiB,EAAE,SAAS,CAAC;IACtD,SAAS,EAAE,cAAc,iBAAiB,EAAE,SAAS,CAAC;IACtD,eAAe,EAAE,CAAC,EAAE,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;IAC5C,eAAe,EAAE,CAAC,EAAE,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,KAAK,IAAI,CAAC;IAC3D,cAAc,EAAE,cAAc,uBAAuB,EAAE,cAAc,CAAC;IACtE,QAAQ,EAAE,cAAc,gBAAgB,EAAE,QAAQ,CAAC;IACnD,cAAc,EAAE,cAAc,uBAAuB,EAAE,cAAc,CAAC;IACtE,YAAY,EAAE,cAAc,oBAAoB,EAAE,YAAY,CAAC;IAC/D,yBAAyB,EAAE,cAAc,mCAAmC,EAAE,yBAAyB,CAAC;IACxG,wBAAwB,EAAE,cAAc,kCAAkC,EAAE,wBAAwB,CAAC;IACrG,uBAAuB,EAAE,cAAc,iCAAiC,EAAE,uBAAuB,CAAC;CAClG,CAAC;AAEF;;GAEG;AACH,KAAK,MAAM,GAAG,GAAG,CAAC"}
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/type.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IAC5B,QAAQ,EAAE,cAAc,gBAAgB,EAAE,QAAQ,CAAC;IACnD,UAAU,EAAE,cAAc,gBAAgB,EAAE,UAAU,CAAC;IACvD,SAAS,EAAE,cAAc,iBAAiB,EAAE,SAAS,CAAC;IACtD,SAAS,EAAE,cAAc,iBAAiB,EAAE,SAAS,CAAC;IACtD,eAAe,EAAE,CAAC,EAAE,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;IAC5C,eAAe,EAAE,CAAC,EAAE,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,KAAK,IAAI,CAAC;IAC3D,cAAc,EAAE,cAAc,uBAAuB,EAAE,cAAc,CAAC;IACtE,QAAQ,EAAE,cAAc,gBAAgB,EAAE,QAAQ,CAAC;IACnD,cAAc,EAAE,cAAc,uBAAuB,EAAE,cAAc,CAAC;IACtE,YAAY,EAAE,cAAc,oBAAoB,EAAE,YAAY,CAAC;IAC/D,yBAAyB,EAAE,cAAc,mCAAmC,EAAE,yBAAyB,CAAC;IACxG,wBAAwB,EAAE,cAAc,kCAAkC,EAAE,wBAAwB,CAAC;IACrG,uBAAuB,EAAE,cAAc,iCAAiC,EAAE,uBAAuB,CAAC;CAClG,CAAC;AAEF;;GAEG;AACH,KAAK,MAAM,GAAG,GAAG,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import { expect, test } from "vitest";
2
- import { createProject as typescriptProject, ts } from "@ts-morph/bootstrap";
2
+ import { createProject as typescriptProject, ts, } from "@ts-morph/bootstrap";
3
3
  import { createRuntimeFile } from "./create-runtime.js";
4
4
  import fs from "node:fs";
5
5
  import { dirname, resolve } from "node:path";
@@ -7,23 +7,31 @@ import { fileURLToPath } from "url";
7
7
  import { defaultCompilerOptions } from "../compile.js";
8
8
  const __filename = fileURLToPath(import.meta.url);
9
9
  const __dirname = dirname(__filename);
10
+ const superStrictRuleOutAnyErrorTsSettings = {
11
+ outDir: "dist",
12
+ declaration: true,
13
+ allowJs: true,
14
+ checkJs: true,
15
+ noImplicitAny: true,
16
+ noUnusedLocals: true,
17
+ noUnusedParameters: true,
18
+ noImplicitReturns: true,
19
+ noImplicitThis: true,
20
+ noUncheckedIndexedAccess: true,
21
+ noPropertyAccessFromIndexSignature: true,
22
+ module: ts.ModuleKind.Node16,
23
+ strict: true,
24
+ };
10
25
  test("runtime type", async () => {
11
26
  const project = await typescriptProject({
12
27
  useInMemoryFileSystem: true,
13
- compilerOptions: {
14
- outDir: "./dist",
15
- declaration: true,
16
- allowJs: true,
17
- checkJs: true,
18
- module: ts.ModuleKind.Node16,
19
- strict: true,
20
- },
28
+ compilerOptions: superStrictRuleOutAnyErrorTsSettings,
21
29
  });
22
30
  const jsdocRuntime = createRuntimeFile({
23
31
  baseLocale: "en",
24
32
  locales: ["en"],
25
33
  compilerOptions: defaultCompilerOptions,
26
- });
34
+ }).replace('import * as pathToRegexp from "@inlang/paraglide-js/path-to-regexp";', "");
27
35
  const file = (path) => {
28
36
  return [path, fs.readFileSync(resolve(__dirname, path), "utf-8")];
29
37
  };
@@ -41,12 +49,41 @@ test("runtime type", async () => {
41
49
  import type { Runtime } from "./type.js"
42
50
 
43
51
  runtime satisfies Runtime
52
+
53
+ // getLocale() should return type should be a union of language tags, not a generic string
54
+ runtime.getLocale() satisfies "de" | "en" | "en-US"
55
+
56
+ // locales should have a narrow type, not a generic string
57
+ runtime.locales satisfies Readonly<Array<"de" | "en" | "en-US">>
58
+
59
+ // setLocale() should fail if the given language tag is not included in locales
60
+ // @ts-expect-error - invalid locale
61
+ runtime.setLocale("fr")
62
+
63
+ // setLocale() should not fail if the given language tag is included in locales
64
+ runtime.setLocale("de")
65
+
66
+ // isLocale should narrow the type of it's argument
67
+ const thing = 5;
68
+
69
+ let a: "de" | "en" | "en-US";
70
+
71
+ if(runtime.isLocale(thing)) {
72
+ a = thing
73
+ } else {
74
+ // @ts-expect-error - thing is not a language tag
75
+ a = thing
76
+ }
77
+
78
+ // to make ts not complain about unused variables
79
+ console.log(a)
44
80
  `);
45
81
  const program = project.createProgram();
46
82
  const diagnostics = ts
47
83
  .getPreEmitDiagnostics(program)
48
84
  // ignore 'export' modifier cannot be applied to ambient modules and module augmentations since they are always visible. /ambient.d.ts
49
- .filter((d) => d.code !== 2668);
85
+ .filter((d) => d.code !== 2668)
86
+ .filter((d) => d.messageText.toString().includes("path-to-regexp"));
50
87
  for (const diagnostic of diagnostics) {
51
88
  console.error(diagnostic.messageText, diagnostic.file?.fileName);
52
89
  }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * The project's base locale.
3
+ *
4
+ * @example
5
+ * if (locale === baseLocale) {
6
+ * // do something
7
+ * }
8
+ */
9
+ export const baseLocale: "en";
10
+ /**
11
+ * The project's locales that have been specified in the settings.
12
+ *
13
+ * @example
14
+ * if (locales.includes(userSelectedLocale) === false) {
15
+ * throw new Error('Locale is not available');
16
+ * }
17
+ */
18
+ export const locales: readonly ["en", "de"];
19
+ /** @type {string} */
20
+ export const cookieName: string;
21
+ /**
22
+ * @type {Array<"cookie" | "baseLocale" | "pathname" | "globalVariable">}
23
+ */
24
+ export const strategy: Array<"cookie" | "baseLocale" | "pathname" | "globalVariable">;
25
+ /**
26
+ * The localized pathnames.
27
+ *
28
+ * @type {Record<string, Record<Locale, string>>}
29
+ */
30
+ export const pathnames: Record<string, Record<Locale, string>>;
31
+ /** @type {string | undefined} */
32
+ export const pathnameBase: string | undefined;
33
+ /**
34
+ * Whether the pathnames are the default pathnames.
35
+ *
36
+ * If the default pathnames are used, the matching is done
37
+ * statically with no runtime matching logic which reduces
38
+ * the bundlesize.
39
+ *
40
+ * @type {boolean}
41
+ */
42
+ export const TREE_SHAKE_IS_DEFAULT_PATHNAMES: boolean;
43
+ export const TREE_SHAKE_COOKIE_STRATEGY_USED: false;
44
+ export const TREE_SHAKE_PATHNAME_STRATEGY_USED: false;
45
+ export const TREE_SHAKE_GLOBAL_VARIABLE_STRATEGY_USED: false;
46
+ //# sourceMappingURL=variables.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"variables.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/variables.js"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,yBAA0B,IAAI,CAAC;AAE/B;;;;;;;GAOG;AACH,4CAA2D;AAE3D,qBAAqB;AACrB,yBADW,MAAM,CACyB;AAE1C;;GAEG;AACH,uBAFU,KAAK,CAAC,QAAQ,GAAG,YAAY,GAAG,UAAU,GAAG,gBAAgB,CAAC,CAE7B;AAE3C;;;;GAIG;AACH,wBAFU,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAEpB;AAE5B,iCAAiC;AACjC,2BADW,MAAM,GAAG,SAAS,CACS;AAEtC;;;;;;;;GAQG;AACH,8CAFU,OAAO,CAEoC;AAErD,8CAA+C,KAAK,CAAC;AAErD,gDAAiD,KAAK,CAAC;AAEvD,uDAAwD,KAAK,CAAC"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * The project's base locale.
3
+ *
4
+ * @example
5
+ * if (locale === baseLocale) {
6
+ * // do something
7
+ * }
8
+ */
9
+ export const baseLocale = "en";
10
+ /**
11
+ * The project's locales that have been specified in the settings.
12
+ *
13
+ * @example
14
+ * if (locales.includes(userSelectedLocale) === false) {
15
+ * throw new Error('Locale is not available');
16
+ * }
17
+ */
18
+ export const locales = /** @type {const} */ (["en", "de"]);
19
+ /** @type {string} */
20
+ export const cookieName = "<cookie-name>";
21
+ /**
22
+ * @type {Array<"cookie" | "baseLocale" | "pathname" | "globalVariable">}
23
+ */
24
+ export const strategy = ["globalVariable"];
25
+ /**
26
+ * The localized pathnames.
27
+ *
28
+ * @type {Record<string, Record<Locale, string>>}
29
+ */
30
+ export const pathnames = {};
31
+ /** @type {string | undefined} */
32
+ export const pathnameBase = undefined;
33
+ /**
34
+ * Whether the pathnames are the default pathnames.
35
+ *
36
+ * If the default pathnames are used, the matching is done
37
+ * statically with no runtime matching logic which reduces
38
+ * the bundlesize.
39
+ *
40
+ * @type {boolean}
41
+ */
42
+ export const TREE_SHAKE_IS_DEFAULT_PATHNAMES = false;
43
+ export const TREE_SHAKE_COOKIE_STRATEGY_USED = false;
44
+ export const TREE_SHAKE_PATHNAME_STRATEGY_USED = false;
45
+ export const TREE_SHAKE_GLOBAL_VARIABLE_STRATEGY_USED = false;
@@ -0,0 +1,13 @@
1
+ export type CookieStrategy = {
2
+ type: "cookie";
3
+ cookieName: string;
4
+ };
5
+ export type I18nRoutingStrategy = {
6
+ type: "i18n-routing";
7
+ };
8
+ export type CustomStrategy = {
9
+ type: "custom";
10
+ };
11
+ export type Strategy = CookieStrategy | CustomStrategy | I18nRoutingStrategy;
12
+ export declare function createStrategyFile(strategy: Strategy): string;
13
+ //# sourceMappingURL=strategy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategy.d.ts","sourceRoot":"","sources":["../../src/compiler/strategy.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG;IAC5B,IAAI,EAAE,QAAQ,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IACjC,IAAI,EAAE,cAAc,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC5B,IAAI,EAAE,QAAQ,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG,cAAc,GAAG,cAAc,GAAG,mBAAmB,CAAC;AAI7E,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,CAe7D"}
@@ -0,0 +1,12 @@
1
+ const variableNames = ["cookieName"];
2
+ export function createStrategyFile(strategy) {
3
+ let result = `/** @type {"cookie" | "custom" | "i18n-routing"} */
4
+ export const type = ${JSON.stringify(strategy.type, null, `\t`)}
5
+ `;
6
+ for (const name of variableNames) {
7
+ result += `export const ${name} = ${JSON.stringify(
8
+ // @ts-expect-error - typescript is not smart enough to know that the name is a valid key of the strategy object
9
+ strategy[name], null, `\t`)}\n`;
10
+ }
11
+ return result;
12
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=strategy.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategy.test.d.ts","sourceRoot":"","sources":["../../src/compiler/strategy.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ import { test, expect } from "vitest";
2
+ import { createStrategyFile } from "./strategy.js";
3
+ test("creates the exports for a strategy", () => {
4
+ const result = createStrategyFile({ type: "cookie", cookieName: "locale" });
5
+ expect(result).toBe(`/** @type {"cookie" | "custom" | "i18n-routing"} */
6
+ export const type = "cookie"
7
+ export const cookieName = "locale"
8
+ `);
9
+ });
@@ -0,0 +1,12 @@
1
+ /**
2
+ * This is a re-export of the path-to-regexp library.
3
+ *
4
+ * The bundling is required to (1) avoid dependency resolution issues
5
+ * because paraglide emits into source code and (2) enable tree-shaking
6
+ * by tricking the bundler to treat path-to-regexp as ESM instead of
7
+ * CJS dependency.
8
+ *
9
+ * See https://github.com/pillarjs/path-to-regexp for more information.
10
+ */
11
+ export { match, compile } from "path-to-regexp";
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/path-to-regexp/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * This is a re-export of the path-to-regexp library.
3
+ *
4
+ * The bundling is required to (1) avoid dependency resolution issues
5
+ * because paraglide emits into source code and (2) enable tree-shaking
6
+ * by tricking the bundler to treat path-to-regexp as ESM instead of
7
+ * CJS dependency.
8
+ *
9
+ * See https://github.com/pillarjs/path-to-regexp for more information.
10
+ */
11
+ export { match, compile } from "path-to-regexp";
@@ -1,5 +1,5 @@
1
1
  export const ENV_VARIABLES = {
2
2
  PARJS_APP_ID: "library.inlang.paraglideJs",
3
3
  PARJS_POSTHOG_TOKEN: "phc_m5yJZCxjOGxF8CJvP5sQ3H0d76xpnLrsmiZHduT4jDz",
4
- PARJS_PACKAGE_VERSION: "2.0.0-beta.15",
4
+ PARJS_PACKAGE_VERSION: "2.0.0-beta.16",
5
5
  };
@@ -1,3 +1,8 @@
1
1
  import type nodeFs from "node:fs/promises";
2
- export declare function writeOutput(outputDirectory: string, output: Record<string, string>, fs: typeof nodeFs): Promise<void>;
2
+ export declare function writeOutput(args: {
3
+ directory: string;
4
+ output: Record<string, string>;
5
+ fs: typeof nodeFs;
6
+ previousOutputHashes?: Record<string, string>;
7
+ }): Promise<Record<string, string> | undefined>;
3
8
  //# sourceMappingURL=write-output.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"write-output.d.ts","sourceRoot":"","sources":["../../../src/services/file-handling/write-output.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,MAAM,kBAAkB,CAAC;AAI3C,wBAAsB,WAAW,CAChC,eAAe,EAAE,MAAM,EACvB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC9B,EAAE,EAAE,OAAO,MAAM,iBA6BjB"}
1
+ {"version":3,"file":"write-output.d.ts","sourceRoot":"","sources":["../../../src/services/file-handling/write-output.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,MAAM,kBAAkB,CAAC;AAE3C,wBAAsB,WAAW,CAAC,IAAI,EAAE;IACvC,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,EAAE,EAAE,OAAO,MAAM,CAAC;IAClB,oBAAoB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC9C,+CA4CA"}