-
Notifications
You must be signed in to change notification settings - Fork 10k
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
4.0.189: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides) #17245
Comments
Export
|
Is this a solution @jiadesen ? I'm getting them same as you as jut trying out v4 in a new project and getting:
See https://elixirforum.com/t/importing-pdf-mjs-into-app-js/59578 with --target=es2022 that is via esbuild. If I use the default of --target=es2017 I get:
Should work or should we go back to v3? |
No, it's just how you're importing it. Have switch to that way to confirm that we have the same issue. We do. |
Searching for the error message Looking at the MDN compatibility data all modern browsers/environments support top level await, and has done so for awhile, hence the only suggestion that we can really provide is to (if possible) use another bundler instead. |
Solved by adding vite configuration:
|
I also encountered the same error, but I was able to resolve the issue by installing the following vite plugin. |
this should be the final answer
|
this fixed it for me, thank you! |
I have the same issue here. I am trying to use pdf.js v4 in an elixir/phoenix framework environment where the bundler is esbuild. Should it work or do we have to switch back to v3? |
It works better, compatible with |
For anyone wanting broader browser support - https://www.npmjs.com/package/vite-plugin-top-level-await. {
// vite.config
plugins: [
// ...
topLevelAwait({
promiseExportName: '__tla',
promiseImportName: i => `__tla_${i}`,
}),
],
} Note: Bundle size will be 30kb larger compared to native es2022. |
And if i'm working in Angular project that's been created with Angular CLI (NO VITE CONFIGURATION FILE !), what should i do ? How to solve this issue with projects that does not based on vite ? |
Agree with @farisshomali, angular out-of-the-box uses Webpack, there is possibility to customize its configuration with (experiments: { topLevelAwait: true }), but it means stick to webpack (not good). Trying to use 'esbuild' (planned to be used by Angular out-of-the-box) and pdfjs - build fails ('esbuild' has no intention to support toplevelawait' at all). So, question is can 'top level await' be avoided by 'pdfjs'? It will make it easier to use any bundler. |
@Snuffleupagus ↑ ? |
@yuri-apanasik I don't know how you use the pdf.js project, but I think it's possible to avoid use "top level await". The top level await is used to load the main bundle of pdf.js, it contains all core features any viewer solution will depends on. The pdf.js also has a web bundle, it contains all code the default viewer needs, and of course it depends the main bundle. The possible way to avoid the top level await is develop a custom gulp task, just build a single bundle which combine the web and main bundles together, then the issue will gone. it's may not be an easy job depending on your knowledge of the pdf.js project. |
@Priestch yes, I am new in pdf.js, we just looked for the tool to convert PDF to PNG on the client side and found pdf.js. So, as lazy developers we just took pre-built 'pdfjs-dist' package and faced with bundler issue. For now we are on PoC phase, so as a solution we just downgraded 'pdfjs-dist' to V3. For the production looks like we should go with custom build solution. Thank you for the suggestion! |
@yuri-apanasik What do you mean "on the client side"? You used pdf.js in browser or node environment? If you use it in browser, all you need is comment out two lines code, and run gulp task You can also change the two files to flatten and replace the Line 305 in 833d7ac
// line 305, 306
// libraryAlias["display-node_stream"] = "src/display/node_stream.js";
// libraryAlias["display-node_utils"] = "src/display/node_utils.js"; |
@Priestch I mean in browser, yes. Thank you one more time for the hint! We'll try to make our own pdfjs build. But as you can see it is not only my problem, a lot of developers who want to easily use pre-built package face with this problem. One of the minimal ways to reproduce is:
Result -> build failed. As you suggested situation can be fixed with custom build, but it makes 'pdfjs' minimal usage example more complicated. |
@Priestch If only in a browser environment, I'd like to know what other code could be annotated and whether this could significantly reduce the size of the build artifact |
@jiadesen it's just quick hack to fix the issue, I think it doesn't reduce the build artifact. |
This seems like a bug in angular, if a default angular app crashes when using a stable JavaScript feature. It's like saying "please don't use classes because Angular has a bug and doesn't support them". |
I don't agree this is an issue with Angular. The v3 works for us for now but we had some badly rendered pdfs lately and want an upgrade. The only v3 that worked so far was 3.8.162 due to svg typings issues. We only use basic rendering so never dug deep enough to understand this library. Both builders in angular produce some errors, the common one being this top-level-await. While I understand Angulars position (zone.js etc), I don't know pdf.js enough to understand forcing incompatible builds. Moreover - there is this legacy build available yet somehow it can't be imported - when building the app it throws not found errors. We are running out of ideas how to workaround the workaround. |
@MikeDabrowski if you are using default Angular configuration with Webpack, you can customize it by installing '@angular-builders/custom-webpack' NPM package and changing '@angular-devkit/build-angular' to '@angular-builders/custom-webpack' in angular.json (some more adjustments should be done, just google it). When you can create the following custom webpack config to avoid top-level-await error: At the same time @Priestch already suggested the solution with custom 'pdfjs' build. @nicolo-ribaudo yes, it is NOT angular bug. It is about bundlers, 'esbuild' for example have no intention to support top-level-await at all (evanw/esbuild#253). So, 'pdfjs' in its full version (without commenting out some unnecessary code in case of usage in browser) can't be used with 'esbuild'. 'webpack'/'vite' need custom configuration. So, trying to make an improvement in V4 with es modules (if I got it right) it appeared that developers who used V3 easily now have to spent hours to understand what is wrong with their build using V4... |
@yuri-apanasik - yep, im just doing it - but v4 changes the location of workers and my app needed them I think. So trying to somehow work that out.
I wouldnt mind that custom build, we could host it. But I dont want to waste time reading how to do it. As you said in one of your comments its like in the 90s -here is kernel, now code build yourself an OS to write play asteroids or sth... Aint nobody got time for that. Any chance someone posts how to prep whole build? For reference our usage is for example: - I am not even sure if we need the workers
|
@Priestch Just for information... I've tried to make custom pdfjs build, there were some troubles: for example, 'node-canvas' have no arm64 build, so had to solve this issue for M1. Finally pdfjs was ok, but unfortunately when I copied result files into the project and build (actually build was also successful) I faced with error in runtime (some class used before its declaration). |
Can we get this issue reopened ? This has not been resolved yet. @yuri-apanasik That is exactly what I was afraid of - more issues when making custom build. The reason we wanted to update to v4 is that we had one incident with pdf badly rendered - some border was misplaced. Could not reproduce it unfortunately, so now we are waiting and keeping an eye for such errors. |
@Snuffleupagus My company is also interested. I emailed you a few days ago but I'm not sure if it went through. Hopefully it didn't get stuck in a spam folder. |
@JHarrisGTI Thanks, I've replied to the email so let's continue there :-) |
in reactjs using pdfjs-dist (npm) v4.2.67 and vite: install:
update vite.config.ts:
import:
my use:
im my case i only need a thumbnail of pdf... |
@JHarrisGTI The PR landed earlier today, and I've tried to reach you (and your colleague) by email; did it get stuck in a spam filter perhaps? |
@Snuffleupagus I got your email about the PR yesterday; I believe Adam has emailed you about payment details. I also reached out to our IT department and they say you shouldn't get stuck in the spam filter again. 🙂 |
@JHarrisGTI could you throw some light about how to use PDFJS v4.3.136 with Angular? |
I've been able to make it work but I have to set the worker in an external cdn or I have to copy/paste the worker file to
|
@Elecash Here are some notes I've pulled together from my git commit history over the last few days. Switch to the legacy buildSince 4.1.392, pdf.js uses Promise.withResolvers; I haven't been able to get that working with any of Angular's build systems. So to get pdf.js 4.3.136 working in Angular, the main thing I've needed to do has been to switch to the legacy build. To do this, replace: import { whatever } from 'pdfjs-dist'` with: import { whatever } from 'pdfjs-dist/legacy/build/pdf.mjs' That's fixed most of my problems. The builder I'm using is Worker threadsIn cases where I need the performance of a separate worker thread, I add this to the {
"glob": "pdf.worker.min.mjs",
"input": "./node_modules/pdfjs-dist/legacy/build",
"output": "./assets"
} This copies pdf.worker.min.mjs from the node_modules folder into the assets folder of my compiled app. Then, in the place I need the worker: import * as pdfJsLib from 'pdfjs-dist/legacy/build/pdf.mjs';
pdfJsLib.GlobalWorkerOptions.workerSrc = 'assets/pdf.worker.min.mjs';
const pdf = await pdfJsLib.getDocument({ url }).promise; Text layersThe renderTextLayer function is deprecated now. Replace: renderTextLayer({ textContentSource, viewport, container }); with: new TextLayer({ textContentSource, viewport, container}).render(); And if you're using this library's CSS, the new version sets the Testing with JestI test my app with Jest. Things I did this patch:
|
@JHarrisGTI this is very similar to what I did, but thanks for sharing! |
First of all, as a general rule, please always provide log output, error messages, and code as text since it's impossible to e.g. copy and search in an image which makes things more cumbersome. @MikeDabrowski If we attempt to work-around your issue, in the way the error message suggests, are you willing to sponsor me for writing the patch? |
@Snuffleupagus I meant 'we' as we the end users of this library. Sorry for the screenshot, here's the text
Are you certain that this warning was not caused by the work that was recently carried out here to enable the library in latest Angular? Maybe I am the only one affected and maybe only my setup is broken. |
Same problem, Esbuild of angular doesn't support dynamic-import of webpack |
I see the same problem @MikeDabrowski reports when I try to use Unrelated to Angular, to make the new version work in my NestJS backend, here's what I changed in tsconfig.json: {
"compilerOptions": {
"module": "Node16",
"moduleResolution": "Node16",
}
} |
this worked for me, using angular@18 & pdfjs-dist@4 package.json
PDF Render Component
thanks to JHarrisGTI for pointing out how to use the legacy import. |
I did that too, my deployed assets contain the worker file but the src like that cant use it.
Have you seen such error? Even if I put the full url to this file - the error is |
@MikeDabrowski I have not seen such an error. |
That's ok. I used cdn url and that worked. It broke in playwright tests so
for that I used the assets url.
This seems to be angular related issue.
…On Wed, 10 Jul 2024, 00:03 JHarrisGTI, ***@***.***> wrote:
@MikeDabrowski <https://github.com/MikeDabrowski> I have not seen such an
error.
|
This worked for me, using angular@18.1.3 & pdfjs-dist@4.5.136 11:21:03 [vite] warning:
.../.angular/cache/18.1.3/vite/deps/pdfjs-dist.js
11783| const worker = yield import(
11784| /*webpackIgnore: true*/
11785| this.workerSrc
| ^^^^^^^^^^^^^^
11786| );
11787| return worker.WorkerMessageHandler;
The above dynamic import cannot be analyzed by Vite.
See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.
Plugin: vite:import-analysis
File: .../.angular/cache/18.1.3/vite/deps/pdfjs-dist.js?v=0c2fc72a but all work fine import {getDocument, GlobalWorkerOptions, PDFPageProxy} from "pdfjs-dist";
private async tryToGetPdf(pdfFile: File) {
GlobalWorkerOptions.workerSrc = 'assets/pdf-worker/build/pdf.worker.min.mjs';
const arrayBuffer = await pdfFile.arrayBuffer();
const loadingTask = getDocument(arrayBuffer);
....
} |
I still face the excact same issues as described above and in many other threads. My API version and worker version doesnt match, but whenver i would upgrade or downgrade either the worker or the pdfjs-dist then it is still not resolved. I have tried to follow along on multiple suggestions and tried countless versions but I still cant seem to get it to work. This is something that I am trying to do in React, NextJs and TypeScript. Is there a posibility to get someone to help or perhaps pairprogram it with me since i feel like it is a lost cause...? |
Attach (recommended) or Link to PDF file here: any
Configuration:
The text was updated successfully, but these errors were encountered: