@x18n/builtins 0.0.2 → 0.0.3

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.
@@ -0,0 +1,8 @@
1
+ import { Plugin, PluginFile, Preset } from "@x18n/core";
2
+ //#region src/yaml-plugin/index.d.ts
3
+ /**
4
+ * Compiles a validated YAML plugin into plugin objects. Each section maps to the
5
+ * options of a built-in factory, so moving to a TS plugin is a small change.
6
+ */
7
+ export declare function compileYamlPlugin(data: PluginFile): Plugin | Preset;
8
+ //#endregion
@@ -0,0 +1,68 @@
1
+ import { placeholderFromShape } from "./placeholders.mjs";
2
+ import { r as regexScopes } from "./regex-B9TBGgXC.mjs";
3
+ //#region src/yaml-plugin/index.ts
4
+ /**
5
+ * Compiles a validated YAML plugin into plugin objects. Each section maps to the
6
+ * options of a built-in factory, so moving to a TS plugin is a small change.
7
+ */
8
+ function compileYamlPlugin(data) {
9
+ const replacements = (data.normalizeKey ?? []).map((rule) => ({
10
+ from: new RegExp(rule.replace.pattern, "g"),
11
+ to: rule.replace.with
12
+ }));
13
+ const localeInfo = data.localeInfo;
14
+ const plugin = {
15
+ name: data.name,
16
+ config(draft) {
17
+ const defaults = data.config;
18
+ if (defaults?.sourceLocale) draft.sourceLocale = defaults.sourceLocale;
19
+ if (defaults?.locales) draft.locales = defaults.locales;
20
+ if (defaults?.resources && draft.resources.length === 0) draft.resources = defaults.resources;
21
+ if (defaults?.keyStyle) draft.keyStyle = defaults.keyStyle;
22
+ if (defaults?.namespace) draft.namespace = {
23
+ separator: defaults.namespace.separator ?? draft.namespace.separator,
24
+ default: defaults.namespace.default ?? draft.namespace.default,
25
+ fromPath: defaults.namespace.fromPath ?? draft.namespace.fromPath
26
+ };
27
+ draft.usage = {
28
+ ...draft.usage,
29
+ calls: [...draft.usage.calls, ...data.scanners?.calls ?? []],
30
+ regex: [...draft.usage.regex, ...data.scanners?.regex ?? []]
31
+ };
32
+ if (data.extract?.templates) draft.extract = {
33
+ ...draft.extract,
34
+ templates: [...draft.extract.templates ?? [], ...data.extract.templates]
35
+ };
36
+ },
37
+ derivedKeys: data.derivedKeys,
38
+ placeholders: data.placeholders?.map(placeholderFromShape),
39
+ normalizeKey: replacements.length > 0 ? (raw) => replacements.reduce((text, rule) => text.replace(rule.from, rule.to), raw) : void 0,
40
+ localeInfo: localeInfo ? (code) => {
41
+ const info = localeInfo[code];
42
+ return info ? {
43
+ ...info.flag ? { flag: info.flag } : {},
44
+ ...info.displayName ? { displayName: info.displayName } : {}
45
+ } : void 0;
46
+ } : void 0
47
+ };
48
+ const plugins = [plugin, ...data.scopes ? [regexScopes(data.scopes.regex, `${data.name}-scopes`)] : []];
49
+ const detect = data.detect;
50
+ if (!detect) return plugins.length === 1 ? plugin : {
51
+ name: data.name,
52
+ plugins
53
+ };
54
+ return {
55
+ name: data.name,
56
+ exclusive: data.exclusive,
57
+ plugins,
58
+ async detect(ctx) {
59
+ const deps = detect.dependencies;
60
+ if (deps?.any && !ctx.hasAnyDependency(deps.any)) return false;
61
+ if (deps?.all && !deps.all.every(ctx.hasDependency)) return false;
62
+ for (const pattern of detect.files ?? []) if (!await ctx.fileExists(pattern)) return false;
63
+ return true;
64
+ }
65
+ };
66
+ }
67
+ //#endregion
68
+ export { compileYamlPlugin };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@x18n/builtins",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "description": "Built-in x18n plugins and presets: formats, locale finders, scanners, checks and reporters",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -33,7 +33,7 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "@vue/compiler-sfc": "^3.5.44",
36
- "@x18n/core": "0.0.2",
36
+ "@x18n/core": "0.0.3",
37
37
  "json5": "^2.2.3",
38
38
  "jsonc-parser": "^3.3.1",
39
39
  "oxc-parser": "^0.154.0",