Created
August 6, 2019 13:59
-
-
Save wojtha/c83ab4ddd1ddb01aed24ce37ddb686fd to your computer and use it in GitHub Desktop.
Webpack build before the test suite is executed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://gist.github.com/naps62/a7dcce679a45592714ea6477108f0419 | |
# See https://github.com/rails/webpacker/issues/59 | |
# See https://raw.githubusercontent.com/codeforamerica/open311status/4fe31fec5e70e55c313616d0a1e11f7f7f460528/spec/support/webpack.rb | |
module WebpackTestBuild | |
TS_FILE = Rails.root.join('tmp', 'webpack-spec-timestamp') | |
PACK_MANIFEST = Rails.root.join('public', 'packs', 'manifest.json') | |
JAVASCRIPT_FILES = Rails.root.join('app', 'javascript', '**', '*') | |
class << self | |
attr_accessor :already_built | |
end | |
def self.run_webpack | |
unless Webpacker.compile | |
raise "Error: webpacker failed to build. Run 'bin/webpack', fix any issues, and try again." | |
end | |
self.already_built = true | |
File.open(TS_FILE, 'w') { |f| f.write(Time.now.utc.to_i) } | |
end | |
def self.run_webpack_if_empty_pack | |
unless File.exists?(PACK_MANIFEST) | |
$stdout.puts 'Webpack: empty pack, compiling...' | |
run_webpack | |
end | |
end | |
def self.run_webpack_if_outdated | |
return if self.already_built | |
if timestamp_outdated? | |
$stdout.puts 'Webpack: timestamp outdated, compiling...' | |
run_webpack | |
end | |
end | |
def self.timestamp_outdated? | |
return true if !File.exists?(TS_FILE) | |
bundle_ts = current_bundle_timestamp | |
return true if !bundle_ts | |
return bundle_ts < last_js_update | |
end | |
def self.current_bundle_timestamp | |
File.read(TS_FILE).to_i | |
rescue StandardError | |
end | |
def self.last_js_update | |
Dir[JAVASCRIPT_FILES].map { |f| File.mtime(f).utc.to_i }.max | |
end | |
end | |
RSpec.configure do |config| | |
config.before(:suite) do | |
WebpackTestBuild.run_webpack_if_empty_pack | |
end | |
config.before(:each, :js) do | |
WebpackTestBuild.run_webpack_if_outdated | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment