-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathplugin.ts
More file actions
129 lines (109 loc) · 3.49 KB
/
plugin.ts
File metadata and controls
129 lines (109 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { join } from "node:path/posix";
import { decode, encode } from "node:querystring";
import { imageSize } from "image-size";
import type { NextConfigComplete } from "next/dist/server/config-shared.js";
import { dedent } from "ts-dedent";
import type { Plugin } from "vite";
import { VITEST_PLUGIN_NAME, isVitestEnv } from "../../utils";
import { getAlias } from "./alias";
const includePattern = /\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/;
const excludeImporterPattern = /\.(css|scss|sass)$/;
const virtualImage = "virtual:next-image";
const virtualNextImage = "virtual:next/image";
const virtualNextLegacyImage = "virtual:next/legacy/image";
const require = createRequire(import.meta.url);
export function vitePluginNextImage(
nextConfigResolver: PromiseWithResolvers<NextConfigComplete>,
) {
let isBrowser = !isVitestEnv;
return {
name: "vite-plugin-storybook-nextjs-image",
enforce: "pre" as const,
async config(config, env) {
if (config.test?.browser?.enabled === true) {
isBrowser = true;
}
return {
resolve: {
alias: getAlias(isBrowser ? "browser" : "node"),
},
};
},
async resolveId(id, importer) {
const [source, queryA] = id.split("?");
if (queryA === "ignore") {
return null;
}
if (
includePattern.test(source) &&
!excludeImporterPattern.test(importer ?? "") &&
!importer?.startsWith(virtualImage)
) {
const isAbsolute = path.isAbsolute(id);
const imagePath = importer
? isAbsolute
? source
: join(path.dirname(importer), source)
: source;
return `${virtualImage}?${encode({ imagePath })}`;
}
if (id === "next/image" && importer !== virtualNextImage) {
return virtualNextImage;
}
if (id === "next/legacy/image" && importer !== virtualNextLegacyImage) {
return virtualNextLegacyImage;
}
return null;
},
async load(id) {
const aliasEnv = isBrowser ? "browser" : "node";
if (virtualNextImage === id) {
return (
await fs.promises.readFile(
require.resolve(`${VITEST_PLUGIN_NAME}/${aliasEnv}/mocks/image`),
)
).toString("utf-8");
}
if (virtualNextLegacyImage === id) {
return (
await fs.promises.readFile(
require.resolve(
`${VITEST_PLUGIN_NAME}/${aliasEnv}/mocks/legacy-image`,
),
)
).toString("utf-8");
}
const [source, query] = id.split("?");
if (virtualImage === source) {
const imagePath = decode(query).imagePath as string;
const nextConfig = await nextConfigResolver.promise;
try {
if (nextConfig.images?.disableStaticImages) {
return dedent`
import image from "${imagePath}?ignore";
export default image;
`;
}
const imageData = await fs.promises.readFile(imagePath);
const { width, height } = imageSize(imageData);
return dedent`
import src from "${imagePath}?ignore";
export default {
src,
height: ${height},
width: ${width},
blurDataURL: src
};
`;
} catch (err) {
console.error(`Could not read image file ${imagePath}:`, err);
return undefined;
}
}
return null;
},
} satisfies Plugin;
}