Skip to content

Commit

Permalink
Output pdf.scripting.js as a JavaScript module (PR 17055 follow-up)
Browse files Browse the repository at this point in the history
To avoid problems with `export` statements in the QuickJS Javascript Engine, we can work-around that by *explicitly* exposing `pdfjsScripting` globally instead.
  • Loading branch information
Snuffleupagus committed Oct 7, 2023
1 parent 0b5f70d commit d548419
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 34 deletions.
39 changes: 6 additions & 33 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
const mainFileConfig = createWebpackConfig(defines, {
filename: "pdf.mjs",
Expand All @@ -456,23 +432,20 @@ function createMainBundle(defines) {
}

function createScriptingBundle(defines, extraOptions = undefined) {
const scriptingAMDName = "pdfjs-dist/build/pdf.scripting";
const scriptingOutputName = "pdf.scripting.js";

const scriptingFileConfig = createWebpackConfig(
defines,
{
filename: scriptingOutputName,
library: scriptingAMDName,
libraryTarget: "umd",
umdNamedDefine: true,
filename: "pdf.scripting.mjs",
library: {
type: "module",
},
},
extraOptions
);
return gulp
.src("./src/pdf.scripting.js")
.pipe(webpack2Stream(scriptingFileConfig))
.pipe(addGlobalExports(scriptingAMDName, "pdfjsScripting"));
.pipe(tweakWebpackOutput("pdfjsScripting"));
}

function createSandboxExternal(defines) {
Expand Down Expand Up @@ -502,7 +475,7 @@ function createTemporaryScriptingBundle(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.
const sandboxDefines = builder.merge(defines, {
PDF_SCRIPTING_JS_SOURCE: fs.readFileSync(scriptingPath).toString(),
Expand Down
2 changes: 1 addition & 1 deletion src/pdf.scripting.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ const pdfjsVersion =
const pdfjsBuild =
typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_BUILD") : void 0;

export { initSandbox };
globalThis.pdfjsScripting = { initSandbox };

0 comments on commit d548419

Please sign in to comment.