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.

How to get minify code?

See original GitHub issue

I want get minify code after build , and my unbuild config this:

import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
  entries: ["./index"],
  declaration: true,
  clean: true,
  failOnWarn: false,
  rollup: {
    emitCJS: true,
    esbuild: {
      minify: true
    }
  }
});

But after pnpm build , I get this error:

Error building E:/project/changan-incall/packages/microApp: Error: Transform failed with 1 error:
<stdin>:1:10: ERROR: Expected ";" but found "EnterMicroAppParams"
Error: Transform failed with 1 error:
<stdin>:1:10: ERROR: Expected ";" but found "EnterMicroAppParams"
    at failureErrorWithLog (E:\project\changan-incall\node_modules\.pnpm\esbuild@0.15.6\node_modules\esbuild\lib\main.js:1624:15)
    at E:\project\changan-incall\node_modules\.pnpm\esbuild@0.15.6\node_modules\esbuild\lib\main.js:1413:29
    at E:\project\changan-incall\node_modules\.pnpm\esbuild@0.15.6\node_modules\esbuild\lib\main.js:678:9
    at handleIncomingPacket (E:\project\changan-incall\node_modules\.pnpm\esbuild@0.15.6\node_modules\esbuild\lib\main.js:775:9)
    at Socket.readFromStdout (E:\project\changan-incall\node_modules\.pnpm\esbuild@0.15.6\node_modules\esbuild\lib\main.js:644:7)
    at Socket.emit (node:events:526:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at Pipe.onStreamRead (node:internal/stream_base_commons:190:23) {
  errors: [
    {
      detail: undefined,
      id: '',
      location: [Object],
      notes: [],
      pluginName: '',
      text: 'Expected ";" but found "EnterMicroAppParams"'
    }
  ],
  warnings: [],
  code: 'PLUGIN_ERROR',
  plugin: 'esbuild',
  hook: 'renderChunk'
}

How Can I resolve this ? Hope get you help genuine.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
dwightjackcommented, Dec 22, 2022

I submitted a quick PR that should fix this issue: https://github.com/unjs/unbuild/pull/185

3reactions
dwightjackcommented, Nov 30, 2022

I guess @Phl3bas is correct. rollup-plugin-dts doesn’t seem to work well with the esbuild plugin used in unbuild. For what I can understand, the esbuild plugin tries to load and minify .d.ts file chunks generated by the dts plugin with the js loader (source) which causes an error because the generated TS definition is not valid JavaScript:

declare function sum(a: number, b: number): number;
//      ^--- esbuild breaks here

unbuild runs two rollup builds, one to transform the source to JS and one (optional) to generate the TS definitions. I managed to fix the issue using the rollup:dts:options hook which receives the option passed to the TS definitions build:

import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
  entries: [
    'src/index',
  ],
  declaration: true,
  clean: true,
  rollup: {
    emitCJS: true,
    esbuild: {
        minify: true,
        target: 'es2015',
    }
  },
  hooks: {
    'rollup:dts:options'(_ctx, options) {
    
      if (Array.isArray(options.plugins)) {
        // find the esbuild plugin instance
        const esbuildPlugin = options.plugins.find((p) => (p as any).name === 'esbuild');
        // renderChunk is where the minification happens
        // but we don't want it to be executed
        // on the TS declaration build 
        (esbuildPlugin as any).renderChunk = () => null
      }
    }
  }
})

This is more of a hack than a workaround, so I guess we should fix the issue with a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Minify JavaScript — Recommended Tools and Methods
To minify JavaScript code, you must parse it, compress it, and get the output. Once it's been minified, it should be almost unreadable...
Read more >
Minify - JavaScript and CSS minifier
Make your website smaller and faster to load by minifying the JS and CSS code. ... This minifier removes whitespace, strips comments, combines...
Read more >
How to 'minify' Javascript code - Stack Overflow
Install it and reload VSCode. Then click on your file, open command palette ( Ctrl+Shift+p ), ant type minify this document ( Ctrl+alt+m...
Read more >
Minify Resources (HTML, CSS, and JavaScript)
To minify HTML, try HTMLMinifier ; To minify CSS, try CSSNano and csso. ; To minify JavaScript, try UglifyJS. The Closure Compiler is...
Read more >
How to Minify JavaScript & CSS? - Code Institute Global
Upload the source code file or paste your source code. · Set the parameters for a particular output. · To minify or compress...
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