Skip to content

Instantly share code, notes, and snippets.

@0x1eef
Created June 21, 2022 13:59
Show Gist options
  • Save 0x1eef/5b975147fac5d23e7acffa00baa61e36 to your computer and use it in GitHub Desktop.
Save 0x1eef/5b975147fac5d23e7acffa00baa61e36 to your computer and use it in GitHub Desktop.
class WebpackFilter < Nanoc::Filter
require 'tempfile'
require 'fileutils'
identifier :webpack
type :text
def run(text, options = {})
tmp_file = Tempfile.new('webpack', item_dir)
##
# Run webpack.
run_webpack File.join(content_dir, item.path), output_dir
##
# Copy WebAssembly files into place.
Dir[File.join(item_dir, "*.wasm")].each do |wasm_file|
FileUtils.mkdir_p(output_dir)
FileUtils.mv(wasm_file, output_dir)
end
##
# Read transpiled contents, and pass it back to nanoc.
File.read File.join(content_dir, item.path)
ensure
tmp_file.unlink
tmp_file.close
end
private
def run_webpack(src, dest)
system "webpack", "--entry", "./#{src}", "--output-path", dest
raise unless $?.success?
end
def output_dir
File.join config.output_dir, File.dirname(item.path)
end
def content_dir
source = config[:data_sources].find { _1[:type] == "filesystem" }
source[:content_dir]
end
def item_dir
File.join(content_dir, File.dirname(item.path))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment