How do you actually reference files handled by this plugin? #184
Answered
by
sapphi-red
NullVoxPopuli
asked this question in
Q&A
-
For example, I have this code: import { createRequire } from 'node:module';
import { viteStaticCopy } from 'vite-plugin-static-copy';
const require = createRequire(import.meta.url);
const packageName = '@pdftron/webviewer';
const pdfTronVersion = require('@pdftron/webviewer/package.json').version;
/**
* NOTE: it's recommended to just use the public directory or an import in javascript.
* but pdfTron expects its own files to not be messed with by any build process.
*/
export const pdfTron = () => [
viteStaticCopy({
structured: true,
targets: [
{
src: 'node_modules/@pdftron/webviewer/public/core',
dest: `assets/webviewer/${pdfTronVersion}/core`,
},
],
}),
{
name: 'ab:configure-pdf-tron',
/**
* We don't want to import `@pdftron/webviewer` directly, because it's already been built for consuming.
* processing it again would slow down the build.
*
* If we were to accidentally process @pdftron/webviewer, we run in to an issue with the code they've provided us.
* "return" is not valid outside a function.
*/
configResolved(config) {
config.optimizeDeps ||= {};
config.optimizeDeps.exclude ||= [];
if (!config.optimizeDeps.exclude.includes(packageName)) {
config.optimizeDeps.exclude.push(packageName);
}
},
},
]; and
and then this in my browser/runtime code: const coreURL = new URL('/assets/webviewer/10.10.1/core/webviewer-core.min.js?url', import.meta.url).href;
const pdfNetURL = new URL('/assets/webviewer/10.10.1/core/pdf/PDFNet.js?url', import.meta.url).href;
const core = document.createElement('script');
const pdfnet = document.createElement('script');
let coreResolve;
let pdfnetResolve;
const promises = Promise.all([
new Promise((resolve) => {
coreResolve = resolve;
}),
new Promise((resolve) => {
pdfnetResolve = resolve;
}),
]);
Object.assign(core, { src: coreURL, onload: coreResolve });
Object.assign(pdfnet, { src: coreURL, onload: pdfnetResolve });
core.src = coreURL;
pdfnet.src = pdfNetURL;
document.head.appendChild(core);
document.head.appendChild(pdfnet);
await promises; yet,
I suspect that the way I constructed the URL is incorrect, as it starts with |
Beta Was this translation helpful? Give feedback.
Answered by
sapphi-red
Jul 9, 2025
Replies: 1 comment 10 replies
-
The referenced paths were wrong. Because |
Beta Was this translation helpful? Give feedback.
10 replies
Answer selected by
NullVoxPopuli
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The referenced paths were wrong. Because
structured: true
keep the structure, the files are placed under/assets/webviewer/${version}/core/node_modules/@pdftron/webviewer/public/core/
.https://stackblitz.com/edit/vitejs-vite-wtmrbftm?file=vite.config.js,src%2Fmain.js&terminal=dev