pdfjs icon indicating copy to clipboard operation
pdfjs copied to clipboard

Convert to es6 modules

Open rkusa opened this issue 6 years ago β€’ 12 comments

Before leaving alpha, the library should be converted to using es6 modules.

rkusa avatar Apr 16 '18 06:04 rkusa

Anyone aware of a tool to automatically convert a library to es6 modules?

rkusa avatar May 03 '18 07:05 rkusa

While I did an initial conversion, I am not yet sure how to handle the fact that pdfjs should work for both server and client. Since Node.js requires the file extension of .mjs

rkusa avatar May 16 '18 07:05 rkusa

Hello have a look here https://github.com/nolanlawson/cjs-to-es6

Other ideas on stackoverflow https://stackoverflow.com/questions/43106130/convert-existing-legacy-library-to-es6-module

jpbourgeon avatar May 26 '18 10:05 jpbourgeon

Hi @jpbourgeon, thanks for the links. I have already converted the library in a local branch, but was not able to find a good es6 modules solution for libraries that target both server (Node.js) and client πŸ˜•

rkusa avatar May 28 '18 06:05 rkusa

Would it help to split the library into two components and take advantage of npmjs namespaces?

Something like @pdfjs/server and @pdfjs/browser

jpbourgeon avatar May 28 '18 06:05 jpbourgeon

I guess there are the following options.

1) Write client-side ES modules (.js)

  • βœ… does work with babel
  • βœ… named imports work (e.g. import {Document} from "pdfjs"
  • ❌ does not work with Node.js ES modules (since .mjs extension required)
  • ❌ does not work with require
  • ➑️ the code has to be transpiled back to common-js and additionally included in the module

2) Write server-side ES modules (.mjs)

  • βœ… does work with babel
  • βœ… named imports work (e.g. import {Document} from "pdfjs"
  • βœ… does work with Node.js ES modules
  • ❌ does not work with require
  • ➑️ the code still has to be transpiled back to CJS modules to get require to work

3) Keep CJS modules

  • βœ… does work with babel
  • ❌ named imports do not work (e.g. import {Document} from "pdfjs")
  • βœ… can be loaded from Node.js ES modules
  • βœ… does work with require
  • ➑️ only drawback seems to be that we have to write import * as pdf from "pdfjs" when loading pdfjs from ES modules (instead of being able to import named exports import {Document} from "pdfjs")

In Summary, either transpile the code for Node.js usage or live with import {Document} from "pdfjs". TBH, I really dislike both options πŸ˜•

rkusa avatar May 29 '18 07:05 rkusa

The conversion happens here https://github.com/rkusa/pdfjs/tree/es6-modules Though, the branch is unfortunately not green yet

rkusa avatar Feb 08 '19 15:02 rkusa

Would it be of interest to move to TS instead of ES6?

AllNamesRTaken avatar May 27 '19 09:05 AllNamesRTaken

Would it be of interest to move to TS instead of ES6?

I've recently had the pleasure to work on a TypeScript codebase [1] using WebStorm [2]Β and I want to report that the overall experience is absolutely amazing and totally washed away any hesitations I had regarding TypeScript. Finally, I feel like I can get similar productive with JavaScript as I am with Python. ES6 did not give me that feeling yet.

[1] https://github.com/hiveeyes/grafana-worldmap-panel [2] https://www.jetbrains.com/webstorm/


@rkusa: Amazing project, greetings from Berlin and keep up the spirit!

amotl avatar May 27 '19 12:05 amotl

My pet project is https://github.com/AllNamesRTaken/GoodCore outside workhours and i could help out is needed.

AllNamesRTaken avatar May 27 '19 18:05 AllNamesRTaken

@AllNamesRTaken: Sounds interesting, thanks for letting us know. Star-given ;].

amotl avatar May 27 '19 18:05 amotl

Would it be of interest to move to TS instead of ES6?

I am, generally speaking, not opposed to TS.

I've recently had the pleasure to work on a TypeScript codebase [1] using WebStorm [2] and I want to report that the overall experience is absolutely amazing and totally washed away any hesitations I had regarding TypeScript.

I can confirm that. In my day job, I also work more with TS than JS and the whole TS dev experience (editor integration, less errors when refactoring, ...) is superior than JS ...


What currently holds me off from converting the codebase to TS:

  • I always disliked Coffeescript projects (example works for all other languages that compile to JS), because of the higher entry barrier for submitting a contribution - I am a bit concerned that it might be similar for people when it comes to TS ... though, I think the advantages of TS a greater than my concerns here
  • The effort required to convert it
  • I am not sure if I'd rather rewrite the lib in something that compiles to wasm (something that would be cooler for my personal use-case of the lib)

rkusa avatar May 29 '19 06:05 rkusa