Skip to content

Commit d302e63

Browse files
committed
Import application JS as a module
Bun.js generates JS bundles in the ESM format and they need be imported with the `type="module"` attribute. Otherwise the module varibles end up in the global scope. See hotwired/turbo#1077 This commit updates the install generator to add the type="module" attribute to the default `javascript_include_tag`. `defer` is no longer needed, as JS modules are deferred by default. Ref. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts This PR also updates the default config to ensure that all bundlers are configured to output ESM bundles. - bun only supports ESM at the moment https://bun.sh/docs/bundler#format - esbuild outputs ESM by default when bundling is enabled https://esbuild.github.io/api/#format-esm - webpacker is configured to output ESM bundles with `output.chunkFormat` https://webpack.js.org/configuration/output/#outputchunkformat - rollup is configured to output ESM bundles with `output.format` https://rollupjs.org/configuration-options/#output-format
1 parent 95a5472 commit d302e63

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

lib/install/install.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
if (app_layout_path = Rails.root.join("app/views/layouts/application.html.erb")).exist?
1515
say "Add JavaScript include tag in application layout"
1616
insert_into_file app_layout_path.to_s,
17-
%(\n <%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>), before: /\s*<\/head>/
17+
%(\n <%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module" %>), before: /\s*<\/head>/
1818
else
1919
say "Default application.html.erb is missing!", :red
20-
say %( Add <%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %> within the <head> tag in your custom layout.)
20+
say %( Add <%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module" %> within the <head> tag in your custom layout.)
2121
end
2222

2323
unless (app_js_entrypoint_path = Rails.root.join("app/javascript/application.js")).exist?

lib/install/rollup/rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
input: "app/javascript/application.js",
55
output: {
66
file: "app/assets/builds/application.js",
7-
format: "iife",
7+
format: "esm",
88
inlineDynamicImports: true,
99
sourcemap: true
1010
},

lib/install/webpack/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
output: {
1111
filename: "[name].js",
1212
sourceMapFilename: "[file].map",
13+
chunkFormat: "module",
1314
path: path.resolve(__dirname, "app/assets/builds"),
1415
},
1516
plugins: [

0 commit comments

Comments
 (0)