babel-plugin-saykit 0.0.0 → 0.2.0
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 +29 -0
- package/dist/index.cjs +38 -0
- package/dist/index.d.cts +13 -0
- package/package.json +48 -3
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# babel-plugin-saykit
|
|
2
|
+
|
|
3
|
+
> Babel plugin for [SayKit](https://saykit.js.org).
|
|
4
|
+
|
|
5
|
+
Use this for Next.js (`.babelrc`), React Native, Expo, Metro, or any Babel-driven pipeline. For Vite, Rollup, Webpack, esbuild, and similar bundlers, use [`unplugin-saykit`](https://github.com/k0d13/saykit/tree/main/packages/plugin-unplugin) instead.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add -D babel-plugin-saykit
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```json title=".babelrc"
|
|
16
|
+
{
|
|
17
|
+
"plugins": ["saykit"]
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```js title="babel.config.js"
|
|
22
|
+
export default {
|
|
23
|
+
plugins: ['saykit'],
|
|
24
|
+
};
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Documentation
|
|
28
|
+
|
|
29
|
+
See [saykit.js.org/integrations/babel](https://saykit.js.org/integrations/babel).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
let node_fs = require("node:fs");
|
|
2
|
+
let node_path = require("node:path");
|
|
3
|
+
let _babel_core = require("@babel/core");
|
|
4
|
+
let _saykit_config_features_loader = require("@saykit/config/features/loader");
|
|
5
|
+
let _saykit_config_features_messages = require("@saykit/config/features/messages");
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
var src_default = (api) => {
|
|
8
|
+
const config = (0, _saykit_config_features_loader.resolveConfig)();
|
|
9
|
+
return {
|
|
10
|
+
name: "saykit",
|
|
11
|
+
parserOverride(code, opts, parse) {
|
|
12
|
+
const id_ = opts.sourceFileName;
|
|
13
|
+
if (!id_ || id_.includes("node_modules")) return parse(code, opts);
|
|
14
|
+
const id = (0, node_path.relative)(process.cwd(), id_).replaceAll("\\", "/").split("?")[0];
|
|
15
|
+
return parse(config.buckets.find((b) => b.match(id))?.transformer.transform(code, id) ?? code, opts);
|
|
16
|
+
},
|
|
17
|
+
visitor: { ImportDeclaration(path, state) {
|
|
18
|
+
const importee = path.node.source.value;
|
|
19
|
+
if (!importee.startsWith(".")) return;
|
|
20
|
+
const importer = state.filename ?? state.file.opts.filename;
|
|
21
|
+
if (!importer) return;
|
|
22
|
+
const id_ = (0, node_path.resolve)((0, node_path.dirname)(importer), importee);
|
|
23
|
+
const id = (0, node_path.relative)(process.cwd(), id_).replaceAll("\\", "/").split("?")[0];
|
|
24
|
+
const bucket = config.buckets.find((b) => b.output.match(id));
|
|
25
|
+
if (!bucket) return;
|
|
26
|
+
const specifier = path.node.specifiers.find((s) => s.type === "ImportDefaultSpecifier");
|
|
27
|
+
if (!specifier) throw path.buildCodeFrameError("SayKit inline imports require a default import");
|
|
28
|
+
const content = (0, node_fs.readFileSync)(id, "utf8");
|
|
29
|
+
try {
|
|
30
|
+
api.addExternalDependency(id_);
|
|
31
|
+
} catch {}
|
|
32
|
+
const entries = bucket.formatter.parse(content).map((m) => [m.id || (0, _saykit_config_features_messages.generateHash)(m.message, m.context), m.translation || m.message]);
|
|
33
|
+
path.replaceWith(_babel_core.types.variableDeclaration("const", [_babel_core.types.variableDeclarator(_babel_core.types.identifier(specifier.local.name), _babel_core.types.objectExpression(entries.map(([key, value]) => _babel_core.types.objectProperty(_babel_core.types.stringLiteral(key), _babel_core.types.stringLiteral(value)))))]));
|
|
34
|
+
} }
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
//#endregion
|
|
38
|
+
module.exports = src_default;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PluginAPI, PluginObj, parse } from "@babel/core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare module '@babel/core' {
|
|
5
|
+
interface PluginAPI {
|
|
6
|
+
addExternalDependency(ref: string): void;
|
|
7
|
+
}
|
|
8
|
+
interface PluginObj {
|
|
9
|
+
parserOverride(code: string, opts: TransformOptions, parse: typeof parse): ParseResult | null;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
declare const _default: (api: PluginAPI) => PluginObj;
|
|
13
|
+
export = _default;
|
package/package.json
CHANGED
|
@@ -1,4 +1,49 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "babel-plugin-saykit",
|
|
3
|
-
"version": "0.
|
|
1
|
+
{
|
|
2
|
+
"name": "babel-plugin-saykit",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Babel plugin for SayKit",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"i18n",
|
|
7
|
+
"rollup",
|
|
8
|
+
"saykit",
|
|
9
|
+
"unplugin",
|
|
10
|
+
"vite",
|
|
11
|
+
"webpack"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/k0d13/saykit#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/k0d13/saykit/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/k0d13/saykit.git",
|
|
21
|
+
"directory": "packages/plugin-babel"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"!dist/**/*.map"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.cjs",
|
|
31
|
+
"default": "./dist/index.cjs"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public",
|
|
36
|
+
"provenance": true
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@babel/core": "^7.29.7",
|
|
40
|
+
"@saykit/config": "^0.2.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/babel__core": "^7.20.5"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"check": "tsc --noEmit",
|
|
47
|
+
"build": "tsdown"
|
|
48
|
+
}
|
|
4
49
|
}
|