【发布时间】:2017-02-27 12:28:59
【问题描述】:
在 Rails 5.1 上运行集成规范时,javascript_pack_tag 助手会在 public/packs/application.js 中为已编译的 packs/application.js 文件生成路径
但是,bin/rails assets:precompile 将使用生产配置编译您的包,其中包括摘要。因此找不到该文件。
【问题讨论】:
标签: ruby-on-rails webpack
在 Rails 5.1 上运行集成规范时,javascript_pack_tag 助手会在 public/packs/application.js 中为已编译的 packs/application.js 文件生成路径
但是,bin/rails assets:precompile 将使用生产配置编译您的包,其中包括摘要。因此找不到该文件。
【问题讨论】:
标签: ruby-on-rails webpack
要将 packs/application.js 编译成public/packs/application.js,请通过以下方式指定开发环境:
RAILS_ENV=development bin/webpack
在运行集成规范之前运行此任务。
【讨论】:
这是我在使用 Rails 5.1 webpacker gem 时在本地测试环境中运行 webpack-dev-server 的设置:
在config/environments/test.rb,添加:
unless ENV['CI'] == 'true'
config.x.webpacker[:dev_server_host] = "http://localhost:8080"
end
然后在spec/rails_helper.rb 或您的测试设置中,添加:
config.add_setting :webpack_dev_server_pid
config.before(:suite) do
unless ENV['CI'] == 'true'
RSpec.configuration.webpack_dev_server_pid = fork do
puts "Child process starting webpack-dev-server"
webpack_dev_server_cmd = [
"#{Rails.root}/node_modules/.bin/webpack-dev-server",
"--config #{Rails.root}/config/webpack/development.js",
"--content-base #{Rails.root}/public/packs",
"--quiet"
].join(" ")
exec(webpack_dev_server_cmd)
end
end
end
config.after(:suite) do
unless ENV['CI'] == 'true'
puts "Killing webpack-dev-server"
Process.kill("HUP",RSpec.configuration.webpack_dev_server_pid)
begin
Timeout.timeout(2) do
Process.wait(RSpec.configuration.webpack_dev_server_pid,0)
end
rescue => Timeout::Error
Process.kill(9,RSpec.configuration.webpack_dev_server_pid)
ensure
RSpec.configuration.webpack_dev_server_pid = nil
end
end
end
此设置允许您在持续集成环境中设置 CI=true 以禁用 webpack-dev-server,而您可以在执行测试套件之前运行 RAILS_ENV=development bin/webpack。
感谢这个 Github comment。
【讨论】:
这是支持所有 Rails 5.1 更新的本地构建命令列表:
brew update
brew install yarn
./bin/yarn install
./bin/yarn add webpack
./bin/webpack-watcher
bundle exec rake db:create
bundle exec rake db:migrate
【讨论】: