-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vite 4.0 support #192
Comments
As far as I can tell, the only change would be updating the peer dep range. A PR would be welcome 🙂 |
I think |
released in v2.2.0 |
I'm not so sure this is fixed. Production builds still fail, I think because Vite does not expose Rollup's
For what it's worth, I had started a PR to fix this but ran into issues related to missing test files and such that are not committed to version control. I settled for using patch diffdiff --git a/node_modules/vite-plugin-inline-source/dist/index.js b/node_modules/vite-plugin-inline-source/dist/index.js
index 3dafedb..dc210cf 100644
--- a/node_modules/vite-plugin-inline-source/dist/index.js
+++ b/node_modules/vite-plugin-inline-source/dist/index.js
@@ -21,7 +21,7 @@ const InlineSourceOptionsSchema = zod_1.default
})
.default({});
const PATTERN = /<([A-z0-9-]+)\s+([^>]*?)src\s*=\s*"([^>]*?)"([^>]*?)\s*((\/>)|(>\s*<\/\s*\1\s*>))/gi;
-const getTransformFunction = (options) => async (source, ctx, id) => {
+const getTransformFunction = (options) => async (source, ctx, id, root) => {
if (id && !id.endsWith(".html")) {
return source;
}
@@ -37,11 +37,11 @@ const getTransformFunction = (options) => async (source, ctx, id) => {
if ((isImg && !isSvgFile) || !shouldInline) {
continue;
}
- let fileContent = ctx.server
- ? (await (0, promises_1.readFile)(`${ctx.server.config.root}/${fileName}`)).toString()
- : // @ts-expect-error don't know these types aren't right
- (await ctx.load({ id: `${fileName}?raw` })).ast?.body?.[0].declaration
- .value;
+
+ const projectRoot = root ?? ctx.server.config.root ?? "";
+ const filePath = node_path_1.default.join(projectRoot, fileName);
+
+ let fileContent = (await (0, promises_1.readFile)(filePath)).toString();
if (isSvgFile && options.optimizeSvgs) {
fileContent = (0, svgo_1.optimize)(fileContent, options.svgoOptions).data;
}
@@ -64,13 +64,18 @@ const getTransformFunction = (options) => async (source, ctx, id) => {
};
function VitePluginInlineSource(opts) {
const transformHtml = getTransformFunction(InlineSourceOptionsSchema.parse(opts));
+ let root;
+
return {
name: "vite-plugin-inline-source",
+ configResolved(ctx) {
+ root = ctx.root;
+ },
transform(source, id) {
return transformHtml(source, this, id);
},
- transformIndexHtml(source, ctx) {
- return transformHtml(source, ctx);
+ async transformIndexHtml(source, ctx) {
+ return transformHtml(source, ctx, undefined, root);
},
};
} |
Curious if you have a reproduction? I'm using it just fine with Vite 4 in a couple projects with no issues accessing Rollup's I'm also curious as to what errors you're getting with the tests.
works fine for me on Mac. Produces the following output:
|
Though I do see vitejs/vite#11469, which would imply that it shouldn't be accessible from the plugin context 🤔 |
I'm trying to replicate my exact error on Stackblitz. I haven't quite gotten there, but was able to at least demonstrate what seems to be an issue with resolving files when there's a custom https://stackblitz.com/edit/vite-ysw8ui?file=vite.config.js Dev seems to work, but a production build will fail. |
custom roots should work with v2.2.2 |
@bienzaaron unfortunately, v2.2.2 doesn't resolve this for me and still requires my patch mentioned here that removes the use of I had started working on a PR to incorporate it, but since I'm not familiar with Vite's asset pipeline I struggled with the tests. Either the tests generate files in a different way than the normal Vite processes or my patch is only a partial solution (though it works in both dev & production for me).
|
I've installed this to use it with Vite 4.0 but doing so requires using npm's
--legacy-peer-deps
flag to bypass errors. I'm not familiar with plugin interface changes between Vite 3 and 4, so I don't know if it's as simple as updating the peer dep range. However, it seems to be working in my testing so far.vite-plugin-inline-source/package.json
Lines 55 to 57 in 9a4f432
I'd be willing to open a PR for this change.
The text was updated successfully, but these errors were encountered: