Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
7 tasks done
JR93 opened this issue Aug 16, 2021 · 11 comments · Fixed by #8561
Closed
7 tasks done

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

JR93 opened this issue Aug 16, 2021 · 11 comments · Fixed by #8561

Comments

@JR93
Copy link

JR93 commented Aug 16, 2021

Describe the bug

When the project use [email protected] 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

@YuYang019
Copy link

YuYang019 commented Aug 17, 2021

I got the same problem and I found a related issue: #4221

@keith0305
Copy link

keith0305 commented Sep 6, 2021

I got the same issue, any known workaround for this?

@ygj6
Copy link
Member

ygj6 commented 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).

@mingyuyuyu
Copy link

mingyuyuyu commented 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

@vamsidotdev
Copy link

Same issue for me. Has anyone found a workaround for this?

@paina-ma
Copy link

paina-ma commented Dec 2, 2021

mark

@mingyuyuyu
Copy link

i resolve this issue, it can write a plugin to resolve it

@Magisk2
Copy link

Magisk2 commented Feb 17, 2022

i also want to know how solved it. Can you talk about it? thank's

@shinjie1210
Copy link

i resolve this issue, it can write a plugin to resolve it

can you talk about how to resolve this? tks

@mingyuyuyu
Copy link

maybe you can use this plugin to edit assets file name

export default () => {
  let config

  return {
    apply: 'build',
    enforce: 'pre',

    configResolved(cf) {
      config = cf;
    },

    async resolveId(id, importer, options) {
      if (config.assetsInclude(id)) {
        const resolution = await this.resolve(id, importer, { skipSelf: true, ...options });
        if (!resolution) return null;

        // you can get current asset url
        const { id: url } = resolution;
        // you can write new url name by return val
        return xxxx;
      }
      return null
    },
  }
}

@shinjie1210
Copy link

i resolve this issue, it can write a plugin to resolve it

can you talk about how to resolve this? tks

thanks a lot

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants