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

当构建库时使用 rollupOptions.output.banner 会导致打包出错误的 umd #8412

Open
7 tasks done
ddosakura opened this issue May 31, 2022 · 7 comments · Fixed by #8414
Open
7 tasks done

当构建库时使用 rollupOptions.output.banner 会导致打包出错误的 umd #8412

ddosakura opened this issue May 31, 2022 · 7 comments · Fixed by #8414
Labels
feat: library mode p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@ddosakura
Copy link
Contributor

Describe the bug

also see #8163.

I determined that this was caused by #7948 and has not been fixed in #8110.

I currently use version 2.9.5 to avoid this problem.

I also tried v3.0.0-alpha 7. It has not been fixed at present.


我也遇到了 #8163 这个问题。

我确定这是由 #7948 引入的错误的正则替换导致的,并且并未在 #8110 中得到修复。

我目前使用 2.9.5 版本来避免这个问题。

我也尝试使用了v3.0.0-alpha.7,目前并未得到修复。

Reproduction

https://github.com/ddosakura/a-bug-for-vite/blob/master/dist/mylib.umd.js

System Info

Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    Yarn: 1.22.15 - ~/.nvm/versions/node/v16.14.2/bin/yarn
    npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
  npmPackages:
    vite: ^2.9.9 => 2.9.9 

or

  Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    Yarn: 1.22.15 - ~/.nvm/versions/node/v16.14.2/bin/yarn
    npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
  npmPackages:
    vite: ^3.0.0-alpha.7 => 3.0.0-alpha.7

Used Package Manager

pnpm

Logs

No response

Validations

@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels May 31, 2022
@sapphi-red
Copy link
Member

sapphi-red commented May 31, 2022

A plugin like this could be used to workaround it.

const bannerPlugin = {
  name: 'banner',
  enforce: 'post',
  generateBundle(options, bundle) {
    const banner = '/*! banner  */'
    const footer = ''

    for (const module of Object.values(bundle)) {
      if (module.type === 'chunk') {
        module.code = banner + module.code + footer
        // should change source map if needed
      }
    }
  }
}

@sapphi-red sapphi-red added p2-edge-case Bug, but has workaround or limited in scope (priority) and removed p3-minor-bug An edge case that only affects very specific usage (priority) labels May 31, 2022
@ddosakura ddosakura closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2022
@ddosakura ddosakura reopened this May 31, 2022
@ddosakura
Copy link
Contributor Author

ddosakura commented May 31, 2022

像这样的插件可以用来解决它。

const bannerPlugin = {
  name: 'banner',
  enforce: 'post',
  generateBundle(options, bundle) {
    const banner = '/*! banner  */'
    const footer = ''

    for (const module of Object.values(bundle)) {
      if (module.type === 'chunk') {
        module.code = banner + module.code + footer
        // should change source map if needed
      }
    }
  }
}

I found that the key to the bug was not rollupOptions.output.banner, but \n, see #8414 . @sapphi-red

@sapphi-red
Copy link
Member

sapphi-red commented May 31, 2022

That PR will work for comments but it won't work with something like var someValueInjected = Object.assign({}, { value: 'by rollup.banner' });.

That said that PR will fix a bug when build.minify is false. No, actually it didin't.

@sapphi-red
Copy link
Member

Reopening as it is not completely fixed.
#8414 (review)

@abernh
Copy link

abernh commented Oct 7, 2022

I adjusted the workaround plugin slightly to keep using build.rollupOptions.output.banner/.footer in my vite.config.js

// rollup-banner-plugin.js
const RollupBannerPlugin = {
  name: 'banner',
  enforce: 'post',
  generateBundle(options, bundle) {
    const banner = options.banner() || ''
    const footer = options.footer() || ''

    for (const module of Object.values(bundle)) {
      if (module.type === 'chunk') {
        module.code = banner + module.code + footer
        // should change source map if needed
      }
    }
  }
}
// vite.config.js
import RollupBannerPlugin from './rollup-banner-plugin.js'
export default defineConfig({
  build: {
  ...
    rollupOptions: {
      output: {
        banner: '/** Here goes my banner */\n\n',
        footer: '\n\n/** and here some footer notes. */',
      },
      plugins: [
        RollupBannerPlugin
      ]
    }
  }
})

@AlttiRi
Copy link

AlttiRi commented Apr 17, 2023

The workaround works, but I think it should work from the box, when a person uses Vite in the library mode.

@AlttiRi
Copy link

AlttiRi commented Apr 17, 2023

However, there is a bug with this workaround — it duplicates the line with "licence" text.

For the follow text:

// ==UserScript==
// @name        HrefTaker
// @license     GPL-3.0
// @version     0.7.1-2023.4.17
// @namespace   gh.alttiri
// ==/UserScript==

the result is

// ==UserScript==
// @name        HrefTaker
// @license     GPL-3.0
// @version     0.7.1-2023.4.17
// @namespace   gh.alttiri
// ==/UserScript==
// @license     GPL-3.0

with unnecessary appended // @license GPL-3.0.


The raw Rollup works fine:

import {rollup} from "rollup";

const bundle = await rollup({
  input: "./main.js",
});

await bundle.write({
  banner: `// ==UserScript==
// @name        HrefTaker
// @license     GPL-3.0
// @version     0.7.1-2023.4.17
// @namespace   gh.alttiri
// ==/UserScript==\n`,
    dir: "./dist"
});

It seems this issue reason is that Vite (even with minify: false) additionally removes all comments and empty lines from the bundle.

I see no option to disable it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: library mode p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants