@inlang/paraglide-js 2.0.11 → 2.0.13

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 (37) hide show
  1. package/dist/cli/steps/initialize-inlang-project.d.ts.map +1 -1
  2. package/dist/cli/utils.d.ts.map +1 -1
  3. package/dist/compiler/compile-bundle.d.ts.map +1 -1
  4. package/dist/compiler/compile-message.d.ts.map +1 -1
  5. package/dist/compiler/compile-pattern.d.ts.map +1 -1
  6. package/dist/compiler/compile-project.d.ts.map +1 -1
  7. package/dist/compiler/create-paraglide.d.ts +20 -14
  8. package/dist/compiler/create-paraglide.d.ts.map +1 -1
  9. package/dist/compiler/create-paraglide.js +18 -14
  10. package/dist/compiler/create-paraglide.test.js +7 -11
  11. package/dist/compiler/index.d.ts +1 -1
  12. package/dist/compiler/index.d.ts.map +1 -1
  13. package/dist/compiler/index.js +1 -1
  14. package/dist/compiler/runtime/assert-is-locale.test.js +3 -3
  15. package/dist/compiler/runtime/extract-locale-from-cookie.test.js +7 -11
  16. package/dist/compiler/runtime/extract-locale-from-request.test.js +57 -77
  17. package/dist/compiler/runtime/extract-locale-from-url.d.ts.map +1 -1
  18. package/dist/compiler/runtime/extract-locale-from-url.js +35 -13
  19. package/dist/compiler/runtime/extract-locale-from-url.test.js +51 -61
  20. package/dist/compiler/runtime/generate-static-localized-urls.test.js +48 -58
  21. package/dist/compiler/runtime/get-locale.test.js +52 -68
  22. package/dist/compiler/runtime/get-url-origin.test.js +2 -4
  23. package/dist/compiler/runtime/localize-href.d.ts.map +1 -1
  24. package/dist/compiler/runtime/localize-href.js +7 -4
  25. package/dist/compiler/runtime/localize-href.test.js +48 -58
  26. package/dist/compiler/runtime/localize-url.test.js +258 -294
  27. package/dist/compiler/runtime/set-locale.test.js +56 -74
  28. package/dist/compiler/runtime/track-message-call.test.js +1 -1
  29. package/dist/compiler/server/middleware.d.ts +4 -2
  30. package/dist/compiler/server/middleware.d.ts.map +1 -1
  31. package/dist/compiler/server/middleware.js +5 -3
  32. package/dist/compiler/server/middleware.test.js +163 -109
  33. package/dist/services/codegen/quotes.d.ts.map +1 -1
  34. package/dist/services/env-variables/index.js +1 -1
  35. package/dist/services/telemetry/capture.d.ts.map +1 -1
  36. package/dist/utilities/detect-json-formatting.d.ts.map +1 -1
  37. package/package.json +7 -7
@@ -1 +1 @@
1
- {"version":3,"file":"initialize-inlang-project.d.ts","sourceRoot":"","sources":["../../../src/cli/steps/initialize-inlang-project.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,KAAK,aAAa,EAClB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzB,eAAO,MAAM,uBAAuB,EAAE,OAAO,CAC5C;IACC,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC;IACvB,MAAM,EAAE,OAAO,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACb,EACD;IACC,OAAO,EAAE,aAAa,CAAC;IACvB,mCAAmC;IACnC,WAAW,EAAE,MAAM,CAAC;CACpB,CA2BD,CAAC;AAgHF,eAAO,MAAM,oBAAoB,QAAe;IAC/C,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC;IACvB,MAAM,EAAE,OAAO,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CACf,KAAG,OAAO,CAAC;IACX,OAAO,EAAE,aAAa,CAAC;IACvB,8CAA8C;IAC9C,WAAW,EAAE,MAAM,CAAC;CACpB,CAwEA,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAazD;AAcD;;GAEG;AACH,eAAO,MAAM,OAAO,sbACga,CAAC"}
1
+ {"version":3,"file":"initialize-inlang-project.d.ts","sourceRoot":"","sources":["../../../src/cli/steps/initialize-inlang-project.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,KAAK,aAAa,EAClB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzB,eAAO,MAAM,uBAAuB,EAAE,OAAO,CAC5C;IACC,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC;IACvB,MAAM,EAAE,OAAO,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACb,EACD;IACC,OAAO,EAAE,aAAa,CAAC;IACvB,mCAAmC;IACnC,WAAW,EAAE,MAAM,CAAC;CACpB,CA2BD,CAAC;AAgHF,eAAO,MAAM,oBAAoB,GAAU,KAAK;IAC/C,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC;IACvB,MAAM,EAAE,OAAO,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CACf,KAAG,OAAO,CAAC;IACX,OAAO,EAAE,aAAa,CAAC;IACvB,8CAA8C;IAC9C,WAAW,EAAE,MAAM,CAAC;CACpB,CAwEA,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAazD;AAcD;;GAEG;AACH,eAAO,MAAM,OAAO,sbACga,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/cli/utils.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,SAAS,EAAE,EAC5D,GAAG,EAAE,GAAG,KACJ,OAAO,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;AAExB;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,OAAO,OAAO,CAAC,MAMnC,CAAC;AAEF,eAAO,MAAM,eAAe,GAAU,CAAC,SAAS,MAAM,WAC5C,MAAM,YACN;IAAE,OAAO,CAAC,EAAE,CAAC,CAAC;IAAC,OAAO,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,CAAC,CAAA;KAAE,EAAE,CAAA;CAAE,KAG9D,OAAO,CAAC,CAAC,CAKX,CAAC"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/cli/utils.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,SAAS,EAAE,EAC5D,GAAG,EAAE,GAAG,KACJ,OAAO,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;AAExB;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,OAAO,OAAO,CAAC,MAMnC,CAAC;AAEF,eAAO,MAAM,eAAe,GAAU,CAAC,SAAS,MAAM,EACrD,SAAS,MAAM,EACf,UAAS;IAAE,OAAO,CAAC,EAAE,CAAC,CAAC;IAAC,OAAO,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,CAAC,CAAA;KAAE,EAAE,CAAA;CAE7D,KACC,OAAO,CAAC,CAAC,CAKX,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"compile-bundle.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-bundle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAK3C,MAAM,MAAM,0BAA0B,GAAG;IACxC,kDAAkD;IAClD,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IACzB,gDAAgD;IAChD,QAAQ,EAAE;QACT,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;KACpC,CAAC;CACF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,SAAU;IACnC,MAAM,EAAE,YAAY,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;IAChD,0BAA0B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAC;CACzE,KAAG,0BA0BH,CAAC"}
1
+ {"version":3,"file":"compile-bundle.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-bundle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAK3C,MAAM,MAAM,0BAA0B,GAAG;IACxC,kDAAkD;IAClD,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IACzB,gDAAgD;IAChD,QAAQ,EAAE;QACT,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;KACpC,CAAC;CACF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM;IACnC,MAAM,EAAE,YAAY,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;IAChD,0BAA0B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAC;CACzE,KAAG,0BA0BH,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"compile-message.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-message.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAK3C;;;GAGG;AACH,eAAO,MAAM,cAAc,iBACZ,WAAW,EAAE,WAClB,OAAO,YACN,OAAO,EAAE,KACjB,QAAQ,CAAC,OAAO,CAUlB,CAAC"}
1
+ {"version":3,"file":"compile-message.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-message.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAK3C;;;GAGG;AACH,eAAO,MAAM,cAAc,GAC1B,cAAc,WAAW,EAAE,EAC3B,SAAS,OAAO,EAChB,UAAU,OAAO,EAAE,KACjB,QAAQ,CAAC,OAAO,CAUlB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"compile-pattern.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-pattern.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAqB,MAAM,aAAa,CAAC;AAC3E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,cAAc,SAAU;IACpC,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,WAAW,EAAE,CAAC;CAC5B,KAAG,QAAQ,CAAC,OAAO,CA4BnB,CAAC"}
1
+ {"version":3,"file":"compile-pattern.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-pattern.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAqB,MAAM,aAAa,CAAC;AAC3E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM;IACpC,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,WAAW,EAAE,CAAC;CAC5B,KAAG,QAAQ,CAAC,OAAO,CA4BnB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"compile-project.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-project.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAIrE,OAAO,EAEN,KAAK,eAAe,EACpB,MAAM,uBAAuB,CAAC;AAU/B;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,SAAgB;IAC1C,OAAO,EAAE,aAAa,CAAC;IACvB,eAAe,CAAC,EAAE,IAAI,CAAC,eAAe,EAAE,IAAI,GAAG,SAAS,GAAG,QAAQ,CAAC,CAAC;CACrE,KAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAsEjC,CAAC;AAEF,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,EAC9C,OAAO,EAAE,CAAC,EAAE,EACZ,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,GACpB,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAY1B"}
1
+ {"version":3,"file":"compile-project.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-project.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAIrE,OAAO,EAEN,KAAK,eAAe,EACpB,MAAM,uBAAuB,CAAC;AAU/B;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,GAAU,MAAM;IAC1C,OAAO,EAAE,aAAa,CAAC;IACvB,eAAe,CAAC,EAAE,IAAI,CAAC,eAAe,EAAE,IAAI,GAAG,SAAS,GAAG,QAAQ,CAAC,CAAC;CACrE,KAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAsEjC,CAAC;AAEF,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,EAC9C,OAAO,EAAE,CAAC,EAAE,EACZ,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,GACpB,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAY1B"}
@@ -5,33 +5,39 @@ import { type CompilerOptions } from "./compiler-options.js";
5
5
  * Creates an in-memory Paraglide module for use in tests and non-bundled environments.
6
6
  *
7
7
  * @example
8
- * const project = await fs.readFile("./project.inlang");
8
+ * const blob = await fs.readFile("./project.inlang");
9
9
  * const paraglide = await createParaglide({
10
- * project,
11
- * compilerOptions, // optional
10
+ * blob,
11
+ * // other options
12
12
  * })
13
13
  *
14
+ * // Access functions
15
+ * paraglide.localizeUrl("https://example.com", { locale: "de" })
16
+ * app.use(paraglide.paraglideMiddleware())
17
+ *
18
+ * You can load a project from a directory as well.
19
+ *
14
20
  * @example
15
- * import { newProject } from "@inlang/sdk";
21
+ * import { loadProjectFromDirectory } from "@inlang/sdk";
22
+ *
23
+ * const project = await loadProjectFromDirectory("./project");
16
24
  *
17
25
  * const paraglide = await createParaglide({
18
- * project: await newProject({
19
- * settings: {
20
- * baseLocale: "en",
21
- * locales: ["en", "de"],
22
- * },
23
- * }),
24
- * compilerOptions, // optional
26
+ * blob: await project.toBlob(),
27
+ * // other options
25
28
  * })
26
29
  *
27
30
  * // Access functions
28
31
  * paraglide.localizeUrl("https://example.com", { locale: "de" })
29
32
  * app.use(paraglide.paraglideMiddleware())
33
+ *
30
34
  */
31
35
  export declare function createParaglide(args: {
32
- project: Blob;
33
- compilerOptions?: Omit<CompilerOptions, "outdir" | "project" | "fs">;
34
- }): Promise<Runtime & ServerRuntime & {
36
+ /**
37
+ * The inlang file.
38
+ */
39
+ blob: Blob;
40
+ } & Omit<CompilerOptions, "outdir" | "project" | "fs">): Promise<Runtime & ServerRuntime & {
35
41
  m: Record<string, unknown>;
36
42
  }>;
37
43
  //# sourceMappingURL=create-paraglide.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-paraglide.d.ts","sourceRoot":"","sources":["../../src/compiler/create-paraglide.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAEN,KAAK,eAAe,EACpB,MAAM,uBAAuB,CAAC;AAK/B;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE;IAC3C,OAAO,EAAE,IAAI,CAAC;IACd,eAAe,CAAC,EAAE,IAAI,CAAC,eAAe,EAAE,QAAQ,GAAG,SAAS,GAAG,IAAI,CAAC,CAAC;CACrE,GAAG,OAAO,CAAC,OAAO,GAAG,aAAa,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,CAAC,CA2EpE"}
1
+ {"version":3,"file":"create-paraglide.d.ts","sourceRoot":"","sources":["../../src/compiler/create-paraglide.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAEN,KAAK,eAAe,EACpB,MAAM,uBAAuB,CAAC;AAK/B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAsB,eAAe,CACpC,IAAI,EAAE;IACL;;OAEG;IACH,IAAI,EAAE,IAAI,CAAC;CACX,GAAG,IAAI,CAAC,eAAe,EAAE,QAAQ,GAAG,SAAS,GAAG,IAAI,CAAC,GACpD,OAAO,CAAC,OAAO,GAAG,aAAa,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,CAAC,CA2EnE"}
@@ -6,32 +6,36 @@ import { loadProjectInMemory } from "@inlang/sdk";
6
6
  * Creates an in-memory Paraglide module for use in tests and non-bundled environments.
7
7
  *
8
8
  * @example
9
- * const project = await fs.readFile("./project.inlang");
9
+ * const blob = await fs.readFile("./project.inlang");
10
10
  * const paraglide = await createParaglide({
11
- * project,
12
- * compilerOptions, // optional
11
+ * blob,
12
+ * // other options
13
13
  * })
14
14
  *
15
+ * // Access functions
16
+ * paraglide.localizeUrl("https://example.com", { locale: "de" })
17
+ * app.use(paraglide.paraglideMiddleware())
18
+ *
19
+ * You can load a project from a directory as well.
20
+ *
15
21
  * @example
16
- * import { newProject } from "@inlang/sdk";
22
+ * import { loadProjectFromDirectory } from "@inlang/sdk";
23
+ *
24
+ * const project = await loadProjectFromDirectory("./project");
17
25
  *
18
26
  * const paraglide = await createParaglide({
19
- * project: await newProject({
20
- * settings: {
21
- * baseLocale: "en",
22
- * locales: ["en", "de"],
23
- * },
24
- * }),
25
- * compilerOptions, // optional
27
+ * blob: await project.toBlob(),
28
+ * // other options
26
29
  * })
27
30
  *
28
31
  * // Access functions
29
32
  * paraglide.localizeUrl("https://example.com", { locale: "de" })
30
33
  * app.use(paraglide.paraglideMiddleware())
34
+ *
31
35
  */
32
36
  export async function createParaglide(args) {
33
37
  // Load the project from the blob
34
- const project = await loadProjectInMemory({ blob: args.project });
38
+ const project = await loadProjectInMemory({ blob: args.blob });
35
39
  const settings = await project.settings.get();
36
40
  // Extract baseLocale and locales from the project
37
41
  const baseLocale = settings.baseLocale;
@@ -57,7 +61,7 @@ export async function createParaglide(args) {
57
61
  locales,
58
62
  compilerOptions: {
59
63
  ...defaultCompilerOptions,
60
- ...args.compilerOptions,
64
+ ...args,
61
65
  },
62
66
  })
63
67
  // remove the polyfill import statement to avoid module resolution issues
@@ -66,7 +70,7 @@ export async function createParaglide(args) {
66
70
  const serverSideRuntime = createServerFile({
67
71
  compiledBundles: [],
68
72
  compilerOptions: {
69
- experimentalMiddlewareLocaleSplitting: args.compilerOptions?.experimentalMiddlewareLocaleSplitting ??
73
+ experimentalMiddlewareLocaleSplitting: args.experimentalMiddlewareLocaleSplitting ??
70
74
  defaultCompilerOptions.experimentalMiddlewareLocaleSplitting,
71
75
  },
72
76
  })
@@ -3,7 +3,7 @@ import { createParaglide } from "./create-paraglide.js";
3
3
  import { newProject } from "@inlang/sdk";
4
4
  test("createParaglideModule should create a module with runtime and server functions", async () => {
5
5
  const paraglide = await createParaglide({
6
- project: await newProject({
6
+ blob: await newProject({
7
7
  settings: {
8
8
  baseLocale: "en",
9
9
  locales: ["en", "de", "fr"],
@@ -21,16 +21,14 @@ test("createParaglideModule should create a module with runtime and server funct
21
21
  });
22
22
  test("createParaglideModule should allow configuring compiler options", async () => {
23
23
  const paraglide = await createParaglide({
24
- project: await newProject({
24
+ blob: await newProject({
25
25
  settings: {
26
26
  baseLocale: "en",
27
27
  locales: ["en", "de", "fr"],
28
28
  },
29
29
  }),
30
- compilerOptions: {
31
- strategy: ["url"],
32
- cookieName: "custom-cookie",
33
- },
30
+ strategy: ["url"],
31
+ cookieName: "custom-cookie",
34
32
  });
35
33
  // Check that compiler options are applied
36
34
  expect(paraglide.strategy).toEqual(["url"]);
@@ -38,15 +36,13 @@ test("createParaglideModule should allow configuring compiler options", async ()
38
36
  });
39
37
  test("createParaglideModule should work with URL localization", async () => {
40
38
  const paraglide = await createParaglide({
41
- project: await newProject({
39
+ blob: await newProject({
42
40
  settings: {
43
41
  baseLocale: "en",
44
42
  locales: ["en", "de", "fr"],
45
43
  },
46
44
  }),
47
- compilerOptions: {
48
- strategy: ["url"],
49
- },
45
+ strategy: ["url"],
50
46
  });
51
47
  // Test URL localization
52
48
  const url = "https://example.com/products";
@@ -58,7 +54,7 @@ test("createParaglideModule should work with URL localization", async () => {
58
54
  });
59
55
  test("createParaglideModule should provide mock message functions that throw helpful errors", async () => {
60
56
  const paraglide = await createParaglide({
61
- project: await newProject({
57
+ blob: await newProject({
62
58
  settings: {
63
59
  baseLocale: "en",
64
60
  locales: ["en", "de", "fr"],
@@ -8,5 +8,5 @@ export { compileBundle } from "./compile-bundle.js";
8
8
  export { compileMessage } from "./compile-message.js";
9
9
  export { compilePattern } from "./compile-pattern.js";
10
10
  export { writeOutput } from "../services/file-handling/write-output.js";
11
- export { createParaglide as createParaglideModule } from "./create-paraglide.js";
11
+ export { createParaglide } from "./create-paraglide.js";
12
12
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/compiler/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,sBAAsB,EACtB,KAAK,eAAe,GACpB,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACzE,YAAY,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,2CAA2C,CAAC;AACxE,OAAO,EAAE,eAAe,IAAI,qBAAqB,EAAE,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/compiler/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,sBAAsB,EACtB,KAAK,eAAe,GACpB,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACzE,YAAY,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,2CAA2C,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC"}
@@ -5,4 +5,4 @@ export { compileBundle } from "./compile-bundle.js";
5
5
  export { compileMessage } from "./compile-message.js";
6
6
  export { compilePattern } from "./compile-pattern.js";
7
7
  export { writeOutput } from "../services/file-handling/write-output.js";
8
- export { createParaglide as createParaglideModule } from "./create-paraglide.js";
8
+ export { createParaglide } from "./create-paraglide.js";
@@ -3,7 +3,7 @@ import { createParaglide } from "../create-paraglide.js";
3
3
  import { newProject } from "@inlang/sdk";
4
4
  test("throws if the locale is not available", async () => {
5
5
  const runtime = await createParaglide({
6
- project: await newProject({
6
+ blob: await newProject({
7
7
  settings: {
8
8
  baseLocale: "en",
9
9
  locales: ["en", "de"],
@@ -14,7 +14,7 @@ test("throws if the locale is not available", async () => {
14
14
  });
15
15
  test("passes if the locale is available", async () => {
16
16
  const runtime = await createParaglide({
17
- project: await newProject({
17
+ blob: await newProject({
18
18
  settings: {
19
19
  baseLocale: "en",
20
20
  locales: ["en", "de"],
@@ -25,7 +25,7 @@ test("passes if the locale is available", async () => {
25
25
  });
26
26
  test("the return value is a Locale", async () => {
27
27
  const runtime = await createParaglide({
28
- project: await newProject({
28
+ blob: await newProject({
29
29
  settings: {
30
30
  baseLocale: "en",
31
31
  locales: ["en", "de"],
@@ -5,7 +5,7 @@ test("returns undefined if document is not available", async () => {
5
5
  // @ts-expect-error - global variable definition
6
6
  globalThis.document = undefined;
7
7
  const runtime = await createParaglide({
8
- project: await newProject({
8
+ blob: await newProject({
9
9
  settings: {
10
10
  baseLocale: "en",
11
11
  locales: ["en", "de"],
@@ -16,16 +16,14 @@ test("returns undefined if document is not available", async () => {
16
16
  });
17
17
  test("matches the locale of a cookie", async () => {
18
18
  const runtime = await createParaglide({
19
- project: await newProject({
19
+ blob: await newProject({
20
20
  settings: {
21
21
  baseLocale: "en",
22
22
  locales: ["en", "de"],
23
23
  },
24
24
  }),
25
- compilerOptions: {
26
- strategy: ["cookie"],
27
- cookieName: "PARAGLIDE_LOCALE",
28
- },
25
+ strategy: ["cookie"],
26
+ cookieName: "PARAGLIDE_LOCALE",
29
27
  });
30
28
  // @ts-expect-error - global variable definition
31
29
  globalThis.document = {};
@@ -39,16 +37,14 @@ test("matches the locale of a cookie", async () => {
39
37
  // different locales
40
38
  test("returns undefined if the locale is not defined in the locales", async () => {
41
39
  const runtime = await createParaglide({
42
- project: await newProject({
40
+ blob: await newProject({
43
41
  settings: {
44
42
  baseLocale: "en",
45
43
  locales: ["en", "de"],
46
44
  },
47
45
  }),
48
- compilerOptions: {
49
- strategy: ["cookie"],
50
- cookieName: "PARAGLIDE_LOCALE",
51
- },
46
+ strategy: ["cookie"],
47
+ cookieName: "PARAGLIDE_LOCALE",
52
48
  });
53
49
  // @ts-expect-error - global variable definition
54
50
  globalThis.document = {};
@@ -3,16 +3,14 @@ import { createParaglide } from "../create-paraglide.js";
3
3
  import { newProject } from "@inlang/sdk";
4
4
  test("returns the locale from the cookie", async () => {
5
5
  const runtime = await createParaglide({
6
- project: await newProject({
6
+ blob: await newProject({
7
7
  settings: {
8
8
  baseLocale: "en",
9
9
  locales: ["en", "fr"],
10
10
  },
11
11
  }),
12
- compilerOptions: {
13
- strategy: ["cookie"],
14
- cookieName: "PARAGLIDE_LOCALE",
15
- },
12
+ strategy: ["cookie"],
13
+ cookieName: "PARAGLIDE_LOCALE",
16
14
  });
17
15
  const request = new Request("http://example.com", {
18
16
  headers: {
@@ -24,21 +22,19 @@ test("returns the locale from the cookie", async () => {
24
22
  });
25
23
  test("returns the locale from the pathname for document requests", async () => {
26
24
  const runtime = await createParaglide({
27
- project: await newProject({
25
+ blob: await newProject({
28
26
  settings: {
29
27
  baseLocale: "en",
30
28
  locales: ["en"],
31
29
  },
32
30
  }),
33
- compilerOptions: {
34
- strategy: ["url", "baseLocale"],
35
- urlPatterns: [
36
- {
37
- pattern: "https://example.com/:path(.*)",
38
- localized: [["en", "https://example.com/en/:path(.*)"]],
39
- },
40
- ],
41
- },
31
+ strategy: ["url", "baseLocale"],
32
+ urlPatterns: [
33
+ {
34
+ pattern: "https://example.com/:path(.*)",
35
+ localized: [["en", "https://example.com/en/:path(.*)"]],
36
+ },
37
+ ],
42
38
  });
43
39
  const request = new Request("https://example.com/en/home", {
44
40
  headers: {
@@ -50,15 +46,13 @@ test("returns the locale from the pathname for document requests", async () => {
50
46
  });
51
47
  test("returns the baseLocale if no other strategy matches", async () => {
52
48
  const runtime = await createParaglide({
53
- project: await newProject({
49
+ blob: await newProject({
54
50
  settings: {
55
51
  baseLocale: "en",
56
52
  locales: ["en"],
57
53
  },
58
54
  }),
59
- compilerOptions: {
60
- strategy: ["baseLocale"],
61
- },
55
+ strategy: ["baseLocale"],
62
56
  });
63
57
  const request = new Request("http://example.com");
64
58
  const locale = runtime.extractLocaleFromRequest(request);
@@ -66,30 +60,26 @@ test("returns the baseLocale if no other strategy matches", async () => {
66
60
  });
67
61
  test("throws an error if no locale is found", async () => {
68
62
  const runtime = await createParaglide({
69
- project: await newProject({
63
+ blob: await newProject({
70
64
  settings: {
71
65
  baseLocale: "en",
72
66
  locales: ["en"],
73
67
  },
74
68
  }),
75
- compilerOptions: {
76
- strategy: ["cookie"],
77
- },
69
+ strategy: ["cookie"],
78
70
  });
79
71
  const request = new Request("http://example.com");
80
72
  expect(() => runtime.extractLocaleFromRequest(request)).toThrow("No locale found. There is an error in your strategy. Try adding 'baseLocale' as the very last strategy.");
81
73
  });
82
74
  test("returns the preferred locale from Accept-Language header", async () => {
83
75
  const runtime = await createParaglide({
84
- project: await newProject({
76
+ blob: await newProject({
85
77
  settings: {
86
78
  baseLocale: "en",
87
79
  locales: ["en", "fr", "de"],
88
80
  },
89
81
  }),
90
- compilerOptions: {
91
- strategy: ["preferredLanguage", "baseLocale"],
92
- },
82
+ strategy: ["preferredLanguage", "baseLocale"],
93
83
  });
94
84
  // simple direct match
95
85
  const request = new Request("http://example.com", {
@@ -119,16 +109,14 @@ test("returns the preferred locale from Accept-Language header", async () => {
119
109
  });
120
110
  test("should fall back to next strategy when cookie contains invalid locale", async () => {
121
111
  const runtime = await createParaglide({
122
- project: await newProject({
112
+ blob: await newProject({
123
113
  settings: {
124
114
  baseLocale: "en",
125
115
  locales: ["en", "fr"],
126
116
  },
127
117
  }),
128
- compilerOptions: {
129
- strategy: ["cookie", "baseLocale"],
130
- cookieName: "PARAGLIDE_LOCALE",
131
- },
118
+ strategy: ["cookie", "baseLocale"],
119
+ cookieName: "PARAGLIDE_LOCALE",
132
120
  });
133
121
  // Request with an invalid locale in cookie
134
122
  const request = new Request("http://example.com", {
@@ -141,15 +129,13 @@ test("should fall back to next strategy when cookie contains invalid locale", as
141
129
  });
142
130
  test("skips over localStorage strategy as it is not supported on the server", async () => {
143
131
  const runtime = await createParaglide({
144
- project: await newProject({
132
+ blob: await newProject({
145
133
  settings: {
146
134
  baseLocale: "en",
147
135
  locales: ["en", "fr"],
148
136
  },
149
137
  }),
150
- compilerOptions: {
151
- strategy: ["localStorage", "baseLocale"],
152
- },
138
+ strategy: ["localStorage", "baseLocale"],
153
139
  });
154
140
  const request = new Request("http://example.com");
155
141
  // expecting baseLocale
@@ -157,24 +143,22 @@ test("skips over localStorage strategy as it is not supported on the server", as
157
143
  });
158
144
  test("resolves the locale from the url for all request types", async () => {
159
145
  const runtime = await createParaglide({
160
- project: await newProject({
146
+ blob: await newProject({
161
147
  settings: {
162
148
  baseLocale: "en",
163
149
  locales: ["en", "fr"],
164
150
  },
165
151
  }),
166
- compilerOptions: {
167
- strategy: ["url", "baseLocale"],
168
- urlPatterns: [
169
- {
170
- pattern: "https://example.com/:path(.*)",
171
- localized: [
172
- ["en", "https://example.com/en/:path(.*)"],
173
- ["fr", "https://example.com/fr/:path(.*)"],
174
- ],
175
- },
176
- ],
177
- },
152
+ strategy: ["url", "baseLocale"],
153
+ urlPatterns: [
154
+ {
155
+ pattern: "https://example.com/:path(.*)",
156
+ localized: [
157
+ ["en", "https://example.com/en/:path(.*)"],
158
+ ["fr", "https://example.com/fr/:path(.*)"],
159
+ ],
160
+ },
161
+ ],
178
162
  });
179
163
  // Non-document request should still use URL strategy
180
164
  const request = new Request("https://example.com/fr/home", {
@@ -187,26 +171,24 @@ test("resolves the locale from the url for all request types", async () => {
187
171
  });
188
172
  test("cookie strategy precedes URL strategy for API requests with wildcards", async () => {
189
173
  const runtime = await createParaglide({
190
- project: await newProject({
174
+ blob: await newProject({
191
175
  settings: {
192
176
  baseLocale: "en",
193
177
  locales: ["en", "fr", "de"],
194
178
  },
195
179
  }),
196
- compilerOptions: {
197
- strategy: ["cookie", "url", "baseLocale"],
198
- cookieName: "PARAGLIDE_LOCALE",
199
- urlPatterns: [
200
- {
201
- pattern: "https://example.com/:path(.*)",
202
- localized: [
203
- ["fr", "https://example.com/fr/:path(.*)"],
204
- ["de", "https://example.com/de/:path(.*)"],
205
- ["en", "https://example.com/:path(.*)"],
206
- ],
207
- },
208
- ],
209
- },
180
+ strategy: ["cookie", "url", "baseLocale"],
181
+ cookieName: "PARAGLIDE_LOCALE",
182
+ urlPatterns: [
183
+ {
184
+ pattern: "https://example.com/:path(.*)",
185
+ localized: [
186
+ ["fr", "https://example.com/fr/:path(.*)"],
187
+ ["de", "https://example.com/de/:path(.*)"],
188
+ ["en", "https://example.com/:path(.*)"],
189
+ ],
190
+ },
191
+ ],
210
192
  });
211
193
  // API request with cookie should use cookie locale
212
194
  const apiRequestWithCookie = new Request("https://example.com/api/data", {
@@ -229,24 +211,22 @@ test("cookie strategy precedes URL strategy for API requests with wildcards", as
229
211
  // https://github.com/opral/inlang-paraglide-js/issues/436
230
212
  test("preferredLanguage precedence over url", async () => {
231
213
  const runtime = await createParaglide({
232
- project: await newProject({
214
+ blob: await newProject({
233
215
  settings: {
234
216
  baseLocale: "en",
235
217
  locales: ["en", "de"],
236
218
  },
237
219
  }),
238
- compilerOptions: {
239
- strategy: ["url", "preferredLanguage"],
240
- urlPatterns: [
241
- {
242
- pattern: ":protocol://:domain(.*)::port??/:path(.*)?",
243
- localized: [
244
- ["de", ":protocol://:domain(.*)::port?/de/:path(.*)?"],
245
- ["en", ":protocol://:domain(.*)::port?/en/:path(.*)?"],
246
- ],
247
- },
248
- ],
249
- },
220
+ strategy: ["url", "preferredLanguage"],
221
+ urlPatterns: [
222
+ {
223
+ pattern: ":protocol://:domain(.*)::port??/:path(.*)?",
224
+ localized: [
225
+ ["de", ":protocol://:domain(.*)::port?/de/:path(.*)?"],
226
+ ["en", ":protocol://:domain(.*)::port?/en/:path(.*)?"],
227
+ ],
228
+ },
229
+ ],
250
230
  });
251
231
  // no locale in url, should use preferredLanguage
252
232
  const request = new Request("https://example.com/home", {
@@ -1 +1 @@
1
- {"version":3,"file":"extract-locale-from-url.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/extract-locale-from-url.js"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,0CAHW,GAAG,GAAC,MAAM,GACR,MAAM,GAAC,SAAS,CA4B5B"}
1
+ {"version":3,"file":"extract-locale-from-url.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/extract-locale-from-url.js"],"names":[],"mappings":"AAmBA;;;;;GAKG;AACH,0CAHW,GAAG,GAAC,MAAM,GACR,MAAM,GAAC,SAAS,CAuC5B"}
@@ -1,6 +1,16 @@
1
1
  import { assertIsLocale } from "./assert-is-locale.js";
2
2
  import { isLocale } from "./is-locale.js";
3
3
  import { baseLocale, TREE_SHAKE_DEFAULT_URL_PATTERN_USED, urlPatterns, } from "./variables.js";
4
+ /**
5
+ * If extractLocaleFromUrl is called many times on the same page and the URL
6
+ * hasn't changed, we don't need to recompute it every time which can get expensive.
7
+ * We might use a LRU cache if needed, but for now storing only the last result is enough.
8
+ * https://github.com/opral/monorepo/pull/3575#discussion_r2066731243
9
+ */
10
+ /** @type {string|undefined} */
11
+ let cachedUrl;
12
+ /** @type {Locale|undefined} */
13
+ let cachedLocale;
4
14
  /**
5
15
  * Extracts the locale from a given URL using native URLPattern.
6
16
  *
@@ -8,24 +18,36 @@ import { baseLocale, TREE_SHAKE_DEFAULT_URL_PATTERN_USED, urlPatterns, } from ".
8
18
  * @returns {Locale|undefined} The extracted locale, or undefined if no locale is found.
9
19
  */
10
20
  export function extractLocaleFromUrl(url) {
21
+ const urlString = typeof url === "string" ? url : url.href;
22
+ if (cachedUrl === urlString) {
23
+ return cachedLocale;
24
+ }
25
+ let result;
11
26
  if (TREE_SHAKE_DEFAULT_URL_PATTERN_USED) {
12
- return defaultUrlPatternExtractLocale(url);
27
+ result = defaultUrlPatternExtractLocale(url);
13
28
  }
14
- const urlObj = typeof url === "string" ? new URL(url) : url;
15
- // Iterate over URL patterns
16
- for (const element of urlPatterns) {
17
- for (const [locale, localizedPattern] of element.localized) {
18
- const match = new URLPattern(localizedPattern, urlObj.href).exec(urlObj.href);
19
- if (!match) {
20
- continue;
21
- }
22
- // Check if the locale is valid
23
- if (assertIsLocale(locale)) {
24
- return locale;
29
+ else {
30
+ const urlObj = typeof url === "string" ? new URL(url) : url;
31
+ // Iterate over URL patterns
32
+ for (const element of urlPatterns) {
33
+ for (const [locale, localizedPattern] of element.localized) {
34
+ const match = new URLPattern(localizedPattern, urlObj.href).exec(urlObj.href);
35
+ if (!match) {
36
+ continue;
37
+ }
38
+ // Check if the locale is valid
39
+ if (assertIsLocale(locale)) {
40
+ result = locale;
41
+ break;
42
+ }
25
43
  }
44
+ if (result)
45
+ break;
26
46
  }
27
47
  }
28
- return undefined;
48
+ cachedUrl = urlString;
49
+ cachedLocale = result;
50
+ return result;
29
51
  }
30
52
  /**
31
53
  * https://github.com/opral/inlang-paraglide-js/issues/381