Skip to content

Commit

Permalink
Merge pull request #47 from tony-sull/fix/blok-name-resolution
Browse files Browse the repository at this point in the history
fix: supports blok names that aren't valid JS variable names
  • Loading branch information
manuelschroederdev authored Oct 24, 2022
2 parents 515d150 + 469bac5 commit 46eb72a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
19 changes: 18 additions & 1 deletion lib/StoryblokComponent.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
---
import components from "virtual:storyblok-components";
import camelcase from "camelcase";
const { blok, ...props } = Astro.props;
const Component = components[blok.component];
/**
* convert blok components to camel case to match vite-plugin-storyblok
*/
const key = camelcase(blok.component);
if (!(key in components)) {
/**
* Component not found! Log an error with details on the blok name that could not be found
* TODO: Add support for a placeholder component as a fallback?
*/
throw new Error(
`Component could not be found for blok "${blok.component}"! Is it defined in astro.config.mjs?`
);
}
const Component = components[key];
---

<Component blok={blok} {...props} />
5 changes: 3 additions & 2 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"prepublishOnly": "npm run build && cp ../README.md ./"
},
"dependencies": {
"@storyblok/js": "^1.8.5"
"@storyblok/js": "^1.8.5",
"camelcase": "^7.0.0"
},
"devDependencies": {
"@cypress/vite-dev-server": "^2.0.7",
Expand Down Expand Up @@ -59,4 +60,4 @@
"publishConfig": {
"access": "public"
}
}
}
14 changes: 10 additions & 4 deletions lib/vite-plugin-storyblok.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Custom Vite plugin by Tony Sull (https://github.com/tony-sull)
*/
import camelcase from "camelcase";

export function vitePluginStoryblok(components) {
const virtualModuleId = "virtual:storyblok-components";
const resolvedVirtualModuleId = "\0" + virtualModuleId;
Expand All @@ -17,11 +19,15 @@ export function vitePluginStoryblok(components) {
const imports = [];
for await (const [key, value] of Object.entries(components)) {
const { id } = await this.resolve("/src/" + value + ".astro");
imports.push(`import ${key} from "${id}"`);
/**
* convert blok names to camel case for valid import names
* StoryblokComponent.astro needs to do the same when resolving components!
*/
imports.push(`import ${camelcase(key)} from "${id}"`);
}
return `${imports.join(";")};export default {${Object.keys(
components
).join(",")}}`;
return `${imports.join(";")};export default {${Object.keys(components)
.map((key) => camelcase(key))
.join(",")}}`;
}
},
};
Expand Down
22 changes: 20 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46eb72a

Please sign in to comment.