rollup-starter-code-splitting icon indicating copy to clipboard operation
rollup-starter-code-splitting copied to clipboard

Example with minification

Open danielnaab opened this issue 7 years ago • 5 comments

It would be useful to demonstrate a bulletproof way to minify output bundles with code-splitting. I'm having trouble on certain edge cases, after working through multiple methods.

danielnaab avatar Jun 29 '18 20:06 danielnaab

@danielnaab perhaps try https://github.com/ampproject/rollup-plugin-closure-compiler/

guybedford avatar Jun 29 '18 20:06 guybedford

@guybedford thanks for the suggestion - sadly it fails on a chunk import when outputting to es format:

/private/var/folders/1z/z7j597t12d182tv_n80x6sq458d1h_/T/5ea703e1-1d91-4a90-835f-faa90525f15b:1: ERROR - Failed to load module "./chunk-7b6e0b9b.js"
import { b as baseEach } from './chunk-7b6e0b9b.js';
^

1 error(s), 0 warning(s)

danielnaab avatar Jun 29 '18 20:06 danielnaab

@danielnaab While I would certainly not call this "bulletproof" (I still run into problems here and there), rollup-plugin-terser works for me with these settings:

terser({
    compress: {
        unused: false,
        collapse_vars: false
    },
    output: {
        comments: false
    },
    sourceMap: false
})

camille-hdl avatar Jul 19 '18 21:07 camille-hdl

The 0.8.x release of rollup-plugin-closure-compiler added support for dynamic imports.

I just tested a local copy of this repo and it appears to work.

// rollup.config.js
import compiler from '@ampproject/rollup-plugin-closure-compiler';

export default {
  input: ["src/main-a.js", "src/main-b.js"],
  output: [
    // ES module version, for modern browsers
    {
      dir: "public/module",
      format: "es",
      sourcemap: true
    },
    // SystemJS version, for older browsers
    {
      dir: "public/nomodule",
      format: "system",
      sourcemap: true
    }
  ],
  plugins: [
    compiler(),
  ],
  experimentalCodeSplitting: true
};

kristoferbaxter avatar Oct 19 '18 00:10 kristoferbaxter

We adjust the minifier based on the output format:

function getConfig(outputFormat) {
  ...
  plugins: [
    ...
    outputFormat === 'esm' ? terser() : uglify()
  ]
}

export default [
  getConfig('system'),
  getConfig('esm')
];

enure avatar Nov 26 '18 23:11 enure