embroider-build / ember-auto-import

Zero config import from npm packages
Other
360 stars 109 forks source link

Breaks when using script defer #420

Open mydea opened 3 years ago

mydea commented 3 years ago

I have an app that is not using ember-auto-import in its application code (as far as I can tell).

I noticed that I get a runtime error:

l.js:2 Uncaught ReferenceError: require is not defined
    at eval (l.js:2)
    at Object.../../../../../tmp/broccoli-20413LssqYisKTYOQ/cache-305-webpack_bundler_ember_auto_import_webpack/l.js (chunk.app.9a9a5f13351ef9d0df2b.js:29)
    at __webpack_require__ (chunk.app.9a9a5f13351ef9d0df2b.js:53)

Where l.js is:

window._eai_r = require;
window._eai_d = define;

When I remove ember-auto-import from my devDependencies, the error goes away.

I would just remove it, but according to https://github.com/emberjs/ember-qunit/blob/master/docs/migration.md it is needed for ember-qunit for the tests (although the tests do work without it, as far as I can tell :thinking: )

ef4 commented 3 years ago

What version of ember-auto-import?

What were you changing when this problem appeared?

mydea commented 3 years ago

Not 100% sure, as the app did not fail (and tests passed), the error is only visible in the console (which I only just noticed).

I would guess it happend when upgrading from ember-auto-import 1.x to 2.x - I just tried downgrading it and the error went away!

I will try to see if I can get a reproduction somehow...!

mydea commented 3 years ago

OK, after a lot of trial and error, I found the culprit!

In this app, defer was added to the vendor JS file in index.html:

<script src="{{rootURL}}assets/vendor.js" defer></script>

As it uses fastboot/prember and thus does not need the JS to work.

It works if:

Here is a reproduction repo: https://github.com/mydea/ember-issue-reproductions/commit/4948624aa766b1d12edc067b545e0d096a630d13

ef4 commented 3 years ago

We need to find a clearer place to document this, but this is a known limitation, see https://github.com/ef4/ember-auto-import#customizing-build-behavior:

publicAssetURL: the public URL to your /assets directory on the web. Many apps won't need to set this because we try to detect it automatically, but you will need to set this explicitly if you're deploying your assets to a different origin than your app (for example, on a CDN) or if you are using Githubissues.

  • Githubissues is a development platform for aggregating issues.