From 5b50e0d414623371449e514a50592c02b3e0c18a Mon Sep 17 00:00:00 2001
From: Yury Delendik <ydelendik@mozilla.com>
Date: Thu, 9 Feb 2017 09:53:52 -0600
Subject: [PATCH] Replaces RequireJS to SystemJS.

---
 .eslintrc                      |  2 +-
 examples/helloworld/hello.js   |  8 +++---
 examples/helloworld/index.html |  3 ++-
 examples/svgviewer/index.html  |  3 ++-
 examples/svgviewer/viewer.js   |  9 ++++---
 package.json                   |  2 +-
 src/worker_loader.js           | 27 ++++++++++----------
 systemjs.config.js             | 45 ++++++++++++++++++++++++++++++++++
 test/font/font_test.html       |  3 ++-
 test/font/jasmine-boot.js      | 10 +++++---
 test/test_slave.html           | 16 ++++++------
 test/unit/jasmine-boot.js      | 38 ++++++++++++----------------
 test/unit/unit_test.html       |  3 ++-
 web/viewer.html                |  3 ++-
 web/viewer.js                  | 10 +++++---
 15 files changed, 118 insertions(+), 64 deletions(-)
 create mode 100644 systemjs.config.js

diff --git a/.eslintrc b/.eslintrc
index 6187b253d..d034a3ce6 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -12,8 +12,8 @@
 
   globals: {
     "PDFJSDev": false,
-    "require": false,
     "exports": false,
+    "SystemJS": false,
   },
 
   "rules": {
diff --git a/examples/helloworld/hello.js b/examples/helloworld/hello.js
index 3571b59a6..b4421b6d0 100644
--- a/examples/helloworld/hello.js
+++ b/examples/helloworld/hello.js
@@ -1,8 +1,10 @@
 'use strict';
 
-// In production, the bundled pdf.js shall be used instead of RequireJS.
-require.config({paths: {'pdfjs': '../../src'}});
-require(['pdfjs/display/api', 'pdfjs/display/global'], function (api, global) {
+// In production, the bundled pdf.js shall be used instead of SystemJS.
+Promise.all([SystemJS.import('pdfjs/display/api'),
+             SystemJS.import('pdfjs/display/global')])
+       .then(function (modules) {
+  var api = modules[0], global = modules[1];
   // In production, change this to point to the built `pdf.worker.js` file.
   global.PDFJS.workerSrc = '../../src/worker_loader.js';
 
diff --git a/examples/helloworld/index.html b/examples/helloworld/index.html
index 4b44fd21c..e27316eac 100644
--- a/examples/helloworld/index.html
+++ b/examples/helloworld/index.html
@@ -2,7 +2,8 @@
 <html>
 
 <head>
-  <script src="../../node_modules/requirejs/require.js"></script>
+  <script src="../../node_modules/systemjs/dist/system.js"></script>
+  <script src="../../systemjs.config.js"></script>
   <script src="hello.js"></script>
 </head>
 
diff --git a/examples/svgviewer/index.html b/examples/svgviewer/index.html
index 70bf357ed..2b19c29f7 100644
--- a/examples/svgviewer/index.html
+++ b/examples/svgviewer/index.html
@@ -6,7 +6,8 @@
 
   <title>PDF.js SVG viewer example</title>
 
-  <script src="../../node_modules/requirejs/require.js"></script>
+  <script src="../../node_modules/systemjs/dist/system.js"></script>
+  <script src="../../systemjs.config.js"></script>
   <script src="viewer.js"></script>
 
   <style>
diff --git a/examples/svgviewer/viewer.js b/examples/svgviewer/viewer.js
index 7d330419e..3a5a831b6 100644
--- a/examples/svgviewer/viewer.js
+++ b/examples/svgviewer/viewer.js
@@ -36,10 +36,11 @@ function renderDocument(pdf, svgLib) {
   }
 }
 
-// In production, the bundled pdf.js shall be used instead of RequireJS.
-require.config({paths: {'pdfjs': '../../src'}});
-require(['pdfjs/display/api', 'pdfjs/display/svg', 'pdfjs/display/global'],
-    function (api, svg, global) {
+Promise.all([SystemJS.import('pdfjs/display/api'),
+             SystemJS.import('pdfjs/display/svg'),
+             SystemJS.import('pdfjs/display/global')])
+       .then(function (modules) {
+  var api = modules[0], svg = modules[1], global = modules[2];
   // In production, change this to point to the built `pdf.worker.js` file.
   global.PDFJS.workerSrc = '../../src/worker_loader.js';
 
diff --git a/package.json b/package.json
index e91aaefd4..bc6a6f932 100644
--- a/package.json
+++ b/package.json
@@ -16,11 +16,11 @@
     "merge-stream": "^1.0.1",
     "mkdirp": "^0.5.1",
     "node-ensure": "^0.0.0",
-    "requirejs": "^2.1.22",
     "rimraf": "^2.4.1",
     "run-sequence": "^1.2.2",
     "shelljs": "~0.4.0",
     "streamqueue": "^1.1.1",
+    "systemjs": "^0.20.7",
     "typogr": "~0.6.5",
     "uglify-js": "^2.6.1",
     "webpack": "^2.2.1",
diff --git a/src/worker_loader.js b/src/worker_loader.js
index 05a5c3685..6745465a7 100644
--- a/src/worker_loader.js
+++ b/src/worker_loader.js
@@ -15,21 +15,20 @@
 
 'use strict';
 
-if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
-  // Patch importScripts to work around a bug in WebKit and Chrome 48-.
-  // See https://crbug.com/572225 and https://webkit.org/b/153317.
-  self.importScripts = (function (importScripts) {
-    return function() {
-      setTimeout(function () {}, 0);
-      return importScripts.apply(this, arguments);
-    };
-  })(importScripts);
-}
+// Patch importScripts to work around a bug in WebKit and Chrome 48-.
+// See https://crbug.com/572225 and https://webkit.org/b/153317.
+self.importScripts = (function (importScripts) {
+  return function() {
+    setTimeout(function () {}, 0);
+    return importScripts.apply(this, arguments);
+  };
+})(importScripts);
 
-importScripts('../node_modules/requirejs/require.js');
+importScripts('./shared/compatibility.js');
+importScripts('../node_modules/systemjs/dist/system.js');
+importScripts('../systemjs.config.js');
 
-require.config({paths: {'pdfjs': '.'}});
-require(['pdfjs/core/network', 'pdfjs/core/worker'],
-    function (network, worker) {
+Promise.all([SystemJS.import('pdfjs/core/network'),
+             SystemJS.import('pdfjs/core/worker')]).then(function () {
   // Worker is loaded at this point.
 });
diff --git a/systemjs.config.js b/systemjs.config.js
new file mode 100644
index 000000000..83a128ac1
--- /dev/null
+++ b/systemjs.config.js
@@ -0,0 +1,45 @@
+/* Copyright 2017 Mozilla Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+'use strict';
+
+(function () {
+  var baseLocation;
+  if (typeof document !== 'undefined') {
+    baseLocation = new URL('./', document.currentScript.src);
+  } else if (typeof location !== 'undefined') {
+    // Probably worker -- walking subfolders until we will reach root.
+    baseLocation = location;
+    while (baseLocation.href.includes('/src/')) {
+      baseLocation = new URL('..', baseLocation);
+    }
+  } else {
+    throw new Error('Cannot configure SystemJS');
+  }
+
+  SystemJS.config({
+    packages: {
+      '': {
+        format: 'amd',
+        defaultExtension: 'js',
+      }
+    },
+    paths: {
+      'pdfjs': new URL('src', baseLocation).href,
+      'pdfjs-web': new URL('web', baseLocation).href,
+      'pdfjs-test': new URL('test', baseLocation).href,
+    }
+  });
+})();
diff --git a/test/font/font_test.html b/test/font/font_test.html
index 86f6645cd..611d3adbd 100644
--- a/test/font/font_test.html
+++ b/test/font/font_test.html
@@ -5,7 +5,8 @@
 
   <link rel="stylesheet" type="text/css" href="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
 
-  <script src="../../node_modules/requirejs/require.js"></script>
+  <script src="../../node_modules/systemjs/dist/system.js"></script>
+  <script src="../../systemjs.config.js"></script>
   <script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
   <script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
   <script src="../unit/testreporter.js"></script>
diff --git a/test/font/jasmine-boot.js b/test/font/jasmine-boot.js
index 3d45ab30a..50a0e8f41 100644
--- a/test/font/jasmine-boot.js
+++ b/test/font/jasmine-boot.js
@@ -41,9 +41,13 @@
 'use strict';
 
 function initializePDFJS(callback) {
-  require.config({paths: {'pdfjs': '../../src'}});
-  require(['pdfjs/core/fonts', 'pdfjs/core/stream', 'pdfjs/core/primitives',
-    'pdfjs/core/cmap'], function (fonts, stream, primitives, cmap) {
+  Promise.all([SystemJS.import('pdfjs/core/fonts'),
+               SystemJS.import('pdfjs/core/stream'),
+               SystemJS.import('pdfjs/core/primitives'),
+               SystemJS.import('pdfjs/core/cmap')])
+         .then(function (modules) {
+    var fonts = modules[0], stream = modules[1],
+        primitives = modules[2], cmap = modules[3];
     // Expose some of the PDFJS members to global scope for tests.
     window.Font = fonts.Font;
     window.ToUnicodeMap = fonts.ToUnicodeMap;
diff --git a/test/test_slave.html b/test/test_slave.html
index 7fa8fb5bb..c40bdabf5 100644
--- a/test/test_slave.html
+++ b/test/test_slave.html
@@ -18,7 +18,8 @@ limitations under the License.
   <head>
     <title>PDF.js test slave</title>
     <meta charset="utf-8">
-    <script src="../node_modules/requirejs/require.js"></script>
+    <script src="../node_modules/systemjs/dist/system.js"></script>
+    <script src="../systemjs.config.js"></script>
     <script src="driver.js"></script>
   </head>
   <body>
@@ -31,12 +32,13 @@ limitations under the License.
     <div id="end"></div>
   </body>
   <script>
-    require.config({paths: {'pdfjs': '../src'}});
-    require(['pdfjs/display/api', 'pdfjs/display/text_layer',
-             'pdfjs/display/annotation_layer', 'pdfjs/display/global',
-             'pdfjs/shared/util'],
-        function (api, textLayer, annotationLayer, global, pdfjsSharedUtil) {
-      window.pdfjsSharedUtil = pdfjsSharedUtil;
+    Promise.all([SystemJS.import('pdfjs/display/api'),
+                 SystemJS.import('pdfjs/display/text_layer'),
+                 SystemJS.import('pdfjs/display/annotation_layer'),
+                 SystemJS.import('pdfjs/display/global'),
+                 SystemJS.import('pdfjs/shared/util')])
+          .then(function (modules) {
+      window.pdfjsSharedUtil = modules[4];
 
       var driver = new Driver({
         disableScrolling: document.getElementById('disableScrolling'),
diff --git a/test/unit/jasmine-boot.js b/test/unit/jasmine-boot.js
index 37b881f05..0ca0b9923 100644
--- a/test/unit/jasmine-boot.js
+++ b/test/unit/jasmine-boot.js
@@ -41,28 +41,22 @@
 'use strict';
 
 function initializePDFJS(callback) {
-  require.config({paths: {'pdfjs': '../../src', 'pdfjs-web': '../../web',
-                 'pdfjs-test': '..'}});
-  require(['pdfjs/display/global', 'pdfjs-test/unit/annotation_spec',
-           'pdfjs-test/unit/api_spec', 'pdfjs-test/unit/bidi_spec',
-           'pdfjs-test/unit/cff_parser_spec', 'pdfjs-test/unit/cmap_spec',
-           'pdfjs-test/unit/crypto_spec', 'pdfjs-test/unit/document_spec',
-           'pdfjs-test/unit/dom_utils_spec', 'pdfjs-test/unit/evaluator_spec',
-           'pdfjs-test/unit/fonts_spec', 'pdfjs-test/unit/function_spec',
-           'pdfjs-test/unit/metadata_spec', 'pdfjs-test/unit/murmurhash3_spec',
-           'pdfjs-test/unit/network_spec', 'pdfjs-test/unit/parser_spec',
-           'pdfjs-test/unit/primitives_spec', 'pdfjs-test/unit/stream_spec',
-           'pdfjs-test/unit/type1_parser_spec',
-           'pdfjs-test/unit/ui_utils_spec', 'pdfjs-test/unit/unicode_spec',
-           'pdfjs-test/unit/util_spec'],
-    function (displayGlobal, testUnitAnnotationSpec, testUnitApiSpec,
-              testUnitBidiSpec, testUnitCFFParserSpec, testUnitCMapSpec,
-              testUnitCryptoSpec, testUnitDocumentSpec, testUnitDOMUtilsSpec,
-              testUnitEvaluatorSpec, testUnitFontsSpec, testUnitFunctionSpec,
-              testUnitMetadataSpec, testUnitMurmurHash3Spec,
-              testUnitNetworkSpec, testUnitParserSpec, testUnitPrimitivesSpec,
-              testUnitStreamSpec, testUnitType1ParserSpec, testUnitUiUtilsSpec,
-              testUnitUnicodeSpec, testUnitUtilSpec) {
+    Promise.all([
+      'pdfjs/display/global', 'pdfjs-test/unit/annotation_spec',
+      'pdfjs-test/unit/api_spec', 'pdfjs-test/unit/bidi_spec',
+      'pdfjs-test/unit/cff_parser_spec', 'pdfjs-test/unit/cmap_spec',
+      'pdfjs-test/unit/crypto_spec', 'pdfjs-test/unit/document_spec',
+      'pdfjs-test/unit/dom_utils_spec', 'pdfjs-test/unit/evaluator_spec',
+      'pdfjs-test/unit/fonts_spec', 'pdfjs-test/unit/function_spec',
+      'pdfjs-test/unit/metadata_spec', 'pdfjs-test/unit/murmurhash3_spec',
+      'pdfjs-test/unit/network_spec', 'pdfjs-test/unit/parser_spec',
+      'pdfjs-test/unit/primitives_spec', 'pdfjs-test/unit/stream_spec',
+      'pdfjs-test/unit/type1_parser_spec', 'pdfjs-test/unit/ui_utils_spec',
+      'pdfjs-test/unit/unicode_spec', 'pdfjs-test/unit/util_spec'
+    ].map(function (moduleName) {
+      return SystemJS.import(moduleName);
+    })).then(function (modules) {
+      var displayGlobal = modules[0];
 
       // Configure the worker.
       displayGlobal.PDFJS.workerSrc = '../../src/worker_loader.js';
diff --git a/test/unit/unit_test.html b/test/unit/unit_test.html
index 09816cf1d..d9528e816 100644
--- a/test/unit/unit_test.html
+++ b/test/unit/unit_test.html
@@ -5,7 +5,8 @@
 
   <link rel="stylesheet" type="text/css" href="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
 
-  <script src="../../node_modules/requirejs/require.js"></script>
+  <script src="../../node_modules/systemjs/dist/system.js"></script>
+  <script src="../../systemjs.config.js"></script>
   <script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
   <script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
   <script src="testreporter.js"></script>
diff --git a/web/viewer.html b/web/viewer.html
index 876ebb0d5..26af8f447 100644
--- a/web/viewer.html
+++ b/web/viewer.html
@@ -53,7 +53,8 @@ See https://github.com/adobe-type-tools/cmap-resources
 <!--#endif-->
 
 <!--#if !PRODUCTION-->
-    <script src="../node_modules/requirejs/require.js"></script>
+    <script src="../node_modules/systemjs/dist/system.js"></script>
+    <script src="../systemjs.config.js"></script>
 <!--#endif-->
 
 <!--#if (GENERIC && !MINIFIED) -->
diff --git a/web/viewer.js b/web/viewer.js
index 58e0e289b..f9e3bbe41 100644
--- a/web/viewer.js
+++ b/web/viewer.js
@@ -171,10 +171,12 @@ function getViewerConfiguration() {
 function webViewerLoad() {
   var config = getViewerConfiguration();
   if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
-    require.config({paths: {'pdfjs': '../src', 'pdfjs-web': '.'}});
-    require(['pdfjs-web/app', 'pdfjs-web/pdf_print_service'], function (web) {
-      window.PDFViewerApplication = web.PDFViewerApplication;
-      web.PDFViewerApplication.run(config);
+    Promise.all([SystemJS.import('pdfjs-web/app'),
+                 SystemJS.import('pdfjs-web/pdf_print_service')])
+           .then(function (modules) {
+      var app = modules[0];
+      window.PDFViewerApplication = app.PDFViewerApplication;
+      app.PDFViewerApplication.run(config);
     });
   } else {
     window.PDFViewerApplication = pdfjsWebApp.PDFViewerApplication;