From 79c2f69c3288494c5ce2809182c896484bf4be5c Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Mon, 21 Dec 2015 13:46:50 -0600 Subject: [PATCH] Adds/modifies examples for node.js and webpack. --- examples/node/domstubs.js | 8 ++++++ examples/node/getinfo.js | 8 +++--- examples/node/pdf2svg.js | 8 ++---- examples/webpack/.gitignore | 1 + examples/webpack/README.md | 18 ++++++++++++++ examples/webpack/index.html | 11 ++++++++ examples/webpack/main.js | 32 ++++++++++++++++++++++++ examples/webpack/package.json | 9 +++++++ examples/webpack/webpack.config.js | 20 +++++++++++++++ make.js | 2 ++ package.json | 1 + src/core/worker.js | 5 ++-- src/display/api.js | 19 +++++++++++--- src/frameworks.js | 40 ++++++++++++++++++++++++++++++ src/pdf.js | 7 +++--- 15 files changed, 170 insertions(+), 19 deletions(-) create mode 100644 examples/webpack/.gitignore create mode 100644 examples/webpack/README.md create mode 100644 examples/webpack/index.html create mode 100644 examples/webpack/main.js create mode 100644 examples/webpack/package.json create mode 100644 examples/webpack/webpack.config.js create mode 100644 src/frameworks.js diff --git a/examples/node/domstubs.js b/examples/node/domstubs.js index 5eb186b8d3118..fd7add63a5187 100644 --- a/examples/node/domstubs.js +++ b/examples/node/domstubs.js @@ -120,9 +120,17 @@ DOMElement.prototype = { }, } +global.window = global; + +global.navigator = { userAgent: 'node' }; + global.document = { childNodes : [], + get currentScript() { + return { src: '' }; + }, + get documentElement() { return this; }, diff --git a/examples/node/getinfo.js b/examples/node/getinfo.js index 9c2faff67eedd..35111502e6313 100644 --- a/examples/node/getinfo.js +++ b/examples/node/getinfo.js @@ -9,13 +9,11 @@ var fs = require('fs'); -// HACK few hacks to let PDF.js be loaded not as a module in global space. -global.window = global; -global.navigator = { userAgent: "node" }; -global.PDFJS = {}; +// HACK adding DOMParser to read XMP metadata. global.DOMParser = require('./domparsermock.js').DOMParserMock; -require('../../build/singlefile/build/pdf.combined.js'); +// Run `node make dist` to generate 'pdfjs-dist' npm package files. +require('../../build/dist'); // Loading file from file system into typed array var pdfPath = process.argv[2] || '../../web/compressed.tracemonkey-pldi-09.pdf'; diff --git a/examples/node/pdf2svg.js b/examples/node/pdf2svg.js index d4defb41ce49f..f5e3d8ff096d4 100644 --- a/examples/node/pdf2svg.js +++ b/examples/node/pdf2svg.js @@ -8,14 +8,10 @@ var fs = require('fs'); // HACK few hacks to let PDF.js be loaded not as a module in global space. -global.window = global; -global.navigator = { userAgent: 'node' }; -global.PDFJS = {}; - require('./domstubs.js'); -PDFJS.workerSrc = true; -require('../../build/singlefile/build/pdf.combined.js'); +// Run `node make dist` to generate 'pdfjs-dist' npm package files. +require('../../build/dist'); // Loading file from file system into typed array var pdfPath = process.argv[2] || '../../web/compressed.tracemonkey-pldi-09.pdf'; diff --git a/examples/webpack/.gitignore b/examples/webpack/.gitignore new file mode 100644 index 0000000000000..c2658d7d1b318 --- /dev/null +++ b/examples/webpack/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/examples/webpack/README.md b/examples/webpack/README.md new file mode 100644 index 0000000000000..228b386fc1252 --- /dev/null +++ b/examples/webpack/README.md @@ -0,0 +1,18 @@ +## Overview + +Example to demonstrate PDF.js library usage with webpack. + +## Getting started + +Build project and install the example dependencies: + + $ node make dist + $ cd examples/webpack + $ npm install + +To build webpack bundles, run `node_modules/.bin/webpack`. If you are running +a web server, you can observe the build results at +http://localhost:8888/examples/webpack/index.html + +See main.js and webpack.config.js files. Please notice that PDF.js packaging +requires 'entry' loader. diff --git a/examples/webpack/index.html b/examples/webpack/index.html new file mode 100644 index 0000000000000..24b1472160046 --- /dev/null +++ b/examples/webpack/index.html @@ -0,0 +1,11 @@ + + + + + webpack example + + + + + + diff --git a/examples/webpack/main.js b/examples/webpack/main.js new file mode 100644 index 0000000000000..2a3509d7a22ad --- /dev/null +++ b/examples/webpack/main.js @@ -0,0 +1,32 @@ +// Any copyright is dedicated to the Public Domain. +// http://creativecommons.org/licenses/publicdomain/ + +// Hello world example for webpack. + +require('pdfjs-dist'); + +var pdfPath = '../helloworld/helloworld.pdf'; + +// It is also possible to disable workers via `PDFJS.disableWorker = true`, +// however that might degrade the UI performance in web browsers. + +// Loading a document. +var loadingTask = PDFJS.getDocument(pdfPath); +loadingTask.promise.then(function (pdfDocument) { + // Request a first page + return pdfDocument.getPage(1).then(function (pdfPage) { + // Display page on the existing canvas with 100% scale. + var viewport = pdfPage.getViewport(1.0); + var canvas = document.getElementById('theCanvas'); + canvas.width = viewport.width; + canvas.height = viewport.height; + var ctx = canvas.getContext('2d'); + var renderTask = pdfPage.render({ + canvasContext: ctx, + viewport: viewport + }); + return renderTask.promise; + }); +}).catch(function (reason) { + console.error('Error: ' + reason); +}); diff --git a/examples/webpack/package.json b/examples/webpack/package.json new file mode 100644 index 0000000000000..f1aabe9e66172 --- /dev/null +++ b/examples/webpack/package.json @@ -0,0 +1,9 @@ +{ + "name": "webpack-pdf.js-example", + "version": "0.1.0", + "devDependencies": { + "webpack": "~1.12.9", + "entry-loader": "~0.1.0", + "pdfjs-dist": "../../build/dist" + } +} diff --git a/examples/webpack/webpack.config.js b/examples/webpack/webpack.config.js new file mode 100644 index 0000000000000..f0735d38a3ef8 --- /dev/null +++ b/examples/webpack/webpack.config.js @@ -0,0 +1,20 @@ +var webpack = require('webpack'); +var path = require('path'); + +module.exports = { + context: __dirname, + entry: './main.js', + output: { + path: path.join(__dirname, '../../build/webpack'), + publicPath: '../../build/webpack/', + filename: 'bundle.js' + }, + plugins: [ + new webpack.optimize.UglifyJsPlugin({ + compressor: { + screw_ie8: true, + warnings: false + } + }) + ] +}; diff --git a/make.js b/make.js index 6d65eab60b1d0..b46026ed720f2 100644 --- a/make.js +++ b/make.js @@ -149,6 +149,7 @@ target.generic = function() { }; builder.build(setup); + cleanupJSSource(GENERIC_DIR + '/build/pdf.js'); cleanupJSSource(GENERIC_DIR + '/web/viewer.js'); cleanupCSSSource(GENERIC_DIR + '/web/viewer.css'); }; @@ -325,6 +326,7 @@ target.dist = function() { var npmManifest = { name: DIST_NAME, version: VERSION, + main: 'build/pdf.js', description: DIST_DESCRIPTION, keywords: DIST_KEYWORDS, homepage: DIST_HOMEPAGE, diff --git a/package.json b/package.json index ca4d41ec84bef..97a39d9ac02ba 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "jsdoc": "^3.3.0-alpha9", "jshint": "~2.8.0", "wintersmith": "^2.0.0", + "node-ensure": "^0.0.0", "rimraf": "^2.4.1", "shelljs": "~0.4.0", "typogr": "~0.6.5", diff --git a/src/core/worker.js b/src/core/worker.js index 78632f7c2c188..2a157c62b2b21 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* globals NetworkManager */ +/* globals NetworkManager, module */ 'use strict'; @@ -648,7 +648,8 @@ var workerConsole = { // Worker thread? -if (typeof window === 'undefined' && typeof require === 'undefined') { +if (typeof window === 'undefined' && + !(typeof module !== 'undefined' && module.require)) { if (!('console' in globalScope)) { globalScope.console = workerConsole; } diff --git a/src/display/api.js b/src/display/api.js index c890df102429d..2167757de9e63 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -58,6 +58,14 @@ var globalScope = sharedGlobal.globalScope; var DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536 +//#if PRODUCTION && !SINGLE_FILE +//#if GENERIC +//#include ../src/frameworks.js +//#else +//var fakeWorkerFilesLoader = null; +//#endif +//#endif + /** * The maximum allowed image size in total pixels e.g. width * height. Images * above this value will not be drawn. Use -1 for no limit. @@ -1192,7 +1200,10 @@ var PDFWorker = (function PDFWorkerClosure() { // PDFJS.fakeWorkerFilesLoadedCapability.resolve(); //#endif //#if PRODUCTION && !SINGLE_FILE -// Util.loadScript(PDFJS.workerSrc, function() { +// var loader = fakeWorkerFilesLoader || function (callback) { +// Util.loadScript(PDFJS.workerSrc, callback); +// }; +// loader(function () { // PDFJS.fakeWorkerFilesLoadedCapability.resolve(); // }); //#endif @@ -1295,8 +1306,10 @@ var PDFWorker = (function PDFWorkerClosure() { }, _setupFakeWorker: function PDFWorker_setupFakeWorker() { - warn('Setting up fake worker.'); - globalScope.PDFJS.disableWorker = true; + if (!globalScope.PDFJS.disableWorker) { + warn('Setting up fake worker.'); + globalScope.PDFJS.disableWorker = true; + } setupFakeWorkerGlobal().then(function () { if (this.destroyed) { diff --git a/src/frameworks.js b/src/frameworks.js new file mode 100644 index 0000000000000..33e34cee93e8b --- /dev/null +++ b/src/frameworks.js @@ -0,0 +1,40 @@ +/* Copyright 2015 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. + */ +/* globals PDFJS, require, module */ + +// included from api.js for GENERIC build + +'use strict'; + +var useRequireEnsure = false; +if (typeof module !== 'undefined' && module.require) { + // node.js - disable worker and set require.ensure. + PDFJS.disableWorker = true; + if (typeof require.ensure === 'undefined') { + require.ensure = require('node-ensure'); + } + useRequireEnsure = true; +} +if (typeof __webpack_require__ !== 'undefined') { + // Webpack - get/bundle pdf.worker.js as additional file. + PDFJS.workerSrc = require('entry?name=[hash]-worker.js!./pdf.worker.js'); + useRequireEnsure = true; +} +var fakeWorkerFilesLoader = useRequireEnsure && function (callback) { + require.ensure([], function () { + require('./pdf.worker.js'); + callback(); + }); +}; diff --git a/src/pdf.js b/src/pdf.js index 1ea5ffa91ecc3..e9e9b6c153c4d 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -12,12 +12,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/*jshint globalstrict: false */ -/* globals PDFJS */ +/* jshint globalstrict: false */ +/* globals PDFJS, global */ // Initializing PDFJS global object (if still undefined) if (typeof PDFJS === 'undefined') { - (typeof window !== 'undefined' ? window : this).PDFJS = {}; + (typeof window !== 'undefined' ? window : + typeof global !== 'undefined' ? global : this).PDFJS = {}; } //#if BUNDLE_VERSION