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
Unfortunately this breaks scripting in e.g. the viewer, see the error messages below, and it's not clear to me if this is a limitation of the QuickJS Javascript Engine itself or "just" an issue with how the Emscripten compiler was configured.

```
Quickjs -- SyntaxError: unsupported keyword: export
    at <initScript>:4008
pdf.sandbox.mjs:53:507329
setDocument: "Cannot start sandbox". pdf_scripting_manager.js:170:15
```
  • Loading branch information
Snuffleupagus committed Oct 6, 2023
1 parent 378f833 commit 8053078
Showing 1 changed file with 6 additions and 33 deletions.
39 changes: 6 additions & 33 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -413,30 +413,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 @@ -451,23 +427,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 @@ -497,7 +470,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

0 comments on commit 8053078

Please sign in to comment.