ember-cli / broccoli-asset-rewrite

Broccoli plugin to rewrite a source tree from an asset map.
MIT License
10 stars 52 forks source link

Rewrite matching incorrect asset instances #105

Open alvarocastro opened 1 month ago

alvarocastro commented 1 month ago

Noticed the rewrite is replacing an instance of "style.css" where it shouldn't.

It happens with this piece of code: (it's a dependency of my project, prosemirror-view)

function iosHacks(dom) {
  if (dom.nodeName == "UL" || dom.nodeName == "OL") {
    var oldCSS = dom.style.cssText; // <- just this line
    dom.style.cssText = oldCSS + "; list-style: square !important";
    window.getComputedStyle(dom).listStyle;
    dom.style.cssText = oldCSS;
  }
}

In the pointed line, looks like the "style.css" part of the string is being matched and replaced with the fingerprinted one, ending up looking like this: (for some reason, the other lines are not being matched and stay as they should)

var oldCSS = dom.style-0d55b35826f83b7acaf51324d2f32623.cssText;

This not only changes the purpose of the code (it's a subtraction now) but also breaks the terser because it's bad js syntax (variable starts with a number) and get this error:

Build Error (TerserWriter)

Invalid syntax: 0d55b35826f83b7acaf51324d2f32623

Update: this seems to be happening after code minification. This is the same function as above but after:

function(e){if("UL"==e.nodeName||"OL"==e.nodeName){var n=e.style.cssText;e.style.cssText=n+"; list-style: square !important",window.getComputedStyle(e).listStyle,e.style.cssText=n}}

The matcher here for assetPath="style.css" seems to be matching with this part e.style.cssText;e.style.cs and then replaces the "style.css" part with "style-THEHASH.css"

Any workaround for this? I have no idea how all of this works this is the first time I dig this deep into broccoli and the ember build process.

alvarocastro commented 1 month ago

Replying to myself in case anyone stumbles with the same issue.

Excluding the file from fingerprinting solves the issue (altho the css won't be fingerprinted)

// ember-cli-build.js
let app = new EmberAddon(defaults, {
  fingerprint: {
    exclude: [
      'style'
    ]
  },
  // ...

Maybe another solution would be to just change the file name to something less chashing, but would need to explore how to do that.