question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lib mode format: es.min

See original GitHub issue

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

// vite config
build: {
  lib: {
    formats: ['es', 'es.min']
  }
}

Alternative

lib: { minifyES: true }

Additional context

https://github.com/vuejs/petite-vue/pull/112

Validations

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:14
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
mindplay-dkcommented, Sep 6, 2022

I don’t understand, is minifying just disabled entirely for es library output?

I’ve tried for example this:

  build: {
    lib: {
      // ...
      formats: ["es"]
    },
    minify: "terser",
    terserOptions: {
      compress: true,
      mangle: true,
    },
  }

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, minify: "esbuild" gives a significantly smaller file, because it actually compresses and minifies - with minify: "terser" it doesn’t minify at all. How is this setting useful?

I was able to manually minify with terser after the fact, so it’s not that this isn’t possible.

npm exec terser -- -c -m --module dist/lib.js > dist/lib.min.js
2reactions
sinediedcommented, Dec 8, 2022

Would love to see this implemented!

Currently this is my workaround:

Add an extra esm format to lib entry (don’t mind the TS error):

    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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build Options - Vite
Note the build.minify option does not minify whitespaces when using the 'es' format in lib mode, as it removes pure annotations and breaks...
Read more >
Create a library using Vite lib mode - YouTube
Most of the tutorials out there are pretty much a basic HelloWorld, but did you know you can also create professional OSS libraries...
Read more >
Custom date and time format strings | Microsoft Learn
Learn to use custom date and time format strings to convert DateTime or DateTimeOffset values into text representations, or to parse strings ...
Read more >
Understand the different javascript modules formats
3 min read. When it comes to building your application, if you're like me you always ask yourself: which format should I use...
Read more >
The Boost Format library - 1.54.0
A format object is constructed from a format-string, and is then given arguments through repeated calls to operator%. Each of those arguments ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found