Sha256: 63852ae7f28061805c17f6a08bc34a6da2708afd778b8431c37319b92a5a9f6c

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 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

  ##
  # @return [Hash]
  #  Returns the default command-line options given
  #  to the webpack executable.
  def self.default_options
    @default_options ||= {}
  end

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

Version data entries

1 entries across 1 versions & 1 rubygems

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