Output pdf.scripting.js as a JavaScript module (PR 17055 follow-up)

To avoid problems with `export` statements in the QuickJS Javascript Engine, we can work-around that by *explicitly* exposing `pdfjsScripting` globally instead.
This commit is contained in:
Jonas Jenwald 2023-10-06 17:26:14 +02:00
parent bab4c7f617
commit 4b489cd4e6
2 changed files with 9 additions and 34 deletions

View File

@ -418,30 +418,6 @@ function tweakWebpackOutput(jsName) {
}); });
} }
function addGlobalExports(amdName, jsName) {
const replacer = [
`module\\.exports = factory\\(\\);`,
`define\\("${amdName}", \\[\\], factory\\);`,
`exports\\["${amdName}"\\] = factory\\(\\);`,
`root\\["${amdName}"\\] = factory\\(\\);`,
];
const regex = new RegExp(`(${replacer.join("|")})`, "gm");
return replace(regex, match => {
switch (match) {
case `module.exports = factory();`:
return `module.exports = root.${jsName} = factory();`;
case `define("${amdName}", [], factory);`:
return `define("${amdName}", [], () => { return (root.${jsName} = factory()); });`;
case `exports["${amdName}"] = factory();`:
return `exports["${amdName}"] = root.${jsName} = factory();`;
case `root["${amdName}"] = factory();`:
return `root["${amdName}"] = root.${jsName} = factory();`;
}
return match;
});
}
function createMainBundle(defines) { function createMainBundle(defines) {
const mainFileConfig = createWebpackConfig(defines, { const mainFileConfig = createWebpackConfig(defines, {
filename: "pdf.mjs", filename: "pdf.mjs",
@ -456,23 +432,20 @@ function createMainBundle(defines) {
} }
function createScriptingBundle(defines, extraOptions = undefined) { function createScriptingBundle(defines, extraOptions = undefined) {
const scriptingAMDName = "pdfjs-dist/build/pdf.scripting";
const scriptingOutputName = "pdf.scripting.js";
const scriptingFileConfig = createWebpackConfig( const scriptingFileConfig = createWebpackConfig(
defines, defines,
{ {
filename: scriptingOutputName, filename: "pdf.scripting.mjs",
library: scriptingAMDName, library: {
libraryTarget: "umd", type: "module",
umdNamedDefine: true, },
}, },
extraOptions extraOptions
); );
return gulp return gulp
.src("./src/pdf.scripting.js") .src("./src/pdf.scripting.js")
.pipe(webpack2Stream(scriptingFileConfig)) .pipe(webpack2Stream(scriptingFileConfig))
.pipe(addGlobalExports(scriptingAMDName, "pdfjsScripting")); .pipe(tweakWebpackOutput());
} }
function createSandboxExternal(defines) { function createSandboxExternal(defines) {
@ -502,7 +475,7 @@ function createTemporaryScriptingBundle(defines, extraOptions = undefined) {
} }
function createSandboxBundle(defines, extraOptions = undefined) { function createSandboxBundle(defines, extraOptions = undefined) {
const scriptingPath = TMP_DIR + "pdf.scripting.js"; const scriptingPath = TMP_DIR + "pdf.scripting.mjs";
// Insert the source as a string to be `eval`-ed in the sandbox. // Insert the source as a string to be `eval`-ed in the sandbox.
const sandboxDefines = builder.merge(defines, { const sandboxDefines = builder.merge(defines, {
PDF_SCRIPTING_JS_SOURCE: fs.readFileSync(scriptingPath).toString(), PDF_SCRIPTING_JS_SOURCE: fs.readFileSync(scriptingPath).toString(),

View File

@ -22,4 +22,6 @@ const pdfjsVersion =
const pdfjsBuild = const pdfjsBuild =
typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_BUILD") : void 0; typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_BUILD") : void 0;
export { initSandbox }; // To avoid problems with `export` statements in the QuickJS Javascript Engine,
// we manually expose `pdfjsScripting` globally instead.
globalThis.pdfjsScripting = { initSandbox };