Skip to content

Update the Babel plugin to remove empty static initialization blocks #18559

Description

@Snuffleupagus

We have static initialization blocks that include pre-processor statements which make them conditionally empty, note e.g. how the following source code:

pdf.js/src/display/api.js

Lines 2016 to 2054 in c60c0d1

static {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
if (isNodeJS) {
// Workers aren't supported in Node.js, force-disabling them there.
this.#isWorkerDisabled = true;
GlobalWorkerOptions.workerSrc ||= PDFJSDev.test("LIB")
? "../pdf.worker.js"
: "./pdf.worker.mjs";
}
// Check if URLs have the same origin. For non-HTTP based URLs, returns
// false.
this._isSameOrigin = (baseUrl, otherUrl) => {
let base;
try {
base = new URL(baseUrl);
if (!base.origin || base.origin === "null") {
return false; // non-HTTP url
}
} catch {
return false;
}
const other = new URL(otherUrl, base);
return base.origin === other.origin;
};
this._createCDNWrapper = url => {
// We will rely on blob URL's property to specify origin.
// We want this function to fail in case if createObjectURL or Blob do
// not exist or fail for some reason -- our Worker creation will fail
// anyway.
const wrapper = `await import("${url}");`;
return URL.createObjectURL(
new Blob([wrapper], { type: "text/javascript" })
);
};
}
}

is transformed into (in that case for the MOZCENTRAL build):
https://searchfox.org/mozilla-central/rev/b6671f4c3afdb2b36e63c9d176f98a5959907259/toolkit/components/pdfjs/content/build/pdf.mjs#10434

Hence it seems like a good idea to, if possible, extend the Babel plugin to remove static initialization blocks that become empty during building.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions