Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable esbuild splitting #2342

Merged
merged 2 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/javascript/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function build() {
],
bundle: true,
sourcemap: true,
format: 'esm',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support looks good: https://caniuse.com/es6-module

splitting: true,
minify: process.env.NODE_ENV === 'production',
watch: process.argv.includes('--watch'),
outdir: '.built-assets',
Expand Down
6 changes: 3 additions & 3 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
= stylesheet_link_tag "internal", "data-turbo-track": "reload"
= stylesheet_link_tag "website", "data-turbo-track": "reload"

= javascript_include_tag('core', 'data-turbo-track': 'reload', 'data-turbo-eval': false)
= javascript_include_tag('core', type: :module, 'data-turbo-track': 'reload', 'data-turbo-eval': false)
- js_packs.each do |pack|
= javascript_include_tag(pack, 'data-turbo-track': 'reload', 'data-turbo-eval': false)
= javascript_include_tag(pack, type: :module, 'data-turbo-track': 'reload', 'data-turbo-eval': false)

- deferred_js_packs.each do |pack|
= javascript_include_tag(pack, 'data-turbo-track': 'reload', 'data-turbo-eval': false, defer: true)
= javascript_include_tag(pack, type: :module, 'data-turbo-track': 'reload', 'data-turbo-eval': false, defer: true)

// TODO - Remove any unused weights
%link{ href: "https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Source+Code+Pro:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;0,900;1,200;1,300;1,400;1,500;1,600;1,700;1,900&display=swap", rel: "stylesheet" }
Expand Down
30 changes: 30 additions & 0 deletions config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,33 @@
config.assets.paths << 'app/images'
config.assets.paths << '.built-assets'
end

# This code is taken from https://github.com/rails/propshaft/blob/main/lib/propshaft/server.rb#L8
# and modified to ignore esbuild derrived chunks and related files
require 'propshaft/server'
module Propshaft
class Server
def call(env)
path, digest = extract_path_and_digest(env)

esbuild_split_asset = (path.include?('-') && path.ends_with?('.js'))
if (asset = @assembly.load_path.find(path)) && (asset.fresh?(digest) || esbuild_split_asset)
compiled_content = @assembly.compilers.compile(asset)

[
200,
{
"Content-Length" => compiled_content.length.to_s,
"Content-Type" => asset.content_type.to_s,
"Accept-Encoding" => "Vary",
"ETag" => asset.digest,
"Cache-Control" => "public, max-age=31536000, immutable"
},
[compiled_content]
]
else
[404, { "Content-Type" => "text/plain", "Content-Length" => "9" }, ["Not found"]]
end
end
end
end