1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

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 }));
}
}

View file

@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals __non_webpack_import__, __non_webpack_require__ */
/* globals __non_webpack_import__ */
import {
BaseCanvasFactory,
@ -28,46 +28,17 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
);
}
if (isNodeJS && !globalThis.__pdfjsPackages__) {
let fs, http, https, url, canvas, path2d_polyfill;
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("LIB")) {
// Native packages.
fs = __non_webpack_require__("fs");
http = __non_webpack_require__("http");
https = __non_webpack_require__("https");
url = __non_webpack_require__("url");
// Optional, third-party, packages.
try {
canvas = __non_webpack_require__("canvas");
} catch {}
try {
path2d_polyfill = __non_webpack_require__("path2d-polyfill");
} catch {}
} else {
// 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");
// Optional, third-party, packages.
try {
canvas = await __non_webpack_import__("canvas");
} catch {}
try {
path2d_polyfill = await __non_webpack_import__("path2d-polyfill");
} catch {}
}
globalThis.__pdfjsPackages__ = {
CanvasRenderingContext2D: canvas?.CanvasRenderingContext2D,
createCanvas: canvas?.createCanvas,
DOMMatrix: canvas?.DOMMatrix,
fs,
http,
https,
polyfillPath2D: path2d_polyfill?.polyfillPath2D,
url,
};
let fs, canvas, path2d_polyfill;
if (isNodeJS) {
// Native packages.
fs = await __non_webpack_import__("fs");
// Optional, third-party, packages.
try {
canvas = await __non_webpack_import__("canvas");
} catch {}
try {
path2d_polyfill = await __non_webpack_import__("path2d-polyfill");
} catch {}
}
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("SKIP_BABEL")) {
@ -75,7 +46,7 @@ if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("SKIP_BABEL")) {
if (globalThis.DOMMatrix || !isNodeJS) {
return;
}
const { DOMMatrix } = globalThis.__pdfjsPackages__;
const DOMMatrix = canvas?.DOMMatrix;
if (DOMMatrix) {
globalThis.DOMMatrix = DOMMatrix;
@ -88,8 +59,8 @@ if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("SKIP_BABEL")) {
if (globalThis.Path2D || !isNodeJS) {
return;
}
const { CanvasRenderingContext2D, polyfillPath2D } =
globalThis.__pdfjsPackages__;
const CanvasRenderingContext2D = canvas?.CanvasRenderingContext2D;
const polyfillPath2D = path2d_polyfill?.polyfillPath2D;
if (CanvasRenderingContext2D && polyfillPath2D) {
globalThis.CanvasRenderingContext2D = CanvasRenderingContext2D;
@ -102,7 +73,6 @@ if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("SKIP_BABEL")) {
const fetchData = function (url) {
return new Promise((resolve, reject) => {
const { fs } = globalThis.__pdfjsPackages__;
fs.readFile(url, (error, data) => {
if (error || !data) {
reject(new Error(error));
@ -120,8 +90,7 @@ class NodeCanvasFactory extends BaseCanvasFactory {
* @ignore
*/
_createCanvas(width, height) {
const { createCanvas } = globalThis.__pdfjsPackages__;
return createCanvas(width, height);
return canvas.createCanvas(width, height);
}
}