filters/: add nanoc-webpack.rb

Replace lib/filters/webpack.rb with
[nanoc-webpack.rb](https://github.com/0x1eef/nanoc-webpack.rb)
This commit is contained in:
0x1eef 2022-11-18 22:24:57 -03:00 committed by Robert
parent 889651cee4
commit 816ea05288
4 changed files with 9 additions and 39 deletions

View file

@ -4,6 +4,7 @@ source "https://rubygems.org"
gem "nanoc", "~> 4.12"
gem "nanoc-live", "~> 1.0"
gem "nanoc-gunzip.rb", "~> 0.1"
gem "nanoc-webpack.rb", "~> 0.1", git: "https://github.com/0x1eef/nanoc-webpack.rb"
gem "rainpress", "~> 1.0"
gem "sass"
gem "standardrb"

View file

@ -1,3 +1,9 @@
GIT
remote: https://github.com/0x1eef/nanoc-webpack.rb
revision: 8d821be4f7d45abaaf79aa9d7656590d158f6d3f
specs:
nanoc-webpack.rb (0.1.0)
GEM
remote: https://rubygems.org/
specs:
@ -141,6 +147,7 @@ DEPENDENCIES
nanoc (~> 4.12)
nanoc-gunzip.rb (~> 0.1)
nanoc-live (~> 1.0)
nanoc-webpack.rb (~> 0.1)!
paint
puma
rainpress (~> 1.0)

1
Rules
View file

@ -2,6 +2,7 @@
# frozen_string_literal: true
require "ryo"
require "nanoc-gunzip"
require "nanoc-webpack"
LOCALES = %w[ar en]
SURAH_ID_TO_SLUG = Ryo.from(

View file

@ -1,39 +0,0 @@
# frozen_string_literal: true
##
# Compiles a JavaScript / TypeScript asset with webpack.
class Nanoc::Filter::Webpack < Nanoc::Filter
include FileUtils
identifier :webpack
type :text
always_outdated
def run(content, options = {})
webpack(temp_file(content))
end
private
def webpack(file)
system "node",
"./node_modules/webpack/bin/webpack.js",
"--entry", File.join("./src", item.identifier.to_s),
"--output-path", File.dirname(file.path),
"--output-filename", File.basename(file.path)
if $?.success?
File.read(file.path).tap { file.tap(&:unlink).close }
else
file.tap(&:unlink).close
exit!($?.exitstatus)
end
end
def temp_file(content)
dir = File.join(Dir.getwd, "tmp", "webpack")
mkdir_p(dir) unless Dir.exist?(dir)
file = Tempfile.new(File.basename(item.identifier.to_s), dir)
file.write(content)
file.tap(&:flush)
end
end