Skip to content

Commit

Permalink
Reduce usage of SystemJS, in the development viewer, even further
Browse files Browse the repository at this point in the history
With these changes SystemJS is now only used, during development, on the worker-thread and in the unit/font-tests, since Firefox is currently missing support for worker modules; please see https://bugzilla.mozilla.org/show_bug.cgi?id=1247687

Hence all the JavaScript files in the `web/` and `src/display/` folders are now loaded *natively* by the browser (during development) using standard `import` statements/calls, thanks to a nice `import-maps` polyfill.

*Please note:* As soon as https://bugzilla.mozilla.org/show_bug.cgi?id=1247687 is fixed in Firefox, we should be able to remove all traces of SystemJS and thus finally be able to use every possible modern JavaScript feature.
  • Loading branch information
Snuffleupagus committed May 20, 2020
1 parent 3f7a99e commit 44ed933
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 15 deletions.
5 changes: 2 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"parserOptions": {
"ecmaVersion": 8,
"ecmaVersion": 2020,
"sourceType": "module",
},

Expand All @@ -17,13 +17,12 @@

"env": {
"browser": true,
"es6": true,
"es2020": true,
"worker": true,
"amd": true,
},

"globals": {
"globalThis": false,
"PDFJSDev": false,
"exports": false,
"SystemJS": false,
Expand Down
2 changes: 1 addition & 1 deletion external/builder/preprocessor2.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function preprocessPDFJSCode(ctx, code) {
},
};
var parseOptions = {
ecmaVersion: 8,
ecmaVersion: 2020,
locations: true,
sourceFile: ctx.sourceFile,
sourceType: "module",
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"eslint-plugin-no-unsanitized": "^3.1.1",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-unicorn": "^20.0.0",
"es-module-shims": "^0.4.6",
"fancy-log": "^1.3.3",
"globals": "^11.12.0",
"gulp": "^4.0.2",
Expand Down
13 changes: 13 additions & 0 deletions src/core/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"parserOptions": {
"ecmaVersion": 2017,
},

"extends": [
"../../.eslintrc"
],

"env": {
"es2017": true,
},
}
4 changes: 3 additions & 1 deletion src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,9 @@ const PDFWorker = (function PDFWorkerClosure() {
}
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
if (typeof SystemJS !== "object") {
throw new Error("SystemJS must be used to load fake worker.");
// Manually load SystemJS, since it's only necessary for fake workers.
await loadScript("../node_modules/systemjs/dist/system.js");
await loadScript("../systemjs.config.js");
}
const worker = await SystemJS.import("pdfjs/core/worker.js");
return worker.WorkerMessageHandler;
Expand Down
4 changes: 2 additions & 2 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ const pdfjsBuild =

if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
const streamsPromise = Promise.all([
SystemJS.import("pdfjs/display/network.js"),
SystemJS.import("pdfjs/display/fetch_stream.js"),
import("pdfjs/display/network.js"),
import("pdfjs/display/fetch_stream.js"),
]);
setPDFNetworkStreamFactory(params => {
return streamsPromise.then(streams => {
Expand Down
4 changes: 3 additions & 1 deletion web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,9 @@ async function loadFakeWorker() {
}
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
if (typeof SystemJS !== "object") {
throw new Error("SystemJS must be used to load fake worker.");
// Manually load SystemJS, since it's only necessary for fake workers.
await loadScript("../node_modules/systemjs/dist/system.js");
await loadScript("../systemjs.config.js");
}
window.pdfjsWorker = await SystemJS.import("pdfjs/core/worker.js");
return undefined;
Expand Down
16 changes: 13 additions & 3 deletions web/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,26 @@
<!--#endif-->

<!--#if !PRODUCTION-->
<script src="../node_modules/systemjs/dist/system.js"></script>
<script src="../systemjs.config.js"></script>
<script defer src="../node_modules/es-module-shims/dist/es-module-shims.js"></script>
<script type="importmap-shim">
{
"imports": {
"pdfjs/": "../src/",
"pdfjs-lib": "../src/pdf.js",
"pdfjs-web/": "./"
}
}
</script>

<script src="viewer.js" type="module-shim"></script>
<!--#endif-->

<!--#if (GENERIC && !MINIFIED) -->
<!--#include viewer-snippet.html-->
<!--#endif-->

<!--#if !MINIFIED -->
<script src="viewer.js"></script>
<!--<script src="viewer.js"></script>-->
<!--#else-->
<!--#include viewer-snippet-minified.html-->
<!--#endif-->
Expand Down
8 changes: 4 additions & 4 deletions web/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ function webViewerLoad() {
const config = getViewerConfiguration();
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
Promise.all([
SystemJS.import("pdfjs-web/app.js"),
SystemJS.import("pdfjs-web/app_options.js"),
SystemJS.import("pdfjs-web/genericcom.js"),
SystemJS.import("pdfjs-web/pdf_print_service.js"),
import("pdfjs-web/app.js"),
import("pdfjs-web/app_options.js"),
import("pdfjs-web/genericcom.js"),
import("pdfjs-web/pdf_print_service.js"),
]).then(function ([app, appOptions, ...otherModules]) {
window.PDFViewerApplication = app.PDFViewerApplication;
window.PDFViewerApplicationOptions = appOptions.AppOptions;
Expand Down

0 comments on commit 44ed933

Please sign in to comment.