ember-cli / broccoli-asset-rewrite

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

rewriteAssetPath matches code blocks #42

Open spieker opened 8 years ago

spieker commented 8 years ago

I have a project called Ted that uses ember-simple-auth. At some point in the ember build pipeline the regex /["'\(=]{1}\s*([^"'\(\)=]*ted\.js[^"'\(\)\\>=]*)(\?[^"'\)> ]*)?\s*[\\]*\s*["'\)> ]{1}/g is used on the vendor file. Since ember-simple-auth has a code part like this

transition and instead transitions to the
{{#crossLink "Configuration/authenticationRoute:property"}}{{/crossLink}} if
the session is not authenticated.

```js
// app/routes/protected.js
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin);

The regex now matches

"}}{{/crossLink}} if the session is not authenticated.

// app/routes/protected.js
import AuthenticatedRouteMixin from '

which leads to this error

File: assets/vendor-6461e56a880aae756be022e1c294a533.js
The Broccoli Plugin: [AssetRewrite] failed with:
TypeError: Cannot read property '2' of null
    at AssetRewrite.rewriteAssetPath (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/broccoli-asset-rewrite/index.js:124:91)
    at AssetRewrite.processString (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/broccoli-asset-rewrite/index.js:163:24)
    at AssetRewrite.processFile (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/broccoli-filter/index.js:165:31)
    at asyncProcessFile (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/broccoli-filter/index.js:122:21)
    at lib$rsvp$$internal$$tryCatch (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/rsvp/dist/rsvp.js:493:16)
    at lib$rsvp$$internal$$invokeCallback (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/rsvp/dist/rsvp.js:505:17)
    at /###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/rsvp/dist/rsvp.js:1001:13
    at lib$rsvp$asap$$flush (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/rsvp/dist/rsvp.js:1198:9)
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)

The broccoli plugin was instantiated at: 
    at AssetRewrite.Plugin (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/broccoli-filter/node_modules/broccoli-plugin/index.js:7:31)
    at AssetRewrite.Filter [as constructor] (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/broccoli-filter/index.js:34:10)
    at new AssetRewrite (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/broccoli-asset-rewrite/index.js:27:10)
    at AssetRewrite (/###/Projects/ted/node_modules/broccoli-asset-rev/node_modules/broccoli-asset-rewrite/index.js:22:12)
    at new AssetRev (/###/Projects/ted/node_modules/broccoli-asset-rev/lib/asset-rev.js:29:10)
    at AssetRev (/###/Projects/ted/node_modules/broccoli-asset-rev/lib/asset-rev.js:7:12)
    at Class.module.exports.postprocessTree (/###/Projects/ted/node_modules/broccoli-asset-rev/index.js:32:14)
    at /###/Projects/ted/node_modules/ember-cli/lib/broccoli/ember-app.js:506:27
    at Array.forEach (native)
    at EmberApp.addonPostprocessTree (/###/Projects/ted/node_modules/ember-cli/lib/broccoli/ember-app.js:504:23)

Any suggestions for a workaround for now?

ghost commented 8 years ago

I'm also experiencing this where broccoli-asset-rev is being called with multiple lines. This causes the regexp to fail. In my instance it is being called on:

assets/bootstrap.css-c7ffd8c7a285780039b2366034f3bca2.map */

/*!
 *  Font Awesome 4.5.0 by @davegandy - http://fontawesome.io - @fontawesome
 *  License - http://fontawesome.io/license

@spieker I have a workaround however I don't think this is actually a bug in this repo, since this isn't expecting a multiline sourcemap input. My instinct is that the bug is actually in broccoli-asset-rev, but I don't know much about how this all really works.

If you want to use my workaround you can add my repo to your package.json.

npm install binhums/broccoli-asset-rewrite --save-dev

I'll submit a pull request anyway to highlight the issue / bug fix.

ghost commented 8 years ago

I don't know if this is what is going on here, the stack trace looks very similar:

https://github.com/ember-cli/ember-cli/issues/6265

apellerano-pw commented 7 years ago

I get this same error, it's preventing us from upgrading to the latest broccoli-asset-rewrite. We want the speed improvements in the latest release but the changes to the regex do not work with our environment. We have lots of vendor js being imported in ember-cli-build.js thru app.import() and the regex greedily matches comments at the start of those imports

apellerano-pw commented 7 years ago

One workaround is to add assets/vendor.js to your fingerprint config's ignore list. (exclude won't work)

spieker commented 7 years ago

In our case we just renamed the output file from /assets/ted.js to /assets/app.js and everything was working again.

williamhaley commented 7 years ago

Same problem here. I wanted to experiment with some fingerprinting settings locally. I was manually enabling fingerprinting in dev by setting app.options.fingerprint.enabled = true; in ember-cli-build.js and running ember build. I got the same error as @spieker except with vendor.css.

Everything works fine for a normal production build though: ember build --environment=production

FilmKnurd commented 7 years ago

Did the same as @spieker. When step debugging, it looks like the rewrite chokes on the comments added during concatenation.

omairvaiyani commented 6 years ago

+1

Build Error (AssetRewrite) in assets/vendor-[fingerprint].css
Cannot read property '2' of null

Stumped!

BillBrower commented 4 years ago

@omairvaiyani Running into this exact error after upgrading an Ember 3.4 app to Ember 3.5. Did you ever figure this out?

apellerano-pw commented 3 years ago

I ran into this again while upgrading from Ember 3.3 to Ember 3.16 (🤞 🐹 🤞), this time in our fingerprinted test-support.js file. I noticed we never grabbed the broccoli-asset-rev 3.0.0 update, which bumps broccoli-asset-rewrite to 2.0.0, and contains this change: https://github.com/ember-cli/broccoli-asset-rewrite/pull/64/files. This fixed my most recent issue!

If you're running into this still, double check that you're using broccoli-asset-rewrite 2.0.0+ (npm ls broccoli-asset-rewrite)