From a8592d86a992a619a6d9d1ce1f7b97386b5a6943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 18 Nov 2024 15:04:14 +0100 Subject: [PATCH] Use JSON imports instead of PDFJSDev.json JSON imports are now supported by all tools used in PDF.js' build process. The `chromecom.js` file is bundled by webpack and import attributes are thus removed, so browser compatibility for this new syntax is not relevant. --- eslint.config.mjs | 2 +- .../builder/babel-plugin-pdfjs-preprocessor.mjs | 15 --------------- external/builder/fixtures_babel/evals-expected.js | 3 --- external/builder/fixtures_babel/evals.js | 1 - external/builder/fixtures_babel/evals.json | 1 - gulpfile.mjs | 1 + web/chromecom.js | 10 +++++----- 7 files changed, 7 insertions(+), 26 deletions(-) delete mode 100644 external/builder/fixtures_babel/evals.json diff --git a/eslint.config.mjs b/eslint.config.mjs index 41ffad5d3f5f6..a9e4f4687e88b 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -77,7 +77,7 @@ export default [ __non_webpack_import__: "readonly", }, - ecmaVersion: 2022, + ecmaVersion: 2025, sourceType: "module", }, diff --git a/external/builder/babel-plugin-pdfjs-preprocessor.mjs b/external/builder/babel-plugin-pdfjs-preprocessor.mjs index 8f8be7a724e75..ff92a7a765022 100644 --- a/external/builder/babel-plugin-pdfjs-preprocessor.mjs +++ b/external/builder/babel-plugin-pdfjs-preprocessor.mjs @@ -1,10 +1,7 @@ import { types as t, transformSync } from "@babel/core"; -import fs from "fs"; -import { join as joinPaths } from "path"; import vm from "vm"; const PDFJS_PREPROCESSOR_NAME = "PDFJSDev"; -const ROOT_PREFIX = "$ROOT/"; function isPDFJSPreprocessor(obj) { return obj.type === "Identifier" && obj.name === PDFJS_PREPROCESSOR_NAME; @@ -40,18 +37,6 @@ function handlePreprocessorAction(ctx, actionName, args, path) { return result; } break; - case "json": - if (!t.isStringLiteral(arg)) { - throw new Error("Path to JSON is not provided"); - } - let jsonPath = arg.value; - if (jsonPath.startsWith(ROOT_PREFIX)) { - jsonPath = joinPaths( - ctx.rootPath, - jsonPath.substring(ROOT_PREFIX.length) - ); - } - return JSON.parse(fs.readFileSync(jsonPath, "utf8")); } throw new Error("Unsupported action"); } catch (e) { diff --git a/external/builder/fixtures_babel/evals-expected.js b/external/builder/fixtures_babel/evals-expected.js index 510e62f1bb523..71eee73d1e89d 100644 --- a/external/builder/fixtures_babel/evals-expected.js +++ b/external/builder/fixtures_babel/evals-expected.js @@ -10,9 +10,6 @@ var g = { }, j: 2 }; -var h = { - test: "test" -}; var i = '0'; var j = { i: 1 diff --git a/external/builder/fixtures_babel/evals.js b/external/builder/fixtures_babel/evals.js index 94ec543b04d19..cabeb0746a949 100644 --- a/external/builder/fixtures_babel/evals.js +++ b/external/builder/fixtures_babel/evals.js @@ -5,7 +5,6 @@ var d = PDFJSDev.test('FALSE'); var e = PDFJSDev.eval('TRUE'); var f = PDFJSDev.eval('TEXT'); var g = PDFJSDev.eval('OBJ'); -var h = PDFJSDev.json('$ROOT/external/builder/fixtures_babel/evals.json'); var i = typeof PDFJSDev === 'undefined' ? PDFJSDev.eval('FALSE') : '0'; var j = typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('OBJ.obj') : '0'; var k = !PDFJSDev.test('TRUE'); diff --git a/external/builder/fixtures_babel/evals.json b/external/builder/fixtures_babel/evals.json deleted file mode 100644 index 000642c425cbf..0000000000000 --- a/external/builder/fixtures_babel/evals.json +++ /dev/null @@ -1 +0,0 @@ -{ "test": "test" } \ No newline at end of file diff --git a/gulpfile.mjs b/gulpfile.mjs index 703053f536726..403d606fee8d8 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -382,6 +382,7 @@ function createWebpackConfig( module: { rules: [ { + test: /\.[mc]?js$/, loader: "babel-loader", exclude: babelExcludeRegExp, options: { diff --git a/web/chromecom.js b/web/chromecom.js index 1ece304791c1d..a2eec5c347ec3 100644 --- a/web/chromecom.js +++ b/web/chromecom.js @@ -20,6 +20,9 @@ import { BasePreferences } from "./preferences.js"; import { GenericL10n } from "./genericl10n.js"; import { GenericScripting } from "./generic_scripting.js"; +// These strings are from chrome/app/resources/generated_resources_*.xtb. +import i18nFileAccessLabels from "./chrome-i18n-allow-access-to-file-urls.json" with { type: "json" }; + if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("CHROME")) { throw new Error( 'Module "pdfjs-web/chromecom" shall not be used outside CHROME build.' @@ -194,11 +197,8 @@ function requestAccessToLocalFile(fileUrl, overlayManager, callback) { // Use Chrome's definition of UI language instead of PDF.js's #lang=..., // because the shown string should match the UI at chrome://extensions. - // These strings are from chrome/app/resources/generated_resources_*.xtb. - const i18nFileAccessLabel = PDFJSDev.json( - "$ROOT/web/chrome-i18n-allow-access-to-file-urls.json" - )[chrome.i18n.getUILanguage?.()]; - + const i18nFileAccessLabel = + i18nFileAccessLabels[chrome.i18n.getUILanguage?.()]; if (i18nFileAccessLabel) { document.getElementById("chrome-file-access-label").textContent = i18nFileAccessLabel;