-
Notifications
You must be signed in to change notification settings - Fork 122
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
Force skipping vite dev server #93
Conversation
def lookup(name, type: nil) | ||
@build_mutex.synchronize { builder.build } if should_build? | ||
def lookup(name, type: nil, force_build: false) | ||
@build_mutex.synchronize { builder.build } if force_build || should_build? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong, we don't want to force build in production for example.
The correct way should be something like config.auto_build && (!dev_server_running? || force_build)
Hi Adrien! On my phone right now, I'll get back to this during the week. At a first glance it seems that premailer supports fetching css from a URI, so perhaps it's a matter of configuring the resolution so that it doesn't assume it's a local file. That would have the added benefit of being extremely fast during development, as Vite would only process the requested stylesheets instead of triggering a full build. |
In my case I am using SCSS with tools like foundation for emails so I do
require a build and not just forwarding the file
On June 20, 2021, GitHub ***@***.***> wrote:
Hi Adrien!
On my phone right now, I'll get back to this during the week.
At a first glance it seems that premailer supports fetching css from a
URI
<https://github.com/premailer/premailer/blob/master/lib/premailer/premailer.rb#L324>,
so perhaps it's a matter of configuring the resolution so that it
doesn't assume it's a local file.
That would have the added benefit of being extremely fast during
development, as Vite would only process the requested stylesheets
instead of triggering a full build.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#93 (comment)>,
or unsubscribe <https://github.com/notifications/unsubscribe-
auth/AAGEHNLNMD5KXOISR6EMXGTTTYF5JANCNFSM47AD4C7Q>.
|
If you can share a minimal repo with the intended usage I can take a look. Vite does not serve the file as-is—it would run it through sass and process any imports within the file, so premailer should be able to parse it correctly. |
I understand what you mean. I think the main issue is that the file premailer reads contains import statements. While the browser can resolve those premailer expects a complete single css file. Is that clearer? Otherwise I will provide with a repro |
Anyway here's a repro :) https://github.com/Intrepidd/vite_ruby_issue Steps are described in the readme, let me know if I can help |
Thanks for sharing a sample repo. As I mentioned before, it's possible to request the processed CSS from Vite. This is insanely faster than building, and it seems to work nicely in the example repo 😃 @Intrepidd Please see the following options, and let me know how it goes! The default The problem is that no I opened a PR in In the meantime, you could define a custom strategy for Premailer, for example, in if Rails.env.development?
module ViteNetworkLoader
def self.load(url)
return unless ViteRuby.instance.dev_server_running?
return unless url.start_with?("/#{ViteRuby.config.public_output_dir}/")
Net::HTTP.get(URI("http://localhost:1111#{url}"), { 'Accept' => 'text/css' }).presence
end
end
Premailer::Rails.config.fetch(:strategies) << ViteNetworkLoader
end Patch for your example appdiff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb
index 76ce8b3..d654ce3 100644
--- a/app/views/layouts/mailer.html.erb
+++ b/app/views/layouts/mailer.html.erb
@@ -2,7 +2,7 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <%= vite_stylesheet_tag 'application.scss', host: "http://localhost:1111" %>
+ <%= vite_stylesheet_tag 'application.scss' %>
</head>
<body>
diff --git a/config/initializers/premailer.rb b/config/initializers/premailer.rb
new file mode 100644
index 0000000..18c51c2
--- /dev/null
+++ b/config/initializers/premailer.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+if Rails.env.development?
+ module ViteNetworkLoader
+ options = Rails.application.config.action_mailer.default_url_options
+ HOST_WITH_PORT = "http://#{options.fetch(:host)}:#{options.fetch(:port)}"
+
+ def self.load(url)
+ return unless ViteRuby.instance.dev_server_running?
+ return unless url.start_with?("/#{ViteRuby.config.public_output_dir}/")
+ Net::HTTP.get(URI("#{HOST_WITH_PORT}#{url}"), { 'Accept' => 'text/css' }).presence
+ end
+ end
+
+ Premailer::Rails.config.fetch(:strategies) << ViteNetworkLoader
+end |
This allows development servers to serve the correct file type. For more information see ElMassimo/vite_ruby#93 (comment)
Thanks! I had no idea you could do that with the accept header! I was confused because from my browser or curl I would see the import statements. Sorry for the confusion and thanks for your help! |
Hi ! Thanks for the great work with vite_ruby ⚡
I was looking into integrating it into my app but got blocked because of premailer
Premailer needs to read and parse a CSS file to be able to inline it properly. Obviously it doesn't work when the dev server is enabled as it expects a classic css file and not ESM.
This PR is a total draft / POC, it aims at being able to skip the vite server, and grab a compiled file when we want to, basically force the on demand behaviour even when the dev server is running.
The wording is very bad,
force_build
isn't correct and should be changed if this goes further than this draft.I just wanted to get your opinion on this, do you see this as something that could make its way into the codebase (if re-done properly obviously), are there any gotchas I didn't see ?
Here is what I had to do :
Thanks !