@storybook/builder-vite 10.1.0-alpha.8 → 10.1.0-beta.0

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/preset.js ADDED
@@ -0,0 +1,228 @@
1
+ import CJS_COMPAT_NODE_URL_8o5hhwpaftc from 'node:url';
2
+ import CJS_COMPAT_NODE_PATH_8o5hhwpaftc from 'node:path';
3
+ import CJS_COMPAT_NODE_MODULE_8o5hhwpaftc from "node:module";
4
+
5
+ var __filename = CJS_COMPAT_NODE_URL_8o5hhwpaftc.fileURLToPath(import.meta.url);
6
+ var __dirname = CJS_COMPAT_NODE_PATH_8o5hhwpaftc.dirname(__filename);
7
+ var require = CJS_COMPAT_NODE_MODULE_8o5hhwpaftc.createRequire(import.meta.url);
8
+
9
+ // ------------------------------------------------------------
10
+ // end of CJS compatibility banner, injected by Storybook's esbuild configuration
11
+ // ------------------------------------------------------------
12
+ import {
13
+ normalize
14
+ } from "./_node-chunks/chunk-FBOBG4CP.js";
15
+
16
+ // src/preset.ts
17
+ import { findConfigFile } from "storybook/internal/common";
18
+
19
+ // src/plugins/vite-inject-mocker/plugin.ts
20
+ import { readFileSync } from "node:fs";
21
+ import { join } from "node:path";
22
+ import { fileURLToPath } from "node:url";
23
+ import { resolvePackageDir } from "storybook/internal/common";
24
+
25
+ // ../../node_modules/@rolldown/pluginutils/dist/index.js
26
+ function exactRegex(str, flags) {
27
+ return new RegExp(`^${escapeRegex(str)}$`, flags);
28
+ }
29
+ var escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
30
+ function escapeRegex(str) {
31
+ return str.replace(escapeRegexRE, "\\$&");
32
+ }
33
+
34
+ // src/plugins/vite-inject-mocker/plugin.ts
35
+ import { dedent } from "ts-dedent";
36
+ var entryPath = "/vite-inject-mocker-entry.js", entryCode = dedent`
37
+ <script type="module" src=".${entryPath}"></script>
38
+ `, server, viteInjectMockerRuntime = (options) => {
39
+ let viteConfig;
40
+ return {
41
+ name: "vite:storybook-inject-mocker-runtime",
42
+ buildStart() {
43
+ viteConfig.command === "build" && this.emitFile({
44
+ type: "chunk",
45
+ id: join(
46
+ resolvePackageDir("storybook"),
47
+ "assets",
48
+ "server",
49
+ "mocker-runtime.template.js"
50
+ ),
51
+ fileName: entryPath.slice(1)
52
+ });
53
+ },
54
+ config() {
55
+ return {
56
+ optimizeDeps: {
57
+ include: ["@vitest/mocker", "@vitest/mocker/browser"]
58
+ },
59
+ resolve: {
60
+ // Aliasing necessary for package managers like pnpm, since resolving modules from a virtual module
61
+ // leads to errors, if the imported module is not a dependency of the project.
62
+ // By resolving the module to the real path, we can avoid this issue.
63
+ alias: {
64
+ "@vitest/mocker/browser": fileURLToPath(import.meta.resolve("@vitest/mocker/browser")),
65
+ "@vitest/mocker": fileURLToPath(import.meta.resolve("@vitest/mocker"))
66
+ }
67
+ }
68
+ };
69
+ },
70
+ configResolved(config) {
71
+ viteConfig = config;
72
+ },
73
+ configureServer(server_) {
74
+ server = server_, options.previewConfigPath && server.watcher.on("change", (file) => {
75
+ file === options.previewConfigPath && server.ws.send({
76
+ type: "custom",
77
+ event: "invalidate-mocker"
78
+ });
79
+ });
80
+ },
81
+ resolveId: {
82
+ filter: {
83
+ id: [exactRegex(entryPath)]
84
+ },
85
+ handler(id) {
86
+ return exactRegex(id).test(entryPath) ? id : null;
87
+ }
88
+ },
89
+ async load(id) {
90
+ return exactRegex(id).test(entryPath) ? readFileSync(
91
+ join(resolvePackageDir("storybook"), "assets", "server", "mocker-runtime.template.js"),
92
+ "utf-8"
93
+ ) : null;
94
+ },
95
+ transformIndexHtml(html) {
96
+ let headTag = html.match(/<head[^>]*>/);
97
+ if (headTag) {
98
+ let headTagIndex = html.indexOf(headTag[0]);
99
+ return html.slice(0, headTagIndex + headTag[0].length) + entryCode + html.slice(headTagIndex + headTag[0].length);
100
+ }
101
+ }
102
+ };
103
+ };
104
+
105
+ // src/plugins/vite-mock/plugin.ts
106
+ import { readFileSync as readFileSync2 } from "node:fs";
107
+ import {
108
+ babelParser,
109
+ extractMockCalls,
110
+ getAutomockCode,
111
+ getRealPath,
112
+ rewriteSbMockImportCalls
113
+ } from "storybook/internal/mocking-utils";
114
+ import { logger } from "storybook/internal/node-logger";
115
+ import { findMockRedirect } from "@vitest/mocker/redirect";
116
+
117
+ // src/plugins/vite-mock/utils.ts
118
+ function getCleanId(id) {
119
+ return id.replace(/^.*\/deps\//, "").replace(/\.js.*$/, "").replace(/_/g, "/");
120
+ }
121
+ function invalidateAllRelatedModules(server2, absPath, pkgName) {
122
+ for (let mod of server2.moduleGraph.idToModuleMap.values())
123
+ (mod.id === absPath || mod.id && getCleanId(mod.id) === pkgName) && server2.moduleGraph.invalidateModule(mod);
124
+ }
125
+
126
+ // src/plugins/vite-mock/plugin.ts
127
+ function viteMockPlugin(options) {
128
+ let viteConfig, mockCalls = [], normalizedPreviewConfigPath = normalize(options.previewConfigPath);
129
+ return [
130
+ {
131
+ name: "storybook:mock-loader",
132
+ configResolved(config) {
133
+ viteConfig = config;
134
+ },
135
+ buildStart() {
136
+ mockCalls = extractMockCalls(options, babelParser, viteConfig.root, findMockRedirect);
137
+ },
138
+ configureServer(server2) {
139
+ async function invalidateAffectedFiles(file) {
140
+ if (file === options.previewConfigPath || file.includes("__mocks__")) {
141
+ let oldMockCalls = mockCalls;
142
+ mockCalls = extractMockCalls(options, babelParser, viteConfig.root, findMockRedirect);
143
+ let previewMod = server2.moduleGraph.getModuleById(options.previewConfigPath);
144
+ previewMod && server2.moduleGraph.invalidateModule(previewMod);
145
+ for (let call of mockCalls)
146
+ invalidateAllRelatedModules(server2, call.absolutePath, call.path);
147
+ let newAbsPaths = new Set(mockCalls.map((c) => c.absolutePath));
148
+ for (let oldCall of oldMockCalls)
149
+ newAbsPaths.has(oldCall.absolutePath) || invalidateAllRelatedModules(server2, oldCall.absolutePath, oldCall.path);
150
+ return server2.ws.send({ type: "full-reload" }), [];
151
+ }
152
+ }
153
+ server2.watcher.on("change", invalidateAffectedFiles), server2.watcher.on("add", invalidateAffectedFiles), server2.watcher.on("unlink", invalidateAffectedFiles);
154
+ },
155
+ load: {
156
+ order: "pre",
157
+ handler(id) {
158
+ let preserveSymlinks = viteConfig.resolve.preserveSymlinks, idNorm = getRealPath(id, preserveSymlinks), cleanId = getCleanId(idNorm);
159
+ for (let call of mockCalls)
160
+ if (!(getRealPath(call.absolutePath, preserveSymlinks) !== idNorm && call.path !== cleanId) && call.redirectPath)
161
+ return this.addWatchFile(call.redirectPath), readFileSync2(call.redirectPath, "utf-8");
162
+ return null;
163
+ }
164
+ },
165
+ transform: {
166
+ order: "pre",
167
+ handler(code, id) {
168
+ for (let call of mockCalls) {
169
+ let preserveSymlinks = viteConfig.resolve.preserveSymlinks, idNorm = getRealPath(id, preserveSymlinks), callNorm = getRealPath(call.absolutePath, preserveSymlinks);
170
+ if (viteConfig.command !== "serve") {
171
+ if (callNorm !== idNorm)
172
+ continue;
173
+ } else {
174
+ let cleanId = getCleanId(idNorm);
175
+ if (call.path !== cleanId && callNorm !== idNorm)
176
+ continue;
177
+ }
178
+ try {
179
+ if (!call.redirectPath) {
180
+ let automockedCode = getAutomockCode(code, call.spy, babelParser);
181
+ return {
182
+ code: automockedCode.toString(),
183
+ map: automockedCode.generateMap()
184
+ };
185
+ }
186
+ } catch (e) {
187
+ return logger.error(`Error automocking ${id}: ${e}`), null;
188
+ }
189
+ }
190
+ return null;
191
+ }
192
+ }
193
+ },
194
+ {
195
+ name: "storybook:mock-loader-preview",
196
+ transform(code, id) {
197
+ if (id === normalizedPreviewConfigPath)
198
+ try {
199
+ return rewriteSbMockImportCalls(code);
200
+ } catch (e) {
201
+ return logger.debug(`Could not transform sb.mock(import(...)) calls in ${id}: ${e}`), null;
202
+ }
203
+ return null;
204
+ }
205
+ }
206
+ ];
207
+ }
208
+
209
+ // src/preset.ts
210
+ async function viteFinal(existing, options) {
211
+ let previewConfigPath = findConfigFile("preview", options.configDir);
212
+ if (!previewConfigPath)
213
+ return existing;
214
+ let coreOptions = await options.presets.apply("core");
215
+ return {
216
+ ...existing,
217
+ plugins: [
218
+ ...existing.plugins ?? [],
219
+ ...previewConfigPath ? [
220
+ viteInjectMockerRuntime({ previewConfigPath }),
221
+ viteMockPlugin({ previewConfigPath, coreOptions, configDir: options.configDir })
222
+ ] : []
223
+ ]
224
+ };
225
+ }
226
+ export {
227
+ viteFinal
228
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/builder-vite",
3
- "version": "10.1.0-alpha.8",
3
+ "version": "10.1.0-beta.0",
4
4
  "description": "A Storybook builder to dev and build with Vite",
5
5
  "keywords": [
6
6
  "storybook",
@@ -33,7 +33,8 @@
33
33
  "default": "./dist/index.js"
34
34
  },
35
35
  "./input/iframe.html": "./input/iframe.html",
36
- "./package.json": "./package.json"
36
+ "./package.json": "./package.json",
37
+ "./preset": "./dist/preset.js"
37
38
  },
38
39
  "files": [
39
40
  "dist/**/*",
@@ -41,6 +42,7 @@
41
42
  "README.md",
42
43
  "*.js",
43
44
  "*.d.ts",
45
+ "preset.js",
44
46
  "!src/**/*"
45
47
  ],
46
48
  "scripts": {
@@ -48,7 +50,8 @@
48
50
  "prep": "jiti ../../../scripts/build/build-package.ts"
49
51
  },
50
52
  "dependencies": {
51
- "@storybook/csf-plugin": "10.1.0-alpha.8",
53
+ "@storybook/csf-plugin": "10.1.0-beta.0",
54
+ "@vitest/mocker": "3.2.4",
52
55
  "ts-dedent": "^2.0.0"
53
56
  },
54
57
  "devDependencies": {
@@ -63,11 +66,11 @@
63
66
  "vite": "^7.0.4"
64
67
  },
65
68
  "peerDependencies": {
66
- "storybook": "^10.1.0-alpha.8",
69
+ "storybook": "^10.1.0-beta.0",
67
70
  "vite": "^5.0.0 || ^6.0.0 || ^7.0.0"
68
71
  },
69
72
  "publishConfig": {
70
73
  "access": "public"
71
74
  },
72
- "gitHead": "a8e7fd8a655c69780bc20b9749d2699e45beae16"
75
+ "gitHead": "a8e7fd8a655c69780bc20b9749d2699e45beae1l"
73
76
  }
package/preset.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/preset.js';