-
I've been struggling to use Vite to inline the javascripts/stylesheets, especially for:
For all those purposes it is desired to inline the full Javascript/Stylesheet source. We've dabbled around with:
This seems to work for the stylesheets so far, but the javascript is difficult to inline, because of ESM, wrapping it with a script produces Has any of you any experience successfully inlining the Javascript? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Hi Stefan! @zealot128 Regarding the styles for mailers, using I assume inlining JS is only relevant for the third use case:
Could you elaborate on why is it desirable to inline the JS? The following error suggests that you are combining separate ESM files into a single one:
Combining modules into a single file is not a trivial task and requires full analysis, a task that is perfect for Rollup (Vite). Alternatively, you could use one of these hacks to inline each file separately. I'd recommend experimenting by either:
You can toggle between a normal build and an iife build by using environment variables within your |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Finishing up this question; In the end we decided that we don't need Vite-processed JS in our PDF's. That usually only amounts to very minuscule amount, like Page Numbers and stuff, so it's better to keep inlined or in a separate js file to embed, irrespective of which PDF generator we use (Wicked, Grover, Chrome etc.). But using CSS is not very difficult; the best way, is just inline everything into the document, like with a little helper (used/tested on Vite4/ViteR 3.3): module ApplicationHelper
def vite_pdf_stylesheet_link_tag(vite_asset)
if ViteRuby.instance.manifest.dev_server_running?
raise NotImplementedError, "vite_pdf_stylesheet_link_tag is not supported in development mode"
end
entry = vite_manifest.resolve_entries(vite_asset)
css_files =
if vite_asset[/css|scss/]
entry[:scripts]
else
entry[:stylesheets]
end
safe_join(
css_files.map do |css_file|
output_path = "./#{ViteRuby.instance.config.public_dir}/#{css_file}"
tag.style File.read(output_path)
end
)
end
end if you pass it a entrypoint scss/css file:
It will inline the file,
It will collect all the sidecard css files that the entrypoints imports - Directly stolen from Vite's own vite_javasvript_link_tag :) |
Beta Was this translation helpful? Give feedback.
Hi Stefan! @zealot128
Regarding the styles for mailers, using
premailer-rails
might also be a good option.I assume inlining JS is only relevant for the third use case:
Could you elaborate on why is it desirable to inline the JS?
The following error suggests that you are combining separate ESM files into a single one:
Combining modules into a single file is not a trivial task and requires full analysis, a task that is perfect for Rollup (Vite). Alternatively, you could use one of these hacks to inline each file separately.
I'…