@inlang/paraglide-js 2.0.0-beta.17 → 2.0.0-beta.19
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.
- package/README.md +3 -3
- package/dist/bundler-plugins/unplugin.d.ts +1 -1
- package/dist/bundler-plugins/unplugin.d.ts.map +1 -1
- package/dist/bundler-plugins/unplugin.js +27 -9
- package/dist/cli/commands/compile/command.d.ts.map +1 -1
- package/dist/cli/commands/compile/command.js +21 -22
- package/dist/cli/commands/init/command.d.ts.map +1 -1
- package/dist/cli/commands/init/command.js +5 -2
- package/dist/cli/index.d.ts +0 -4
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +0 -4
- package/dist/cli/steps/index.d.ts +0 -1
- package/dist/cli/steps/index.d.ts.map +1 -1
- package/dist/cli/steps/index.js +0 -1
- package/dist/cli/steps/run-compiler.d.ts.map +1 -1
- package/dist/compiler/compile-bundle.d.ts +0 -2
- package/dist/compiler/compile-bundle.d.ts.map +1 -1
- package/dist/compiler/compile-bundle.js +1 -1
- package/dist/compiler/compile-bundle.test.js +0 -2
- package/dist/compiler/compile-local-variable.d.ts +17 -0
- package/dist/compiler/compile-local-variable.d.ts.map +1 -0
- package/dist/compiler/compile-local-variable.js +38 -0
- package/dist/compiler/compile-local-variable.test.d.ts +2 -0
- package/dist/compiler/compile-local-variable.test.d.ts.map +1 -0
- package/dist/compiler/compile-local-variable.test.js +71 -0
- package/dist/compiler/compile-message.d.ts +1 -2
- package/dist/compiler/compile-message.d.ts.map +1 -1
- package/dist/compiler/compile-message.js +31 -8
- package/dist/compiler/compile-message.test.js +166 -6
- package/dist/compiler/compile-pattern.d.ts +13 -5
- package/dist/compiler/compile-pattern.d.ts.map +1 -1
- package/dist/compiler/compile-pattern.js +31 -19
- package/dist/compiler/compile-pattern.test.js +43 -35
- package/dist/compiler/compile-project.d.ts +1 -1
- package/dist/compiler/compile-project.d.ts.map +1 -1
- package/dist/compiler/compile-project.js +1 -3
- package/dist/compiler/compile-project.test.js +84 -6
- package/dist/compiler/compile.d.ts +1 -123
- package/dist/compiler/compile.d.ts.map +1 -1
- package/dist/compiler/compile.js +1 -9
- package/dist/compiler/compile.test.js +3 -2
- package/dist/compiler/compiler-options.d.ts +172 -0
- package/dist/compiler/compiler-options.d.ts.map +1 -0
- package/dist/compiler/compiler-options.js +8 -0
- package/dist/compiler/index.d.ts +1 -1
- package/dist/compiler/index.d.ts.map +1 -1
- package/dist/compiler/index.js +1 -1
- package/dist/compiler/output-structure/locale-modules.d.ts +1 -1
- package/dist/compiler/output-structure/locale-modules.d.ts.map +1 -1
- package/dist/compiler/output-structure/locale-modules.js +13 -11
- package/dist/compiler/output-structure/locale-modules.test.js +1 -1
- package/dist/compiler/output-structure/message-modules.d.ts +1 -1
- package/dist/compiler/output-structure/message-modules.d.ts.map +1 -1
- package/dist/compiler/output-structure/message-modules.js +7 -2
- package/dist/compiler/output-structure/message-modules.test.js +1 -1
- package/dist/compiler/registry.d.ts +10 -10
- package/dist/compiler/registry.d.ts.map +1 -1
- package/dist/compiler/registry.js +20 -31
- package/dist/compiler/runtime/create-runtime.d.ts +2 -3
- package/dist/compiler/runtime/create-runtime.d.ts.map +1 -1
- package/dist/compiler/runtime/create-runtime.js +30 -61
- package/dist/compiler/runtime/e2e.test.js +17 -0
- package/dist/compiler/runtime/extract-locale-from-cookie.d.ts.map +1 -1
- package/dist/compiler/runtime/extract-locale-from-cookie.js +6 -1
- package/dist/compiler/runtime/extract-locale-from-cookie.test.js +18 -0
- package/dist/compiler/runtime/extract-locale-from-request.d.ts +7 -1
- package/dist/compiler/runtime/extract-locale-from-request.d.ts.map +1 -1
- package/dist/compiler/runtime/extract-locale-from-request.js +11 -5
- package/dist/compiler/runtime/extract-locale-from-request.test.js +10 -6
- package/dist/compiler/runtime/extract-locale-from-url.d.ts +8 -0
- package/dist/compiler/runtime/extract-locale-from-url.d.ts.map +1 -0
- package/dist/compiler/runtime/extract-locale-from-url.js +48 -0
- package/dist/compiler/runtime/extract-locale-from-url.test.d.ts +2 -0
- package/dist/compiler/runtime/extract-locale-from-url.test.d.ts.map +1 -0
- package/dist/compiler/runtime/extract-locale-from-url.test.js +89 -0
- package/dist/compiler/runtime/get-locale.d.ts +16 -0
- package/dist/compiler/runtime/get-locale.d.ts.map +1 -1
- package/dist/compiler/runtime/get-locale.js +36 -11
- package/dist/compiler/runtime/get-locale.test.js +57 -10
- package/dist/compiler/runtime/get-url-origin.d.ts +20 -0
- package/dist/compiler/runtime/get-url-origin.d.ts.map +1 -0
- package/dist/compiler/runtime/get-url-origin.js +29 -0
- package/dist/compiler/runtime/get-url-origin.test.d.ts +2 -0
- package/dist/compiler/runtime/get-url-origin.test.d.ts.map +1 -0
- package/dist/compiler/runtime/get-url-origin.test.js +19 -0
- package/dist/compiler/runtime/localize-href.d.ts +46 -0
- package/dist/compiler/runtime/localize-href.d.ts.map +1 -0
- package/dist/compiler/runtime/localize-href.js +82 -0
- package/dist/compiler/runtime/localize-href.test.d.ts +2 -0
- package/dist/compiler/runtime/localize-href.test.d.ts.map +1 -0
- package/dist/compiler/runtime/localize-href.test.js +136 -0
- package/dist/compiler/runtime/localize-url.d.ts +13 -0
- package/dist/compiler/runtime/localize-url.d.ts.map +1 -0
- package/dist/compiler/runtime/localize-url.js +115 -0
- package/dist/compiler/runtime/localize-url.test.d.ts +2 -0
- package/dist/compiler/runtime/localize-url.test.d.ts.map +1 -0
- package/dist/compiler/runtime/localize-url.test.js +204 -0
- package/dist/compiler/runtime/server-middleware.d.ts +13 -0
- package/dist/compiler/runtime/server-middleware.d.ts.map +1 -0
- package/dist/compiler/runtime/server-middleware.js +34 -0
- package/dist/compiler/runtime/server-middleware.test.d.ts +2 -0
- package/dist/compiler/runtime/server-middleware.test.d.ts.map +1 -0
- package/dist/compiler/runtime/server-middleware.test.js +53 -0
- package/dist/compiler/runtime/set-locale.d.ts +1 -0
- package/dist/compiler/runtime/set-locale.d.ts.map +1 -1
- package/dist/compiler/runtime/set-locale.js +30 -18
- package/dist/compiler/runtime/set-locale.test.js +23 -9
- package/dist/compiler/runtime/type.d.ts +13 -12
- package/dist/compiler/runtime/type.d.ts.map +1 -1
- package/dist/compiler/runtime/type.test.js +1 -1
- package/dist/compiler/runtime/variables.d.ts +10 -18
- package/dist/compiler/runtime/variables.d.ts.map +1 -1
- package/dist/compiler/runtime/variables.js +5 -17
- package/dist/compiler/types.d.ts +1 -1
- package/dist/compiler/urlpattern-polyfill/index.d.ts +2 -0
- package/dist/compiler/urlpattern-polyfill/index.d.ts.map +1 -0
- package/dist/compiler/urlpattern-polyfill/index.js +1 -0
- package/dist/playground.d.ts +2 -0
- package/dist/playground.d.ts.map +1 -0
- package/dist/playground.js +5 -0
- package/dist/services/env-variables/index.js +1 -1
- package/dist/services/logger/index.js +3 -3
- package/dist/services/telemetry/capture.d.ts +1 -2
- package/dist/services/telemetry/capture.d.ts.map +1 -1
- package/dist/services/telemetry/stack-detection.d.ts +1 -1
- package/dist/services/telemetry/stack-detection.d.ts.map +1 -1
- package/dist/services/telemetry/stack-detection.js +3 -8
- package/dist/services/telemetry/stack-detection.test.js +9 -12
- package/dist/urlpattern-polyfill/index.d.ts +1 -1
- package/dist/urlpattern-polyfill/index.d.ts.map +1 -1
- package/dist/urlpattern-polyfill/index.js +1 -1
- package/package.json +13 -9
package/README.md
CHANGED
|
@@ -35,10 +35,10 @@ _If you are using a Bundler use one of the [Bundler Plugins](usage#usage-with-a-
|
|
|
35
35
|
|
|
36
36
|
## Using messages in code
|
|
37
37
|
|
|
38
|
-
After running the compiler import the messages with `import
|
|
38
|
+
After running the compiler import the messages with `import { m } from "./paraglide/messages"`. By convention, a wildcard import is used.
|
|
39
39
|
|
|
40
40
|
```js
|
|
41
|
-
import
|
|
41
|
+
import { m } from "./paraglide/messages.js";
|
|
42
42
|
|
|
43
43
|
m.greeting({ name: "Samuel" }); // Hello Samuel!
|
|
44
44
|
```
|
|
@@ -49,7 +49,7 @@ To change the locale, use the `setLocale` function.
|
|
|
49
49
|
|
|
50
50
|
```js
|
|
51
51
|
import { setLocale } from "./paraglide/runtime.js";
|
|
52
|
-
import
|
|
52
|
+
import { m } from "./paraglide/messages.js";
|
|
53
53
|
|
|
54
54
|
m.greeting({ name: "Samuel" }); // Hello Samuel!
|
|
55
55
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { UnpluginFactory } from "unplugin";
|
|
2
|
-
import {
|
|
2
|
+
import type { CompilerOptions } from "../compiler/compiler-options.js";
|
|
3
3
|
export declare const unpluginFactory: UnpluginFactory<CompilerOptions>;
|
|
4
4
|
//# sourceMappingURL=unplugin.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unplugin.d.ts","sourceRoot":"","sources":["../../src/bundler-plugins/unplugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"unplugin.d.ts","sourceRoot":"","sources":["../../src/bundler-plugins/unplugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAMhD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAcvE,eAAO,MAAM,eAAe,EAAE,eAAe,CAAC,eAAe,CAqD3D,CAAC"}
|
|
@@ -2,19 +2,28 @@ import { compile } from "../compiler/compile.js";
|
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { nodeNormalizePath } from "../utilities/node-normalize-path.js";
|
|
5
|
-
import { Logger } from "../
|
|
5
|
+
import { Logger } from "../services/logger/index.js";
|
|
6
6
|
const PLUGIN_NAME = "unplugin-paraglide-js";
|
|
7
7
|
const logger = new Logger();
|
|
8
8
|
let compilationResult;
|
|
9
|
+
// https://github.com/opral/inlang-paraglide-js/issues/371
|
|
10
|
+
//
|
|
11
|
+
// has the second benefit that paraglide js only compiles once
|
|
12
|
+
// per enviornment build (browser, server, etc.)
|
|
13
|
+
let hasInitiallyCompiled = false;
|
|
9
14
|
export const unpluginFactory = (args) => ({
|
|
10
15
|
name: PLUGIN_NAME,
|
|
11
16
|
enforce: "pre",
|
|
12
17
|
async buildStart() {
|
|
18
|
+
if (hasInitiallyCompiled === true) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
13
21
|
logger.info("Compiling inlang project...");
|
|
14
22
|
compilationResult = await compile({
|
|
15
23
|
fs: wrappedFs,
|
|
16
24
|
...args,
|
|
17
25
|
});
|
|
26
|
+
hasInitiallyCompiled = true;
|
|
18
27
|
logger.success("Compilation complete");
|
|
19
28
|
for (const path of Array.from(readFiles)) {
|
|
20
29
|
this.addWatchFile(path);
|
|
@@ -22,17 +31,26 @@ export const unpluginFactory = (args) => ({
|
|
|
22
31
|
},
|
|
23
32
|
async watchChange(path) {
|
|
24
33
|
const shouldCompile = readFiles.has(path) && !path.includes("cache");
|
|
25
|
-
if (shouldCompile) {
|
|
26
|
-
|
|
27
|
-
logger.info(`Re-compiling inlang project... File "${path}" has changed.`);
|
|
28
|
-
compilationResult = await compile({
|
|
29
|
-
fs: wrappedFs,
|
|
30
|
-
...args,
|
|
31
|
-
}, compilationResult?.outputHashes);
|
|
32
|
-
logger.success("Compilation complete");
|
|
34
|
+
if (shouldCompile === false) {
|
|
35
|
+
return;
|
|
33
36
|
}
|
|
37
|
+
readFiles.clear();
|
|
38
|
+
logger.info(`Re-compiling inlang project... File "${path}" has changed.`);
|
|
39
|
+
compilationResult = await compile({
|
|
40
|
+
fs: wrappedFs,
|
|
41
|
+
...args,
|
|
42
|
+
}, compilationResult?.outputHashes);
|
|
43
|
+
logger.success("Compilation complete");
|
|
34
44
|
},
|
|
35
45
|
webpack(compiler) {
|
|
46
|
+
compiler.options.resolve = {
|
|
47
|
+
...compiler.options.resolve,
|
|
48
|
+
fallback: {
|
|
49
|
+
...compiler.options.resolve?.fallback,
|
|
50
|
+
// https://stackoverflow.com/a/72989932
|
|
51
|
+
async_hooks: false,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
36
54
|
//we need the compiler to run before the build so that the message-modules will be present
|
|
37
55
|
//In the other bundlers `buildStart` already runs before the build. In webpack it's a race condition
|
|
38
56
|
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, async () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command.d.ts","sourceRoot":"","sources":["../../../../src/cli/commands/compile/command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"command.d.ts","sourceRoot":"","sources":["../../../../src/cli/commands/compile/command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAUpC,eAAO,MAAM,cAAc,SAkDzB,CAAC"}
|
|
@@ -1,38 +1,37 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
|
-
import fs from "node:fs";
|
|
3
2
|
import { resolve } from "node:path";
|
|
4
3
|
import { Logger } from "../../../services/logger/index.js";
|
|
5
|
-
import { runCompiler } from "../../steps/run-compiler.js";
|
|
6
4
|
import { DEFAULT_OUTDIR } from "../../defaults.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
5
|
+
import { compile } from "../../../compiler/compile.js";
|
|
6
|
+
import { defaultCompilerOptions, } from "../../../compiler/compiler-options.js";
|
|
9
7
|
export const compileCommand = new Command()
|
|
10
8
|
.name("compile")
|
|
11
9
|
.summary("Compiles inlang Paraglide-JS")
|
|
12
10
|
.requiredOption("--project <path>", 'The path to the inlang project. Example: "./project.inlang"')
|
|
13
11
|
.requiredOption("--outdir <path>", 'The path to the output directory. Example: "./src/paraglide"', DEFAULT_OUTDIR)
|
|
12
|
+
.option("--strategy <items...>", [
|
|
13
|
+
"The strategy to be used.",
|
|
14
|
+
"",
|
|
15
|
+
"Example: --strategy cookie globalVariable baseLocale",
|
|
16
|
+
"Read more on https://inlang.com/m/gerre34r/library-inlang-paraglideJs/strategy",
|
|
17
|
+
].join("\n"))
|
|
14
18
|
.requiredOption("--silent", "Only log errors to the console", false)
|
|
15
19
|
.action(async (options) => {
|
|
16
20
|
const logger = new Logger({ silent: options.silent, prefix: true });
|
|
17
21
|
const path = resolve(process.cwd(), options.project);
|
|
18
|
-
logger.info(`Compiling inlang project
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
project,
|
|
26
|
-
fs: fs.promises,
|
|
27
|
-
outdir: options.outdir,
|
|
28
|
-
});
|
|
29
|
-
logger.success("Successfully compiled the project.");
|
|
30
|
-
const errors = await project.errors.get();
|
|
31
|
-
if (errors.length > 0) {
|
|
32
|
-
logger.warn(`But the project reported the following warnings and/or errors that might have influenced compilation:`);
|
|
33
|
-
for (const error of errors) {
|
|
34
|
-
logger.error(`${error}`);
|
|
35
|
-
}
|
|
22
|
+
logger.info(`Compiling inlang project ...`);
|
|
23
|
+
try {
|
|
24
|
+
await compile({
|
|
25
|
+
project: path,
|
|
26
|
+
outdir: options.outdir,
|
|
27
|
+
strategy: options.strategy ?? defaultCompilerOptions.strategy,
|
|
28
|
+
});
|
|
36
29
|
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
logger.error("Error while compiling inlang project.");
|
|
32
|
+
logger.error(e);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
logger.success(`Successfully compiled inlang project.`);
|
|
37
36
|
process.exit(0);
|
|
38
37
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command.d.ts","sourceRoot":"","sources":["../../../../src/cli/commands/init/command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;
|
|
1
|
+
{"version":3,"file":"command.d.ts","sourceRoot":"","sources":["../../../../src/cli/commands/init/command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAQ3D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAO9C,eAAO,MAAM,WAAW,SAiErB,CAAC;AAEJ,eAAO,MAAM,+BAA+B,EAAE,OAAO,CACpD;IACC,EAAE,EAAE,cAAc,kBAAkB,CAAC,CAAC;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;CACxB,EACD,OAAO,CAYP,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,OAAO,CAC7C;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,cAAc,kBAAkB,CAAC,CAAA;CAAE,EACzD;IAAE,eAAe,EAAE,MAAM,CAAA;CAAE,CAU3B,CAAC;AAEF,eAAO,MAAM,2BAA2B,EAAE,OAAO,CAChD;IACC,EAAE,EAAE,cAAc,kBAAkB,CAAC,CAAC;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;CACxB,EACD,OAAO,CAuDP,CAAC"}
|
|
@@ -9,11 +9,11 @@ import { maybeAddSherlock } from "../../steps/maybe-add-sherlock.js";
|
|
|
9
9
|
import { maybeUpdateTsConfig } from "../../steps/update-ts-config.js";
|
|
10
10
|
import { promptForOutdir } from "../../steps/prompt-for-outdir.js";
|
|
11
11
|
import { updatePackageJson } from "../../steps/update-package-json.js";
|
|
12
|
-
import { runCompiler } from "../../steps/run-compiler.js";
|
|
13
12
|
import nodeFs from "node:fs";
|
|
14
13
|
import { ENV_VARIABLES } from "../../../services/env-variables/index.js";
|
|
15
14
|
import { detectBundler } from "../../steps/detect-bundler.js";
|
|
16
15
|
import { addVitePlugin } from "../../steps/add-vite-plugin.js";
|
|
16
|
+
import { compile } from "../../../compiler/compile.js";
|
|
17
17
|
export const initCommand = new Command()
|
|
18
18
|
.name("init")
|
|
19
19
|
.summary("Initializes inlang Paraglide-JS.")
|
|
@@ -41,7 +41,10 @@ export const initCommand = new Command()
|
|
|
41
41
|
const ctx7 = await maybeUpdateTsConfig(ctx6);
|
|
42
42
|
const ctx8 = await maybeAddSherlock(ctx7);
|
|
43
43
|
try {
|
|
44
|
-
await
|
|
44
|
+
await compile({
|
|
45
|
+
project: ctx8.projectPath,
|
|
46
|
+
outdir: ctx8.outdir,
|
|
47
|
+
});
|
|
45
48
|
ctx.logger.success("Run paraglide compiler");
|
|
46
49
|
}
|
|
47
50
|
catch {
|
package/dist/cli/index.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
export declare const cli: Command;
|
|
3
|
-
export * as Utils from "./utils.js";
|
|
4
|
-
export * as Defaults from "./defaults.js";
|
|
5
|
-
export * as Steps from "./steps/index.js";
|
|
6
|
-
export { Logger } from "../services/logger/index.js";
|
|
7
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/cli/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAKpC,eAAO,MAAM,GAAG,SAK8B,CAAC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAKpC,eAAO,MAAM,GAAG,SAK8B,CAAC"}
|
package/dist/cli/index.js
CHANGED
|
@@ -8,7 +8,3 @@ export const cli = new Command()
|
|
|
8
8
|
.addCommand(initCommand)
|
|
9
9
|
.showHelpAfterError()
|
|
10
10
|
.version(ENV_VARIABLES.PARJS_PACKAGE_VERSION);
|
|
11
|
-
export * as Utils from "./utils.js";
|
|
12
|
-
export * as Defaults from "./defaults.js";
|
|
13
|
-
export * as Steps from "./steps/index.js";
|
|
14
|
-
export { Logger } from "../services/logger/index.js";
|
|
@@ -4,7 +4,6 @@ export { detectBundler } from "./detect-bundler.js";
|
|
|
4
4
|
export { initializeInlangProject } from "./initialize-inlang-project.js";
|
|
5
5
|
export { maybeAddSherlock } from "./maybe-add-sherlock.js";
|
|
6
6
|
export { promptForOutdir } from "./prompt-for-outdir.js";
|
|
7
|
-
export { runCompiler } from "./run-compiler.js";
|
|
8
7
|
export { updatePackageJson } from "./update-package-json.js";
|
|
9
8
|
export { maybeUpdateTsConfig } from "./update-ts-config.js";
|
|
10
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/cli/steps/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAC/E,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/cli/steps/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAC/E,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC"}
|
package/dist/cli/steps/index.js
CHANGED
|
@@ -4,6 +4,5 @@ export { detectBundler } from "./detect-bundler.js";
|
|
|
4
4
|
export { initializeInlangProject } from "./initialize-inlang-project.js";
|
|
5
5
|
export { maybeAddSherlock } from "./maybe-add-sherlock.js";
|
|
6
6
|
export { promptForOutdir } from "./prompt-for-outdir.js";
|
|
7
|
-
export { runCompiler } from "./run-compiler.js";
|
|
8
7
|
export { updatePackageJson } from "./update-package-json.js";
|
|
9
8
|
export { maybeUpdateTsConfig } from "./update-ts-config.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run-compiler.d.ts","sourceRoot":"","sources":["../../../src/cli/steps/run-compiler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzB,eAAO,MAAM,WAAW,EAAE,OAAO,CAChC;IACC,OAAO,EAAE,aAAa,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC;CACvB,EACD,OAAO,
|
|
1
|
+
{"version":3,"file":"run-compiler.d.ts","sourceRoot":"","sources":["../../../src/cli/steps/run-compiler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzB,eAAO,MAAM,WAAW,EAAE,OAAO,CAChC;IACC,OAAO,EAAE,aAAa,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC;CACvB,EACD,OAAO,CAWP,CAAC"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { Bundle, BundleNested, Message } from "@inlang/sdk";
|
|
2
|
-
import type { Registry } from "./registry.js";
|
|
3
2
|
import type { Compiled } from "./types.js";
|
|
4
3
|
export type CompiledBundleWithMessages = {
|
|
5
4
|
/** The compilation result for the bundle index */
|
|
@@ -15,6 +14,5 @@ export type CompiledBundleWithMessages = {
|
|
|
15
14
|
export declare const compileBundle: (args: {
|
|
16
15
|
bundle: BundleNested;
|
|
17
16
|
fallbackMap: Record<string, string | undefined>;
|
|
18
|
-
registry: Registry;
|
|
19
17
|
}) => CompiledBundleWithMessages;
|
|
20
18
|
//# sourceMappingURL=compile-bundle.d.ts.map
|
|
@@ -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;
|
|
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;AAKjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAI3C,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;CAChD,KAAG,0BAmCH,CAAC"}
|
|
@@ -20,7 +20,7 @@ export const compileBundle = (args) => {
|
|
|
20
20
|
if (compiledMessages[message.locale]) {
|
|
21
21
|
throw new Error(`Duplicate locale: ${message.locale}`);
|
|
22
22
|
}
|
|
23
|
-
const compiledMessage = compileMessage(args.bundle.declarations, message, message.variants
|
|
23
|
+
const compiledMessage = compileMessage(args.bundle.declarations, message, message.variants);
|
|
24
24
|
// set the pattern for the language tag
|
|
25
25
|
compiledMessages[message.locale] = compiledMessage;
|
|
26
26
|
}
|
|
@@ -6,7 +6,6 @@ test("compiles to jsdoc", async () => {
|
|
|
6
6
|
en: "en",
|
|
7
7
|
"en-US": "en",
|
|
8
8
|
},
|
|
9
|
-
registry: {},
|
|
10
9
|
bundle: mockBundle,
|
|
11
10
|
});
|
|
12
11
|
expect(result.bundle.code).toMatchInlineSnapshot(`"/**
|
|
@@ -60,7 +59,6 @@ const mockBundle = {
|
|
|
60
59
|
test("throws if a JS keyword is used as an identifier", async () => {
|
|
61
60
|
expect(() => compileBundle({
|
|
62
61
|
fallbackMap: {},
|
|
63
|
-
registry: {},
|
|
64
62
|
bundle: {
|
|
65
63
|
id: "then",
|
|
66
64
|
declarations: [],
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { LocalVariable } from "@inlang/sdk";
|
|
2
|
+
/**
|
|
3
|
+
* Compiles a local variable.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const code = compileLocalVariable({
|
|
7
|
+
* type: "local-variable",
|
|
8
|
+
* name: "myVar",
|
|
9
|
+
* value: { type: "literal", value: "Hello" }
|
|
10
|
+
* });
|
|
11
|
+
* >> code === "const myVar = 'Hello';"
|
|
12
|
+
*/
|
|
13
|
+
export declare function compileLocalVariable(args: {
|
|
14
|
+
locale: string;
|
|
15
|
+
declaration: LocalVariable;
|
|
16
|
+
}): string;
|
|
17
|
+
//# sourceMappingURL=compile-local-variable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-local-variable.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-local-variable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGX,aAAa,EAEb,MAAM,aAAa,CAAC;AAErB;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE;IAC1C,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,aAAa,CAAC;CAC3B,GAAG,MAAM,CAUT"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiles a local variable.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const code = compileLocalVariable({
|
|
6
|
+
* type: "local-variable",
|
|
7
|
+
* name: "myVar",
|
|
8
|
+
* value: { type: "literal", value: "Hello" }
|
|
9
|
+
* });
|
|
10
|
+
* >> code === "const myVar = 'Hello';"
|
|
11
|
+
*/
|
|
12
|
+
export function compileLocalVariable(args) {
|
|
13
|
+
const annotation = args.declaration.value.annotation;
|
|
14
|
+
const value = compileAnnotation(compileLiteralOrVarRef(args.declaration.value.arg), args.locale, annotation);
|
|
15
|
+
return `const ${args.declaration.name} = ${value};`;
|
|
16
|
+
}
|
|
17
|
+
function compileAnnotation(str, locale, annotation) {
|
|
18
|
+
if (!annotation) {
|
|
19
|
+
return str;
|
|
20
|
+
}
|
|
21
|
+
return `registry.${annotation.name}("${locale}", ${str}, ${compileOptions(annotation.options)})`;
|
|
22
|
+
}
|
|
23
|
+
function compileOptions(options) {
|
|
24
|
+
if (options.length === 0) {
|
|
25
|
+
return "{}";
|
|
26
|
+
}
|
|
27
|
+
const entries = options.map((option) => `${option.name}: ${compileLiteralOrVarRef(option.value)}`);
|
|
28
|
+
const code = "{ " + entries.join(", ") + " }";
|
|
29
|
+
return code;
|
|
30
|
+
}
|
|
31
|
+
function compileLiteralOrVarRef(value) {
|
|
32
|
+
switch (value.type) {
|
|
33
|
+
case "literal":
|
|
34
|
+
return `"${value.value}"`;
|
|
35
|
+
case "variable-reference":
|
|
36
|
+
return `i.${value.name}`;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-local-variable.test.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-local-variable.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { compileLocalVariable } from "./compile-local-variable.js";
|
|
3
|
+
test("compiles a literal local variable", () => {
|
|
4
|
+
const code = compileLocalVariable({
|
|
5
|
+
locale: "en",
|
|
6
|
+
declaration: {
|
|
7
|
+
type: "local-variable",
|
|
8
|
+
name: "myVar",
|
|
9
|
+
value: { type: "expression", arg: { type: "literal", value: "Hello" } },
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
expect(code).toEqual('const myVar = "Hello";');
|
|
13
|
+
});
|
|
14
|
+
test("compiles a variable reference local variable", () => {
|
|
15
|
+
const code = compileLocalVariable({
|
|
16
|
+
locale: "en",
|
|
17
|
+
declaration: {
|
|
18
|
+
type: "local-variable",
|
|
19
|
+
name: "myVar",
|
|
20
|
+
value: {
|
|
21
|
+
type: "expression",
|
|
22
|
+
arg: { type: "variable-reference", name: "name" },
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
expect(code).toEqual("const myVar = i.name;");
|
|
27
|
+
});
|
|
28
|
+
test("compiles a local variable with an annotation and empty options", () => {
|
|
29
|
+
const code = compileLocalVariable({
|
|
30
|
+
locale: "en",
|
|
31
|
+
declaration: {
|
|
32
|
+
type: "local-variable",
|
|
33
|
+
name: "myVar",
|
|
34
|
+
value: {
|
|
35
|
+
type: "expression",
|
|
36
|
+
arg: { type: "literal", value: "Hello" },
|
|
37
|
+
annotation: {
|
|
38
|
+
type: "function-reference",
|
|
39
|
+
name: "myFunction",
|
|
40
|
+
options: [],
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
expect(code).toEqual('const myVar = registry.myFunction("en", "Hello", {});');
|
|
46
|
+
});
|
|
47
|
+
test("compiles a local variable with an annotation and options", () => {
|
|
48
|
+
const code = compileLocalVariable({
|
|
49
|
+
locale: "en",
|
|
50
|
+
declaration: {
|
|
51
|
+
type: "local-variable",
|
|
52
|
+
name: "myVar",
|
|
53
|
+
value: {
|
|
54
|
+
type: "expression",
|
|
55
|
+
arg: { type: "literal", value: "Hello" },
|
|
56
|
+
annotation: {
|
|
57
|
+
type: "function-reference",
|
|
58
|
+
name: "myFunction",
|
|
59
|
+
options: [
|
|
60
|
+
{ name: "option1", value: { type: "literal", value: "value1" } },
|
|
61
|
+
{
|
|
62
|
+
name: "option2",
|
|
63
|
+
value: { type: "variable-reference", name: "varRef" },
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
expect(code).toEqual('const myVar = registry.myFunction("en", "Hello", { option1: "value1", option2: i.varRef });');
|
|
71
|
+
});
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type { Declaration, Message, Variant } from "@inlang/sdk";
|
|
2
|
-
import type { Registry } from "./registry.js";
|
|
3
2
|
import type { Compiled } from "./types.js";
|
|
4
3
|
/**
|
|
5
4
|
* Returns the compiled message as a string
|
|
6
5
|
*
|
|
7
6
|
*/
|
|
8
|
-
export declare const compileMessage: (declarations: Declaration[], message: Message, variants: Variant[]
|
|
7
|
+
export declare const compileMessage: (declarations: Declaration[], message: Message, variants: Variant[]) => Compiled<Message>;
|
|
9
8
|
//# sourceMappingURL=compile-message.d.ts.map
|
|
@@ -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;
|
|
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,33 +1,45 @@
|
|
|
1
1
|
import { compilePattern } from "./compile-pattern.js";
|
|
2
2
|
import { doubleQuote } from "../services/codegen/quotes.js";
|
|
3
3
|
import { inputsType } from "./jsdoc-types.js";
|
|
4
|
+
import { compileLocalVariable } from "./compile-local-variable.js";
|
|
4
5
|
/**
|
|
5
6
|
* Returns the compiled message as a string
|
|
6
7
|
*
|
|
7
8
|
*/
|
|
8
|
-
export const compileMessage = (declarations, message, variants
|
|
9
|
+
export const compileMessage = (declarations, message, variants) => {
|
|
9
10
|
// return empty string instead?
|
|
10
11
|
if (variants.length == 0) {
|
|
11
12
|
throw new Error("Message must have at least one variant");
|
|
12
13
|
}
|
|
13
14
|
const hasMultipleVariants = variants.length > 1;
|
|
14
15
|
return hasMultipleVariants
|
|
15
|
-
? compileMessageWithMultipleVariants(declarations, message, variants
|
|
16
|
-
: compileMessageWithOneVariant(declarations, message, variants
|
|
16
|
+
? compileMessageWithMultipleVariants(declarations, message, variants)
|
|
17
|
+
: compileMessageWithOneVariant(declarations, message, variants);
|
|
17
18
|
};
|
|
18
|
-
function compileMessageWithOneVariant(declarations, message, variants
|
|
19
|
+
function compileMessageWithOneVariant(declarations, message, variants) {
|
|
19
20
|
const variant = variants[0];
|
|
20
21
|
if (!variant || variants.length !== 1) {
|
|
21
22
|
throw new Error("Message must have exactly one variant");
|
|
22
23
|
}
|
|
23
24
|
const inputs = declarations.filter((decl) => decl.type === "input-variable");
|
|
24
25
|
const hasInputs = inputs.length > 0;
|
|
25
|
-
const compiledPattern = compilePattern(
|
|
26
|
+
const compiledPattern = compilePattern({
|
|
27
|
+
pattern: variant.pattern,
|
|
28
|
+
declarations,
|
|
29
|
+
});
|
|
30
|
+
const compiledLocalVariables = [];
|
|
31
|
+
for (const declaration of declarations) {
|
|
32
|
+
if (declaration.type === "local-variable") {
|
|
33
|
+
compiledLocalVariables.push(compileLocalVariable({ declaration, locale: message.locale }));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
26
36
|
const code = `/** @type {(inputs: ${inputsType(inputs)}) => string} */
|
|
27
|
-
export const ${message.bundleId} = (${hasInputs ? "i" : ""}) =>
|
|
37
|
+
export const ${message.bundleId} = (${hasInputs ? "i" : ""}) => {
|
|
38
|
+
${compiledLocalVariables.join("\n\t")}return ${compiledPattern.code}
|
|
39
|
+
};`;
|
|
28
40
|
return { code, node: message };
|
|
29
41
|
}
|
|
30
|
-
function compileMessageWithMultipleVariants(declarations, message, variants
|
|
42
|
+
function compileMessageWithMultipleVariants(declarations, message, variants) {
|
|
31
43
|
if (variants.length <= 1) {
|
|
32
44
|
throw new Error("Message must have more than one variant");
|
|
33
45
|
}
|
|
@@ -36,7 +48,10 @@ function compileMessageWithMultipleVariants(declarations, message, variants, reg
|
|
|
36
48
|
// TODO make sure that matchers use keys instead of indexes
|
|
37
49
|
const compiledVariants = [];
|
|
38
50
|
for (const variant of variants) {
|
|
39
|
-
const compiledPattern = compilePattern(
|
|
51
|
+
const compiledPattern = compilePattern({
|
|
52
|
+
pattern: variant.pattern,
|
|
53
|
+
declarations,
|
|
54
|
+
});
|
|
40
55
|
// todo account for all matches in the selector (if a match is missing, it should be the catchall)
|
|
41
56
|
const isCatchAll = variant.matches.every((match) => match.type === "catchall-match");
|
|
42
57
|
if (isCatchAll) {
|
|
@@ -60,9 +75,17 @@ function compileMessageWithMultipleVariants(declarations, message, variants, reg
|
|
|
60
75
|
continue;
|
|
61
76
|
compiledVariants.push(`if (${conditions.join(" && ")}) return ${compiledPattern.code};`);
|
|
62
77
|
}
|
|
78
|
+
const compiledLocalVariables = [];
|
|
79
|
+
for (const declaration of declarations) {
|
|
80
|
+
if (declaration.type === "local-variable") {
|
|
81
|
+
compiledLocalVariables.push(compileLocalVariable({ declaration, locale: message.locale }));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
63
84
|
const code = `/** @type {(inputs: ${inputsType(inputs)}) => string} */
|
|
64
85
|
export const ${message.bundleId} = (${hasInputs ? "i" : ""}) => {
|
|
86
|
+
${compiledLocalVariables.join("\n\t")}
|
|
65
87
|
${compiledVariants.join("\n\t")}
|
|
88
|
+
return \`${message.id}\`;
|
|
66
89
|
};`;
|
|
67
90
|
return { code, node: message };
|
|
68
91
|
}
|