@next/codemod 15.0.4-canary.2 → 15.0.4-canary.20
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/lib/utils.js +5 -0
- package/package.json +2 -2
- package/transforms/revalidate-to-expire.js +110 -0
package/lib/utils.js
CHANGED
|
@@ -106,5 +106,10 @@ exports.TRANSFORMER_INQUIRER_CHOICES = [
|
|
|
106
106
|
value: 'app-dir-runtime-config-experimental-edge',
|
|
107
107
|
version: '15.0.0-canary.179',
|
|
108
108
|
},
|
|
109
|
+
{
|
|
110
|
+
title: 'Transform `revalidateTag` and `revalidatePath` to `expireTag` and `expirePath`',
|
|
111
|
+
value: 'revalidate-to-expire',
|
|
112
|
+
version: '15.0.4-canary.12',
|
|
113
|
+
},
|
|
109
114
|
];
|
|
110
115
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@next/codemod",
|
|
3
|
-
"version": "15.0.4-canary.
|
|
3
|
+
"version": "15.0.4-canary.20",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -40,6 +40,6 @@
|
|
|
40
40
|
"@types/jscodeshift": "0.11.0",
|
|
41
41
|
"@types/prompts": "2.4.2",
|
|
42
42
|
"@types/semver": "7.3.1",
|
|
43
|
-
"typescript": "5.
|
|
43
|
+
"typescript": "5.6.3"
|
|
44
44
|
}
|
|
45
45
|
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = transformer;
|
|
4
|
+
const parser_1 = require("../lib/parser");
|
|
5
|
+
function transformer(file, _api) {
|
|
6
|
+
const j = (0, parser_1.createParserFromPath)(file.path);
|
|
7
|
+
const root = j(file.source);
|
|
8
|
+
if (!root.length) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const nextCacheImports = root.find(j.ImportDeclaration, {
|
|
12
|
+
source: {
|
|
13
|
+
value: 'next/cache',
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
if (!nextCacheImports.length) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
let nextCacheNamespace = '';
|
|
20
|
+
let revalidatePathName = 'revalidatePath';
|
|
21
|
+
let revalidateTagName = 'revalidateTag';
|
|
22
|
+
let expirePathName = 'expirePath';
|
|
23
|
+
let expireTagName = 'expireTag';
|
|
24
|
+
nextCacheImports.forEach((path) => {
|
|
25
|
+
let hasExpirePath = false;
|
|
26
|
+
let hasExpireTag = false;
|
|
27
|
+
// Set the alias name for callee if exists.
|
|
28
|
+
path.node.specifiers.forEach((specifier) => {
|
|
29
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
30
|
+
if (specifier.imported.name === 'expirePath') {
|
|
31
|
+
expirePathName = specifier.local?.name ?? 'expirePath';
|
|
32
|
+
hasExpirePath = true;
|
|
33
|
+
}
|
|
34
|
+
if (specifier.imported.name === 'expireTag') {
|
|
35
|
+
expireTagName = specifier.local?.name ?? 'expireTag';
|
|
36
|
+
hasExpireTag = true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
// Remove the revalidate functions from the import specifiers if
|
|
41
|
+
// expire functions are also imported.
|
|
42
|
+
path.node.specifiers = path.node.specifiers.filter((specifier) => {
|
|
43
|
+
if (specifier.type !== 'ImportSpecifier') {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
if (hasExpireTag && specifier.imported.name === 'revalidateTag') {
|
|
47
|
+
revalidateTagName = specifier.local?.name ?? 'revalidateTag';
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (hasExpirePath && specifier.imported.name === 'revalidatePath') {
|
|
51
|
+
revalidatePathName = specifier.local?.name ?? 'revalidatePath';
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
});
|
|
56
|
+
path.node.specifiers.forEach((specifier) => {
|
|
57
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
58
|
+
if (specifier.imported.name === 'revalidateTag') {
|
|
59
|
+
specifier.imported.name = 'expireTag';
|
|
60
|
+
}
|
|
61
|
+
if (specifier.imported.name === 'revalidatePath') {
|
|
62
|
+
specifier.imported.name = 'expirePath';
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// import * as <namespace> from 'next/cache'
|
|
66
|
+
if (specifier.type === 'ImportNamespaceSpecifier') {
|
|
67
|
+
nextCacheNamespace = specifier.local.name;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
root.find(j.CallExpression).forEach((path) => {
|
|
72
|
+
const callee = path.node.callee;
|
|
73
|
+
// Handle direct function calls:
|
|
74
|
+
// ```ts
|
|
75
|
+
// revalidateTag('next')
|
|
76
|
+
// revalidatePath('/next')
|
|
77
|
+
// ```
|
|
78
|
+
if (callee.type === 'Identifier' &&
|
|
79
|
+
(callee.name === revalidateTagName || callee.name === revalidatePathName)) {
|
|
80
|
+
callee.name =
|
|
81
|
+
callee.name === revalidateTagName ? expireTagName : expirePathName;
|
|
82
|
+
}
|
|
83
|
+
// Handle namespace calls:
|
|
84
|
+
// ```ts
|
|
85
|
+
// import * as cache from 'next/cache'
|
|
86
|
+
// ```
|
|
87
|
+
if (callee.type === 'MemberExpression' &&
|
|
88
|
+
callee.object.type === 'Identifier' &&
|
|
89
|
+
callee.object.name === nextCacheNamespace) {
|
|
90
|
+
// cache.revalidateTag('next')
|
|
91
|
+
// cache.revalidatePath('/next')
|
|
92
|
+
if (callee.property.type === 'Identifier' &&
|
|
93
|
+
(callee.property.name === 'revalidateTag' ||
|
|
94
|
+
callee.property.name === 'revalidatePath')) {
|
|
95
|
+
callee.property.name =
|
|
96
|
+
callee.property.name === 'revalidateTag' ? 'expireTag' : 'expirePath';
|
|
97
|
+
}
|
|
98
|
+
// cache['revalidateTag']('next')
|
|
99
|
+
// cache['revalidatePath']('/next')
|
|
100
|
+
if (callee.property.type === 'StringLiteral' &&
|
|
101
|
+
(callee.property.value === 'revalidateTag' ||
|
|
102
|
+
callee.property.value === 'revalidatePath')) {
|
|
103
|
+
callee.property.value =
|
|
104
|
+
callee.property.value === 'revalidateTag' ? 'expireTag' : 'expirePath';
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return root.toSource();
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=revalidate-to-expire.js.map
|