Output JavaScript modules for the LIB build-target (PR 17055 follow-up)

This *finally* allows us to mark the entire PDF.js library as a "module", which should thus conclude the (multi-year) effort to re-factor and improve how we import files/resources in the code-base.

This also means that the `gulp ci-test` target, which is what's run in GitHub Actions, now uses JavaScript modules since that's supported in modern Node.js versions.
This commit is contained in:
Jonas Jenwald 2023-10-13 12:11:29 +02:00
parent 96258449e3
commit 38245500fd
9 changed files with 58 additions and 102 deletions

View file

@ -12,10 +12,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals __non_webpack_import__ */
import {
AbortException,
assert,
isNodeJS,
MissingPDFException,
PromiseCapability,
} from "../shared/util.js";
@ -30,10 +32,18 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
);
}
let fs, http, https, url;
if (isNodeJS) {
// Native packages.
fs = await __non_webpack_import__("fs");
http = await __non_webpack_import__("http");
https = await __non_webpack_import__("https");
url = await __non_webpack_import__("url");
}
const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;
function parseUrl(sourceUrl) {
const { url } = globalThis.__pdfjsPackages__;
const parsedUrl = url.parse(sourceUrl);
if (parsedUrl.protocol === "file:" || parsedUrl.host) {
return parsedUrl;
@ -339,13 +349,11 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._request = null;
if (this._url.protocol === "http:") {
const { http } = globalThis.__pdfjsPackages__;
this._request = http.request(
createRequestOptions(this._url, stream.httpHeaders),
handleResponse
);
} else {
const { https } = globalThis.__pdfjsPackages__;
this._request = https.request(
createRequestOptions(this._url, stream.httpHeaders),
handleResponse
@ -388,13 +396,11 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {
this._request = null;
if (this._url.protocol === "http:") {
const { http } = globalThis.__pdfjsPackages__;
this._request = http.request(
createRequestOptions(this._url, this._httpHeaders),
handleResponse
);
} else {
const { https } = globalThis.__pdfjsPackages__;
this._request = https.request(
createRequestOptions(this._url, this._httpHeaders),
handleResponse
@ -419,7 +425,6 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
path = path.replace(/^\//, "");
}
const { fs } = globalThis.__pdfjsPackages__;
fs.lstat(path, (error, stat) => {
if (error) {
if (error.code === "ENOENT") {
@ -449,7 +454,6 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader {
path = path.replace(/^\//, "");
}
const { fs } = globalThis.__pdfjsPackages__;
this._setReadableStream(fs.createReadStream(path, { start, end: end - 1 }));
}
}