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).
This commit is contained in:
Jonas Jenwald 2020-12-04 13:03:24 +01:00
parent 715b8aa389
commit d742e3cde8
2 changed files with 12 additions and 26 deletions

View File

@ -98,6 +98,7 @@ const DEFINES = Object.freeze({
PRODUCTION: true, PRODUCTION: true,
SKIP_BABEL: true, SKIP_BABEL: true,
TESTING: false, TESTING: false,
NO_SOURCE_MAP: false,
// The main build targets: // The main build targets:
GENERIC: false, GENERIC: false,
MOZCENTRAL: false, MOZCENTRAL: false,
@ -106,7 +107,6 @@ const DEFINES = Object.freeze({
COMPONENTS: false, COMPONENTS: false,
LIB: false, LIB: false,
IMAGE_DECODERS: false, IMAGE_DECODERS: false,
NO_SOURCE_MAP: false,
}); });
function transform(charEncoding, transformFunction) { function transform(charEncoding, transformFunction) {
@ -350,36 +350,22 @@ function createSandboxBundle(defines, code) {
var sandboxAMDName = "pdfjs-dist/build/pdf.sandbox"; var sandboxAMDName = "pdfjs-dist/build/pdf.sandbox";
var sandboxOutputName = "pdf.sandbox.js"; 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, filename: sandboxOutputName,
library: sandboxAMDName, library: sandboxAMDName,
libraryTarget: "umd", libraryTarget: "umd",
umdNamedDefine: true, umdNamedDefine: true,
}); });
// The code is the one from the bundle pdf.scripting.js return gulp
// 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") .src("./src/pdf.sandbox.js")
.pipe(webpack2Stream(sandboxFileConfig)) .pipe(webpack2Stream(sandboxFileConfig))
.pipe(replaceWebpackRequire()) .pipe(replaceWebpackRequire())
.pipe(replaceJSRootName(sandboxAMDName, "pdfjsSandbox")) .pipe(replaceJSRootName(sandboxAMDName, "pdfjsSandbox"));
// put the code in a string to be eval-ed in the sandbox
.pipe(replace("/* INITIALIZATION_CODE */", `${code}`))
);
} }
function buildSandbox(defines, dir) { function buildSandbox(defines, dir) {

View File

@ -48,7 +48,7 @@ class Sandbox {
"module = Object.create(null);", "module = Object.create(null);",
// Next line is replaced by code from initialization.js // Next line is replaced by code from initialization.js
// when we create the bundle for the sandbox. // when we create the bundle for the sandbox.
"/* INITIALIZATION_CODE */", PDFJSDev.eval("PDF_SCRIPTING_JS_SOURCE"),
`data = ${sandboxData};`, `data = ${sandboxData};`,
`module.exports.initSandbox({ data, extra: {${extraStr}}, out: this});`, `module.exports.initSandbox({ data, extra: {${extraStr}}, out: this});`,
"delete exports;", "delete exports;",