Skip to content

Commit 6b961c4

Browse files
committed
Update Webpack to version 5.99.5 (issue 19808)
In Webpack version `5.99.0` the way that `export` statements are handled was changed slightly, with much less boilerplate code being generated, which unfortunately breaks our `tweakWebpackOutput` function that's used to expose the exported properties globally and that e.g. the viewer depends upon. Given that we were depending on formatting that should most likely be viewed as nothing more than an internal implementation detail in Webpack, we instead work-around this by manually defining the structures that were previously generated. Obviously this will lead to a tiny bit more manual work in the future, however we don't change the API-surface often enough that it should be a big issue *and* the relevant unit-tests are updated such that it shouldn't be possible to break this. *NOTE:* In the future we might want to consider no longer using global properties like this, and instead rely only on proper `export`s throughout the code-base. However changing this would likely be non-trivial (given edge-cases), and it'd be an `api-major` change, so let's just do the minimal amount of work to unblock Webpack updates for now.
1 parent c071f44 commit 6b961c4

13 files changed

Lines changed: 182 additions & 64 deletions

gulpfile.mjs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,6 @@ function createWebpackConfig(
362362
// V8 chokes on very long sequences, work around that.
363363
sequences: false,
364364
},
365-
mangle: {
366-
// Ensure that the `tweakWebpackOutput` function works.
367-
reserved: ["__webpack_exports__"],
368-
},
369365
keep_classnames: true,
370366
keep_fnames: true,
371367
module: isModule,
@@ -463,13 +459,6 @@ function checkChromePreferencesFile(chromePrefsPath, webPrefs) {
463459
return ret;
464460
}
465461

466-
function tweakWebpackOutput(jsName) {
467-
return replace(
468-
/((?:\s|,)__webpack_exports__)(?:\s?)=(?:\s?)({};)/gm,
469-
(match, p1, p2) => `${p1} = globalThis.${jsName} = ${p2}`
470-
);
471-
}
472-
473462
function createMainBundle(defines) {
474463
const mainFileConfig = createWebpackConfig(defines, {
475464
filename: defines.MINIFIED ? "pdf.min.mjs" : "pdf.mjs",
@@ -479,8 +468,7 @@ function createMainBundle(defines) {
479468
});
480469
return gulp
481470
.src("./src/pdf.js", { encoding: false })
482-
.pipe(webpack2Stream(mainFileConfig))
483-
.pipe(tweakWebpackOutput("pdfjsLib"));
471+
.pipe(webpack2Stream(mainFileConfig));
484472
}
485473

486474
function createScriptingBundle(defines, extraOptions = undefined) {
@@ -548,8 +536,7 @@ function createSandboxBundle(defines, extraOptions = undefined) {
548536

549537
return gulp
550538
.src("./src/pdf.sandbox.js", { encoding: false })
551-
.pipe(webpack2Stream(sandboxFileConfig))
552-
.pipe(tweakWebpackOutput("pdfjsSandbox"));
539+
.pipe(webpack2Stream(sandboxFileConfig));
553540
}
554541

555542
function createWorkerBundle(defines) {
@@ -561,8 +548,7 @@ function createWorkerBundle(defines) {
561548
});
562549
return gulp
563550
.src("./src/pdf.worker.js", { encoding: false })
564-
.pipe(webpack2Stream(workerFileConfig))
565-
.pipe(tweakWebpackOutput("pdfjsWorker"));
551+
.pipe(webpack2Stream(workerFileConfig));
566552
}
567553

568554
function createWebBundle(defines, options) {
@@ -610,8 +596,7 @@ function createComponentsBundle(defines) {
610596
});
611597
return gulp
612598
.src("./web/pdf_viewer.component.js", { encoding: false })
613-
.pipe(webpack2Stream(componentsFileConfig))
614-
.pipe(tweakWebpackOutput("pdfjsViewer"));
599+
.pipe(webpack2Stream(componentsFileConfig));
615600
}
616601

617602
function createImageDecodersBundle(defines) {
@@ -625,8 +610,7 @@ function createImageDecodersBundle(defines) {
625610
});
626611
return gulp
627612
.src("./src/pdf.image_decoders.js", { encoding: false })
628-
.pipe(webpack2Stream(componentsFileConfig))
629-
.pipe(tweakWebpackOutput("pdfjsImageDecoders"));
613+
.pipe(webpack2Stream(componentsFileConfig));
630614
}
631615

632616
function createCMapBundle() {

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"ttest": "^4.0.0",
5454
"typescript": "^5.8.3",
5555
"vinyl": "^3.0.0",
56-
"webpack": "^5.98.0",
56+
"webpack": "^5.99.5",
5757
"webpack-stream": "^7.0.0",
5858
"yargs": "^17.7.2"
5959
},

src/display/api.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,13 @@ class PDFWorker {
21362136
);
21372137
};
21382138
}
2139+
2140+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
2141+
this._resetGlobalState = () => {
2142+
this.#isWorkerDisabled = false;
2143+
delete globalThis.pdfjsWorker;
2144+
};
2145+
}
21392146
}
21402147

