-
Hi, We generate a PDF and therefore we need the CSS styles inline. Webpacker provided a // example file, this gets returned
@import "@styles/base";
@import "@styles/grid"; How can I get the compiled scss file? And how can I enforce a manifest file development (without a build)? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi there! Vite also provides a manifest, which is implicitly read from when using helpers such as If you need to inline styles, you could do so without having to build your files, by using a helper to detect if the Vite dev server is running and simply fetching the file in CSS format (it will be precompiled on the fly), or reading the file locally when the build has already run: def vite_inline_style(path)
path = ViteRuby.instance.manifest.path_for(path)
css = if ViteRuby.instance.dev_server_running?
uri = URI("#{ViteRuby.config.origin}#{path}")
Net::HTTP.get(uri, {"Accept" => "text/css"})
else
File.read(Rails.root.join("public#{path}"))
end
"<style>#{css}</style>".html_safe
end This makes iterating on PDF styles very fast, as you wouldn't need to wait for rebuilds after tweaking them. An even better approach, is to have a development-only endpoint that serves the HTML for the PDF, in which case you can visit the page directly and get HMR for styles, example: <% if local_assigns[:html_test] %>
<%= vite_client_tag %>
<%= vite_stylesheet_tag 'print.scss', media: 'all' %>
<% else %>
<%= vite_inline_style 'print.scss' %>
<% end %> |
Beta Was this translation helpful? Give feedback.
Hi there!
Vite also provides a manifest, which is implicitly read from when using helpers such as
vite_stylesheet_tag
when the dev server is not running or in production.If you need to inline styles, you could do so without having to build your files, by using a helper to detect if the Vite dev server is running and simply fetching the file in CSS format (it will be precompiled on the fly), or reading the file locally when the build has already run: