Skip to content

Commit

Permalink
Define constants in separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathonMA committed Jan 2, 2024
1 parent d227ae6 commit ba38934
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
27 changes: 27 additions & 0 deletions lib/dartsass/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Dartsass
module Runner
EXEC_PATH = "#{Pathname.new(__dir__).to_s}/../../exe/dartsass"
CSS_LOAD_PATH = Rails.root.join("app/assets/stylesheets")
CSS_BUILD_PATH = Rails.root.join("app/assets/builds")

module_function

def dartsass_build_mapping
Rails.application.config.dartsass.builds.map { |input, output|
"#{CSS_LOAD_PATH.join(input)}:#{CSS_BUILD_PATH.join(output)}"
}
end

def dartsass_build_options
Rails.application.config.dartsass.build_options.flat_map(&:split)
end

def dartsass_load_paths
[ CSS_LOAD_PATH ].concat(Rails.application.config.assets.paths).flat_map { |path| ["--load-path", path.to_s] }
end

def dartsass_compile_command
[ RbConfig.ruby, EXEC_PATH ].concat(dartsass_build_options).concat(dartsass_load_paths).concat(dartsass_build_mapping)
end
end
end
26 changes: 3 additions & 23 deletions lib/tasks/build.rake
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
EXEC_PATH = "#{Pathname.new(__dir__).to_s}/../../exe/dartsass"
CSS_LOAD_PATH = Rails.root.join("app/assets/stylesheets")
CSS_BUILD_PATH = Rails.root.join("app/assets/builds")

def dartsass_build_mapping
Rails.application.config.dartsass.builds.map { |input, output|
"#{CSS_LOAD_PATH.join(input)}:#{CSS_BUILD_PATH.join(output)}"
}
end

def dartsass_build_options
Rails.application.config.dartsass.build_options.flat_map(&:split)
end

def dartsass_load_paths
[ CSS_LOAD_PATH ].concat(Rails.application.config.assets.paths).flat_map { |path| ["--load-path", path.to_s] }
end

def dartsass_compile_command
[ RbConfig.ruby, EXEC_PATH ].concat(dartsass_build_options).concat(dartsass_load_paths).concat(dartsass_build_mapping)
end
require "dartsass/runner"

namespace :dartsass do
desc "Build your Dart Sass CSS"
task build: :environment do
system(*dartsass_compile_command, exception: true)
system(*Dartsass::Runner.dartsass_compile_command, exception: true)
end

desc "Watch and build your Dart Sass CSS on file changes"
task watch: :environment do
system(*dartsass_compile_command, "--watch", exception: true)
system(*Dartsass::Runner.dartsass_compile_command, "--watch", exception: true)
end
end

Expand Down

0 comments on commit ba38934

Please sign in to comment.