vite icon indicating copy to clipboard operation
vite copied to clipboard

change root path, entry html's source require will not found

Open huchangfa123 opened this issue 3 years ago • 7 comments

Describe the bug

After changing root path, in vite.config . image source which required in entry html , will not found.

image image

Reproduction

https://stackblitz.com/edit/vitejs-vite-1dynea?file=ss%2Findex.html,vite.config.js&terminal=dev

System Info

System:
    OS: macOS 10.15.7
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 149.41 MB / 32.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.17.0
    Yarn: 1.22.10
    npm: 6.14.13
  Browsers:
    Chrome: 92.0.4515.159
    Safari: 14.1.2
  npmPackages:
    vite: ^2.5.0 => 2.5.0

Used Package Manager

yarn

Logs

No response

Validations

huchangfa123 avatar Aug 27 '21 04:08 huchangfa123

I can reproduce this with the react template and moving the index.html to a folder. It's very similar to https://github.com/vitejs/vite/issues/4760. Likely the same core issue with Vite now allowing or incorrectly resolving files outside of the root.

bluwy avatar Mar 18 '22 06:03 bluwy

Here's how I got it working the way I wanted it:

export default defineConfig({
  envDir: __dirname,
  publicDir: Path.join(__dirname, "public"),
  root: Path.join(__dirname, "src"),
  build: {
    outDir: Path.join(__dirname, "dist"),
    rollupOptions: {
      input: [Path.join(__dirname, "src/index.html")],
    },
  },
  plugins: [
    viteReact(),
  ],
  resolve: {
    alias: {
      "@": Path.resolve(__dirname, "./src"),
    },
  },
});

ghost avatar Feb 07 '23 05:02 ghost

I have looked at the related problems you mentioned in the past, but I have not found relevant solutions in them

zhang592135258 avatar Apr 17 '24 01:04 zhang592135258

@waynebloss what's your folder structure

Rey-Wang avatar Apr 27 '24 06:04 Rey-Wang

@Rey-Wang

  • dist/
  • public/
  • src/
    • index.html

And the vite config is on the root level with dist, public, src…

ghost avatar Apr 27 '24 13:04 ghost

#16629 should make this easier. The idea is you'd keep root set to the actual root directory and set entryRoot to "src" or wherever your index.html is kept.

aleclarson avatar May 07 '24 22:05 aleclarson