Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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());
}

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
4 changes: 3 additions & 1 deletion src/pdf.scripting.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ const pdfjsVersion =
const pdfjsBuild =
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 };
Snuffleupagus marked this conversation as resolved.
Show resolved Hide resolved