Skip to content

Commit

Permalink
Use configured astro vite plugins during build
Browse files Browse the repository at this point in the history
  • Loading branch information
Tate-CC committed Dec 21, 2023
1 parent 1a3d399 commit d5547a4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Unreleased

* Adds support for the `astro:content` and `astro:assets` modules inside Bookshop components.
* Astro Bookshop will now use your configured Vite plugins when building components.

## v3.8.2 (December 5, 2023)

Expand Down
36 changes: 33 additions & 3 deletions javascript-modules/engines/astro-engine/lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ export const buildPlugins = [

build.onResolve({ filter: /^astro:.*$/ }, async (args) => {
const type = args.path.replace("astro:", "");
if(type !== 'content' && type !== 'assets'){
console.error(`Error: The 'astro:${type}' module is not supported inside Bookshop components.`)
if (type !== "content" && type !== "assets") {
console.error(
`Error: The 'astro:${type}' module is not supported inside Bookshop components.`
);
throw new Error("Unsupported virtual module");
}
let dir = "";
if (typeof __dirname !== 'undefined') {
if (typeof __dirname !== "undefined") {
dir = __dirname;
} else {
dir = dirname(import.meta.url);
Expand Down Expand Up @@ -157,6 +159,34 @@ export const buildPlugins = [
return { path: args.importer, namespace: "style" };
}
);
build.onLoad({ filter: /.*/ }, async (args) => {
if (astroConfig.vite?.plugins) {
const text = await fs.promises.readFile(args.path, "utf8");
for (const plugin of astroConfig.vite.plugins) {
if (!plugin.transform) {
continue;
}

const result = await plugin.transform(
text,
args.path.replace(process.cwd(), "")
);

if (!result) {
continue;
}

if (typeof result !== "string" && !result.code) {
return;
}

return {
contents: typeof result === "string" ? result : result.code,
loader: "js",
};
}
}
});
},
},
];
Expand Down

0 comments on commit d5547a4

Please sign in to comment.