代码之家  ›  专栏  ›  技术社区  ›  Álvaro

Vite构建到其相对dist文件夹

  •  0
  • Álvaro  · 技术社区  · 1 年前

    我已经开始使用Vite,在开发中一切都很好,但在进行构建时 没有显示任何内容,因为它生成到根 / 这是一个问题,因为我想将其部署到中的服务器 https://example.com/iframes/app 这样我就可以使用iframe访问这个url了。

    所以我改变了 vite.confi.js

    base: './', // This will ensure that paths are relative to the current directory
    build: {
      outDir: 'dist',
      assetsDir: 'assets',
    },
    

    这适用于除上的所有资产之外的所有内容 public ,在dev上一切都很好,但在构建时,浏览器会在服务器的根级别查找它

    `https://example.com/asset-on-public.svg`
    

    而不是所需的

    `https://example.com/iframes/app/asset-on-public.svg`
    

    有什么想法吗?

    2 回复  |  直到 1 年前
        1
  •  1
  •   AKX Bryan Oakley    1 年前

    As documented 这个 base URL应为 './' 如果您想要“嵌入式部署”,即Vite使用相对URL。

    如果你想让Vite package, transform, etc. your assets, you need to import them (默认情况下,它们将变成URL字符串),例如:

    import closeSvg from './close.svg';
    
    function CloseButton() {
      return <img src={closeSvg} />;
    }
    
    
    function CloseBackgroundDiv() {
      return <div style={{backgroundImage: `url(${closeSvg})`}} />;
    }
    
        2
  •  0
  •   Lalit Pagare    1 年前

    在vite-config文件中更改基本配置,然后尝试运行npm-run-build或yarn-run-build。

        export default defineConfig({
         plugins: [react()],
         base: "./",
        });
    

    然后,在终端中(如果您使用的是npm)

    npm run build
    

    出现此问题的原因是未正确指定dist文件夹的基,因此请尝试更改vite配置文件中的基。