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

feat: add asset name (#3028) #3050

Merged
merged 4 commits into from
Aug 9, 2021
Merged

feat: add asset name (#3028) #3050

merged 4 commits into from
Aug 9, 2021

Conversation

doabit
Copy link
Contributor

@doabit doabit commented Apr 19, 2021

Description

Add name property for asset type bundle. The PR can solve the problem for #3028.

Additional context

It can solve the #3026 and other similar problem too. With the asset name, everyone can generate custom manifest format for need. eg the old manifest format

export default {
  root: './src',
  build: {
    manifest: false,
    rollupOptions: {
      input: './src/entry/main.js'
    }
  },
  plugins: [
    {
      async generateBundle(options, bundle) {
        let manifest = {}
        for (const file in bundle) {
          const chunk = bundle[file]
          if (chunk.type === 'chunk') {
            if (chunk.isEntry) {
              manifest[chunk.name + '.js'] = {
                file: chunk.fileName,
                imports: chunk.imports
              }
            }
          } else if (chunk.name) {
            manifest[chunk.name] = { file: chunk.fileName }
          }
        }
        this.emitFile({
          fileName: 'manifest.json',
          type: 'asset',
          source: JSON.stringify(manifest, null, 2)
        })
      }
    }
  ]
}

The manifest.json should looks like

{
  "images/favicon.svg": {
    "file": "assets/favicon.17e50649.svg"
  },
  "images/demo.png": {
    "file": "assets/demo.098465ec.png"
  },
  "font/font.ttf": {
    "file": "assets/font.a0d06a46.ttf"
  },
  "main.js": {
    "file": "assets/main.c58b35ad.js",
    "imports": []
  },
  "main.css": {
    "file": "assets/main.babcb300.css"
  }
}

Then can get the assets/demo.098465ec.png by images/demo.png for backend.

src
├── entry
│   └── main.js
├── font
│   └── font.ttf
├── images
│   ├── demo.png
│   └── favicon.svg
└── style
    └── style.css

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sanny-io
Copy link

I hope this gets added. I'm kind of confused right now how we are supposed to make use of manifest.json in its current state.

@patak-dev
Copy link
Member

We discussed this with Evan and we have his approval to merge this feat. @doabit would you merge main so we test this with the latest CI and test suite?

@doabit
Copy link
Contributor Author

doabit commented Jun 7, 2021

@patak-js Merged.

patak-dev
patak-dev previously approved these changes Jun 7, 2021
@patak-dev patak-dev dismissed their stale review June 7, 2021 07:00

See comment

patak-dev
patak-dev previously approved these changes Jun 7, 2021
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm rechecking the docs for https://rollupjs.org/guide/en/#thisemitfileemittedfile-emittedchunk--emittedasset--string, name is also a way to define the output fileName using the assetFileNames pattern in case fileName is not provided to emmitFile. We are providing fileName, so whatever name we use is ignored here.
@doabit Just to check here, do you have an example where this pattern is used elsewhere in the rollup ecosystem? Even if this not standard, I think it makes sense in our context.

@Shinigami92 Shinigami92 added the p2-nice-to-have Not breaking anything but nice to have (priority) label Jun 7, 2021
@doabit
Copy link
Contributor Author

doabit commented Jun 8, 2021

@patak-js Sorry, for me, this PR is just to solve the problem of custom manifest.json format, because of i'm using ruby on rails with vite and the default manifest.json can't work well for me.

antfu
antfu previously approved these changes Aug 9, 2021
@antfu antfu dismissed stale reviews from patak-dev and themself via 4b79656 August 9, 2021 05:22
@antfu antfu merged commit a055938 into vitejs:main Aug 9, 2021
aleclarson pushed a commit to aleclarson/vite that referenced this pull request Nov 8, 2021
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

manifest asset should use filename as key
5 participants