Skip to content
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

javascript module support #1128

Open
rubydesign opened this issue Feb 4, 2023 · 1 comment
Open

javascript module support #1128

rubydesign opened this issue Feb 4, 2023 · 1 comment

Comments

@rubydesign
Copy link

As a long time haml user i really like how haml makes the html very precise

Now javascript modules are supported by browsers, and rails 7 has the importmap support.
So we can drop packers, web or otherwise.
But JS module code and non module code don't mix well, and currently there is no support for writing module js in haml.

I read the issues and it seems a similar request was made 5 years ago, but i think things have changed to revisit the topic.
Then it was said modules is a fringe thing, that's not true anymore
And it was said that one can just use a script tag And while this is true, we would not need a the whole javascript filter if it were the whole story.

So, reading the code, i don't really see a way to get options into a filter on a case by case basis. In a way i think this would be nicest, if one could do :javascript{module: true}
But if that is too disruptive, i would suggest a :module filter, that is very similar to javascript filter, just adds the module

I can make a pr if the feature would be accepted. Probably will write the filter just for myself anyway.

@MyklClason
Copy link
Contributor

MyklClason commented Oct 21, 2023

Need this myself and ended up quickly write up an initializer for it. Wasn't sure how to handle xhtml and didn't need it so left it as it was in the code. I was able to find the logic for adding filters in the codebase and just used that. I ended up just being a bit lazy and used javascriptm as the name. Not sure if :module would cause conflicts or something. Especially if I used "Module" as the actual filename, but seems that wasn't actually an issue once I saw how it was registered, but left it as is for now.

If someone wants to add tests and do a PR for this, go ahead.

# config/initializers/haml_js_modules.rb
# Add support for <script type="module"></script>
module Haml
  class Filters
    class JavascriptModule < Javascript
      def compile(node)
        case @format
        when :xhtml
          compile_xhtml(node)
        else
          compile_html(node)
        end
      end

      private

      def compile_html(node)
        temple = [:multi]
        temple << [:static, "<script type='module'>\n"]
        compile_text!(temple, node, '  ')
        temple << [:static, "\n</script>"]
        temple
      end

      def compile_xhtml(node)
        temple = [:multi]
        temple << [:static, "<script type='text/javascript'>\n  //<![CDATA[\n"]
        compile_text!(temple, node, '    ')
        temple << [:static, "\n  //]]>\n</script>"]
        temple
      end
    end
  end
end

module Haml
  class Filters
    register :javascriptm,       JavascriptModule
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants