@next/codemod 15.6.0-canary.4 → 15.6.0-canary.41
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 +0 -5
- package/package.json +2 -2
- package/transforms/withamp-to-config.js +0 -135
package/lib/utils.js
CHANGED
|
@@ -41,11 +41,6 @@ exports.TRANSFORMER_INQUIRER_CHOICES = [
|
|
|
41
41
|
value: 'url-to-withrouter',
|
|
42
42
|
version: '6.0.0',
|
|
43
43
|
},
|
|
44
|
-
{
|
|
45
|
-
title: 'Transforms the withAmp HOC into Next.js 9 page configuration',
|
|
46
|
-
value: 'withamp-to-config',
|
|
47
|
-
version: '8.0.0',
|
|
48
|
-
},
|
|
49
44
|
{
|
|
50
45
|
title: 'Transforms anonymous components into named components to make sure they work with Fast Refresh',
|
|
51
46
|
value: 'name-default-component',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@next/codemod",
|
|
3
|
-
"version": "15.6.0-canary.
|
|
3
|
+
"version": "15.6.0-canary.41",
|
|
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.9.2"
|
|
44
44
|
}
|
|
45
45
|
}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// One-time usage file. You can delete me after running the codemod!
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.default = transformer;
|
|
5
|
-
function injectAmp(j, o, desiredAmpValue) {
|
|
6
|
-
const init = o.node.init;
|
|
7
|
-
switch (init.type) {
|
|
8
|
-
case 'ObjectExpression': {
|
|
9
|
-
const overwroteAmpKey = init.properties.some((prop) => {
|
|
10
|
-
switch (prop.type) {
|
|
11
|
-
case 'Property':
|
|
12
|
-
case 'ObjectProperty':
|
|
13
|
-
if (!(prop.key.type === 'Identifier' && prop.key.name === 'amp')) {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
prop.value = desiredAmpValue;
|
|
17
|
-
return true;
|
|
18
|
-
default:
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
if (!overwroteAmpKey) {
|
|
23
|
-
init.properties.push(j.objectProperty(j.identifier('amp'), desiredAmpValue));
|
|
24
|
-
}
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
default: {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function transformer(file, api) {
|
|
33
|
-
const j = api.jscodeshift.withParser('tsx');
|
|
34
|
-
const root = j(file.source);
|
|
35
|
-
const done = () => root.toSource();
|
|
36
|
-
const imports = root.find(j.ImportDeclaration, {
|
|
37
|
-
source: { value: 'next/amp' },
|
|
38
|
-
});
|
|
39
|
-
if (imports.length < 1) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
let hadWithAmp = false;
|
|
43
|
-
const ampImportNames = [];
|
|
44
|
-
imports.forEach((ampImport) => {
|
|
45
|
-
const ampImportShift = j(ampImport);
|
|
46
|
-
const withAmpImport = ampImportShift.find(j.ImportSpecifier, {
|
|
47
|
-
imported: { name: 'withAmp' },
|
|
48
|
-
});
|
|
49
|
-
if (withAmpImport.length < 1) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
hadWithAmp = true;
|
|
53
|
-
withAmpImport.forEach((element) => {
|
|
54
|
-
ampImportNames.push(element.value.local.name);
|
|
55
|
-
j(element).remove();
|
|
56
|
-
});
|
|
57
|
-
if (ampImport.value.specifiers.length === 0) {
|
|
58
|
-
ampImportShift.remove();
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
if (!hadWithAmp) {
|
|
62
|
-
return done();
|
|
63
|
-
}
|
|
64
|
-
const defaultExportsShift = root.find(j.ExportDefaultDeclaration);
|
|
65
|
-
if (defaultExportsShift.length < 1) {
|
|
66
|
-
return done();
|
|
67
|
-
}
|
|
68
|
-
let desiredAmpValue = j.booleanLiteral(true);
|
|
69
|
-
const defaultExport = defaultExportsShift.nodes()[0];
|
|
70
|
-
const removedWrapper = ampImportNames.some((ampImportName) => {
|
|
71
|
-
const ampWrapping = j(defaultExport).find(j.CallExpression, {
|
|
72
|
-
callee: { name: ampImportName },
|
|
73
|
-
});
|
|
74
|
-
if (ampWrapping.length < 1) {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
ampWrapping.forEach((e) => {
|
|
78
|
-
if (e.value.arguments.length < 1) {
|
|
79
|
-
j(e).remove();
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
const withAmpOptions = e.value.arguments[1];
|
|
83
|
-
if (withAmpOptions && withAmpOptions.type === 'ObjectExpression') {
|
|
84
|
-
const isHybrid = withAmpOptions.properties.some((prop) => {
|
|
85
|
-
if (!(prop.type === 'Property' || prop.type === 'ObjectProperty')) {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
if (!(prop.key && prop.key.name === 'hybrid')) {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
return ((prop.value.type === 'Literal' ||
|
|
92
|
-
prop.value.type === 'BooleanLiteral') &&
|
|
93
|
-
prop.value.value === true);
|
|
94
|
-
});
|
|
95
|
-
if (isHybrid) {
|
|
96
|
-
desiredAmpValue = j.stringLiteral('hybrid');
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
j(e).replaceWith(e.value.arguments[0]);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
return true;
|
|
103
|
-
});
|
|
104
|
-
if (!removedWrapper) {
|
|
105
|
-
return done();
|
|
106
|
-
}
|
|
107
|
-
const namedExportsShift = root.find(j.ExportNamedDeclaration);
|
|
108
|
-
const hadExistingConfig = namedExportsShift.some((namedExport) => {
|
|
109
|
-
const configExportedObject = j(namedExport).find(j.VariableDeclarator, {
|
|
110
|
-
id: { name: 'config' },
|
|
111
|
-
});
|
|
112
|
-
if (configExportedObject.length > 0) {
|
|
113
|
-
return configExportedObject.some((exportedObject) => injectAmp(j, exportedObject, desiredAmpValue));
|
|
114
|
-
}
|
|
115
|
-
const configReexported = j(namedExport).find(j.ExportSpecifier, {
|
|
116
|
-
local: { name: 'config' },
|
|
117
|
-
});
|
|
118
|
-
if (configReexported.length > 0) {
|
|
119
|
-
const configObjects = root
|
|
120
|
-
.findVariableDeclarators('config')
|
|
121
|
-
.filter((el) => el.scope.isGlobal);
|
|
122
|
-
return configObjects.some((configObject) => injectAmp(j, configObject, desiredAmpValue));
|
|
123
|
-
}
|
|
124
|
-
return false;
|
|
125
|
-
});
|
|
126
|
-
if (!hadExistingConfig) {
|
|
127
|
-
defaultExportsShift.insertAfter(j.exportNamedDeclaration(j.variableDeclaration('const', [
|
|
128
|
-
j.variableDeclarator(j.identifier('config'), j.objectExpression([
|
|
129
|
-
j.objectProperty(j.identifier('amp'), desiredAmpValue),
|
|
130
|
-
])),
|
|
131
|
-
])));
|
|
132
|
-
}
|
|
133
|
-
return done();
|
|
134
|
-
}
|
|
135
|
-
//# sourceMappingURL=withamp-to-config.js.map
|