Skip to content

Instantly share code, notes, and snippets.

@kofigumbs
Last active March 30, 2019 19:58
Show Gist options
  • Save kofigumbs/c78a77aa298cd5f713ed14d354101bf8 to your computer and use it in GitHub Desktop.
Save kofigumbs/c78a77aa298cd5f713ed14d354101bf8 to your computer and use it in GitHub Desktop.
Compile Elm files and include the JavaScript output in a Jekyll site
# Initially based on https://github.com/sonnym/jekyll_elm
#
# USAGE:
# Copy/paste this file into the `_plugins` directory of your Jekyll project.
# For every .elm file in your project, it will generate a .js file in your site
# using the same name and relative path.
#
# As-is, the converter expects the following directory structure:
#
# your-jekyll-project/
# ⌞ _plugins/
# ⌞ elm_converter.rb
# ⌞ elm.json
require "jekyll"
require "tempfile"
class ElmConverter < Jekyll::Converter
MODE = Jekyll.env == "development" ? "debug" : "optimize"
safe false
def matches(ext) ext == ".elm" end
def output_ext(ext) ".js" end
def convert(content)
Tempfile.open [ "source", ".elm" ] do |source|
Tempfile.open [ "output", ".js" ] do |output|
File.write source.path, content
raise unless system "cd #{@config["source"]} && \
npx elm make #{source.path} --#{MODE} --output #{output.path}"
output.read
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment