Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type { import('@storybook/web-components-vite').StorybookConfig } */
// had to add @rollup/rollup-linux-x64-gnu as an optional dep
// https://github.com/vitejs/vite/discussions/15532
/** @type { import('@storybook/web-components-vite').StorybookConfig } */
const config = {
stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
addons: [
Expand Down
20 changes: 16 additions & 4 deletions src/pages/guides/ecosystem/storybook.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ In this example we are handling for CSS Module scripts:
id.endsWith(".css") &&
!id.endsWith(".module.css")
) {
// add .type so Constructable Stylesheets are not precessed by Vite's default pipeline
// append .type to the end of Constructable Stylesheet file paths so that they are not automatically precessed by Vite's default pipeline
return path.join(path.dirname(importer), `${id}.type`);
}
},
Expand Down Expand Up @@ -233,6 +233,7 @@ For example, if you're using Greenwood's [Raw Plugin](https://github.com/Project
```js
import { defineConfig } from "vite";
import fs from "fs/promises";
import path from 'path';
// 1) import the greenwood plugin and lifecycle helpers
import { greenwoodPluginImportRaw } from "@greenwood/plugin-import-raw";
import { readAndMergeConfig } from "@greenwood/cli/src/lifecycles/config.js";
Expand All @@ -248,14 +249,25 @@ For example, if you're using Greenwood's [Raw Plugin](https://github.com/Project

// 4) customize Vite
function transformRawImports() {
const hint = "?type=raw";

return {
name: "transform-raw-imports",
enforce: "pre",
resolveId: (id, importer) => {

if (
id.endsWith(hint)
) {
// append .type to the end of .css file paths so they are not automatically precessed by Vite's default CSS pipeline
return path.join(path.dirname(importer), `${id.slice(0, id.indexOf(hint))}.type${hint}`);
}
},
load: async (id) => {
if (id.endsWith("?type=raw")) {
const filename = id.slice(0, -9);
if (id.endsWith(hint)) {
const filename = id.slice(0, id.indexOf(`.type${hint}`));
const contents = await fs.readFile(filename, "utf-8");
const response = await rawResource.intercept(null, null, new Response(contents));
const response = await rawResource.intercept(new URL(`file://${filename}`), null, new Response(contents));
const body = await response.text();

return body;
Expand Down
22 changes: 17 additions & 5 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function transformConstructableStylesheetsPlugin() {
id.endsWith(".css") &&
!id.endsWith(".module.css")
) {
// add .type so Constructable Stylesheets are not precessed by Vite's default pipeline
// add .type so Constructable Stylesheets are not precessed by Vite's default pipeline
return path.join(path.dirname(importer), `${id}.type`);
}
},
Expand All @@ -39,7 +39,7 @@ function transformConstructableStylesheetsPlugin() {
const filename = id.slice(0, -5);
const contents = await fs.readFile(filename, "utf-8");
const url = new URL(`file://${id.replace(".type", "")}`);
// "coerce" native conststructable stylesheets into inline JS so Vite / Rollup do not complain
// "coerce" native constructable stylesheets into inline JS so Vite / Rollup do not complain
const request = new Request(url, {
headers: {
Accept: "text/javascript",
Expand All @@ -55,14 +55,26 @@ function transformConstructableStylesheetsPlugin() {
}

function transformRawImports() {
const hint = "?type=raw";

return {
name: "transform-raw-imports",
enforce: "pre",
resolveId: (id, importer) => {
if (id.endsWith(hint)) {
// append .type so .css file paths so they are not precessed by Vite's default CSS pipeline
return path.join(path.dirname(importer), `${id.slice(0, id.indexOf(hint))}.type${hint}`);
}
},
load: async (id) => {
if (id.endsWith("?type=raw")) {
const filename = id.slice(0, -9);
if (id.endsWith(hint)) {
const filename = id.slice(0, id.indexOf(`.type${hint}`));
const contents = await fs.readFile(filename, "utf-8");
const response = await rawResource.intercept(null, null, new Response(contents));
const response = await rawResource.intercept(
new URL(`file://${filename}`),
null,
new Response(contents),
);
const body = await response.text();

return body;
Expand Down