简体   繁体   中英

PDF file gets exlcluded from build files Vite

My package.json looks like this:

{
    "name": "portfolio",
    "version": "1.0.0",
    "author": "Udbhav Prasad",
    "license": "MIT",
    "engines": {
        "node": "14.x"
    },
    "files": [
        "dist/UdbhavPrasad-Resume.pdf"
    ],
    "scripts": {
        "dev": "vite",
        "build": "vite build",
        "start": "vite preview"
    },
    "dependencies": {
        "three": "^0.128.0",
        "vite": "^2.3.3"
    }
}

But when I build it, in /dist is excludes the resume and wont serve it when GET request is sent

Thank you

Well the easiest way to get your pdf file built is simply to place it at the root of your /public folder. That way it would appear at the root of your /dist folder once built.

See https://vitejs.dev/guide/assets.html#the-public-directory for more info.

Suppose your HTML look like this:

<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>PDF</title>
    </head>
    <body>
        <a download="" href="./pdf/UdbhavPrasad-Resume.pdf" class="cv-download">Download CV</a>
        
        <script type="module" src="./js/main.js"></script>
    </body>
</html>

Then in your main.js file, you can get the URL with ?url . then set the URL to your preferred place. That's it.

import pdfURL from "../pdf/UdbhavPrasad-Resume.pdf?url";

document.querySelector(".cv-download").href = pdfURL;

For more information read this https://vitejs.dev/guide/assets.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM