discourse/spec/integrity/coding_style_spec.rb
David Taylor cbc28e8e33
Enable Embroider/Webpack code spliting for Wizard (#24919)
(extracted from #23678)

* Move Wizard back into main app, remove Wizard addon
* Remove Wizard-related resolver or build hacks
* Install and enable `@embroider/router`
* Add "wizard" to `splitAtRoutes`

In a fully optimized Embroider app, route-based code splitting more
or less Just Work™ – install `@embroider/router`, subclass from it,
configure which routes you want to split and that's about it.

However, our app is not "fully optimized", by which I mean we are
not able to turn on all the `static*` flags.

In Embroider, "static" means "statically analyzable". Specifically
it means that all inter-dependencies between modules (files) are
explicitly expressed as `import`s, as opposed to `{{i18n ...}}`
magically means "look for the default export in app/helpers/i18n.js"
or something even more dynamic with the resolver.

Without turning on those flags, Embroider behaves conservatively,
slurps up all `app` files eagerly into the primary bundle/chunks.
So, while you _could_ turn on route-based code splitting, there
won't be much to split.

The commits leading up to this involves a bunch of refactors and
cleanups that 1) works perfectly fine in the classic build, 2) are
good and useful in their own right, but also 3) re-arranged things
such that most dependencies are now explicit.

With those in place, I was able to move all the wizard code into
the "app/static" folder. Embroider does not eagerly pull things from
this folder into any bundle, unless something explicitly "asks" for
them via `imports`. Conversely, things from this folder are not
registered with the resolver and are not added to the `loader.js`
registry.

In conjunction with route-based code splitting, we now have the
ability to split out islands of on-demand functionalities from the
main app bundle.

When you split a route in Embroider, it automatically creates a
bundle/entrypoint with the relevant routes/templates/controllers
matching that route prefix. Anything they import will be added to
the bundle as well, assuming they are not already in the main app
bundle, which is where the "app/static" folder comes into play.

The "app/static" folder name is not special. It is configured in
ember-cli-build.js. Alternatively, we could have left everything
in their normal locations, and add more fine-grained paths to the
`staticAppPaths` array. I just thought it would be easy to manage
and scale, and less error-prone to do it this way.

Note that putting things in `app/static` does not guarantee that
it would not be part of the main app bundle. For example, if we
were to add an `import ... from "app/static/wizard/...";` in a
main bundle file (say, `app.js`), then that chunk of the module
graph would be pulled in. (Consider using `await import(...)`?)

Overtime, we can build better tooling (e.g. lint rules and babel
macros to make things less repetitive) as we expand the use of
this pattern, but this is a start.

Co-authored-by: Godfrey Chan <godfreykfc@gmail.com>
2023-12-20 13:15:06 +00:00

63 lines
2.2 KiB
Ruby

# frozen_string_literal: true
def list_files(base_dir, pattern = "*")
Dir[File.join("#{base_dir}", pattern)]
end
def grep_files(files, regex)
files.select { |file| grep_file(file, regex) }
end
def grep_file(file, regex)
lines = File.open(file).grep(regex)
lines.count > 0 ? file : nil
end
RSpec.describe "Coding style" do
describe "Post Migrations" do
def check_offenses(files, method_name, constant_name)
method_name_regex = /#{Regexp.escape(method_name)}/
constant_name_regex = /#{Regexp.escape(constant_name)}/
offenses = files.reject { |file| is_valid?(file, method_name_regex, constant_name_regex) }
expect(offenses).to be_empty, <<~TEXT
You need to use the constant #{constant_name} when you use
#{method_name} in order to help with restoring backups.
Please take a look at existing migrations to see how to use it correctly.
Offenses:
#{offenses.join("\n")}
TEXT
end
def is_valid?(file, method_name_regex, constant_name_regex)
contains_method_name = File.open(file).grep(method_name_regex).any?
contains_constant_name = File.open(file).grep(constant_name_regex).any?
contains_method_name ? contains_constant_name : true
end
it "ensures dropped tables and columns are stored in constants" do
migration_files = list_files("db/post_migrate", "**/*.rb")
check_offenses(migration_files, "ColumnDropper.execute_drop", "DROPPED_COLUMNS")
check_offenses(migration_files, "TableDropper.execute_drop", "DROPPED_TABLES")
end
end
describe "non-colocated component templates" do
{
"discourse" => "app/assets/javascripts/discourse/app/templates/components",
"admin" => "app/assets/javascripts/admin/addon/templates/components",
"chat/discourse" => "plugins/chat/assets/javascripts/discourse/templates/components",
"chat/admin" => "plugins/chat/assets/javascripts/admin/templates/components",
"styleguide" => "plugins/styleguide/assets/javascripts/discourse/templates/components",
}.each_pair do |name, dir|
it "do not exist for #{name}" do
expect(list_files(dir)).to eq([])
end
end
end
end