From d742e3cde82c9dd9f57df87c76d7b15617db16cf Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 4 Dec 2020 13:03:24 +0100 Subject: [PATCH] Actually utilize the PDF.js build-system fully when bundling the `pdf.sandbox.js` file There's no good reason, as far as I can tell, to use search-and-replace to include the *stringified* `pdf.scripting.js` file in the built `pdf.sandbox.js` file. Instead we could, and even should, utilize the existing `PDFJSDev.eval(...)`-functionality, which is not only simpler but will also be more efficient as well (no need for a regular expression). --- gulpfile.js | 36 +++++++++++------------------------- src/pdf.sandbox.js | 2 +- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index b1bf1e5d3..d2145cb93 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -98,6 +98,7 @@ const DEFINES = Object.freeze({ PRODUCTION: true, SKIP_BABEL: true, TESTING: false, + NO_SOURCE_MAP: false, // The main build targets: GENERIC: false, MOZCENTRAL: false, @@ -106,7 +107,6 @@ const DEFINES = Object.freeze({ COMPONENTS: false, LIB: false, IMAGE_DECODERS: false, - NO_SOURCE_MAP: false, }); function transform(charEncoding, transformFunction) { @@ -350,36 +350,22 @@ function createSandboxBundle(defines, code) { var sandboxAMDName = "pdfjs-dist/build/pdf.sandbox"; var sandboxOutputName = "pdf.sandbox.js"; - var sandboxFileConfig = createWebpackConfig(defines, { + // Insert the code as a string to be `eval`-ed in the sandbox. + const sandboxDefines = builder.merge(defines, { + PDF_SCRIPTING_JS_SOURCE: code, + }); + var sandboxFileConfig = createWebpackConfig(sandboxDefines, { filename: sandboxOutputName, library: sandboxAMDName, libraryTarget: "umd", umdNamedDefine: true, }); - // The code is the one from the bundle pdf.scripting.js - // so in order to have it in a string (which will be eval-ed - // in the sandbox) we must escape some chars. - // This way we've all the code (initialization+sandbox) in - // the same bundle. - code = code.replace(/["\\\n\t]/g, match => { - if (match === "\n") { - return "\\n"; - } - if (match === "\t") { - return "\\t"; - } - return `\\${match}`; - }); - return ( - gulp - .src("./src/pdf.sandbox.js") - .pipe(webpack2Stream(sandboxFileConfig)) - .pipe(replaceWebpackRequire()) - .pipe(replaceJSRootName(sandboxAMDName, "pdfjsSandbox")) - // put the code in a string to be eval-ed in the sandbox - .pipe(replace("/* INITIALIZATION_CODE */", `${code}`)) - ); + return gulp + .src("./src/pdf.sandbox.js") + .pipe(webpack2Stream(sandboxFileConfig)) + .pipe(replaceWebpackRequire()) + .pipe(replaceJSRootName(sandboxAMDName, "pdfjsSandbox")); } function buildSandbox(defines, dir) { diff --git a/src/pdf.sandbox.js b/src/pdf.sandbox.js index 8bb6b37b8..fe1f93dfc 100644 --- a/src/pdf.sandbox.js +++ b/src/pdf.sandbox.js @@ -48,7 +48,7 @@ class Sandbox { "module = Object.create(null);", // Next line is replaced by code from initialization.js // when we create the bundle for the sandbox. - "/* INITIALIZATION_CODE */", + PDFJSDev.eval("PDF_SCRIPTING_JS_SOURCE"), `data = ${sandboxData};`, `module.exports.initSandbox({ data, extra: {${extraStr}}, out: this});`, "delete exports;",