PDF.js worker source inlined as base64 in library mode. #17958
Unanswered
theoforcier
asked this question in
Q&A
Replies: 2 comments
-
Facing a similar issue, is there a way to access the 'build' & get PDF worker's src? |
Beta Was this translation helpful? Give feedback.
0 replies
-
pdfjs.js import path from "node:path";
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
const prefix = 'virtual:pdfjs-dist/';
let pdfjsCommand = 'serve';
export default {
name: 'pdfjs-plugin',
config(config, { command }) {
pdfjsCommand = command;
},
resolveId(source) {
if (source.startsWith(prefix)) {
return '\0' + source;
}
return null;
},
async load(id) {
if (id.startsWith('\0' + prefix)) {
const idUrl = new URL(id.substring(1));
if (pdfjsCommand === 'serve') {
return {
code: `import workerUrl from "${idUrl.pathname}?url"; export default workerUrl;`,
map: null
};
}
return {
code: `const file = new URL("../${idUrl.pathname}", import.meta.url); export default file;`,
map: null
};
}
return null;
},
async generateBundle(options, bundle) {
const destFilePath = path.resolve(options.dir, 'pdfjs-dist/build');
const destDir = path.dirname(destFilePath);
if (!existsSync(destDir)) {
await fs.mkdir(destDir, { recursive: true });
}
const pdfjsFolder = new URL('.', import.meta.resolve('pdfjs-dist'));
await fs.cp(pdfjsFolder, destFilePath, {recursive: true});
}
}; vite.config.js import pdfjsPlugin from './pdfjs.js';
export default defineConfig({
plugins: [pdfjsPlugin]
}); and then in your code import workerUrl from 'virtual:pdfjs-dist/build/pdf.worker.min.mjs';
import sandboxUrl from 'virtual:pdfjs-dist/build/pdf.sandbox.min.mjs'; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to provide the path for pdfjs-dist worker as such:
However my CSP refuses to load the script due to it being inlined by vite as
data:text/javascript;base64...
.I know that Vite's build.assetsInlineLimit is ignored when using build.lib, and adding
data:
to my CSP's script-src is not an option.I have looked into these related issues:
#3295
#4454
#13172
But cannot find a solution which works for my use case, has anyone found a workaround for something like this?
Beta Was this translation helpful? Give feedback.
All reactions