Sha256: a08ef73ba654353038106eb01ddd0cd9951c3527ed2cb1d0e4c692b0769a053f

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

##
# Compiles a textual nanoc item with webpack.
class Nanoc::Webpack::Filter < Nanoc::Filter
  require_relative "filter/dependable"
  Error = Class.new(RuntimeError)
  include FileUtils
  include Dependable

  identifier :webpack
  type :text

  ##
  # @example
  #   Nanoc::Webpack.default_options.merge!(
  #     "--cache-type" => "memory"
  #   )
  #
  # @return [Hash]
  #  Returns the default command-line options given to webpack.
  def self.default_options
    @default_options ||= {"--cache-type" => "filesystem"}
  end

  def run(content, options = {})
    args = options[:args] || options["args"] || {}
    depend_on dependable(paths: options[:depend_on], reject: options[:reject])
              .map { items[_1] }
    webpack temporary_file_for(content),
            args: self.class.default_options.merge(args)
  end

  private

  def webpack(file, args: {})
    sh "node",
       "./node_modules/webpack/bin/webpack.js",
       "--entry", File.join(Dir.getwd, item.attributes[:content_filename]),
       "--output-path", File.dirname(file.path),
       "--output-filename", File.basename(file.path),
       *webpack_args(args)
    if $?.success?
      File.read(file.path).tap { file.tap(&:unlink).close }
    else
      rm_f Dir.glob(File.join(File.dirname(file.path), "*"))
      file.close
      raise Error, "webpack.js exited unsuccessfully (exit code: #{$?.exitstatus})", []
    end
  end

  def webpack_args(args)
    args.each_with_object([]) do |(key, value), ary|
      if value.equal?(true)
        ary << key
      else
        ary.concat [key, value.to_s]
      end
    end
  end

  def temporary_file_for(content)
    dir = File.join(Dir.getwd, "tmp", "nanoc-webpack.rb")
    mkdir_p(dir) unless Dir.exist?(dir)
    file = Tempfile.new(File.basename(@item.identifier.to_s), dir)
    file.write(content)
    file.tap(&:flush)
  end

  def sh(*args)
    print "webpack: ", args.join(" "), "\n"
    system(*args)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nanoc-webpack.rb-0.5.6 lib/nanoc/webpack/filter.rb