DEV: Allow Ember CLI assets to be used by development Rails app (#16511)
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)
2022-04-21 23:26:34 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-11-07 18:24:49 +08:00
|
|
|
class EmberCli < ActiveSupport::CurrentAttributes
|
|
|
|
# Cache which persists for the duration of a request
|
2023-11-10 19:16:06 +08:00
|
|
|
attribute :request_cache
|
2023-11-07 18:24:49 +08:00
|
|
|
|
2023-09-04 20:56:34 +08:00
|
|
|
def self.dist_dir
|
|
|
|
"#{Rails.root}/app/assets/javascripts/discourse/dist"
|
|
|
|
end
|
|
|
|
|
2022-08-22 16:56:39 +08:00
|
|
|
def self.assets
|
2023-11-10 19:16:06 +08:00
|
|
|
cache[:assets] ||= Dir.glob("**/*.{js,map,txt}", base: "#{dist_dir}/assets")
|
2022-08-22 16:56:39 +08:00
|
|
|
end
|
2022-05-11 17:23:32 +08:00
|
|
|
|
DEV: Allow Ember CLI assets to be used by development Rails app (#16511)
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)
2022-04-21 23:26:34 +08:00
|
|
|
def self.script_chunks
|
2023-11-10 19:16:06 +08:00
|
|
|
return cache[:script_chunks] if cache[:script_chunks]
|
DEV: Allow Ember CLI assets to be used by development Rails app (#16511)
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)
2022-04-21 23:26:34 +08:00
|
|
|
|
2023-11-07 18:24:49 +08:00
|
|
|
chunk_infos = JSON.parse(File.read("#{dist_dir}/assets.json"))
|
2023-09-04 20:56:34 +08:00
|
|
|
|
2023-11-07 18:24:49 +08:00
|
|
|
chunk_infos.transform_keys! { |key| key.delete_prefix("assets/").delete_suffix(".js") }
|
2023-09-04 20:56:34 +08:00
|
|
|
|
2023-11-07 18:24:49 +08:00
|
|
|
chunk_infos.transform_values! do |value|
|
|
|
|
value["assets"].map { |chunk| chunk.delete_prefix("assets/").delete_suffix(".js") }
|
|
|
|
end
|
DEV: Allow Ember CLI assets to be used by development Rails app (#16511)
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)
2022-04-21 23:26:34 +08:00
|
|
|
|
2023-11-07 19:34:19 +08:00
|
|
|
# Special case - vendor.js is fingerprinted by Embroider in production, but not run through Webpack
|
|
|
|
if !assets.include?("vendor.js") &&
|
|
|
|
fingerprinted = assets.find { |a| a.match?(/^vendor\..*\.js$/) }
|
|
|
|
chunk_infos["vendor"] = [fingerprinted.delete_suffix(".js")]
|
|
|
|
end
|
|
|
|
|
2023-11-10 19:16:06 +08:00
|
|
|
cache[:script_chunks] = chunk_infos
|
DEV: Allow Ember CLI assets to be used by development Rails app (#16511)
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)
2022-04-21 23:26:34 +08:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2022-05-11 17:23:32 +08:00
|
|
|
def self.is_ember_cli_asset?(name)
|
2023-09-10 17:18:43 +08:00
|
|
|
assets.include?(name) || script_chunks.values.flatten.include?(name.delete_suffix(".js"))
|
2022-05-11 17:23:32 +08:00
|
|
|
end
|
2022-08-30 23:27:14 +08:00
|
|
|
|
|
|
|
def self.ember_version
|
|
|
|
@version ||=
|
|
|
|
begin
|
2023-04-20 20:57:40 +08:00
|
|
|
ember_source_package_raw =
|
|
|
|
File.read("#{Rails.root}/app/assets/javascripts/node_modules/ember-source/package.json")
|
2022-08-30 23:27:14 +08:00
|
|
|
JSON.parse(ember_source_package_raw)["version"]
|
|
|
|
end
|
|
|
|
end
|
2023-06-09 18:14:11 +08:00
|
|
|
|
|
|
|
def self.workbox_dir_name
|
|
|
|
return @workbox_base_dir if defined?(@workbox_base_dir)
|
|
|
|
|
|
|
|
@workbox_base_dir =
|
|
|
|
if (full_path = Dir.glob("app/assets/javascripts/discourse/dist/assets/workbox-*")[0])
|
|
|
|
File.basename(full_path)
|
|
|
|
end
|
|
|
|
end
|
2023-09-04 20:56:34 +08:00
|
|
|
|
2023-09-11 16:12:37 +08:00
|
|
|
def self.has_tests?
|
|
|
|
File.exist?("#{dist_dir}/tests/index.html")
|
|
|
|
end
|
2023-10-27 00:29:53 +08:00
|
|
|
|
2023-11-10 19:16:06 +08:00
|
|
|
def self.cache
|
|
|
|
if Rails.env.development?
|
|
|
|
self.request_cache ||= {}
|
|
|
|
else
|
|
|
|
@production_cache ||= {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-27 00:29:53 +08:00
|
|
|
def self.clear_cache!
|
2023-11-13 18:34:06 +08:00
|
|
|
self.request_cache = nil
|
2023-11-10 19:16:06 +08:00
|
|
|
@production_cache = nil
|
2023-10-27 00:29:53 +08:00
|
|
|
end
|
DEV: Allow Ember CLI assets to be used by development Rails app (#16511)
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)
2022-04-21 23:26:34 +08:00
|
|
|
end
|