Skip to content

Commit b233f06

Browse files
update storybook and CSS related greenwood plugins docs example refinement
1 parent 74ca790 commit b233f06

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

.storybook/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/** @type { import('@storybook/web-components-vite').StorybookConfig } */
21
// had to add @rollup/rollup-linux-x64-gnu as an optional dep
32
// https://github.com/vitejs/vite/discussions/15532
3+
/** @type { import('@storybook/web-components-vite').StorybookConfig } */
44
const config = {
55
stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
66
addons: [

src/pages/guides/ecosystem/storybook.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ For example, if you're using Greenwood's [Raw Plugin](https://github.com/Project
233233
```js
234234
import { defineConfig } from "vite";
235235
import fs from "fs/promises";
236+
import path from 'path';
236237
// 1) import the greenwood plugin and lifecycle helpers
237238
import { greenwoodPluginImportRaw } from "@greenwood/plugin-import-raw";
238239
import { readAndMergeConfig } from "@greenwood/cli/src/lifecycles/config.js";
@@ -248,14 +249,25 @@ For example, if you're using Greenwood's [Raw Plugin](https://github.com/Project
248249
249250
// 4) customize Vite
250251
function transformRawImports() {
252+
const hint = "?type=raw";
253+
251254
return {
252255
name: "transform-raw-imports",
253256
enforce: "pre",
257+
resolveId: (id, importer) => {
258+
259+
if (
260+
id.endsWith(hint)
261+
) {
262+
// append .type so .css file paths so they are not precessed by Vite's default CSS pipeline
263+
return path.join(path.dirname(importer), `${id.slice(0, id.indexOf(hint))}.type${hint}`);
264+
}
265+
},
254266
load: async (id) => {
255-
if (id.endsWith("?type=raw")) {
256-
const filename = id.slice(0, -9);
267+
if (id.endsWith(hint)) {
268+
const filename = id.slice(0, id.indexOf(`.type${hint}`));
257269
const contents = await fs.readFile(filename, "utf-8");
258-
const response = await rawResource.intercept(null, null, new Response(contents));
270+
const response = await rawResource.intercept(new URL(`file://${filename}`), null, new Response(contents));
259271
const body = await response.text();
260272
261273
return body;
@@ -264,6 +276,7 @@ For example, if you're using Greenwood's [Raw Plugin](https://github.com/Project
264276
};
265277
}
266278
279+
267280
export default defineConfig({
268281
// 5) add it the plugins option
269282
plugins: [transformRawImports()],

vite.config.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function transformConstructableStylesheetsPlugin() {
3030
id.endsWith(".css") &&
3131
!id.endsWith(".module.css")
3232
) {
33-
// add .type so Constructable Stylesheets are not precessed by Vite's default pipeline
33+
// add .type so Constructable Stylesheets are not precessed by Vite's default pipeline
3434
return path.join(path.dirname(importer), `${id}.type`);
3535
}
3636
},
@@ -39,7 +39,7 @@ function transformConstructableStylesheetsPlugin() {
3939
const filename = id.slice(0, -5);
4040
const contents = await fs.readFile(filename, "utf-8");
4141
const url = new URL(`file://${id.replace(".type", "")}`);
42-
// "coerce" native conststructable stylesheets into inline JS so Vite / Rollup do not complain
42+
// "coerce" native constructable stylesheets into inline JS so Vite / Rollup do not complain
4343
const request = new Request(url, {
4444
headers: {
4545
Accept: "text/javascript",
@@ -55,14 +55,26 @@ function transformConstructableStylesheetsPlugin() {
5555
}
5656

5757
function transformRawImports() {
58+
const hint = "?type=raw";
59+
5860
return {
5961
name: "transform-raw-imports",
6062
enforce: "pre",
63+
resolveId: (id, importer) => {
64+
if (id.endsWith(hint)) {
65+
// append .type so .css file paths so they are not precessed by Vite's default CSS pipeline
66+
return path.join(path.dirname(importer), `${id.slice(0, id.indexOf(hint))}.type${hint}`);
67+
}
68+
},
6169
load: async (id) => {
62-
if (id.endsWith("?type=raw")) {
63-
const filename = id.slice(0, -9);
70+
if (id.endsWith(hint)) {
71+
const filename = id.slice(0, id.indexOf(`.type${hint}`));
6472
const contents = await fs.readFile(filename, "utf-8");
65-
const response = await rawResource.intercept(null, null, new Response(contents));
73+
const response = await rawResource.intercept(
74+
new URL(`file://${filename}`),
75+
null,
76+
new Response(contents),
77+
);
6678
const body = await response.text();
6779

6880
return body;

0 commit comments

Comments
 (0)