From 1838ec0427d975f04e3857e0b590a61fe462f105 Mon Sep 17 00:00:00 2001
From: Gregory Jordan <gjuggler@gmail.com>
Date: Thu, 19 Dec 2013 08:18:47 -0700
Subject: [PATCH] Add a singlefile target to build one concatenated file

---
 make.js            | 65 ++++++++++++++++++++++++++++++++++++++++------
 src/display/api.js |  8 +++++-
 2 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/make.js b/make.js
index 6b5b84985..bcc22f978 100644
--- a/make.js
+++ b/make.js
@@ -52,7 +52,8 @@ var DEFINES = {
   FIREFOX: false,
   MOZCENTRAL: false,
   B2G: false,
-  CHROME: false
+  CHROME: false,
+  SINGLE_FILE: false
 };
 
 //
@@ -273,23 +274,22 @@ target.bundle = function(args) {
   if (!test('-d', BUILD_DIR))
     mkdir(BUILD_DIR);
 
-  var MAIN_SRC_FILES = [
+  var SHARED_SRC_FILES = [
     'shared/util.js',
     'shared/colorspace.js',
     'shared/pattern.js',
     'shared/function.js',
     'shared/annotation.js',
+  ];
+
+  var MAIN_SRC_FILES = SHARED_SRC_FILES.concat([
     'display/api.js',
     'display/metadata.js',
     'display/canvas.js',
     'display/font_loader.js'
-  ];
+  ]);
 
   var WORKER_SRC_FILES = [
-    'shared/util.js',
-    'shared/pattern.js',
-    'shared/function.js',
-    'shared/annotation.js',
     'core/network.js',
     'core/chunked_stream.js',
     'core/pdf_manager.js',
@@ -297,7 +297,6 @@ target.bundle = function(args) {
     'core/obj.js',
     'core/charsets.js',
     'core/cidmaps.js',
-    'shared/colorspace.js',
     'core/crypto.js',
     'core/evaluator.js',
     'core/fonts.js',
@@ -314,6 +313,12 @@ target.bundle = function(args) {
     'core/cmap.js'
   ];
 
+  if (!defines.SINGLE_FILE) {
+    // We want shared_src_files in both pdf.js and pdf.worker.js
+    // unless it's being built in singlefile mode.
+    WORKER_SRC_FILES = SHARED_SRC_FILES.concat(WORKER_SRC_FILES);
+  }
+
   var EXT_SRC_FILES = [
     '../external/jpgjs/jpg.js'
   ];
@@ -328,6 +333,50 @@ target.bundle = function(args) {
   rm(srcCopy);
 };
 
+//
+// make singlefile
+// Concatenates pdf.js and pdf.worker.js into one big pdf.combined.js, and
+// flags the script loader to not attempt to load the separate worker JS file.
+//
+target.singlefile = function() {
+  cd(ROOT_DIR);
+  echo();
+  echo('### Creating singlefile build');
+
+  var SINGLE_FILE_DIR = BUILD_DIR + '/singlefile/';
+  var SINGLE_FILE_TARGET = BUILD_DIR + 'pdf.combined.js';
+
+  var defines = builder.merge(DEFINES, {SINGLE_FILE: true});
+  target.bundle({defines: defines});
+
+  cd(ROOT_DIR);
+
+  rm('-rf', SINGLE_FILE_DIR);
+  mkdir('-p', SINGLE_FILE_DIR);
+  mkdir('-p', SINGLE_FILE_DIR + BUILD_DIR);
+
+  var setup = {
+    defines: defines,
+    copy: [],
+    preprocess: [
+      [BUILD_TARGETS, SINGLE_FILE_DIR + BUILD_DIR]
+    ]
+  };
+  builder.build(setup);
+
+  cd(SINGLE_FILE_DIR);
+
+  echo();
+  echo('### Concatenating pdf.js and pdf.worker.js into pdf.combined.js');
+  var pdfJs = cat(BUILD_TARGET);
+  pdfJs += cat(BUILD_WORKER_TARGET);
+  pdfJs.to(SINGLE_FILE_TARGET);
+
+  rm(BUILD_TARGET);
+  rm(BUILD_WORKER_TARGET);
+
+};
+
 function cleanupJSSource(file) {
   var content = cat(file);
 
diff --git a/src/display/api.js b/src/display/api.js
index 2a2587e04..9804dfbfb 100644
--- a/src/display/api.js
+++ b/src/display/api.js
@@ -545,6 +545,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
     // all requirements to run parts of pdf.js in a web worker.
     // Right now, the requirement is, that an Uint8Array is still an Uint8Array
     // as it arrives on the worker. Chrome added this with version 15.
+//#if !SINGLE_FILE
     if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') {
       var workerSrc = PDFJS.workerSrc;
       if (!workerSrc) {
@@ -591,6 +592,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
         info('The worker has been disabled.');
       }
     }
+//#endif    
     // Either workers are disabled, not supported or have thrown an exception.
     // Thus, we fallback to a faked worker.
     globalScope.PDFJS.disableWorker = true;
@@ -619,7 +621,11 @@ var WorkerTransport = (function WorkerTransportClosure() {
         // pdf.worker.js file is needed.
 //#if !PRODUCTION
         Util.loadScript(PDFJS.workerSrc);
-//#else
+//#endif
+//#if PRODUCTION && SINGLE_FILE
+//      PDFJS.fakeWorkerFilesLoadedPromise.resolve();
+//#endif
+//#if PRODUCTION && !SINGLE_FILE
 //      Util.loadScript(PDFJS.workerSrc, function() {
 //        PDFJS.fakeWorkerFilesLoadedPromise.resolve();
 //      });