Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Top-level await is not supported #1948

Open
dwickern opened this issue May 28, 2024 · 2 comments
Open

Top-level await is not supported #1948

dwickern opened this issue May 28, 2024 · 2 comments

Comments

@dwickern
Copy link

Reproduction here: https://github.com/dwickern/ember-pdfjs-reproduction/tree/embroider

The module which imports PDF.js will have its own exports broken. In the reproduction, the application route imports pdf.js. Its export resolves to a promise instead of the route class:

image

This results in an error:

Uncaught Error: Assertion Failed: Failed to create an instance of 'route:application'. Most likely an improperly defined class or an invalid module export.
    at assert (index.js:118:1)
    at InternalFactoryManager.create (index.js:372:1)
    at Proxy.create (index.js:204:1)
    at instantiateFactory (index.js:276:1)
    at lookup (index.js:223:1)
    at Container.lookup (index.js:121:1)
    at ApplicationInstance.lookup (container_proxy.js:19:1)
    at PrivateRouter.getRoute (router.js:183:1)
    at UnresolvedRouteInfoByParam.fetchRoute (router_js.js:714:1)
    at get route (router_js.js:648:1)

If you import PDF.js in a service instead, all @service injections for that service will resolve to a Promise<Service> instead of the service.

There's a related problem with ember-auto-import embroider-build/ember-auto-import#614

@ef4
Copy link
Contributor

ef4 commented May 28, 2024

pdf.js. is using top-level await. We can't make that work until we do emberjs/rfcs#938.

A workaround until then is to access pdf.js via dynamic import only:

diff --git a/app/routes/application.js b/app/routes/application.js
index 1c602d9..9555db4 100644
--- a/app/routes/application.js
+++ b/app/routes/application.js
@@ -1,10 +1,10 @@
 import Route from '@ember/routing/route';
-import { getDocument } from 'pdfjs-dist/webpack.mjs';
 
 const url = '/fw4.pdf';
 
 export default class ApplicationRoute extends Route {
   async model() {
+    const { getDocument } = await import('pdfjs-dist/webpack.mjs');
     const loadingTask = getDocument({ url });
     return await loadingTask.promise;
   }

This worked for me in your reproduction app.

@ef4 ef4 changed the title Error importing pdf.js Top-level await is not supported May 28, 2024
@dwickern
Copy link
Author

PDF.js 4.3.136 just released and they no longer use top-level await, so it works now without the dynamic import.

Related issue mozilla/pdf.js#17245 and fix mozilla/pdf.js#18051

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants