|
| 1 | +import includesAny from "array-includes-any"; |
| 2 | + |
| 3 | +// common plugins |
| 4 | +import resolve from "@rollup/plugin-node-resolve"; |
| 5 | +import commonjs from "@rollup/plugin-commonjs"; |
| 6 | +import { terser } from "rollup-plugin-terser"; |
| 7 | +// @ts-ignore |
| 8 | +import autoExternal from "rollup-plugin-auto-external"; |
| 9 | + |
| 10 | +export function createPlugins( |
| 11 | + languages: Array<string> = ["ts", "js", "json", "coffee"], |
| 12 | + babel: boolean = true, |
| 13 | + extraPlugins?: Array<any> |
| 14 | +) { |
| 15 | + let plugins = [ |
| 16 | + autoExternal({ |
| 17 | + builtins: true, |
| 18 | + dependencies: false, |
| 19 | + peerDependencies: false, |
| 20 | + }), |
| 21 | + |
| 22 | + // so Rollup can find externals |
| 23 | + resolve({ |
| 24 | + extensions: [".ts", ".js", ".coffee", ".tsx", ".jsx", ".mjs"], |
| 25 | + preferBuiltins: true, |
| 26 | + }), |
| 27 | + |
| 28 | + // so Rollup can convert externals to an ES module |
| 29 | + commonjs(), |
| 30 | + ]; |
| 31 | + |
| 32 | + // language specific |
| 33 | + // typescript |
| 34 | + if (includesAny(languages, ["ts", ".ts", "typescript", "TypeScript"])) { |
| 35 | + const typescript = require("@rollup/plugin-typescript"); |
| 36 | + plugins.push( |
| 37 | + typescript({ |
| 38 | + noEmitOnError: false, |
| 39 | + }) |
| 40 | + ); |
| 41 | + } |
| 42 | + // coffeescript |
| 43 | + if ( |
| 44 | + includesAny(languages, [ |
| 45 | + "coffee", |
| 46 | + ".coffee", |
| 47 | + "coffeescript", |
| 48 | + "coffee-script", |
| 49 | + "CoffeeScript", |
| 50 | + ]) |
| 51 | + ) { |
| 52 | + const coffeescript = require("rollup-plugin-coffee-script"); |
| 53 | + plugins.push(coffeescript()); |
| 54 | + } |
| 55 | + // json |
| 56 | + if (includesAny(languages, ["json", ".json", "JSON"])) { |
| 57 | + const json = require("@rollup/plugin-json"); |
| 58 | + plugins.push(json({ compact: true })); |
| 59 | + } |
| 60 | + |
| 61 | + // css only |
| 62 | + if (includesAny(languages, ["css", ".css"])) { |
| 63 | + console.log(` |
| 64 | + css only was chosen to bundle css files into a single file. |
| 65 | + This plugin requires you to import css files in a dummy js file and pass it as an input to rollup. |
| 66 | + This should be done in a separate step from src code bundling |
| 67 | + `); |
| 68 | + const cssOnly = require("rollup-plugin-css-only"); |
| 69 | + plugins.push(cssOnly({ output: "dist/bundle.css" })); |
| 70 | + // minify css |
| 71 | + if (process.env.NODE_ENV === "production") { |
| 72 | + const execute = require("rollup-plugin-execute"); |
| 73 | + plugins.push(execute(["csso dist/bundle.css --output dist/bundle.css"])); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // babel for js and coffee |
| 78 | + if (babel || languages.includes("babel")) { |
| 79 | + const { babel } = require("@rollup/plugin-babel"); |
| 80 | + plugins.push( |
| 81 | + babel({ |
| 82 | + extensions: [".js", ".jsx", ".mjs", ".coffee"], |
| 83 | + babelHelpers: "bundled", |
| 84 | + }) |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + // extra plugins |
| 89 | + if (extraPlugins) { |
| 90 | + plugins.push(...extraPlugins); |
| 91 | + } |
| 92 | + |
| 93 | + // minify only in production mode |
| 94 | + if (process.env.NODE_ENV === "production") { |
| 95 | + plugins.push( |
| 96 | + terser({ |
| 97 | + ecma: 2018, |
| 98 | + warnings: true, |
| 99 | + compress: { |
| 100 | + drop_console: false, |
| 101 | + }, |
| 102 | + }) |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + return plugins; |
| 107 | +} |
0 commit comments