Convert web/debugger.js to a *basic* module

The various functionality in `web/debugger.js` is currently *indirectly* added to the global scope, since that's how `var` works when it's used outside of any functions/closures.
Given how this functionality is being accessed/used, not just in the viewer but also in the API and textLayer, simply converting the entire file to a module isn't trivial[1]. However, we can at least export the `PDFBug`-part properly and then `import` that dynamically in the viewer.
Also, to improve the code a little bit, we're now *explicitly* exporting the necessary functionality globally.

According to MDN, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility, all the browsers that we now support have dynamic `imports` implementations.

---
[1] We could probably pass around references to the necessary functionality, but given how this is being used I'm just not sure it's worth the effort. Also, adding *official* support for these viewer-specific debugging tools in the API feels both unnecessary and unfortunate.
This commit is contained in:
Jonas Jenwald 2022-04-03 11:51:49 +02:00
parent 7c8a92da77
commit 8fa73dbfab
3 changed files with 41 additions and 27 deletions

View file

@ -327,6 +327,10 @@ function replaceWebpackRequire() {
return replace("__webpack_require__", "__w_pdfjs_require__");
}
function replaceNonWebpackImport() {
return replace("__non_webpack_import__", "import");
}
function replaceJSRootName(amdName, jsName) {
// Saving old-style JS module name.
return replace(
@ -349,6 +353,7 @@ function createMainBundle(defines) {
.src("./src/pdf.js")
.pipe(webpack2Stream(mainFileConfig))
.pipe(replaceWebpackRequire())
.pipe(replaceNonWebpackImport())
.pipe(replaceJSRootName(mainAMDName, "pdfjsLib"));
}
@ -370,6 +375,7 @@ function createScriptingBundle(defines, extraOptions = undefined) {
.src("./src/pdf.scripting.js")
.pipe(webpack2Stream(scriptingFileConfig))
.pipe(replaceWebpackRequire())
.pipe(replaceNonWebpackImport())
.pipe(
replace(
'root["' + scriptingAMDName + '"] = factory()',
@ -428,6 +434,7 @@ function createSandboxBundle(defines, extraOptions = undefined) {
.src("./src/pdf.sandbox.js")
.pipe(webpack2Stream(sandboxFileConfig))
.pipe(replaceWebpackRequire())
.pipe(replaceNonWebpackImport())
.pipe(replaceJSRootName(sandboxAMDName, "pdfjsSandbox"));
}
@ -445,6 +452,7 @@ function createWorkerBundle(defines) {
.src("./src/pdf.worker.js")
.pipe(webpack2Stream(workerFileConfig))
.pipe(replaceWebpackRequire())
.pipe(replaceNonWebpackImport())
.pipe(replaceJSRootName(workerAMDName, "pdfjsWorker"));
}
@ -460,7 +468,10 @@ function createWebBundle(defines, options) {
defaultPreferencesDir: options.defaultPreferencesDir,
}
);
return gulp.src("./web/viewer.js").pipe(webpack2Stream(viewerFileConfig));
return gulp
.src("./web/viewer.js")
.pipe(webpack2Stream(viewerFileConfig))
.pipe(replaceNonWebpackImport());
}
function createComponentsBundle(defines) {
@ -477,6 +488,7 @@ function createComponentsBundle(defines) {
.src("./web/pdf_viewer.component.js")
.pipe(webpack2Stream(componentsFileConfig))
.pipe(replaceWebpackRequire())
.pipe(replaceNonWebpackImport())
.pipe(replaceJSRootName(componentsAMDName, "pdfjsViewer"));
}
@ -494,6 +506,7 @@ function createImageDecodersBundle(defines) {
.src("./src/pdf.image_decoders.js")
.pipe(webpack2Stream(componentsFileConfig))
.pipe(replaceWebpackRequire())
.pipe(replaceNonWebpackImport())
.pipe(replaceJSRootName(imageDecodersAMDName, "pdfjsImageDecoders"));
}
@ -1427,12 +1440,14 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
// __non_webpack_require__ has to be used.
// In this target, we don't create a bundle, so we have to replace the
// occurrences of __non_webpack_require__ ourselves.
function babelPluginReplaceNonWebPackRequire(babel) {
function babelPluginReplaceNonWebpackImports(babel) {
return {
visitor: {
Identifier(curPath, state) {
if (curPath.node.name === "__non_webpack_require__") {
curPath.replaceWith(babel.types.identifier("require"));
} else if (curPath.node.name === "__non_webpack_import__") {
curPath.replaceWith(babel.types.identifier("import"));
}
},
},
@ -1454,7 +1469,7 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
regenerator: true,
},
],
babelPluginReplaceNonWebPackRequire,
babelPluginReplaceNonWebpackImports,
],
}).code;
const removeCjsSrc =