Skip to content

Commit 1ccc82b

Browse files
Merge pull request #19733 from Snuffleupagus/import-vite-ignore
[api-minor] Attempt to improve support for using the PDF.js builds with Vite
2 parents 97bf09b + 8bcc366 commit 1ccc82b

File tree

8 files changed

+19
-12
lines changed

8 files changed

+19
-12
lines changed

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default [
7676
globals: {
7777
...globals.worker,
7878
PDFJSDev: "readonly",
79-
__non_webpack_import__: "readonly",
79+
__raw_import__: "readonly",
8080
},
8181

8282
ecmaVersion: 2025,

external/builder/babel-plugin-pdfjs-preprocessor.mjs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,22 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
153153
path.replaceWith(t.inherits(t.valueToNode(result), path.node));
154154
}
155155

156-
if (t.isIdentifier(node.callee, { name: "__non_webpack_import__" })) {
156+
if (t.isIdentifier(node.callee, { name: "__raw_import__" })) {
157157
if (node.arguments.length !== 1) {
158-
throw new Error("Invalid `__non_webpack_import__` usage.");
158+
throw new Error("Invalid `__raw_import__` usage.");
159159
}
160-
// Replace it with a standard `import`-call and
161-
// ensure that Webpack will leave it alone.
160+
// Replace it with a standard `import`-call and attempt to ensure that
161+
// various bundlers will leave it alone; this *must* include Webpack.
162162
const source = node.arguments[0];
163163
source.leadingComments = [
164164
{
165165
type: "CommentBlock",
166166
value: "webpackIgnore: true",
167167
},
168+
{
169+
type: "CommentBlock",
170+
value: "@vite-ignore",
171+
},
168172
];
169173
path.replaceWith(t.importExpression(source));
170174
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { Test } from "import-name";
22
import { Test2 } from './non-alias';
33
export { Test3 } from "import-name";
4-
await import(/*webpackIgnore: true*/"./non-alias");
4+
await import(
5+
/*webpackIgnore: true*/
6+
/*@vite-ignore*/
7+
"./non-alias");
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { Test } from 'import-alias';
22
import { Test2 } from './non-alias';
33
export { Test3 } from 'import-alias';
4-
await __non_webpack_import__("./non-alias");
4+
await __raw_import__("./non-alias");

src/core/jpx.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class JpxImage {
5757
try {
5858
const mod = await (typeof PDFJSDev === "undefined"
5959
? import(path) // eslint-disable-line no-unsanitized/method
60-
: __non_webpack_import__(path));
60+
: __raw_import__(path));
6161
instance = mod.default();
6262
} catch (e) {
6363
warn(`JpxImage#getJsModule: ${e}`);

src/display/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2416,7 +2416,7 @@ class PDFWorker {
24162416
const worker =
24172417
typeof PDFJSDev === "undefined"
24182418
? await import("pdfjs/pdf.worker.js")
2419-
: await __non_webpack_import__(this.workerSrc);
2419+
: await __raw_import__(this.workerSrc);
24202420
return worker.WorkerMessageHandler;
24212421
};
24222422

web/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ const PDFViewerApplication = {
286286
const { PDFBug } =
287287
typeof PDFJSDev === "undefined"
288288
? await import(AppOptions.get("debuggerSrc")) // eslint-disable-line no-unsanitized/method
289-
: await __non_webpack_import__(AppOptions.get("debuggerSrc"));
289+
: await __raw_import__(AppOptions.get("debuggerSrc"));
290290

291291
this._PDFBug = PDFBug;
292292
};
@@ -299,7 +299,7 @@ const PDFViewerApplication = {
299299
if (typeof PDFJSDev === "undefined") {
300300
globalThis.pdfjsWorker = await import("pdfjs/pdf.worker.js");
301301
} else {
302-
await __non_webpack_import__(PDFWorker.workerSrc);
302+
await __raw_import__(PDFWorker.workerSrc);
303303
}
304304
} catch (ex) {
305305
console.error("_parseHashParams:", ex);

web/generic_scripting.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class GenericScripting {
3939
const sandbox =
4040
typeof PDFJSDev === "undefined"
4141
? import(sandboxBundleSrc) // eslint-disable-line no-unsanitized/method
42-
: __non_webpack_import__(sandboxBundleSrc);
42+
: __raw_import__(sandboxBundleSrc);
4343
sandbox
4444
.then(pdfjsSandbox => {
4545
resolve(pdfjsSandbox.QuickJSSandbox());

0 commit comments

Comments
 (0)