Terser: Annotate function as always pure (and what does it mean)

457 views Asked by At

To assist with optimization Terser supports the pure comment to indicate that a function call is pure. Specifically, the docs give only the following example.

const x = /*#__PURE__*/i_am_dropped_if_x_is_not_used()

But, usually, if I define a pure function every use of it is pure. Is there a way to use comments to indicate that the function is always pure? If not do I have to manually list out the functions and pass them into the pure_funcs option?

--

Also, does the pure annotation really require that that the function be pure in a mathematical/functional programming sense, or is it enough that it be idempotent? For instance, if the function getModel(id) keeps a cache behind the scenes to improve performance will it cause problems if I mark it as pure?

1

There are 1 answers

0
ILScc On

Use something like this in your config:

new TerserPlugin({
  terserOptions: {
    compress: {
      pure_funcs: [
        '<yourFunction>',
      ],
    },
  },
}),

You can find documentation in compress options here.