mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
22a7905f2d
Previously, accessing the Rails app directly in development mode would give you assets from our 'legacy' Ember asset pipeline. The only way to run with Ember CLI assets was to run ember-cli as a proxy. This was quite limiting when working on things which are bypassed when using the ember-cli proxy (e.g. changes to `application.html.erb`). Also, since `ember-auto-import` introduced chunking, visiting `/theme-qunit` under Ember CLI was failing to include all necessary chunks. This commit teaches Sprockets about our Ember CLI assets so that they can be used in development mode, and are automatically collected up under `/public/assets` during `assets:precompile`. As a bonus, this allows us to remove all the custom manifest modification from `assets:precompile`. The key changes are: - Introduce a shared `EmberCli.enabled?` helper - When ember-cli is enabled, add ember-cli `/dist/assets` as the top-priority Rails asset directory - Have ember-cli output a `chunks.json` manifest, and teach `preload_script` to read it and append the correct chunks to their associated `afterFile` - Remove most custom ember-cli logic from the `assets:precompile` step. Instead, rely on Rails to take care of pulling the 'precompiled' assets into the `public/assets` directory. Move the 'renaming' logic to runtime, so it can be used in development mode as well. - Remove fingerprinting from `ember-cli-build`, and allow Rails to take care of things Long-term, we may want to replace Sprockets with the lighter-weight Propshaft. The changes made in this commit have been made with that long-term goal in mind. tldr: when you visit the rails app directly, you'll now be served the current ember-cli assets. To keep these up-to-date make sure either `ember serve`, or `ember build --watch` is running. If you really want to load the old non-ember-cli assets, then you should start the server with `EMBER_CLI_PROD_ASSETS=0`. (the legacy asset pipeline will be removed very soon)
40 lines
1.1 KiB
Ruby
40 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module EmberCli
|
|
ALIASES ||= {
|
|
"application" => "discourse",
|
|
"discourse/tests/test-support-rails" => "test-support",
|
|
"discourse/tests/test-helpers-rails" => "test-helpers"
|
|
}
|
|
|
|
def self.enabled?
|
|
ENV["EMBER_CLI_PROD_ASSETS"] != "0"
|
|
end
|
|
|
|
def self.script_chunks
|
|
return @@chunk_infos if defined? @@chunk_infos
|
|
|
|
raw_chunk_infos = JSON.parse(File.read("#{Rails.configuration.root}/app/assets/javascripts/discourse/dist/chunks.json"))
|
|
|
|
chunk_infos = raw_chunk_infos["scripts"].map do |info|
|
|
logical_name = info["afterFile"][/\Aassets\/(.*)\.js\z/, 1]
|
|
chunks = info["scriptChunks"].map { |filename| filename[/\Aassets\/(.*)\.js\z/, 1] }
|
|
[logical_name, chunks]
|
|
end.to_h
|
|
|
|
@@chunk_infos = chunk_infos if Rails.env.production?
|
|
chunk_infos
|
|
rescue Errno::ENOENT
|
|
{}
|
|
end
|
|
|
|
# Some assets have changed name following the switch
|
|
# to ember-cli. When the switch is complete, we can
|
|
# drop this method and update all the references
|
|
# to use the new names
|
|
def self.transform_name(name)
|
|
return name if !enabled?
|
|
ALIASES[name] || name
|
|
end
|
|
end
|