vite-plugin-storybook-nextjs 0.0.2 → 0.0.3
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/dist/index.d.ts +12 -0
- package/dist/index.js +143 -208
- package/dist/plugins/next-image/alias/image-context.d.ts +13 -0
- package/dist/plugins/next-image/alias/image-context.js +1 -1
- package/dist/plugins/next-image/alias/image-default-loader.d.ts +5 -0
- package/dist/plugins/next-image/alias/next-image.d.ts +30 -0
- package/dist/plugins/next-image/alias/next-legacy-image.d.ts +6 -0
- package/package.json +4 -4
- package/dist/index.cjs +0 -960
- package/dist/plugins/next-image/alias/image-context.cjs +0 -8
- package/dist/plugins/next-image/alias/image-default-loader.cjs +0 -40
- package/dist/plugins/next-image/alias/next-image.cjs +0 -53
- package/dist/plugins/next-image/alias/next-legacy-image.cjs +0 -27
package/dist/index.cjs
DELETED
|
@@ -1,960 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var path = require('path');
|
|
4
|
-
var defineEnvPlugin = require('next/dist/build/webpack/plugins/define-env-plugin');
|
|
5
|
-
var env = require('@next/env');
|
|
6
|
-
var Log = require('next/dist/build/output/log');
|
|
7
|
-
var swc = require('next/dist/build/swc');
|
|
8
|
-
var loadConfig = require('next/dist/server/config');
|
|
9
|
-
var constants = require('next/dist/shared/lib/constants');
|
|
10
|
-
var fs = require('fs/promises');
|
|
11
|
-
var fetchCssFromGoogleFonts = require('next/dist/compiled/@next/font/dist/google/fetch-css-from-google-fonts');
|
|
12
|
-
var getFontAxes = require('next/dist/compiled/@next/font/dist/google/get-font-axes');
|
|
13
|
-
var getGoogleFontsUrl = require('next/dist/compiled/@next/font/dist/google/get-google-fonts-url');
|
|
14
|
-
var validateGoogleFontFunctionCall = require('next/dist/compiled/@next/font/dist/google/validate-google-font-function-call');
|
|
15
|
-
var loaderUtils = require('next/dist/compiled/loader-utils3');
|
|
16
|
-
var validateLocalFontFunctionCall = require('next/dist/compiled/@next/font/dist/local/validate-local-font-function-call');
|
|
17
|
-
var dedent3 = require('ts-dedent');
|
|
18
|
-
var loadJsConfig = require('next/dist/build/load-jsconfig');
|
|
19
|
-
var findPagesDir = require('next/dist/lib/find-pages-dir');
|
|
20
|
-
var utils = require('next/dist/build/utils');
|
|
21
|
-
var options = require('next/dist/build/swc/options');
|
|
22
|
-
var fs2 = require('fs');
|
|
23
|
-
var os = require('os');
|
|
24
|
-
var url = require('url');
|
|
25
|
-
var imageSizeOf = require('image-size');
|
|
26
|
-
|
|
27
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
28
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
29
|
-
|
|
30
|
-
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
31
|
-
var Log__default = /*#__PURE__*/_interopDefault(Log);
|
|
32
|
-
var loadConfig__default = /*#__PURE__*/_interopDefault(loadConfig);
|
|
33
|
-
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
34
|
-
var loaderUtils__default = /*#__PURE__*/_interopDefault(loaderUtils);
|
|
35
|
-
var dedent3__default = /*#__PURE__*/_interopDefault(dedent3);
|
|
36
|
-
var loadJsConfig__default = /*#__PURE__*/_interopDefault(loadJsConfig);
|
|
37
|
-
var fs2__default = /*#__PURE__*/_interopDefault(fs2);
|
|
38
|
-
var imageSizeOf__default = /*#__PURE__*/_interopDefault(imageSizeOf);
|
|
39
|
-
|
|
40
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
41
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
42
|
-
}) : x)(function(x) {
|
|
43
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
44
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
45
|
-
});
|
|
46
|
-
var nextDistPath = /(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/;
|
|
47
|
-
async function getConfig(dir) {
|
|
48
|
-
const conf = await loadConfig__default.default(constants.PHASE_TEST, dir);
|
|
49
|
-
return conf;
|
|
50
|
-
}
|
|
51
|
-
async function getConfigPaths(dir) {
|
|
52
|
-
return constants.CONFIG_FILES.map((file) => path.join(dir, file));
|
|
53
|
-
}
|
|
54
|
-
async function loadEnvironmentConfig(dir) {
|
|
55
|
-
const dev = false;
|
|
56
|
-
return env.loadEnvConfig(dir, dev, Log__default.default);
|
|
57
|
-
}
|
|
58
|
-
async function loadSWCBindingsEagerly(nextConfig) {
|
|
59
|
-
await swc.loadBindings(nextConfig?.experimental?.useWasmBinary);
|
|
60
|
-
if (swc.lockfilePatchPromise.cur) {
|
|
61
|
-
await swc.lockfilePatchPromise.cur;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
function shouldOutputCommonJs(filename) {
|
|
65
|
-
return filename.endsWith(".cjs") || nextDistPath.test(filename);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// src/plugins/next-env/plugin.ts
|
|
69
|
-
function vitePluginNextConfig(rootDir) {
|
|
70
|
-
let nextConfig;
|
|
71
|
-
let envConfig;
|
|
72
|
-
let isDev;
|
|
73
|
-
const resolvedDir = path.resolve(rootDir);
|
|
74
|
-
return {
|
|
75
|
-
name: "vite-plugin-storybook-nextjs-swc",
|
|
76
|
-
async config(config, env) {
|
|
77
|
-
nextConfig = await getConfig(resolvedDir);
|
|
78
|
-
envConfig = (await loadEnvironmentConfig(resolvedDir)).combinedEnv;
|
|
79
|
-
isDev = env.mode === "development";
|
|
80
|
-
const publicNextEnvMap = Object.fromEntries(
|
|
81
|
-
Object.entries(envConfig).filter(([key]) => key.startsWith("NEXT_PUBLIC_")).map(([key, value]) => {
|
|
82
|
-
return [`process.env.${key}`, JSON.stringify(value)];
|
|
83
|
-
})
|
|
84
|
-
);
|
|
85
|
-
return {
|
|
86
|
-
...config,
|
|
87
|
-
define: {
|
|
88
|
-
...config.define,
|
|
89
|
-
...publicNextEnvMap,
|
|
90
|
-
...defineEnvPlugin.getDefineEnv({
|
|
91
|
-
isTurbopack: false,
|
|
92
|
-
config: nextConfig,
|
|
93
|
-
isClient: true,
|
|
94
|
-
isEdgeServer: false,
|
|
95
|
-
isNodeOrEdgeCompilation: false,
|
|
96
|
-
isNodeServer: false,
|
|
97
|
-
clientRouterFilters: void 0,
|
|
98
|
-
dev: isDev,
|
|
99
|
-
middlewareMatchers: void 0,
|
|
100
|
-
hasRewrites: false,
|
|
101
|
-
distDir: nextConfig.distDir,
|
|
102
|
-
fetchCacheKeyPrefix: nextConfig?.experimental?.fetchCacheKeyPrefix
|
|
103
|
-
})
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
var cssCache = /* @__PURE__ */ new Map();
|
|
110
|
-
async function getFontFaceDeclarations(options) {
|
|
111
|
-
const {
|
|
112
|
-
fontFamily,
|
|
113
|
-
weights,
|
|
114
|
-
styles,
|
|
115
|
-
selectedVariableAxes,
|
|
116
|
-
display,
|
|
117
|
-
variable
|
|
118
|
-
} = validateGoogleFontFunctionCall.validateGoogleFontFunctionCall(options.fontFamily, options.props);
|
|
119
|
-
const fontAxes = getFontAxes.getFontAxes(
|
|
120
|
-
fontFamily,
|
|
121
|
-
weights,
|
|
122
|
-
styles,
|
|
123
|
-
selectedVariableAxes
|
|
124
|
-
);
|
|
125
|
-
const url = getGoogleFontsUrl.getGoogleFontsUrl(fontFamily, fontAxes, display);
|
|
126
|
-
try {
|
|
127
|
-
const hasCachedCSS = cssCache.has(url);
|
|
128
|
-
const fontFaceCSS = hasCachedCSS ? cssCache.get(url) : await fetchCssFromGoogleFonts.fetchCSSFromGoogleFonts(url, fontFamily, true).catch(() => null);
|
|
129
|
-
if (!hasCachedCSS) {
|
|
130
|
-
cssCache.set(url, fontFaceCSS);
|
|
131
|
-
} else {
|
|
132
|
-
cssCache.delete(url);
|
|
133
|
-
}
|
|
134
|
-
if (fontFaceCSS === null) {
|
|
135
|
-
throw new Error(
|
|
136
|
-
`Failed to fetch \`${fontFamily}\` from Google Fonts with URL: \`${url}\``
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
return {
|
|
140
|
-
id: loaderUtils__default.default.getHashDigest(url, "md5", "hex", 6),
|
|
141
|
-
fontFamily,
|
|
142
|
-
fontFaceCSS,
|
|
143
|
-
weights,
|
|
144
|
-
styles,
|
|
145
|
-
variable
|
|
146
|
-
};
|
|
147
|
-
} catch (error) {
|
|
148
|
-
throw new Error(
|
|
149
|
-
`Failed to fetch \`${fontFamily}\` from Google Fonts with URL: \`${url}\``
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
var getPlaceholderFontUrl = (refId) => `__%%import.meta.ROLLUP_FILE_URL_${refId}%%__`;
|
|
154
|
-
getPlaceholderFontUrl.regexp = /__%%import\.meta\.ROLLUP_FILE_URL_(.*?)%%__/g;
|
|
155
|
-
async function getFontFaceDeclarations2(options) {
|
|
156
|
-
const localFontSrc = options.props.metaSrc;
|
|
157
|
-
const {
|
|
158
|
-
weight,
|
|
159
|
-
style,
|
|
160
|
-
variable,
|
|
161
|
-
declarations = []
|
|
162
|
-
} = validateLocalFontFunctionCall.validateLocalFontFunctionCall("", options.props);
|
|
163
|
-
const id = `font-${loaderUtils__default.default.getHashDigest(
|
|
164
|
-
Buffer.from(JSON.stringify(localFontSrc)),
|
|
165
|
-
"md5",
|
|
166
|
-
"hex",
|
|
167
|
-
6
|
|
168
|
-
)}`;
|
|
169
|
-
const fontDeclarations = declarations.map(
|
|
170
|
-
({ prop, value }) => `${prop}: ${value};`
|
|
171
|
-
).join("\n");
|
|
172
|
-
const getFontFaceCSS = () => {
|
|
173
|
-
if (localFontSrc) {
|
|
174
|
-
if ("fontReferenceId" in localFontSrc) {
|
|
175
|
-
return dedent3.dedent`@font-face {
|
|
176
|
-
font-family: ${id};
|
|
177
|
-
src: url(${localFontSrc.fontReferenceId ? getPlaceholderFontUrl(localFontSrc.fontReferenceId) : `.${localFontSrc.fontPath}`})
|
|
178
|
-
${fontDeclarations}
|
|
179
|
-
}`;
|
|
180
|
-
}
|
|
181
|
-
return localFontSrc.map((font) => {
|
|
182
|
-
return dedent3.dedent`@font-face {
|
|
183
|
-
font-family: ${id};
|
|
184
|
-
src: url(${font.path.fontReferenceId ? getPlaceholderFontUrl(font.path.fontReferenceId) : `.${font.path.fontPath}`});
|
|
185
|
-
${font.weight ? `font-weight: ${font.weight};` : ""}
|
|
186
|
-
${font.style ? `font-style: ${font.style};` : ""}
|
|
187
|
-
${fontDeclarations}
|
|
188
|
-
}`;
|
|
189
|
-
}).join("");
|
|
190
|
-
}
|
|
191
|
-
return "";
|
|
192
|
-
};
|
|
193
|
-
return {
|
|
194
|
-
id,
|
|
195
|
-
fontFamily: id,
|
|
196
|
-
fontFaceCSS: getFontFaceCSS(),
|
|
197
|
-
weights: weight ? [weight] : [],
|
|
198
|
-
styles: style ? [style] : [],
|
|
199
|
-
variable
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// src/plugins/next-font/utils/get-css-meta.ts
|
|
204
|
-
function getCSSMeta(options) {
|
|
205
|
-
const className = getClassName(options);
|
|
206
|
-
const style = getStylesObj(options);
|
|
207
|
-
const variableClassName = `__variable_${className}`;
|
|
208
|
-
const classNamesCSS = `
|
|
209
|
-
.${className} {
|
|
210
|
-
font-family: ${options.fontFamily};
|
|
211
|
-
${isNextCSSPropertyValid(options.styles) ? `font-style: ${options.styles[0]};` : ""}
|
|
212
|
-
${isNextCSSPropertyValid(options.weights) ? `font-weight: ${options.weights[0]};` : ""}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
${options.variable ? `.${variableClassName} { ${options.variable}: '${options.fontFamily}'; }` : ""}
|
|
216
|
-
`;
|
|
217
|
-
const fontFaceCSS = `${changeFontDisplayToSwap(options.fontFaceCSS)}`;
|
|
218
|
-
return {
|
|
219
|
-
className,
|
|
220
|
-
fontFaceCSS,
|
|
221
|
-
classNamesCSS,
|
|
222
|
-
style,
|
|
223
|
-
...options.variable ? { variableClassName } : {}
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
function getClassName({ styles, weights, fontFamily }) {
|
|
227
|
-
const font = fontFamily.replaceAll(" ", "-").toLowerCase();
|
|
228
|
-
const style = isNextCSSPropertyValid(styles) ? styles[0] : null;
|
|
229
|
-
const weight = isNextCSSPropertyValid(weights) ? weights[0] : null;
|
|
230
|
-
return `${font}${style ? `-${style}` : ""}${weight ? `-${weight}` : ""}`;
|
|
231
|
-
}
|
|
232
|
-
function getStylesObj({ styles, weights, fontFamily }) {
|
|
233
|
-
return {
|
|
234
|
-
fontFamily,
|
|
235
|
-
...isNextCSSPropertyValid(styles) ? { fontStyle: styles[0] } : {},
|
|
236
|
-
...isNextCSSPropertyValid(weights) ? { fontWeight: weights[0] } : {}
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
function isNextCSSPropertyValid(prop) {
|
|
240
|
-
return prop.length === 1 && prop[0] !== "variable";
|
|
241
|
-
}
|
|
242
|
-
function changeFontDisplayToSwap(css) {
|
|
243
|
-
return css.replaceAll("font-display: optional;", "font-display: block;");
|
|
244
|
-
}
|
|
245
|
-
function setFontDeclarationsInHead({
|
|
246
|
-
id,
|
|
247
|
-
fontFaceCSS,
|
|
248
|
-
classNamesCSS
|
|
249
|
-
}) {
|
|
250
|
-
const regex = new RegExp(getPlaceholderFontUrl.regexp);
|
|
251
|
-
const fontPaths = fontFaceCSS.matchAll(regex);
|
|
252
|
-
const fontPathsImportUrls = [];
|
|
253
|
-
if (fontPaths) {
|
|
254
|
-
for (const match of fontFaceCSS.matchAll(regex)) {
|
|
255
|
-
fontPathsImportUrls.push({
|
|
256
|
-
id: match[1],
|
|
257
|
-
path: match[0].replaceAll(/__%%|%%__/g, "")
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
return dedent3__default.default`
|
|
262
|
-
const fontPaths = [${fontPathsImportUrls.map((fontPath) => `{id: '${fontPath.id}', path: ${fontPath.path}}`).join(", ")}];
|
|
263
|
-
if (!document.getElementById('id-${id}')) {
|
|
264
|
-
let fontDeclarations = \`${fontFaceCSS}\`;
|
|
265
|
-
fontPaths.forEach((fontPath, i) => {
|
|
266
|
-
fontDeclarations = fontDeclarations.replace('__%%import.meta.ROLLUP_FILE_URL_' + fontPath.id + '%%__', fontPath.path.toString());
|
|
267
|
-
});
|
|
268
|
-
const style = document.createElement('style');
|
|
269
|
-
style.setAttribute('id', 'font-face-${id}');
|
|
270
|
-
style.innerHTML = fontDeclarations;
|
|
271
|
-
document.head.appendChild(style);
|
|
272
|
-
|
|
273
|
-
const classNamesCSS = \`${classNamesCSS}\`;
|
|
274
|
-
const classNamesStyle = document.createElement('style');
|
|
275
|
-
classNamesStyle.setAttribute('id', 'classnames-${id}');
|
|
276
|
-
classNamesStyle.innerHTML = classNamesCSS;
|
|
277
|
-
document.head.appendChild(classNamesStyle);
|
|
278
|
-
}
|
|
279
|
-
`;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// src/plugins/next-font/plugin.ts
|
|
283
|
-
var includePattern = /next(\\|\/|\\\\).*(\\|\/|\\\\)target\.css.*$/;
|
|
284
|
-
var virtualModuleId = "virtual:next-font";
|
|
285
|
-
function configureNextFont() {
|
|
286
|
-
let devMode = true;
|
|
287
|
-
const fontAssetPaths = /* @__PURE__ */ new Map();
|
|
288
|
-
return {
|
|
289
|
-
name: "configure-next-font",
|
|
290
|
-
async config(config, env) {
|
|
291
|
-
devMode = env.mode === "development";
|
|
292
|
-
return config;
|
|
293
|
-
},
|
|
294
|
-
configureServer(server) {
|
|
295
|
-
server.middlewares.use(async (req, res, next) => {
|
|
296
|
-
if (req.url && fontAssetPaths.has(req.url)) {
|
|
297
|
-
const fontAbsoluteAssetPath = fontAssetPaths.get(req.url);
|
|
298
|
-
const fontFileExtension = path__default.default.extname(fontAbsoluteAssetPath);
|
|
299
|
-
try {
|
|
300
|
-
const fontData = await fs__default.default.readFile(fontAbsoluteAssetPath);
|
|
301
|
-
switch (fontFileExtension) {
|
|
302
|
-
case ".woff":
|
|
303
|
-
res.setHeader("Content-Type", "font/woff");
|
|
304
|
-
break;
|
|
305
|
-
case ".woff2":
|
|
306
|
-
res.setHeader("Content-Type", "font/woff2");
|
|
307
|
-
break;
|
|
308
|
-
case ".ttf":
|
|
309
|
-
res.setHeader("Content-Type", "font/ttf");
|
|
310
|
-
break;
|
|
311
|
-
case ".otf":
|
|
312
|
-
res.setHeader("Content-Type", "font/otf");
|
|
313
|
-
break;
|
|
314
|
-
default:
|
|
315
|
-
res.setHeader("Content-Type", "font");
|
|
316
|
-
}
|
|
317
|
-
res.end(fontData);
|
|
318
|
-
} catch (e) {
|
|
319
|
-
console.error(
|
|
320
|
-
`Could not read font file ${fontAbsoluteAssetPath}:`,
|
|
321
|
-
e
|
|
322
|
-
);
|
|
323
|
-
res.statusCode = 404;
|
|
324
|
-
res.end();
|
|
325
|
-
}
|
|
326
|
-
} else {
|
|
327
|
-
next();
|
|
328
|
-
}
|
|
329
|
-
});
|
|
330
|
-
},
|
|
331
|
-
async resolveId(source, importer) {
|
|
332
|
-
if (!includePattern.test(source) || !importer) {
|
|
333
|
-
return null;
|
|
334
|
-
}
|
|
335
|
-
const [sourceWithoutQuery, rawQuery] = source.split("?");
|
|
336
|
-
const queryParams = JSON.parse(rawQuery);
|
|
337
|
-
const fontOptions = {
|
|
338
|
-
filename: queryParams.path ?? "",
|
|
339
|
-
fontFamily: queryParams.import ?? "",
|
|
340
|
-
props: queryParams.arguments?.[0] ?? {},
|
|
341
|
-
source: importer
|
|
342
|
-
};
|
|
343
|
-
if (fontOptions.source.endsWith("html")) {
|
|
344
|
-
return null;
|
|
345
|
-
}
|
|
346
|
-
let fontFaceDeclaration;
|
|
347
|
-
const pathSep = path__default.default.sep;
|
|
348
|
-
if (sourceWithoutQuery.endsWith(
|
|
349
|
-
["next", "font", "google", "target.css"].join(pathSep)
|
|
350
|
-
)) {
|
|
351
|
-
fontFaceDeclaration = await getFontFaceDeclarations(fontOptions);
|
|
352
|
-
}
|
|
353
|
-
if (sourceWithoutQuery.endsWith(
|
|
354
|
-
["next", "font", "local", "target.css"].join(pathSep)
|
|
355
|
-
)) {
|
|
356
|
-
const importerDirPath = path__default.default.dirname(fontOptions.filename);
|
|
357
|
-
const emitFont = async (importerRelativeFontPath) => {
|
|
358
|
-
const fontExtension = path__default.default.extname(importerRelativeFontPath);
|
|
359
|
-
const fontBaseName = path__default.default.basename(
|
|
360
|
-
importerRelativeFontPath,
|
|
361
|
-
fontExtension
|
|
362
|
-
);
|
|
363
|
-
const fontPath = path__default.default.join(importerDirPath, importerRelativeFontPath);
|
|
364
|
-
if (devMode) {
|
|
365
|
-
fontAssetPaths.set(importerRelativeFontPath, fontPath);
|
|
366
|
-
return {
|
|
367
|
-
fontPath: importerRelativeFontPath,
|
|
368
|
-
fontReferenceId: void 0
|
|
369
|
-
};
|
|
370
|
-
}
|
|
371
|
-
try {
|
|
372
|
-
const fontData = await fs__default.default.readFile(fontPath);
|
|
373
|
-
const fontReferenceId = this.emitFile({
|
|
374
|
-
name: `${fontBaseName}${fontExtension}`,
|
|
375
|
-
type: "asset",
|
|
376
|
-
source: fontData
|
|
377
|
-
});
|
|
378
|
-
return { fontReferenceId, fontPath };
|
|
379
|
-
} catch (err) {
|
|
380
|
-
console.error(`Could not read font file ${fontPath}:`, err);
|
|
381
|
-
return void 0;
|
|
382
|
-
}
|
|
383
|
-
};
|
|
384
|
-
const loaderOptions = {
|
|
385
|
-
...fontOptions
|
|
386
|
-
};
|
|
387
|
-
if (loaderOptions) {
|
|
388
|
-
if (typeof fontOptions.props.src === "string") {
|
|
389
|
-
const font = await emitFont(fontOptions.props.src);
|
|
390
|
-
loaderOptions.props.metaSrc = font;
|
|
391
|
-
} else {
|
|
392
|
-
loaderOptions.props.metaSrc = (await Promise.all(
|
|
393
|
-
(fontOptions.props.src ?? []).map(async (fontSrc) => {
|
|
394
|
-
const font = await emitFont(fontSrc.path);
|
|
395
|
-
if (!font) {
|
|
396
|
-
return void 0;
|
|
397
|
-
}
|
|
398
|
-
return {
|
|
399
|
-
...fontSrc,
|
|
400
|
-
path: font
|
|
401
|
-
};
|
|
402
|
-
})
|
|
403
|
-
)).filter((font) => font !== void 0);
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
fontFaceDeclaration = await getFontFaceDeclarations2(loaderOptions);
|
|
407
|
-
}
|
|
408
|
-
return {
|
|
409
|
-
id: `${virtualModuleId}?${rawQuery}`,
|
|
410
|
-
meta: {
|
|
411
|
-
fontFaceDeclaration
|
|
412
|
-
}
|
|
413
|
-
};
|
|
414
|
-
},
|
|
415
|
-
load(id) {
|
|
416
|
-
const [source] = id.split("?");
|
|
417
|
-
if (source !== virtualModuleId) {
|
|
418
|
-
return void 0;
|
|
419
|
-
}
|
|
420
|
-
const moduleInfo = this.getModuleInfo(id);
|
|
421
|
-
const fontFaceDeclaration = moduleInfo?.meta?.fontFaceDeclaration;
|
|
422
|
-
if (typeof fontFaceDeclaration !== "undefined") {
|
|
423
|
-
const cssMeta = getCSSMeta(fontFaceDeclaration);
|
|
424
|
-
return `
|
|
425
|
-
${setFontDeclarationsInHead({
|
|
426
|
-
fontFaceCSS: cssMeta.fontFaceCSS,
|
|
427
|
-
id: fontFaceDeclaration.id,
|
|
428
|
-
classNamesCSS: cssMeta.classNamesCSS
|
|
429
|
-
})}
|
|
430
|
-
|
|
431
|
-
export default {
|
|
432
|
-
className: "${cssMeta.className}",
|
|
433
|
-
style: ${JSON.stringify(cssMeta.style)}
|
|
434
|
-
${cssMeta.variableClassName ? `, variable: "${cssMeta.variableClassName}"` : ""}
|
|
435
|
-
}
|
|
436
|
-
`;
|
|
437
|
-
}
|
|
438
|
-
return "export default {}";
|
|
439
|
-
}
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
// src/utils/swc/styles.ts
|
|
444
|
-
function getStyledComponentsOptions(styledComponentsConfig, development) {
|
|
445
|
-
if (!styledComponentsConfig) {
|
|
446
|
-
return null;
|
|
447
|
-
}
|
|
448
|
-
if (typeof styledComponentsConfig === "object") {
|
|
449
|
-
return {
|
|
450
|
-
...styledComponentsConfig,
|
|
451
|
-
displayName: styledComponentsConfig.displayName ?? Boolean(development)
|
|
452
|
-
};
|
|
453
|
-
}
|
|
454
|
-
return {
|
|
455
|
-
displayName: Boolean(development)
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
function getEmotionOptions(emotionConfig, development) {
|
|
459
|
-
if (!emotionConfig) {
|
|
460
|
-
return null;
|
|
461
|
-
}
|
|
462
|
-
let autoLabel = !!development;
|
|
463
|
-
switch (typeof emotionConfig === "object" && emotionConfig.autoLabel) {
|
|
464
|
-
case "never":
|
|
465
|
-
autoLabel = false;
|
|
466
|
-
break;
|
|
467
|
-
case "always":
|
|
468
|
-
autoLabel = true;
|
|
469
|
-
break;
|
|
470
|
-
}
|
|
471
|
-
return {
|
|
472
|
-
enabled: true,
|
|
473
|
-
autoLabel,
|
|
474
|
-
sourcemap: development,
|
|
475
|
-
...typeof emotionConfig === "object" && {
|
|
476
|
-
importMap: emotionConfig.importMap,
|
|
477
|
-
labelFormat: emotionConfig.labelFormat,
|
|
478
|
-
sourcemap: development && emotionConfig.sourceMap
|
|
479
|
-
}
|
|
480
|
-
};
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
// src/utils/swc/options.ts
|
|
484
|
-
var regeneratorRuntimePath = __require.resolve(
|
|
485
|
-
"next/dist/compiled/regenerator-runtime"
|
|
486
|
-
);
|
|
487
|
-
function getBaseSWCOptions({
|
|
488
|
-
filename,
|
|
489
|
-
development,
|
|
490
|
-
hasReactRefresh,
|
|
491
|
-
globalWindow,
|
|
492
|
-
esm,
|
|
493
|
-
modularizeImports,
|
|
494
|
-
swcPlugins,
|
|
495
|
-
compilerOptions,
|
|
496
|
-
resolvedBaseUrl,
|
|
497
|
-
jsConfig,
|
|
498
|
-
swcCacheDir
|
|
499
|
-
}) {
|
|
500
|
-
const parserConfig = options.getParserOptions({ filename, jsConfig });
|
|
501
|
-
const paths = jsConfig?.compilerOptions?.paths;
|
|
502
|
-
const enableDecorators = Boolean(
|
|
503
|
-
jsConfig?.compilerOptions?.experimentalDecorators
|
|
504
|
-
);
|
|
505
|
-
const emitDecoratorMetadata = Boolean(
|
|
506
|
-
jsConfig?.compilerOptions?.emitDecoratorMetadata
|
|
507
|
-
);
|
|
508
|
-
const useDefineForClassFields = Boolean(
|
|
509
|
-
jsConfig?.compilerOptions?.useDefineForClassFields
|
|
510
|
-
);
|
|
511
|
-
const plugins = (swcPlugins ?? []).filter(Array.isArray).map(([name, options]) => [__require.resolve(name), options]);
|
|
512
|
-
return {
|
|
513
|
-
jsc: {
|
|
514
|
-
...resolvedBaseUrl && paths ? {
|
|
515
|
-
baseUrl: resolvedBaseUrl.baseUrl,
|
|
516
|
-
paths
|
|
517
|
-
} : {},
|
|
518
|
-
externalHelpers: false,
|
|
519
|
-
parser: parserConfig,
|
|
520
|
-
experimental: {
|
|
521
|
-
keepImportAttributes: true,
|
|
522
|
-
emitAssertForImportAttributes: true,
|
|
523
|
-
plugins,
|
|
524
|
-
cacheRoot: swcCacheDir
|
|
525
|
-
},
|
|
526
|
-
transform: {
|
|
527
|
-
legacyDecorator: enableDecorators,
|
|
528
|
-
decoratorMetadata: emitDecoratorMetadata,
|
|
529
|
-
useDefineForClassFields,
|
|
530
|
-
react: {
|
|
531
|
-
importSource: jsConfig?.compilerOptions?.jsxImportSource ?? (compilerOptions?.emotion ? "@emotion/react" : "react"),
|
|
532
|
-
runtime: "automatic",
|
|
533
|
-
pragmaFrag: "React.Fragment",
|
|
534
|
-
throwIfNamespace: true,
|
|
535
|
-
development: !!development,
|
|
536
|
-
useBuiltins: true,
|
|
537
|
-
refresh: !!hasReactRefresh
|
|
538
|
-
},
|
|
539
|
-
optimizer: {
|
|
540
|
-
simplify: false,
|
|
541
|
-
// TODO: Figuring out for what globals are exactly used for
|
|
542
|
-
globals: {
|
|
543
|
-
typeofs: {
|
|
544
|
-
window: globalWindow ? "object" : "undefined"
|
|
545
|
-
},
|
|
546
|
-
envs: {
|
|
547
|
-
NODE_ENV: development ? '"development"' : '"production"'
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
},
|
|
551
|
-
regenerator: {
|
|
552
|
-
importPath: regeneratorRuntimePath
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
},
|
|
556
|
-
sourceMaps: "inline",
|
|
557
|
-
removeConsole: compilerOptions?.removeConsole,
|
|
558
|
-
reactRemoveProperties: false,
|
|
559
|
-
// Map the k-v map to an array of pairs.
|
|
560
|
-
modularizeImports: modularizeImports ? Object.fromEntries(
|
|
561
|
-
Object.entries(modularizeImports).map(([mod, config]) => [
|
|
562
|
-
mod,
|
|
563
|
-
{
|
|
564
|
-
...config,
|
|
565
|
-
transform: typeof config.transform === "string" ? config.transform : Object.entries(config.transform).map(([key, value]) => [
|
|
566
|
-
key,
|
|
567
|
-
value
|
|
568
|
-
])
|
|
569
|
-
}
|
|
570
|
-
])
|
|
571
|
-
) : void 0,
|
|
572
|
-
relay: compilerOptions?.relay,
|
|
573
|
-
// Always transform styled-jsx and error when `client-only` condition is triggered
|
|
574
|
-
styledJsx: {},
|
|
575
|
-
// Disable css-in-js libs (without client-only integration) transform on server layer for server components
|
|
576
|
-
emotion: getEmotionOptions(compilerOptions?.emotion, development),
|
|
577
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
578
|
-
styledComponents: getStyledComponentsOptions(
|
|
579
|
-
compilerOptions?.styledComponents,
|
|
580
|
-
development
|
|
581
|
-
),
|
|
582
|
-
serverComponents: void 0,
|
|
583
|
-
serverActions: void 0,
|
|
584
|
-
// For app router we prefer to bundle ESM,
|
|
585
|
-
// On server side of pages router we prefer CJS.
|
|
586
|
-
preferEsm: esm
|
|
587
|
-
};
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
// src/utils/swc/transform.ts
|
|
591
|
-
var getVitestSWCTransformConfig = ({
|
|
592
|
-
filename,
|
|
593
|
-
inputSourceMap,
|
|
594
|
-
isServerEnvironment,
|
|
595
|
-
loadedJSConfig,
|
|
596
|
-
nextDirectories,
|
|
597
|
-
nextConfig,
|
|
598
|
-
rootDir,
|
|
599
|
-
isDev
|
|
600
|
-
}) => {
|
|
601
|
-
const esm = true;
|
|
602
|
-
const baseOptions = getBaseSWCOptions({
|
|
603
|
-
filename,
|
|
604
|
-
development: false,
|
|
605
|
-
hasReactRefresh: false,
|
|
606
|
-
globalWindow: !isServerEnvironment,
|
|
607
|
-
modularizeImports: nextConfig.modularizeImports,
|
|
608
|
-
jsConfig: loadedJSConfig.jsConfig,
|
|
609
|
-
resolvedBaseUrl: loadedJSConfig.resolvedBaseUrl,
|
|
610
|
-
swcPlugins: nextConfig.experimental.swcPlugins,
|
|
611
|
-
compilerOptions: nextConfig?.compilerOptions,
|
|
612
|
-
esm,
|
|
613
|
-
swcCacheDir: path__default.default.join(
|
|
614
|
-
rootDir,
|
|
615
|
-
nextConfig.distDir ?? ".next",
|
|
616
|
-
"cache",
|
|
617
|
-
"swc"
|
|
618
|
-
)
|
|
619
|
-
});
|
|
620
|
-
const useCjsModules = shouldOutputCommonJs(filename);
|
|
621
|
-
return {
|
|
622
|
-
...baseOptions,
|
|
623
|
-
fontLoaders: {
|
|
624
|
-
fontLoaders: ["next/font/local", "next/font/google"],
|
|
625
|
-
relativeFilePathFromRoot: path__default.default.relative(rootDir, filename)
|
|
626
|
-
},
|
|
627
|
-
cjsRequireOptimizer: {
|
|
628
|
-
packages: {
|
|
629
|
-
"next/server": {
|
|
630
|
-
transforms: {
|
|
631
|
-
NextRequest: "next/dist/server/web/spec-extension/request",
|
|
632
|
-
NextResponse: "next/dist/server/web/spec-extension/response",
|
|
633
|
-
ImageResponse: "next/dist/server/web/spec-extension/image-response",
|
|
634
|
-
userAgentFromString: "next/dist/server/web/spec-extension/user-agent",
|
|
635
|
-
userAgent: "next/dist/server/web/spec-extension/user-agent"
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
},
|
|
640
|
-
...isServerEnvironment ? {
|
|
641
|
-
env: {
|
|
642
|
-
targets: {
|
|
643
|
-
// Targets the current version of Node.js
|
|
644
|
-
node: process.versions.node
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
} : {
|
|
648
|
-
env: {
|
|
649
|
-
targets: utils.getSupportedBrowsers(rootDir, isDev)
|
|
650
|
-
}
|
|
651
|
-
},
|
|
652
|
-
module: {
|
|
653
|
-
type: esm && !useCjsModules ? "es6" : "commonjs"
|
|
654
|
-
},
|
|
655
|
-
disableNextSsg: true,
|
|
656
|
-
disablePageConfig: true,
|
|
657
|
-
pagesDir: nextDirectories.pagesDir,
|
|
658
|
-
appDir: nextDirectories.appDir,
|
|
659
|
-
inputSourceMap: inputSourceMap && typeof inputSourceMap === "object" ? JSON.stringify(inputSourceMap) : void 0,
|
|
660
|
-
sourceFileName: filename,
|
|
661
|
-
filename
|
|
662
|
-
};
|
|
663
|
-
};
|
|
664
|
-
|
|
665
|
-
// src/utils/typescript.ts
|
|
666
|
-
var isDefined = (value) => value !== void 0;
|
|
667
|
-
|
|
668
|
-
// src/plugins/next-swc/plugin.ts
|
|
669
|
-
var excluded = /[\\/](cache[\\/][^\\/]+\.zip[\\/]node_modules|virtual:)[\\/]/g;
|
|
670
|
-
var included = /\.((c|m)?(j|t)sx?)$/;
|
|
671
|
-
function vitePluginNextSwc(rootDir, nextConfigResolver) {
|
|
672
|
-
let loadedJSConfig;
|
|
673
|
-
let nextDirectories;
|
|
674
|
-
let isServerEnvironment;
|
|
675
|
-
let isDev;
|
|
676
|
-
const resolvedDir = path.resolve(rootDir);
|
|
677
|
-
return {
|
|
678
|
-
name: "vite-plugin-storybook-nextjs-swc",
|
|
679
|
-
async config(config, env) {
|
|
680
|
-
const nextConfig = await nextConfigResolver.promise;
|
|
681
|
-
nextDirectories = findPagesDir.findPagesDir(resolvedDir);
|
|
682
|
-
loadedJSConfig = await loadJsConfig__default.default(resolvedDir, nextConfig);
|
|
683
|
-
(await loadEnvironmentConfig(resolvedDir)).combinedEnv;
|
|
684
|
-
isDev = env.mode === "development";
|
|
685
|
-
await loadSWCBindingsEagerly(nextConfig);
|
|
686
|
-
const serverWatchIgnored = config.server?.watch?.ignored;
|
|
687
|
-
const isServerWatchIgnoredArray = Array.isArray(serverWatchIgnored);
|
|
688
|
-
if (config.test?.environment === "node" || config.test?.environment === "edge-runtime" || config.test?.browser?.enabled !== false) {
|
|
689
|
-
isServerEnvironment = true;
|
|
690
|
-
}
|
|
691
|
-
return {
|
|
692
|
-
resolve: {
|
|
693
|
-
alias: {
|
|
694
|
-
"@opentelemetry/api": "next/dist/compiled/@opentelemetry/api"
|
|
695
|
-
}
|
|
696
|
-
},
|
|
697
|
-
esbuild: {
|
|
698
|
-
// We will use Next.js custom SWC transpiler instead of Vite's build-in esbuild
|
|
699
|
-
exclude: [/node_modules/, /.m?(t|j)sx?/]
|
|
700
|
-
},
|
|
701
|
-
server: {
|
|
702
|
-
watch: {
|
|
703
|
-
ignored: [
|
|
704
|
-
...isServerWatchIgnoredArray ? serverWatchIgnored : [serverWatchIgnored],
|
|
705
|
-
"/.next/"
|
|
706
|
-
].filter(isDefined)
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
};
|
|
710
|
-
},
|
|
711
|
-
async transform(code, id) {
|
|
712
|
-
if (!excluded.test(id) && included.test(id)) {
|
|
713
|
-
const inputSourceMap = this.getCombinedSourcemap();
|
|
714
|
-
const output = await swc.transform(
|
|
715
|
-
code,
|
|
716
|
-
getVitestSWCTransformConfig({
|
|
717
|
-
filename: id,
|
|
718
|
-
inputSourceMap,
|
|
719
|
-
isServerEnvironment,
|
|
720
|
-
loadedJSConfig,
|
|
721
|
-
nextConfig: await nextConfigResolver.promise,
|
|
722
|
-
nextDirectories,
|
|
723
|
-
rootDir,
|
|
724
|
-
isDev
|
|
725
|
-
})
|
|
726
|
-
);
|
|
727
|
-
return output;
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
};
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
// src/polyfills/promise-with-resolvers.ts
|
|
734
|
-
if (typeof Promise.withResolvers === "undefined") {
|
|
735
|
-
Promise.withResolvers = () => {
|
|
736
|
-
let resolve4;
|
|
737
|
-
let reject;
|
|
738
|
-
const promise = new Promise((res, rej) => {
|
|
739
|
-
resolve4 = res;
|
|
740
|
-
reject = rej;
|
|
741
|
-
});
|
|
742
|
-
return { promise, resolve: resolve4, reject };
|
|
743
|
-
};
|
|
744
|
-
}
|
|
745
|
-
var includePattern2 = /\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/;
|
|
746
|
-
var excludeImporterPattern = /\.(css|scss|sass)$/;
|
|
747
|
-
var virtualImage = "virtual:next-image";
|
|
748
|
-
var virtualNextImage = "virtual:next/image";
|
|
749
|
-
var virtualNextLegacyImage = "virtual:next/legacy/image";
|
|
750
|
-
var virtualNextImageDefaultLoader = "virtual:next/image-default-loader";
|
|
751
|
-
var virtualNextImageContext = "virtual:next/image-context";
|
|
752
|
-
var sharp;
|
|
753
|
-
try {
|
|
754
|
-
sharp = __require("sharp");
|
|
755
|
-
if (sharp && sharp.concurrency() > 1) {
|
|
756
|
-
const divisor = process.env.NODE_ENV === "development" ? 4 : 2;
|
|
757
|
-
sharp.concurrency(Math.floor(Math.max(os.cpus().length / divisor, 1)));
|
|
758
|
-
}
|
|
759
|
-
} catch (e) {
|
|
760
|
-
console.warn(
|
|
761
|
-
"You have to install sharp in order to use image optimization features in Next.js. AVIF support is also disabled."
|
|
762
|
-
);
|
|
763
|
-
}
|
|
764
|
-
function vitePluginNextImage(nextConfigResolver) {
|
|
765
|
-
return {
|
|
766
|
-
name: "vite-plugin-storybook-nextjs-image",
|
|
767
|
-
config(config, env) {
|
|
768
|
-
env.mode === "development";
|
|
769
|
-
return {
|
|
770
|
-
...config,
|
|
771
|
-
resolve: {
|
|
772
|
-
...config.resolve,
|
|
773
|
-
alias: {
|
|
774
|
-
react: "next/dist/compiled/react",
|
|
775
|
-
"react-dom": "next/dist/compiled/react-dom"
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
};
|
|
779
|
-
},
|
|
780
|
-
async resolveId(id, importer) {
|
|
781
|
-
if (includePattern2.test(id) && !excludeImporterPattern.test(importer ?? "") && !importer?.startsWith(virtualImage)) {
|
|
782
|
-
return {
|
|
783
|
-
id: `${virtualImage}?${id}`,
|
|
784
|
-
meta: {
|
|
785
|
-
id,
|
|
786
|
-
importer: importer ?? ""
|
|
787
|
-
}
|
|
788
|
-
};
|
|
789
|
-
}
|
|
790
|
-
if (id === "next/image" && importer !== virtualNextImage) {
|
|
791
|
-
return virtualNextImage;
|
|
792
|
-
}
|
|
793
|
-
if (id === "next/legacy/image" && importer !== virtualNextLegacyImage) {
|
|
794
|
-
return virtualNextLegacyImage;
|
|
795
|
-
}
|
|
796
|
-
if (id === "sb-original/image-context") {
|
|
797
|
-
return virtualNextImageContext;
|
|
798
|
-
}
|
|
799
|
-
if (id === "sb-original/default-loader") {
|
|
800
|
-
return virtualNextImageDefaultLoader;
|
|
801
|
-
}
|
|
802
|
-
return null;
|
|
803
|
-
},
|
|
804
|
-
async load(id) {
|
|
805
|
-
if (virtualNextImage === id) {
|
|
806
|
-
return (await fs2__default.default.promises.readFile(
|
|
807
|
-
url.fileURLToPath(
|
|
808
|
-
new url.URL(
|
|
809
|
-
"./plugins/next-image/alias/next-image.js",
|
|
810
|
-
(typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href))
|
|
811
|
-
)
|
|
812
|
-
)
|
|
813
|
-
)).toString("utf-8");
|
|
814
|
-
}
|
|
815
|
-
if (virtualNextLegacyImage === id) {
|
|
816
|
-
return (await fs2__default.default.promises.readFile(
|
|
817
|
-
url.fileURLToPath(
|
|
818
|
-
new url.URL(
|
|
819
|
-
"./plugins/next-image/alias/next-legacy-image.js",
|
|
820
|
-
(typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href))
|
|
821
|
-
)
|
|
822
|
-
)
|
|
823
|
-
)).toString("utf-8");
|
|
824
|
-
}
|
|
825
|
-
if (virtualNextImageDefaultLoader === id) {
|
|
826
|
-
return (await fs2__default.default.promises.readFile(
|
|
827
|
-
url.fileURLToPath(
|
|
828
|
-
new url.URL(
|
|
829
|
-
"./plugins/next-image/alias/image-default-loader.js",
|
|
830
|
-
(typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href))
|
|
831
|
-
)
|
|
832
|
-
)
|
|
833
|
-
)).toString("utf-8");
|
|
834
|
-
}
|
|
835
|
-
if (virtualNextImageContext === id) {
|
|
836
|
-
return (await fs2__default.default.promises.readFile(
|
|
837
|
-
url.fileURLToPath(
|
|
838
|
-
new url.URL(
|
|
839
|
-
"./plugins/next-image/alias/image-context.js",
|
|
840
|
-
(typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href))
|
|
841
|
-
)
|
|
842
|
-
)
|
|
843
|
-
)).toString("utf-8");
|
|
844
|
-
}
|
|
845
|
-
const [source] = id.split("?");
|
|
846
|
-
if (virtualImage === source) {
|
|
847
|
-
const moduleInfo = this.getModuleInfo(id);
|
|
848
|
-
const meta = moduleInfo?.meta;
|
|
849
|
-
path__default.default.basename(id);
|
|
850
|
-
const nextConfig = await nextConfigResolver.promise;
|
|
851
|
-
const extension = meta.id.split(".").pop();
|
|
852
|
-
const imagePath = path__default.default.join(path__default.default.dirname(meta.importer), meta.id);
|
|
853
|
-
try {
|
|
854
|
-
if (nextConfig.images?.disableStaticImages) {
|
|
855
|
-
return dedent3__default.default`
|
|
856
|
-
import image from "${imagePath}";
|
|
857
|
-
export default image;
|
|
858
|
-
`;
|
|
859
|
-
}
|
|
860
|
-
const imageData = await fs2__default.default.promises.readFile(imagePath);
|
|
861
|
-
let width;
|
|
862
|
-
let height;
|
|
863
|
-
if (extension === "avif" && sharp) {
|
|
864
|
-
const transformer = sharp(Buffer.from(imageData));
|
|
865
|
-
const result = await transformer.metadata();
|
|
866
|
-
width = result.width;
|
|
867
|
-
height = result.height;
|
|
868
|
-
} else {
|
|
869
|
-
const result = imageSizeOf__default.default(imageData);
|
|
870
|
-
width = result.width;
|
|
871
|
-
height = result.height;
|
|
872
|
-
}
|
|
873
|
-
return dedent3__default.default`
|
|
874
|
-
import src from "${imagePath}";
|
|
875
|
-
export default {
|
|
876
|
-
src,
|
|
877
|
-
height: ${height},
|
|
878
|
-
width: ${width},
|
|
879
|
-
blurDataURL: src
|
|
880
|
-
};
|
|
881
|
-
`;
|
|
882
|
-
} catch (err) {
|
|
883
|
-
console.error(`Could not read font file ${imagePath}:`, err);
|
|
884
|
-
return void 0;
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
return null;
|
|
888
|
-
}
|
|
889
|
-
};
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
// src/index.ts
|
|
893
|
-
function VitePlugin({ dir = process.cwd() } = {}) {
|
|
894
|
-
const resolvedDir = path.resolve(dir);
|
|
895
|
-
const nextConfigResolver = Promise.withResolvers();
|
|
896
|
-
const nextFontPlugin = configureNextFont();
|
|
897
|
-
const nextSwcPlugin = vitePluginNextSwc(dir, nextConfigResolver);
|
|
898
|
-
const nextEnvPlugin = vitePluginNextConfig(dir);
|
|
899
|
-
const nextImagePlugin = vitePluginNextImage(nextConfigResolver);
|
|
900
|
-
return {
|
|
901
|
-
name: "vite-plugin-storybook-nextjs",
|
|
902
|
-
async buildStart() {
|
|
903
|
-
for (const configPath of await getConfigPaths(resolvedDir)) {
|
|
904
|
-
this.addWatchFile(configPath);
|
|
905
|
-
}
|
|
906
|
-
},
|
|
907
|
-
enforce: "pre",
|
|
908
|
-
configureServer(server) {
|
|
909
|
-
nextFontPlugin.configureServer.call(this, server);
|
|
910
|
-
},
|
|
911
|
-
async config(config, env) {
|
|
912
|
-
nextConfigResolver.resolve(await getConfig(resolvedDir));
|
|
913
|
-
const mergedNextSwcConfig = await nextSwcPlugin.config.call(
|
|
914
|
-
this,
|
|
915
|
-
config,
|
|
916
|
-
env
|
|
917
|
-
);
|
|
918
|
-
const mergedNextEnvConfig = await nextEnvPlugin.config.call(
|
|
919
|
-
this,
|
|
920
|
-
mergedNextSwcConfig,
|
|
921
|
-
env
|
|
922
|
-
);
|
|
923
|
-
const mergedNextFontConfig = await nextFontPlugin.config.call(
|
|
924
|
-
this,
|
|
925
|
-
mergedNextEnvConfig,
|
|
926
|
-
env
|
|
927
|
-
);
|
|
928
|
-
const mergedNextImageConfig = await nextImagePlugin.config.call(
|
|
929
|
-
this,
|
|
930
|
-
mergedNextFontConfig,
|
|
931
|
-
env
|
|
932
|
-
);
|
|
933
|
-
return mergedNextImageConfig;
|
|
934
|
-
},
|
|
935
|
-
async resolveId(source, importer, options) {
|
|
936
|
-
const nextFontResolver = await nextFontPlugin.resolveId.call(
|
|
937
|
-
this,
|
|
938
|
-
source,
|
|
939
|
-
importer
|
|
940
|
-
);
|
|
941
|
-
if (nextFontResolver) {
|
|
942
|
-
return nextFontResolver;
|
|
943
|
-
}
|
|
944
|
-
return nextImagePlugin.resolveId.call(this, source, importer);
|
|
945
|
-
},
|
|
946
|
-
load(id) {
|
|
947
|
-
const nextFontLoaderResult = nextFontPlugin.load.call(this, id);
|
|
948
|
-
if (nextFontLoaderResult) {
|
|
949
|
-
return nextFontLoaderResult;
|
|
950
|
-
}
|
|
951
|
-
return nextImagePlugin.load.call(this, id);
|
|
952
|
-
},
|
|
953
|
-
transform(code, id) {
|
|
954
|
-
return nextSwcPlugin.transform.call(this, code, id);
|
|
955
|
-
}
|
|
956
|
-
};
|
|
957
|
-
}
|
|
958
|
-
var src_default = VitePlugin;
|
|
959
|
-
|
|
960
|
-
module.exports = src_default;
|