From cbbb9bb82de263c9ec25451b9d73af34e3e588bf Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Wed, 23 Dec 2015 17:46:08 -0600 Subject: [PATCH] Adds UMD header to pdf.js and pdf.worker.js files. --- external/umdutils/verifier.js | 3 +++ make.js | 20 +++++++++++--- src/display/api.js | 20 ++++++++++---- src/frameworks.js | 13 ++++++--- src/pdf.js | 51 +++++++++++++++++------------------ src/shared/global.js | 9 ++++++- 6 files changed, 77 insertions(+), 39 deletions(-) diff --git a/external/umdutils/verifier.js b/external/umdutils/verifier.js index 864739091..1e3fd5b48 100644 --- a/external/umdutils/verifier.js +++ b/external/umdutils/verifier.js @@ -55,6 +55,9 @@ var path = require('path'); */ function parseUmd(filePath) { var jscode = fs.readFileSync(filePath).toString(); + if (/\/\*\s*umdutils\s+ignore\s*\*\//.test(jscode)) { + throw new Error('UMD processing ignored'); + } // Extracts header and body. var umdStart = '\\(function\\s\\(root,\\sfactory\\)\\s\\{'; var umdImports = '\\}\\(this,\\sfunction\\s\\(exports\\b'; diff --git a/make.js b/make.js index c544ccadf..3423d0b6c 100644 --- a/make.js +++ b/make.js @@ -484,7 +484,7 @@ target.bundle = function(args) { echo(); echo('### Bundling files into ' + BUILD_TARGET); - function bundle(filename, outfilename, files) { + function bundle(filename, outfilename, files, distname) { var bundleContent = cat(files), bundleVersion = VERSION, bundleBuild = exec('git log --format="%h" -n 1', @@ -500,12 +500,18 @@ target.bundle = function(args) { // Removes AMD and CommonJS branches from UMD headers. bundleContent = stripUMDHeaders(bundleContent); + var amdName = 'pdfjs-dist/build/' + distname.replace(/\.js$/, ''); + var jsName = amdName.replace(/[\-_\.\/]\w/g, function (all) { + return all[1].toUpperCase(); + }); // This just preprocesses the empty pdf.js file, we don't actually want to // preprocess everything yet since other build targets use this file. builder.preprocess(filename, outfilename, builder.merge(defines, {BUNDLE: bundleContent, BUNDLE_VERSION: bundleVersion, - BUNDLE_BUILD: bundleBuild})); + BUNDLE_BUILD: bundleBuild, + BUNDLE_AMD_NAME: amdName, + BUNDLE_JS_NAME: jsName})); } if (!test('-d', BUILD_DIR)) { @@ -524,6 +530,9 @@ target.bundle = function(args) { SRC_DIR + 'core/worker.js' ]; + var mainFileName = 'pdf.js'; + var workerFileName = 'pdf.worker.js'; + // Extension does not need svg.js and network.js files. if (!defines.FIREFOX && !defines.MOZCENTRAL) { MAIN_SRC_FILES.push(SRC_DIR + 'display/svg.js'); @@ -535,6 +544,8 @@ target.bundle = function(args) { // the main pdf.js output. MAIN_SRC_FILES = MAIN_SRC_FILES.concat(WORKER_SRC_FILES); WORKER_SRC_FILES = null; // no need for worker file + mainFileName = 'pdf.combined.js'; + workerFileName = null; } // Reading UMD headers and building loading orders of modules. The @@ -549,12 +560,13 @@ target.bundle = function(args) { cd(SRC_DIR); - bundle('pdf.js', ROOT_DIR + BUILD_TARGET, mainFiles); + bundle('pdf.js', ROOT_DIR + BUILD_TARGET, mainFiles, mainFileName); if (workerFiles) { var srcCopy = ROOT_DIR + BUILD_DIR + 'pdf.worker.js.temp'; cp('pdf.js', srcCopy); - bundle(srcCopy, ROOT_DIR + BUILD_WORKER_TARGET, workerFiles); + bundle(srcCopy, ROOT_DIR + BUILD_WORKER_TARGET, workerFiles, + workerFileName); rm(srcCopy); } }; diff --git a/src/display/api.js b/src/display/api.js index bfa7b4040..6a01c50d3 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* globals pdfjsFilePath */ 'use strict'; @@ -1177,6 +1178,18 @@ var PDFPageProxy = (function PDFPageProxyClosure() { var PDFWorker = (function PDFWorkerClosure() { var nextFakeWorkerId = 0; + function getWorkerSrc() { + if (PDFJS.workerSrc) { + return PDFJS.workerSrc; + } +//#if PRODUCTION && !(MOZCENTRAL || FIREFOX) +// if (pdfjsFilePath) { +// return pdfjsFilePath.replace(/\.js$/i, '.worker.js'); +// } +//#endif + error('No PDFJS.workerSrc specified'); + } + // Loads worker code into main thread. function setupFakeWorkerGlobal() { if (!PDFJS.fakeWorkerFilesLoadedCapability) { @@ -1201,7 +1214,7 @@ var PDFWorker = (function PDFWorkerClosure() { //#endif //#if PRODUCTION && !SINGLE_FILE // var loader = fakeWorkerFilesLoader || function (callback) { -// Util.loadScript(PDFJS.workerSrc, callback); +// Util.loadScript(getWorkerSrc(), callback); // }; // loader(function () { // PDFJS.fakeWorkerFilesLoadedCapability.resolve(); @@ -1243,10 +1256,7 @@ var PDFWorker = (function PDFWorkerClosure() { // Uint8Array as it arrives on the worker. (Chrome added this with v.15.) //#if !SINGLE_FILE if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') { - var workerSrc = PDFJS.workerSrc; - if (!workerSrc) { - error('No PDFJS.workerSrc specified'); - } + var workerSrc = getWorkerSrc(); try { // Some versions of FF can't create a worker on localhost, see: diff --git a/src/frameworks.js b/src/frameworks.js index 33e34cee9..95b4d36da 100644 --- a/src/frameworks.js +++ b/src/frameworks.js @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* globals PDFJS, require, module */ +/* globals PDFJS, require, module, requirejs */ // included from api.js for GENERIC build @@ -32,9 +32,16 @@ if (typeof __webpack_require__ !== 'undefined') { PDFJS.workerSrc = require('entry?name=[hash]-worker.js!./pdf.worker.js'); useRequireEnsure = true; } -var fakeWorkerFilesLoader = useRequireEnsure && function (callback) { +if (typeof requirejs !== 'undefined' && requirejs.toUrl) { + PDFJS.workerSrc = requirejs.toUrl('pdfjs-dist/build/pdf.worker.js'); +} +var fakeWorkerFilesLoader = useRequireEnsure ? (function (callback) { require.ensure([], function () { require('./pdf.worker.js'); callback(); }); -}; +}) : (typeof requirejs !== 'undefined') ? (function (callback) { + requirejs(['pdfjs-dist/build/pdf.worker'], function (worker) { + callback(); + }); +}) : null; diff --git a/src/pdf.js b/src/pdf.js index e9e9b6c15..c5276e5ba 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -13,36 +13,35 @@ * limitations under the License. */ /* jshint globalstrict: false */ -/* globals PDFJS, global */ +/* umdutils ignore */ -// Initializing PDFJS global object (if still undefined) -if (typeof PDFJS === 'undefined') { - (typeof window !== 'undefined' ? window : - typeof global !== 'undefined' ? global : this).PDFJS = {}; -} - -//#if BUNDLE_VERSION -//#expand PDFJS.version = '__BUNDLE_VERSION__'; -//#endif -//#if BUNDLE_BUILD -//#expand PDFJS.build = '__BUNDLE_BUILD__'; -//#endif - -(function pdfjsWrapper() { +(function (root, factory) { + 'use strict'; + if (typeof define === 'function' && define.amd) { +//#expand define('__BUNDLE_AMD_NAME__', ['exports'], factory); + } else if (typeof exports !== 'undefined') { + factory(exports); + } else { +//#expand factory((root.__BUNDLE_JS_NAME__ = {})); + } +}(this, function (exports) { // Use strict in our context only - users might not want it 'use strict'; +//#expand var pdfjsVersion = '__BUNDLE_VERSION__'; +//#expand var pdfjsBuild = '__BUNDLE_BUILD__'; + + var pdfjsFilePath = + typeof document !== 'undefined' && document.currentScript ? + document.currentScript.src : null; + + var pdfjsLibs = {}; + + (function pdfjsWrapper() { + //#expand __BUNDLE__ -}).call((typeof window === 'undefined') ? this : window); + }).call(pdfjsLibs); -//#if !(MOZCENTRAL || FIREFOX) -if (!PDFJS.workerSrc && typeof document !== 'undefined') { - // workerSrc is not set -- using last script url to define default location - PDFJS.workerSrc = (function () { - 'use strict'; - var pdfJsSrc = document.currentScript.src; - return pdfJsSrc && pdfJsSrc.replace(/\.js$/i, '.worker.js'); - })(); -} -//#endif + exports.PDFJS = pdfjsLibs.pdfjsSharedGlobal.PDFJS; +})); diff --git a/src/shared/global.js b/src/shared/global.js index 219afe324..94b8ef9fe 100644 --- a/src/shared/global.js +++ b/src/shared/global.js @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* globals global */ +/* globals global, pdfjsVersion, pdfjsBuild */ 'use strict'; @@ -39,6 +39,13 @@ globalScope.PDFJS = {}; } + if (typeof pdfjsVersion !== 'undefined') { + globalScope.PDFJS.version = pdfjsVersion; + } + if (typeof pdfjsVersion !== 'undefined') { + globalScope.PDFJS.build = pdfjsBuild; + } + globalScope.PDFJS.pdfBug = false; exports.globalScope = globalScope;