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

Library Mode: esm is not minified but cjs and umd are minified #8848

Closed
7 tasks done
guoyunhe opened this issue Jun 29, 2022 · 2 comments
Closed
7 tasks done

Library Mode: esm is not minified but cjs and umd are minified #8848

guoyunhe opened this issue Jun 29, 2022 · 2 comments

Comments

@guoyunhe
Copy link

Describe the bug

In Library Mode, we usually expect that the output (CJS, ESM, UMD) are not minified. This is easier for debugging when developing applications.

ESM looks good:

function foobar() {
  console.log("foobar");
}
export { foobar as default };

But vite will minify CJS and UMD:

"use strict";function o(){console.log("foobar")}module.exports=o;
(function(e,o){typeof exports=="object"&&typeof module!="undefined"?module.exports=o():typeof define=="function"&&define.amd?define(o):(e=typeof globalThis!="undefined"?globalThis:e||self,e.Foobar=o())})(this,function(){"use strict";function e(){console.log("foobar")}return e});

I know build.minify: false can turn this off. But it is better to make it automatic.

Reproduction

https://stackblitz.com/edit/vitejs-vite-fdafhp?file=vite.config.js,index.js,dist%2Fvite-starter.cjs.js&terminal=dev

System Info

System:
    OS: macOS 12.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 798.25 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.14.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.3.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 103.0.5060.53
    Edge: 103.0.1264.37
    Firefox: 100.0.2
    Safari: 15.5

Used Package Manager

npm

Logs

No response

Validations

@bluwy
Copy link
Member

bluwy commented Jun 29, 2022

Minify is intentionally on by default in lib mode. es is not minified as it messes with tree-shaking, though partial minification is supported in Vite 3 with #8811. There's also #6555 to completely minify regardless of treeshaking issues. If minifying is not desirable, build.minify: false should be set.

@bluwy bluwy closed this as completed Jun 29, 2022
@songlairui
Copy link
Contributor

songlairui commented Jul 10, 2022

not a bug.

minify has no effect for build phase,
rollupOptions.compact only effect module wrapper, rollup-doc output.compact

you can minify by rollup plugin

vite.config.ts

import { terser } from "rollup-plugin-terser";

export default defineConfig(() => ({
  build: {
    lib: {
        entry: '...',
        formats:['es']
    },
    rollupOptions: {
        // only for build phase, # https://vitejs.dev/guide/api-plugin.html#rollup-plugin-compatibility
        plugins: [terser()],
    }
  }
}))

@github-actions github-actions bot locked and limited conversation to collaborators Jul 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants