@flightdev/babel-plugin-cache 0.0.2
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/LICENSE +21 -0
- package/README.md +49 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +140 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Flight Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @flight-framework/babel-plugin-cache
|
|
2
|
+
|
|
3
|
+
Babel plugin for "use cache" directive in Flight Framework.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @flight-framework/babel-plugin-cache
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### babel.config.js
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
module.exports = {
|
|
17
|
+
plugins: [
|
|
18
|
+
['@flight-framework/babel-plugin-cache', {
|
|
19
|
+
defaultTTL: 3600,
|
|
20
|
+
}],
|
|
21
|
+
],
|
|
22
|
+
};
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### In Code
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
async function getReviews(productId: string) {
|
|
29
|
+
"use cache";
|
|
30
|
+
return fetch(`/api/reviews/${productId}`).then(r => r.json());
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Output
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { cachedFn } from '@flight-framework/cache-components';
|
|
38
|
+
|
|
39
|
+
const getReviews = cachedFn('getReviews', async (productId: string) => {
|
|
40
|
+
return fetch(`/api/reviews/${productId}`).then(r => r.json());
|
|
41
|
+
}, { ttl: 3600 });
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Options
|
|
45
|
+
|
|
46
|
+
| Option | Type | Default | Description |
|
|
47
|
+
|--------|------|---------|-------------|
|
|
48
|
+
| `defaultTTL` | number | 3600 | Default TTL in seconds |
|
|
49
|
+
| `importSource` | string | `@flight-framework/cache-components` | Import source |
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @flightdev/babel-plugin-cache
|
|
3
|
+
*
|
|
4
|
+
* Babel plugin that transforms "use cache" directive into cached function calls.
|
|
5
|
+
*/
|
|
6
|
+
interface PluginOptions {
|
|
7
|
+
/** Default TTL for cached functions (seconds) */
|
|
8
|
+
defaultTTL?: number;
|
|
9
|
+
/** Import source for cachedFn */
|
|
10
|
+
importSource?: string;
|
|
11
|
+
/** Name of the runtime function */
|
|
12
|
+
runtimeFn?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Babel plugin for "use cache" directive
|
|
16
|
+
*/
|
|
17
|
+
declare function flightCachePlugin({ types: t }: {
|
|
18
|
+
types: any;
|
|
19
|
+
}, options?: PluginOptions): any;
|
|
20
|
+
|
|
21
|
+
export { type PluginOptions, flightCachePlugin as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function flightCachePlugin({ types: t }, options = {}) {
|
|
3
|
+
const {
|
|
4
|
+
defaultTTL = 3600,
|
|
5
|
+
importSource = "@flightdev/cache-components",
|
|
6
|
+
runtimeFn = "cachedFn"
|
|
7
|
+
} = options;
|
|
8
|
+
let importIdentifier = null;
|
|
9
|
+
let needsImport = false;
|
|
10
|
+
return {
|
|
11
|
+
name: "flight-cache",
|
|
12
|
+
visitor: {
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
Program: {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
enter(path) {
|
|
17
|
+
needsImport = false;
|
|
18
|
+
importIdentifier = null;
|
|
19
|
+
for (const node of path.node.body) {
|
|
20
|
+
if (node.type === "ImportDeclaration" && node.source.value === importSource) {
|
|
21
|
+
for (const specifier of node.specifiers) {
|
|
22
|
+
if (specifier.type === "ImportSpecifier" && specifier.imported?.name === runtimeFn) {
|
|
23
|
+
importIdentifier = specifier.local;
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
+
exit(path) {
|
|
32
|
+
if (needsImport && !importIdentifier) {
|
|
33
|
+
const id = path.scope.generateUidIdentifier(runtimeFn);
|
|
34
|
+
importIdentifier = id;
|
|
35
|
+
const importDecl = t.importDeclaration(
|
|
36
|
+
[t.importSpecifier(id, t.identifier(runtimeFn))],
|
|
37
|
+
t.stringLiteral(importSource)
|
|
38
|
+
);
|
|
39
|
+
path.unshiftContainer("body", importDecl);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
44
|
+
FunctionDeclaration(path) {
|
|
45
|
+
if (!hasUseCacheDirective(path)) return;
|
|
46
|
+
if (!path.node.id) return;
|
|
47
|
+
const fnName = path.node.id.name;
|
|
48
|
+
needsImport = true;
|
|
49
|
+
removeUseCacheDirective(path);
|
|
50
|
+
const arrowFn = t.arrowFunctionExpression(
|
|
51
|
+
path.node.params,
|
|
52
|
+
path.node.body,
|
|
53
|
+
path.node.async
|
|
54
|
+
);
|
|
55
|
+
const cacheCall = t.callExpression(
|
|
56
|
+
importIdentifier || t.identifier("__cachedFn"),
|
|
57
|
+
[
|
|
58
|
+
t.stringLiteral(fnName),
|
|
59
|
+
arrowFn,
|
|
60
|
+
t.objectExpression([
|
|
61
|
+
t.objectProperty(
|
|
62
|
+
t.identifier("ttl"),
|
|
63
|
+
t.numericLiteral(defaultTTL)
|
|
64
|
+
)
|
|
65
|
+
])
|
|
66
|
+
]
|
|
67
|
+
);
|
|
68
|
+
const varDecl = t.variableDeclaration("const", [
|
|
69
|
+
t.variableDeclarator(t.identifier(fnName), cacheCall)
|
|
70
|
+
]);
|
|
71
|
+
path.replaceWith(varDecl);
|
|
72
|
+
},
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
+
ArrowFunctionExpression(path) {
|
|
75
|
+
if (!hasUseCacheDirective(path)) return;
|
|
76
|
+
const parent = path.parentPath;
|
|
77
|
+
if (!parent.isVariableDeclarator()) return;
|
|
78
|
+
const id = parent.node.id;
|
|
79
|
+
if (!t.isIdentifier(id)) return;
|
|
80
|
+
const fnName = id.name;
|
|
81
|
+
needsImport = true;
|
|
82
|
+
removeUseCacheDirective(path);
|
|
83
|
+
const newArrowFn = t.arrowFunctionExpression(
|
|
84
|
+
path.node.params,
|
|
85
|
+
path.node.body,
|
|
86
|
+
path.node.async
|
|
87
|
+
);
|
|
88
|
+
const cacheCall = t.callExpression(
|
|
89
|
+
importIdentifier || t.identifier("__cachedFn"),
|
|
90
|
+
[
|
|
91
|
+
t.stringLiteral(fnName),
|
|
92
|
+
newArrowFn,
|
|
93
|
+
t.objectExpression([
|
|
94
|
+
t.objectProperty(
|
|
95
|
+
t.identifier("ttl"),
|
|
96
|
+
t.numericLiteral(defaultTTL)
|
|
97
|
+
)
|
|
98
|
+
])
|
|
99
|
+
]
|
|
100
|
+
);
|
|
101
|
+
path.replaceWith(cacheCall);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function hasUseCacheDirective(path) {
|
|
107
|
+
const body = path.node.body;
|
|
108
|
+
if (body.type !== "BlockStatement") return false;
|
|
109
|
+
for (const stmt of body.directives || []) {
|
|
110
|
+
if (stmt.value.value === "use cache") {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (body.body.length > 0) {
|
|
115
|
+
const first = body.body[0];
|
|
116
|
+
if (first.type === "ExpressionStatement" && first.expression.type === "StringLiteral" && first.expression.value === "use cache") {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
function removeUseCacheDirective(path) {
|
|
123
|
+
const body = path.node.body;
|
|
124
|
+
if (body.type !== "BlockStatement") return;
|
|
125
|
+
if (body.directives) {
|
|
126
|
+
body.directives = body.directives.filter(
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
128
|
+
(d) => d.value.value !== "use cache"
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
if (body.body.length > 0) {
|
|
132
|
+
const first = body.body[0];
|
|
133
|
+
if (first.type === "ExpressionStatement" && first.expression.type === "StringLiteral" && first.expression.value === "use cache") {
|
|
134
|
+
body.body.shift();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export {
|
|
139
|
+
flightCachePlugin as default
|
|
140
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flightdev/babel-plugin-cache",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Babel plugin for 'use cache' directive in Flight Framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@babel/core": ">=7.0.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@babel/core": "^7.24.0",
|
|
21
|
+
"@types/babel__core": "^7.20.0",
|
|
22
|
+
"tsup": "^8.0.0",
|
|
23
|
+
"typescript": "^5.0.0",
|
|
24
|
+
"vitest": "^2.0.0"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md"
|
|
29
|
+
],
|
|
30
|
+
"keywords": [
|
|
31
|
+
"flight",
|
|
32
|
+
"babel",
|
|
33
|
+
"plugin",
|
|
34
|
+
"cache",
|
|
35
|
+
"directive"
|
|
36
|
+
],
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
40
|
+
"dev": "tsup src/index.ts --format esm --dts --watch",
|
|
41
|
+
"test": "vitest run",
|
|
42
|
+
"test:watch": "vitest",
|
|
43
|
+
"typecheck": "tsc --noEmit"
|
|
44
|
+
}
|
|
45
|
+
}
|