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

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

Closed
Snuffleupagus opened this issue Aug 5, 2024 · 1 comment · Fixed by #18562
Closed

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

Snuffleupagus opened this issue Aug 5, 2024 · 1 comment · Fixed by #18562
Labels

Comments

@Snuffleupagus
Copy link
Collaborator

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.

@nicolo-ribaudo
Copy link
Contributor

I'll take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants