Skip to content

Vite deduplication causing problems with non-deterministic asset order #2456

@mvolfik

Description

@mvolfik

Describe the bug

Vite (thankfully) deduplicates byte-to-byte equal asset files. In my project I create the assets dynamically with a custom plugin using pluginContext.emitFile(). However, I heavily use async, so the order of the emit calls is not deterministic, therefore the single file from vite deduplication has different name in client-side and server-side code. And I build with adapter-static, which crawls ssr code, but copies client assets, so it finds a non-existing file.

Reproduction

https://github.com/mvolfik/probable-waffle

In plugin.js, the same file is emitted for both imports with different name. Manual delays are added to emit kitten2 first in ssr mode, and kitten1 otherwise.

Logs

$ svelte-kit build
vite v2.5.8 building for production...
transforming (1) .svelte-kit/build/components/layout.svelte
✓ 16 modules transformed.
.svelte-kit/output/client/_app/assets/kitten1-d3409436          9.52 KiB
.svelte-kit/output/client/_app/manifest.json                    1.26 KiB
.svelte-kit/output/client/_app/layout.svelte-99156149.js        0.53 KiB / brotli: 0.31 KiB
.svelte-kit/output/client/_app/error.svelte-4b13167a.js         1.55 KiB / brotli: 0.64 KiB
.svelte-kit/output/client/_app/pages/index.svelte-df531a7b.js   1.25 KiB / brotli: 0.55 KiB
.svelte-kit/output/client/_app/assets/start-61d1577b.css        0.16 KiB / brotli: 0.11 KiB
.svelte-kit/output/client/_app/chunks/vendor-82fce94c.js        7.07 KiB / brotli: 2.63 KiB
.svelte-kit/output/client/_app/start-f0c4fa82.js                17.64 KiB / brotli: 5.62 KiB
vite v2.5.8 building SSR bundle for production...
transforming (1) .svelte-kit/build/app.js
✓ 16 modules transformed.
.svelte-kit/output/server/app.js   51.12 KiB

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-static
> 404 /_app/assets/kitten2-aa8efc02 (linked from /)
Error: 404 /_app/assets/kitten2-aa8efc02 (linked from /)
    at file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:86:11
    at visit (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:213:5)
    at async visit (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:281:6)
    at async prerender (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:291:6)
    at async Object.prerender (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:352:4)
    at async adapt (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/adapter-static/index.js:13:4)
    at async adapt (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:377:2)
    at async file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/cli.js:877:5
error Command failed with exit code 1.

(removed irrelevant warnings for missing <img alt )

System Info

System:
    OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (4) x64 Intel(R) Core(TM) i3-3110M CPU @ 2.40GHz
    Memory: 290.81 MB / 7.35 GB
    Container: Yes
    Shell: 3.1.0 - /usr/bin/fish
  Binaries:
    Node: 14.17.6 - /usr/bin/node
    Yarn: 1.22.11 - ~/.local/bin/yarn
    npm: 7.21.0 - ~/.local/bin/npm
  Browsers:
    Chromium: 93.0.4577.82
    Firefox: 92.0
  npmPackages:
    @sveltejs/adapter-static: next => 1.0.0-next.19 
    @sveltejs/kit: next => 1.0.0-next.169 
    svelte: ^3.42.6 => 3.42.6

Severity

blocking an upgrade

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp3-edge-caseSvelteKit cannot be used in an uncommon wayvite

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions