From 5ff63523b3784af5b4123279517f72f34b4173f5 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Thu, 3 Jan 2019 19:37:21 +1100 Subject: [PATCH] Show how to skip imports for optional plugins (#2627) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The most common Rollup plugins are pretty fast to import (by Node standards), but even those will commonly add on a handful of milliseconds, and it’s not unheard of to have imports literally take seconds. If you don’t need it, why load it? I think that this is a good pattern to recommend. eslint and stylelint are two good examples of unreasonably slow modules that will commonly be optional. stylelint can take a second or two, and eslint a few hundred milliseconds, because neither of them care enough about startup time. https://github.com/stylelint/stylelint/issues/2454 was me reporting it for stylelint. I wish more people used Rollup on their packages before submitting them to npm. --- docs/999-big-list-of-options.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/999-big-list-of-options.md b/docs/999-big-list-of-options.md index 2f63ca2a5f7..ef549b721c5 100755 --- a/docs/999-big-list-of-options.md +++ b/docs/999-big-list-of-options.md @@ -210,20 +210,21 @@ See [Using plugins](guide/en#using-plugins) for more information on how to use p // rollup.config.js import resolve from 'rollup-plugin-node-resolve'; import commonjs from 'rollup-plugin-commonjs'; -import {terser} from 'rollup-plugin-terser'; const isProduction = process.env.NODE_ENV === 'production'; -export default { +export default (async () => ({ entry: 'main.js', plugins: [ resolve(), commonjs(), - isProduction && terser() + isProduction && (await import('rollup-plugin-terser')).terser() ] -}; +})(); ``` +(This example also demonstrates how to use an async IIFE and dynamic imports to avoid unnecessary module loading, which can be surprisingly slow.) + ### Advanced functionality #### cache