-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Lib mode format: es.min #6555
Comments
What about #6670 ? :P |
This commit also makes it possible to import the source SCSS styles from dependent code. Caveats/differences from previous setup: - `dist/tagin.module.min.js` isn't actually minified - see vitejs/vite#6555. - no css sourcemap are generated - see vitejs/vite#2830.
In #8754, I've updated so that |
is there any progress about this topic? |
I don't understand, is minifying just disabled entirely for I've tried for example this:
It doesn't minify or mangle at all - I have no control of the terser options at all? I can understand having sensible defaults, so you don't accidentally break tree shaking - but blocking me from configuring terser entirely? And not even a warning - it just doesn't work. Very confusing. At this time, I was able to manually minify with
|
Excuse me, has this problem been solved yet? |
Any solution? |
As a workaround you can use a small plugin to minify the file: import esbuild from 'esbuild';
const plugin = {
name: 'minify',
closeBundle: () => {
esbuild.buildSync({
entryPoints: ['./dist/lib.js'],
minify: true,
allowOverwrite: true,
outfile: './dist/lib.js'
})
}
}; |
Would love to see this implemented! Currently this is my workaround: Add an extra lib: {
entry: './src/index.ts',
formats: ['es', 'esm'],
fileName: (format) => ({
es: `${pkg.name}.js`,
esm: `${pkg.name}.min.js`,
})[format] Then add this plugin to the build: import { transform } from 'esbuild';
function minifyEs() {
return {
name: 'minifyEs',
renderChunk: {
order: 'post',
async handler(code, chunk, outputOptions) {
if (outputOptions.format === 'es' && chunk.fileName.endsWith('.min.js')) {
return await transform(code, { minify: true });
}
return code;
},
}
};
} This will generate the ES amd ES minified variants. |
@sinedied Thanks. I've got formats: [
'cjs',
// @ts-expect-error
'esm',
// @ts-expect-error
'system',
], (BTW: 'system' format is supported by rollup and seems to work, despite vite not documenting it as an option.) |
Please support #6585 |
so stupid, there is no way to minify 'es', terserOptions' compress and mangle doesn't work at all. |
If it helps, I've made a custom plugin that implements terser on import { resolve } from 'node:path'
import { defineConfig } from 'vite'
import { minify } from "terser";
import autoprefixer from 'autoprefixer'
function minifyBundles() {
return {
name: "minifyBundles",
async generateBundle(options, bundle) {
for (let key in bundle) {
if (bundle[key].type == 'chunk' && key.endsWith('.js')) {
const minifyCode = await minify(bundle[key].code, { sourceMap: false })
bundle[key].code = minifyCode.code
}
}
return bundle
},
}
}
export default defineConfig({
appType: 'custom',
css: {
devSourcemap: true,
postcss: {
plugins: [
autoprefixer
],
}
},
build: {
target: ['es2015'],
outDir: 'dist',
emptyOutDir: true,
cssCodeSplit: true,
sourcemap: false,
lib: {
formats: ['es'],
entry: [
resolve(__dirname, 'src/scripts/main.js'),
resolve(__dirname, 'src/scripts/critical.js'),
],
fileName: '[name]',
},
},
plugins: [
minifyBundles()
]
}) |
Coming back end of 2023, this is a fairly important missing feature. For modern libraries esm is the way to go and no way to minify the output just makes it useless. Sticking to rollup for builds is the way to go atm |
Is there any plan to look at this does anyone know? |
No news. This is my vite build && esbuild my/lib/path.js --minify --outfile=my/lib/path.js --allow-overwrite I build using |
2.5 years on, this is beginning to feel like a pressing matter. |
Maybe I got something wrong, but why not use the minify flag directly in the build options?
I get a minified version. Is the configurability the problem? |
any update? |
This is yet another example of a small plugin which will minify the lib - I haven't tested it beyond my very simple use case. benefits:
const minifyBundle = ():Plugin=>({
name:"minify-bundle",
async generateBundle(_,bundle)
{
for (const asset of Object.values(bundle))
{
if (asset.type == "chunk")
asset.code = (await esbuild.transform(asset.code,{minify:true})).code;
}
}
}); |
Thanks @jmlee2k the code you provide works for me This said : being allow to minify a lib file should be by default for a bundler ? |
In my case, I need to bundle a script that is directly imported by the browser somewhere else (so no further tree-shaking). I noticed that the
vite/packages/vite/src/node/plugins/esbuild.ts Lines 422 to 442 in cf813cc
Wouldn't changing line 429 to esbuild: {
minifyWhitespace: true
} explicitly in their vite config. |
fix vitejs#6555) To produce es bundles that can be tree-shaken, lib mode disables minifyWhitespace by default when using the es format. But there are cases where we want an es bundle that is as small as possible and that does not need to be tree-shakable. This change allows minifyWhitespace to be re-enabled if the user so desires.
I opened a pull request that would make it possible to re-enable whitespace minification by setting esbuild: {
minifyWhitespace: true
} |
At the moment I've been using this kind of config instead of lib mode to serve ESM in a CDN-like manner: export default defineConfig({
build: {
rollupOptions: {
input: resolve(__dirname, "src/index.js"),
output: {
assetFileNames: (chunkInfo) => {
return "[name][extname]";
},
entryFileNames: "[name].js",
},
preserveEntrySignatures: "strict",
},
sourcemap: true,
},
}); Seems to work, but I am also not sure if I am missing some key information regarding why we cannot minify in lib mode. |
Clear and concise description of the problem
Currently when building in lib mode we do not minify the ES build because that would remove pure annotations and break treeshaking. However, this is assuming that the ES build is only used with bundlers. In the future, more users could be using ES builds over CDNs with native ES imports, so a minified ES build could be necessary for those cases.
Suggested solution
Alternative
lib: { minifyES: true }
Additional context
vuejs/petite-vue#112
Validations
The text was updated successfully, but these errors were encountered: