@cleartrip/ct-design-image 5.27.0-SNAPSHOT-native-main.7 → 5.27.0-SNAPSHOT-native-main.9
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,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build-time babel plugin for @cleartrip/ct-design-image.
|
|
3
|
+
*
|
|
4
|
+
* The web `Image` component defaults its renderer to a framework-agnostic
|
|
5
|
+
* `const ImageComponent = 'img'` so the source stays free of a hard
|
|
6
|
+
* `next/image` dependency. This plugin rewrites that default at bundle
|
|
7
|
+
* time so every published output renders through `next/image` instead:
|
|
8
|
+
*
|
|
9
|
+
* const ImageComponent = 'img';
|
|
10
|
+
* →
|
|
11
|
+
* import Image from 'next/image';
|
|
12
|
+
* const ImageComponent = Image;
|
|
13
|
+
*
|
|
14
|
+
* `next` is a peer dependency of this package (see package.json), so the
|
|
15
|
+
* injected import is left external by rollup-plugin-peer-deps-external
|
|
16
|
+
* rather than inlined into the bundle.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const path = require('path');
|
|
20
|
+
|
|
21
|
+
const TARGET_NAME = 'ImageComponent';
|
|
22
|
+
const TARGET_VALUE = 'img';
|
|
23
|
+
const NEXT_IMAGE_SOURCE = 'next/image';
|
|
24
|
+
const NEXT_IMAGE_LOCAL = 'Image';
|
|
25
|
+
|
|
26
|
+
module.exports = function injectNextImagePlugin({ types: t }) {
|
|
27
|
+
return {
|
|
28
|
+
name: 'inject-next-image',
|
|
29
|
+
visitor: {
|
|
30
|
+
Program: {
|
|
31
|
+
enter(_path, state) {
|
|
32
|
+
state.injectedNextImage = false;
|
|
33
|
+
},
|
|
34
|
+
exit(programPath, state) {
|
|
35
|
+
if (!state.injectedNextImage) return;
|
|
36
|
+
|
|
37
|
+
const body = programPath.node.body;
|
|
38
|
+
const alreadyImported = body.some(
|
|
39
|
+
(node) =>
|
|
40
|
+
t.isImportDeclaration(node) &&
|
|
41
|
+
node.source.value === NEXT_IMAGE_SOURCE &&
|
|
42
|
+
node.specifiers.some(
|
|
43
|
+
(specifier) =>
|
|
44
|
+
t.isImportDefaultSpecifier(specifier) &&
|
|
45
|
+
specifier.local.name === NEXT_IMAGE_LOCAL,
|
|
46
|
+
),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
if (!alreadyImported) {
|
|
50
|
+
body.unshift(
|
|
51
|
+
t.importDeclaration(
|
|
52
|
+
[t.importDefaultSpecifier(t.identifier(NEXT_IMAGE_LOCAL))],
|
|
53
|
+
t.stringLiteral(NEXT_IMAGE_SOURCE),
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
VariableDeclarator(declaratorPath, state) {
|
|
61
|
+
// Only rewrite the renderer default inside Image.tsx itself — not
|
|
62
|
+
// Image.native.tsx or any other file that happens to pass through
|
|
63
|
+
// this babel config.
|
|
64
|
+
const filename = state.filename || state.file?.opts?.filename;
|
|
65
|
+
if (!filename || path.basename(filename) !== 'Image.tsx') return;
|
|
66
|
+
|
|
67
|
+
const { node } = declaratorPath;
|
|
68
|
+
if (
|
|
69
|
+
t.isIdentifier(node.id, { name: TARGET_NAME }) &&
|
|
70
|
+
t.isStringLiteral(node.init, { value: TARGET_VALUE })
|
|
71
|
+
) {
|
|
72
|
+
node.init = t.identifier(NEXT_IMAGE_LOCAL);
|
|
73
|
+
state.injectedNextImage = true;
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cleartrip/ct-design-image",
|
|
3
|
-
"version": "5.27.0-SNAPSHOT-native-main.
|
|
3
|
+
"version": "5.27.0-SNAPSHOT-native-main.9",
|
|
4
4
|
"description": "Image component — web <img>/custom component, native FastImage + SvgUri.",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"main": "./dist/ct-design-image.cjs.js",
|
|
@@ -21,18 +21,19 @@
|
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
23
23
|
"dist",
|
|
24
|
+
"babel-plugin-inject-next-image.js",
|
|
24
25
|
"src"
|
|
25
26
|
],
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@cleartrip/ct-design-
|
|
28
|
-
"@cleartrip/ct-design-
|
|
29
|
-
"@cleartrip/ct-design-
|
|
30
|
-
"@cleartrip/ct-design-common-utils": "5.27.0-SNAPSHOT-native-main.
|
|
31
|
-
"@cleartrip/ct-design-
|
|
28
|
+
"@cleartrip/ct-design-event-propagation": "5.27.0-SNAPSHOT-native-main.9",
|
|
29
|
+
"@cleartrip/ct-design-style-manager": "5.27.0-SNAPSHOT-native-main.9",
|
|
30
|
+
"@cleartrip/ct-design-conditional-wrap": "5.27.0-SNAPSHOT-native-main.9",
|
|
31
|
+
"@cleartrip/ct-design-common-utils": "5.27.0-SNAPSHOT-native-main.9",
|
|
32
|
+
"@cleartrip/ct-design-theme": "5.27.0-SNAPSHOT-native-main.9"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
35
|
"next": "16.3.0",
|
|
35
|
-
"@cleartrip/ct-design-types": "5.27.0-SNAPSHOT-native-main.
|
|
36
|
+
"@cleartrip/ct-design-types": "5.27.0-SNAPSHOT-native-main.9"
|
|
36
37
|
},
|
|
37
38
|
"peerDependencies": {
|
|
38
39
|
"react": ">=16.8.0",
|