diff --git a/lib/tasks/webpacker/compile.rake b/lib/tasks/webpacker/compile.rake index 9001064f5..ac02ba900 100644 --- a/lib/tasks/webpacker/compile.rake +++ b/lib/tasks/webpacker/compile.rake @@ -8,7 +8,7 @@ namespace :webpacker do puts "Compiling webpacker assets 🎉" asset_host = Rails.application.config.action_controller.asset_host asset_env = asset_host ? "ASSET_HOST=#{asset_host}" : "" - result = `#{asset_env} NODE_ENV=#{Webpacker::Env.current} ./bin/webpack --json` + result = `#{asset_env} NODE_ENV=#{Webpacker.env} ./bin/webpack --json` unless $?.success? puts JSON.parse(result)["errors"] @@ -18,17 +18,6 @@ namespace :webpacker do puts "Compiled digests for all packs in #{Webpacker::Configuration.packs_path}: " puts JSON.parse(File.read(Webpacker::Configuration.manifest_path)) end - - desc "Compile javascript packs using webpack for test with digests" - task compile_before_test: ["webpacker:compile"] do - Webpacker::Manifest.load(Webpacker::Manifest.file_path) - end -end - -# Compile packs prior to system and controller tests running -if Rake::Task.task_defined?("test:system") - Rake::Task["test:system"].enhance(["webpacker:compile_before_test"]) - Rake::Task["test:controllers"].enhance(["webpacker:compile_before_test"]) end # Compile packs after we've compiled all other assets during precompilation diff --git a/lib/webpacker.rb b/lib/webpacker.rb index 2e43bdc37..2a2791634 100644 --- a/lib/webpacker.rb +++ b/lib/webpacker.rb @@ -1,9 +1,24 @@ module Webpacker - def self.bootstrap + extend self + + def bootstrap Webpacker::Env.load Webpacker::Configuration.load Webpacker::Manifest.load end + + def compile + Webpacker::Compiler.compile + Webpacker::Manifest.load + end + + def env + Webpacker::Env.current.inquiry + end end +require "webpacker/env" +require "webpacker/configuration" +require "webpacker/manifest" +require "webpacker/compiler" require "webpacker/railtie" if defined?(Rails) diff --git a/lib/webpacker/compiler.rb b/lib/webpacker/compiler.rb new file mode 100644 index 000000000..e16a9a9b5 --- /dev/null +++ b/lib/webpacker/compiler.rb @@ -0,0 +1,20 @@ +require "rake" + +module Webpacker::Compiler + extend self + + def compile + compile_task.invoke + compile_task.reenable + end + + private + def compile_task + @compile_task ||= load_rake_task("webpacker:compile") + end + + def load_rake_task(name) + @load_rakefile ||= Rake.load_rakefile(Rails.root.join("Rakefile")) + Rake::Task[name] + end +end diff --git a/lib/webpacker/configuration.rb b/lib/webpacker/configuration.rb index 67282c3cc..e50ff82da 100644 --- a/lib/webpacker/configuration.rb +++ b/lib/webpacker/configuration.rb @@ -1,6 +1,6 @@ # Loads webpacker configuration from config/webpack/paths.yml + require "webpacker/file_loader" -require "webpacker/env" class Webpacker::Configuration < Webpacker::FileLoader class << self @@ -25,7 +25,7 @@ def packs_path end def paths - load if Webpacker::Env.development? + load if Webpacker.env.development? raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Configuration.load must be called first") unless instance instance.data end @@ -46,6 +46,6 @@ def source_path private def load return super unless File.exist?(@path) - HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Webpacker::Env.current]) + HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Webpacker.env]) end end diff --git a/lib/webpacker/env.rb b/lib/webpacker/env.rb index e4c3d3b82..147def958 100644 --- a/lib/webpacker/env.rb +++ b/lib/webpacker/env.rb @@ -8,10 +8,6 @@ def current instance.data end - def development? - current == "development" - end - def file_path Rails.root.join("config", "webpack", "paths.yml") end diff --git a/lib/webpacker/helper.rb b/lib/webpacker/helper.rb index 9660c7d70..dd62a39bb 100644 --- a/lib/webpacker/helper.rb +++ b/lib/webpacker/helper.rb @@ -1,5 +1,3 @@ -require "webpacker/manifest" - module Webpacker::Helper # Computes the full path for a given webpacker asset. # Return relative path using manifest.json and passes it to asset_url helper diff --git a/lib/webpacker/manifest.rb b/lib/webpacker/manifest.rb index 5b8575ce8..0c0dc6dcc 100644 --- a/lib/webpacker/manifest.rb +++ b/lib/webpacker/manifest.rb @@ -6,8 +6,6 @@ # "/packs/calendar-1016838bab065ae1e314.css" for long-term caching require "webpacker/file_loader" -require "webpacker/env" -require "webpacker/configuration" class Webpacker::Manifest < Webpacker::FileLoader class << self @@ -16,14 +14,33 @@ def file_path end def lookup(name) - load if Webpacker::Env.development? - raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Manifest.load must be called first") unless instance - instance.data[name.to_s] || raise(Webpacker::FileLoader::NotFoundError.new("Can't find #{name} in #{file_path}. Is webpack still compiling?")) + load if Webpacker.env.development? + + if Webpacker.env.test? + find(name) || compile_and_find!(name) + else + find!(name) + end end def lookup_path(name) Rails.root.join(File.join(Webpacker::Configuration.output_path, lookup(name))) end + + private + def find(name) + instance.data[name.to_s] if instance + end + + def find!(name) + raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Manifest.load must be called first") unless instance + instance.data[name.to_s] || raise(Webpacker::FileLoader::NotFoundError.new("Can't find #{name} in #{file_path}. Is webpack still compiling?")) + end + + def compile_and_find!(name) + Webpacker.compile + find!(name) + end end private diff --git a/test/env_test.rb b/test/env_test.rb index 4aef8f0f6..abc102f44 100644 --- a/test/env_test.rb +++ b/test/env_test.rb @@ -3,10 +3,8 @@ class EnvTest < Minitest::Test def test_current_env assert_equal Webpacker::Env.current, "production" - end - - def test_env_is_development? - refute_predicate Webpacker::Env, :development? + assert_equal Webpacker.env, "production" + assert Webpacker.env.production? end def test_file_path