Skip to content

Commit

Permalink
Merge pull request #49668 from skipkayhil/hm-fix-rails-ujs-start
Browse files Browse the repository at this point in the history
Fix rails-ujs auto start() in bundled environments
  • Loading branch information
eileencodes committed Oct 24, 2023
2 parents 121e0ad + f761780 commit 4c5c904
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 12 deletions.
3 changes: 3 additions & 0 deletions actionview/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "eslint:recommended",
"globals": {
"__esm": "readonly"
},
"rules": {
"semi": ["error", "never"],
"quotes": ["error", "double"],
Expand Down
4 changes: 4 additions & 0 deletions actionview/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Fix `@rails/ujs` calling `start()` an extra time when using bundlers

*Hartley McGuire*, *Ryunosuke Sato*

* Fix the `capture` view helper compatibility with HAML and Slim

When a blank string was captured in HAML or Slim (and possibly other template engines)
Expand Down
7 changes: 0 additions & 7 deletions actionview/app/assets/javascripts/rails-ujs.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,11 +683,4 @@ if (typeof jQuery !== "undefined" && jQuery && jQuery.ajax) {
}));
}

if (typeof exports !== "object" && typeof module === "undefined") {
window.Rails = Rails;
if (fire(document, "rails:attachBindings")) {
start();
}
}

export { Rails as default };
2 changes: 1 addition & 1 deletion actionview/app/javascript/rails-ujs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ if (typeof jQuery !== "undefined" && jQuery && jQuery.ajax) {
// difference between what happens in a bundler and what happens using a
// sprockets compiler. In the sprockets case, Rails.start() is called
// automatically, but it is not in the ESModule case.
if (typeof exports !== "object" && typeof module === "undefined") {
if (__esm == false && typeof exports !== "object" && typeof module === "undefined") {
// The coffeescript bundle would set this at the very top. The Rollup bundle
// doesn't set this until the entire bundle has finished running, so we need
// to make sure its set before firing the rails:attachBindings event for
Expand Down
1 change: 1 addition & 0 deletions actionview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^19.0.1",
"@rollup/plugin-node-resolve": "^11.0.1",
"@rollup/plugin-replace": "^5.0.4",
"eslint": "^4.19.1",
"eslint-plugin-import": "^2.23.4",
"jquery": "^2.2.0",
Expand Down
13 changes: 11 additions & 2 deletions actionview/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { terser } from "rollup-plugin-terser"
import replace from "@rollup/plugin-replace"

const banner = `
/*
Expand Down Expand Up @@ -33,7 +34,11 @@ export default [
banner,
},
plugins: [
terser(terserOptions)
replace({
preventAssignment: true,
values: { __esm: false },
}),
terser(terserOptions),
]
},

Expand All @@ -45,7 +50,11 @@ export default [
banner,
},
plugins: [
terser(terserOptions)
replace({
preventAssignment: true,
values: { __esm: true },
}),
terser(terserOptions),
]
}
]
5 changes: 5 additions & 0 deletions actionview/rollup.config.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Rollup configuration for compiling the UJS tests

import commonjs from "@rollup/plugin-commonjs"
import replace from "@rollup/plugin-replace"
import resolve from "@rollup/plugin-node-resolve"

export default {
Expand All @@ -12,6 +13,10 @@ export default {
},

plugins: [
replace({
preventAssignment: true,
values: { __esm: false }, // false because the tests expects start() to be called automatically
}),
resolve(),
commonjs()
]
Expand Down
38 changes: 36 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
resolved "https://registry.npmjs.org/@jridgewell/source-map/-/source-map-0.3.4.tgz"
integrity sha512-KE/SxsDqNs3rrWwFHcRh15ZLVFrI0YoZtgAdIyIq9k5hUNmiWRXXThPomIxHuL20sLdgzbDFyvkUMna14bvtrw==

"@jridgewell/sourcemap-codec@^1.4.15":
version "1.4.15"
resolved "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz"
integrity sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==

"@rails/activestorage@>= 7.1.0-alpha":
version "7.1.0-beta1"
resolved "https://registry.npmjs.org/@rails/activestorage/-/activestorage-7.1.0-beta1.tgz"
Expand Down Expand Up @@ -60,6 +65,14 @@
is-module "^1.0.0"
resolve "^1.19.0"

"@rollup/plugin-replace@^5.0.4":
version "5.0.4"
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-5.0.4.tgz#fef548dc751d06747e8dca5b0e8e1fbf647ac7e1"
integrity sha512-E2hmRnlh09K8HGT0rOnnri9OTh+BILGr7NVJGB30S4E3cLRn3J0xjdiyOZ74adPs4NiAMgrjUMGAZNJDBgsdmQ==
dependencies:
"@rollup/pluginutils" "^5.0.1"
magic-string "^0.30.3"

"@rollup/pluginutils@^3.1.0":
version "3.1.0"
resolved "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-3.1.0.tgz"
Expand All @@ -69,11 +82,25 @@
estree-walker "^1.0.1"
picomatch "^2.2.2"

"@rollup/pluginutils@^5.0.1":
version "5.0.5"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-5.0.5.tgz#bbb4c175e19ebfeeb8c132c2eea0ecb89941a66c"
integrity sha512-6aEYR910NyP73oHiJglti74iRyOwgFU4x3meH/H8OJx6Ry0j6cOVZ5X/wTvub7G7Ao6qaHBEaNsV3GLJkSsF+Q==
dependencies:
"@types/estree" "^1.0.0"
estree-walker "^2.0.2"
picomatch "^2.3.1"

"@types/estree@*", "@types/estree@0.0.39":
version "0.0.39"
resolved "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/estree@^1.0.0":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.1.tgz#aa22750962f3bf0e79d753d3cc067f010c95f194"
integrity sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==

"@types/json5@^0.0.29":
version "0.0.29"
resolved "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz"
Expand Down Expand Up @@ -1786,7 +1813,7 @@ estree-walker@^1.0.1:
resolved "https://registry.npmjs.org/estree-walker/-/estree-walker-1.0.1.tgz"
integrity sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==

estree-walker@^2.0.1:
estree-walker@^2.0.1, estree-walker@^2.0.2:
version "2.0.2"
resolved "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz"
integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==
Expand Down Expand Up @@ -3045,6 +3072,13 @@ magic-string@^0.25.7:
dependencies:
sourcemap-codec "^1.4.8"

magic-string@^0.30.3:
version "0.30.5"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.30.5.tgz#1994d980bd1c8835dc6e78db7cbd4ae4f24746f9"
integrity sha512-7xlpfBaQaP/T6Vh8MO/EqXSW5En6INHEvEXQiuff7Gku0PWjU3uf6w/j9o7O+SpB5fOAkrI5HeoNgwjEO0pFsA==
dependencies:
"@jridgewell/sourcemap-codec" "^1.4.15"

make-dir@^2.0.0:
version "2.1.0"
resolved "https://registry.npmjs.org/make-dir/-/make-dir-2.1.0.tgz"
Expand Down Expand Up @@ -3573,7 +3607,7 @@ performance-now@^2.1.0:
resolved "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz"
integrity sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==

picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.2:
picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.2, picomatch@^2.3.1:
version "2.3.1"
resolved "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==
Expand Down

0 comments on commit 4c5c904

Please sign in to comment.