21412148
constructor({

src/pdf.image_decoders.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ const pdfjsVersion =
2929
const pdfjsBuild =
3030
typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_BUILD") : void 0;
3131

32+
globalThis.pdfjsImageDecoders = {
33+
getVerbosityLevel,
34+
Jbig2Error,
35+
Jbig2Image,
36+
JpegError,
37+
JpegImage,
38+
JpxError,
39+
JpxImage,
40+
setVerbosityLevel,
41+
VerbosityLevel,
42+
};
43+
3244
export {
3345
getVerbosityLevel,
3446
Jbig2Error,

src/pdf.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,59 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING || GENERIC")) {
9393
};
9494
}
9595

96+
globalThis.pdfjsLib = {
97+
AbortException,
98+
AnnotationEditorLayer,
99+
AnnotationEditorParamsType,
100+
AnnotationEditorType,
101+
AnnotationEditorUIManager,
102+
AnnotationLayer,
103+
AnnotationMode,
104+
AnnotationType,
105+
build,
106+
ColorPicker,
107+
createValidAbsoluteUrl,
108+
DOMSVGFactory,
109+
DrawLayer,
110+
FeatureTest,
111+
fetchData,
112+
getDocument,
113+
getFilenameFromUrl,
114+
getPdfFilenameFromUrl,
115+
getUuid,
116+
getXfaPageViewport,
117+
GlobalWorkerOptions,
118+
ImageKind,
119+
InvalidPDFException,
120+
isDataScheme,
121+
isPdfFile,
122+
isValidExplicitDest,
123+
MathClamp,
124+
noContextMenu,
125+
normalizeUnicode,
126+
OPS,
127+
OutputScale,
128+
PasswordResponses,
129+
PDFDataRangeTransport,
130+
PDFDateString,
131+
PDFWorker,
132+
PermissionFlag,
133+
PixelsPerInch,
134+
RenderingCancelledException,
135+
ResponseException,
136+
setLayerDimensions,
137+
shadow,
138+
SignatureExtractor,
139+
stopEvent,
140+
SupportedImageMimeTypes,
141+
TextLayer,
142+
TouchManager,
143+
Util,
144+
VerbosityLevel,
145+
version,
146+
XfaLayer,
147+
};
148+
96149
export {
97150
AbortException,
98151
AnnotationEditorLayer,

src/pdf.sandbox.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,8 @@ function QuickJSSandbox() {
145145
return ModuleLoader().then(module => new Sandbox(window, module));
146146
}
147147

148+
globalThis.pdfjsSandbox = {
149+
QuickJSSandbox,
150+
};
151+
148152
export { QuickJSSandbox };

src/pdf.worker.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ const pdfjsVersion =
2222
const pdfjsBuild =
2323
typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_BUILD") : void 0;
2424

25+
globalThis.pdfjsWorker = {
26+
WorkerMessageHandler,
27+
};
28+
2529
export { WorkerMessageHandler };

test/unit/pdf.image_decoders_spec.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ import { Jbig2Error, Jbig2Image } from "../../src/core/jbig2.js";
2222
import { JpegError, JpegImage } from "../../src/core/jpg.js";
2323
import { JpxError, JpxImage } from "../../src/core/jpx.js";
2424

25+
const expectedAPI = Object.freeze({
26+
getVerbosityLevel,
27+
Jbig2Error,
28+
Jbig2Image,
29+
JpegError,
30+
JpegImage,
31+
JpxError,
32+
JpxImage,
33+
setVerbosityLevel,
34+
VerbosityLevel,
35+
});
36+
2537
describe("pdfimage_api", function () {
2638
it("checks that the *official* PDF.js-image decoders API exposes the expected functionality", async function () {
2739
// eslint-disable-next-line no-unsanitized/method
@@ -33,16 +45,10 @@ describe("pdfimage_api", function () {
3345

3446
// The imported Object contains an (automatically) inserted Symbol,
3547
// hence we copy the data to allow using a simple comparison below.
36-
expect({ ...pdfimageAPI }).toEqual({
37-
getVerbosityLevel,
38-
Jbig2Error,
39-
Jbig2Image,
40-
JpegError,
41-
JpegImage,
42-
JpxError,
43-
JpxImage,
44-
setVerbosityLevel,
45-
VerbosityLevel,
46-
});
48+
expect({ ...pdfimageAPI }).toEqual(expectedAPI);
49+
50+
expect(Object.keys(globalThis.pdfjsImageDecoders).sort()).toEqual(
51+
Object.keys(expectedAPI).sort()
52+
);
4753
});
4854
});

test/unit/pdf.worker_spec.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@
1313
* limitations under the License.
1414
*/
1515

16+
import { PDFWorker } from "../../src/display/api.js";
1617
import { WorkerMessageHandler } from "../../src/core/worker.js";
1718

19+
const expectedAPI = Object.freeze({
20+
WorkerMessageHandler,
21+
});
22+
1823
describe("pdfworker_api", function () {
24+
afterEach(function () {
25+
// Avoid interfering with other unit-tests, since `globalThis.pdfjsWorker`
26+
// being defined will impact loading and usage of the worker.
27+
PDFWorker._resetGlobalState();
28+
});
29+
1930
it("checks that the *official* PDF.js-worker API exposes the expected functionality", async function () {
2031
// eslint-disable-next-line no-unsanitized/method
2132
const pdfworkerAPI = await import(
@@ -26,8 +37,10 @@ describe("pdfworker_api", function () {
2637

2738
// The imported Object contains an (automatically) inserted Symbol,
2839
// hence we copy the data to allow using a simple comparison below.
29-
expect({ ...pdfworkerAPI }).toEqual({
30-
WorkerMessageHandler,
31-
});
40+
expect({ ...pdfworkerAPI }).toEqual(expectedAPI);
41+
42+
expect(Object.keys(globalThis.pdfjsWorker).sort()).toEqual(
43+
Object.keys(expectedAPI).sort()
44+
);
3245
});
3346
});

0 commit comments

Comments
 (0)