Skip to content

Commit 204ede0

Browse files
docs: de-duplicate vite specific configurations from storybook guide
1 parent b915d4b commit 204ede0

1 file changed

Lines changed: 2 additions & 141 deletions

File tree

src/pages/guides/ecosystem/storybook.md

Lines changed: 2 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ We were not able to detect the right builder for your project. Please select one
4747
Webpack 5
4848
```
4949
50+
> See our Vitest docs to see additional configuration examples for [Import Attributes](/guides/ecosystem/vitest/#import-attributes) and [Greenwood resource plugins](/guides/ecosystem/vitest/#resource-plugins); updating your _vite.config.js_ file instead.
51+
5052
## Usage
5153
5254
You should now be good to start writing your first story! 📚
@@ -145,147 +147,6 @@ You'll want to create a CommonJS version with the following name, depending on w
145147
146148
<!-- prettier-ignore-end -->
147149
148-
## Import Attributes
149-
150-
As [Vite does not support Import Attributes](https://github.com/vitejs/vite/issues/14674), we will need to create a _vite.config.js_ and write a [custom plugin](https://vitejs.dev/guide/api-plugin) to work around this.
151-
152-
In this example we are handling for CSS Module scripts:
153-
154-
<!-- prettier-ignore-start -->
155-
156-
<app-ctc-block variant="snippet" heading="vite.config.js">
157-
158-
```js
159-
import { defineConfig } from "vite";
160-
import fs from "node:fs/promises";
161-
import path from "node:path";
162-
// 1) import the greenwood plugin and lifecycle helpers
163-
import { greenwoodPluginStandardCss } from "@greenwood/cli/src/plugins/resource/plugin-standard-css.js";
164-
import { readAndMergeConfig } from "@greenwood/cli/src/lifecycles/config.js";
165-
import { initContext } from "@greenwood/cli/src/lifecycles/context.js";
166-
167-
// 2) initialize Greenwood lifecycles
168-
const config = await readAndMergeConfig();
169-
const context = await initContext({ config });
170-
const compilation = { context, config };
171-
172-
// 3) initialize the plugin
173-
const standardCssResource = greenwoodPluginStandardCss.provider(compilation);
174-
175-
// 4) customize Vite
176-
function transformConstructableStylesheetsPlugin() {
177-
return {
178-
name: "transform-constructable-stylesheets",
179-
enforce: "pre",
180-
resolveId: (id, importer) => {
181-
if (
182-
// you'll need to configure this `importer` line to the location of your own components
183-
importer?.indexOf("/src/components/") >= 0 &&
184-
id.endsWith(".css") &&
185-
!id.endsWith(".module.css")
186-
) {
187-
// append .type to the end of Constructable Stylesheet file paths so that they are not automatically precessed by Vite's default pipeline
188-
return path.join(path.dirname(importer), `${id}.type`);
189-
}
190-
},
191-
load: async (id) => {
192-
if (id.endsWith(".css.type")) {
193-
const filename = id.slice(0, -5);
194-
const contents = await fs.readFile(filename, "utf-8");
195-
const url = new URL(`file://${id.replace(".type", "")}`);
196-
// "coerce" native constructable stylesheets into inline JS so Vite / Rollup do not complain
197-
const request = new Request(url, {
198-
headers: {
199-
Accept: "text/javascript",
200-
},
201-
});
202-
const response = await standardCssResource.intercept(url, request, new Response(contents));
203-
const body = await response.text();
204-
205-
return body;
206-
}
207-
},
208-
};
209-
}
210-
211-
export default defineConfig({
212-
// 5) add it the plugins option
213-
plugins: [transformConstructableStylesheetsPlugin()],
214-
});
215-
```
216-
217-
</app-ctc-block>
218-
219-
<!-- prettier-ignore-end -->
220-
221-
Phew, should be all set now.
222-
223-
## Resource Plugins
224-
225-
If you're using one of Greenwood's [resource plugins](/docs/plugins/), you'll need a _vite.config.js_ so we can create a custom transformation plugin that can leverage Greenwood's plugins to automatically handle custom transformations.
226-
227-
For example, if you're using Greenwood's [Raw Plugin](https://github.com/ProjectEvergreen/greenwood/tree/master/packages/plugin-import-raw), you'll need to create a wrapping Vite plugin to handle this transformation.
228-
229-
<!-- prettier-ignore-start -->
230-
231-
<app-ctc-block variant="snippet" heading="vite.config.js">
232-
233-
```js
234-
import { defineConfig } from "vite";
235-
import fs from "node:fs/promises";
236-
import path from 'node:path';
237-
// 1) import the greenwood plugin and lifecycle helpers
238-
import { greenwoodPluginImportRaw } from "@greenwood/plugin-import-raw";
239-
import { readAndMergeConfig } from "@greenwood/cli/src/lifecycles/config.js";
240-
import { initContext } from "@greenwood/cli/src/lifecycles/context.js";
241-
242-
// 2) initialize Greenwood lifecycles
243-
const config = await readAndMergeConfig();
244-
const context = await initContext({ config });
245-
const compilation = { context, config };
246-
247-
// 3) initialize the plugin
248-
const rawResource = greenwoodPluginImportRaw()[0].provider(compilation);
249-
250-
// 4) customize Vite
251-
function transformRawImports() {
252-
const hint = "?type=raw";
253-
254-
return {
255-
name: "transform-raw-imports",
256-
enforce: "pre",
257-
resolveId: (id, importer) => {
258-
259-
if (
260-
id.endsWith(hint)
261-
) {
262-
// append .type to the end of .css file paths so they are not automatically precessed by Vite's default CSS pipeline
263-
return path.join(path.dirname(importer), `${id.slice(0, id.indexOf(hint))}.type${hint}`);
264-
}
265-
},
266-
load: async (id) => {
267-
if (id.endsWith(hint)) {
268-
const filename = id.slice(0, id.indexOf(`.type${hint}`));
269-
const contents = await fs.readFile(filename, "utf-8");
270-
const response = await rawResource.intercept(new URL(`file://${filename}`), null, new Response(contents));
271-
const body = await response.text();
272-
273-
return body;
274-
}
275-
},
276-
};
277-
}
278-
279-
export default defineConfig({
280-
// 5) add it the plugins option
281-
plugins: [transformRawImports()],
282-
});
283-
```
284-
285-
</app-ctc-block>
286-
287-
<!-- prettier-ignore-end -->
288-
289150
## Content as Data
290151
291152
If you are using any of Greenwood's Content as Data [Client APIs](/docs/content-as-data/data-client/), you'll want to configure Storybook to mock the HTTP calls Greenwood's data client makes, and provide the desired response needed based on the API being called.

0 commit comments

Comments
 (0)