output.assetFileNames does not work when the project use @vitejs/plugin-legacy

See original GitHub issue

Describe the bug

When the project use vite@2.5.0-beta.3 and @vitejs/plugin-legacy, output.assetFileNames does not work, and the polyfills-legacy file is outside the js directory. But it works well when remove @vitejs/plugin-legacy.

vite.config.js:

import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'
import legacy from '@vitejs/plugin-legacy'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [reactRefresh(), legacy()],
  build: {
    rollupOptions: {
      output: {
        entryFileNames: 'assets/js/[name]-[hash].js',
        chunkFileNames: 'assets/js/[name]-[hash].js',
        assetFileNames: assetInfo => {
          if (/\.css$/.test(assetInfo.name)) {
            return 'assets/css/[name]-[hash][extname]'
          }
          return 'assets/img/[name]-[hash][extname]'
        }
      }
    }
  }
})

Because @vitejs/plugin-legacy will create and add an output configuration

packages/plugin-legacy/index.js#L219:

const { rollupOptions } = config.build
const { output } = rollupOptions
if (Array.isArray(output)) {
  rollupOptions.output = [...output.map(createLegacyOutput), ...output]
} else {
  rollupOptions.output = [createLegacyOutput(output), output || {}]
}

When rollupOptions.output is an array, it defaults to ‘<assetsDir>/[name].[hash][extname]’

packages/vite/src/node/plugins/asset.ts#L309:

const output = config.build?.rollupOptions?.output
const assetFileNames =
  (output && !Array.isArray(output) ? output.assetFileNames : undefined) ??
  // defaults to '<assetsDir>/[name].[hash][extname]'
  // slightly different from rollup's one ('assets/[name]-[hash][extname]')
  path.posix.join(config.build.assetsDir, '[name].[hash][extname]')

Reproduction

https://github.com/JR93/vite-asset-filenames-legacy-bug

System Info

System:
    OS: Windows 10 10.0.19041
    CPU: (8) x64 Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz   
    Memory: 8.68 GB / 15.89 GB
  Binaries:
    Node: 12.16.3 - C:\Program Files\nodejs\node.EXE       
    Yarn: 1.22.10 - C:\Program Files\nodejs\yarn.CMD       
    npm: 6.14.4 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1023.0), Chromium (92.0.902.73)
    Internet Explorer: 11.0.19041.906
  npmPackages:
    vite: ^2.5.0-beta.3 => 2.5.0

Used Package Manager

yarn

Logs

$ vite build
vite v2.5.0 building for production...
✓ 27 modules transformed.
dist/assets/js/index-legacy-21b357f9.js    2.88 KiB / brotli: 1.27 KiB
dist/assets/polyfills-legacy.7969d14b.js   53.90 KiB / brotli: 19.53 KiB
dist/assets/js/vendor-legacy-8de15e13.js   127.96 KiB / brotli: 36.34 KiB
dist/assets/favicon.17e50649.svg     1.49 KiB
dist/assets/logo.ecc203fb.svg        2.61 KiB
dist/index.html                      1.59 KiB
dist/assets/css/index-d93758c6.css   0.77 KiB / brotli: 0.41 KiB
dist/assets/js/index-a1c55ec7.js     1.66 KiB / brotli: 0.68 KiB
dist/assets/js/vendor-9c242a8b.js    127.61 KiB / brotli: 36.05 KiB

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mingyuyuyucommented, Nov 4, 2021

yeah,its issue for me, its not plugin-legacy problem,when output is array,it does not work because plugins/assets.ts take a default assetFileName. i think it’s bad, can provide a config to set custom asset name when output is array? @yyx990803

1reaction
ygj6commented, Sep 6, 2021

In fact, the affected output files are static resource files and special polyfills-legacy files, for static resource files, they are referenced by chunks in all outputs, and for polyfills-legacy files, it is the SystemJS runtime generated when rollupOptions.output.format is system, and it is referenced by legacy chunks in all outputs.

So they are behaviors that occur when you set output options to multiple outputs options, and you can think of them as “public” and it is not necessary to package them into different output directories (plugin-legacy generates multiple outputs).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Certain files not being bundled in ViteJS - Stack Overflow
While bunldling a Vanilla JS project with image assets, using npm run build , there was one image that was not being bundled....
Read more >
rollup-plugin-css-only - npm Package Health Analysis - Snyk
Snyk scans all the packages in your projects for vulnerabilities and ... No known security issues ... By default the plugin will use...
Read more >
rollup-plugin-styles - npm
Start using rollup-plugin-styles in your project by running `npm i rollup-plugin-styles`. There are 108 other projects in the npm registry ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found