|
| 1 | +import commonjs from "@rollup/plugin-commonjs"; |
| 2 | +import typescript from "@rollup/plugin-typescript"; |
| 3 | +import cssOnly from "rollup-plugin-css-only"; |
| 4 | +import { babel } from "@rollup/plugin-babel"; |
| 5 | +import { terser } from "rollup-plugin-terser"; |
| 6 | +import { rollup } from "rollup"; |
| 7 | +import fs from "fs"; |
| 8 | + |
| 9 | +const makeConfig = ({ format, minify, types, css }) => |
| 10 | + // : { |
| 11 | + // format: 'esm' | 'cjs'; |
| 12 | + // minify: boolean; |
| 13 | + // types: boolean; |
| 14 | + // }): RollupOptions => { |
| 15 | + { |
| 16 | + const outputName = `${process.argv[2]}.${format}.${ |
| 17 | + minify ? "min.js" : "js" |
| 18 | + }`; |
| 19 | + |
| 20 | + return { |
| 21 | + input: "./src/index.ts", |
| 22 | + external: (id, parent, resolved) => { |
| 23 | + const isExternal = !(id.startsWith(".") || resolved); |
| 24 | + return isExternal; |
| 25 | + }, |
| 26 | + treeshake: { |
| 27 | + // We assume reading a property of an object never has side-effects. |
| 28 | + propertyReadSideEffects: false, |
| 29 | + }, |
| 30 | + output: { |
| 31 | + dir: "./dist", |
| 32 | + entryFileNames: outputName, |
| 33 | + format, |
| 34 | + esModule: true, |
| 35 | + sourcemap: true, |
| 36 | + exports: "named", |
| 37 | + }, |
| 38 | + plugins: [ |
| 39 | + // all bundled external modules need to be converted from CJS to ESM |
| 40 | + commonjs(), |
| 41 | + typescript({ |
| 42 | + emitDeclarationOnly: types, |
| 43 | + noEmit: !types, |
| 44 | + declarationDir: "./dist", |
| 45 | + }), |
| 46 | + css && cssOnly({ output: "bundle.css" }), |
| 47 | + babel({ |
| 48 | + exclude: "node_modules/**", |
| 49 | + extensions: ["ts", "tsx"], |
| 50 | + babelHelpers: "bundled", |
| 51 | + }), |
| 52 | + minify && |
| 53 | + terser({ |
| 54 | + output: { comments: false }, |
| 55 | + compress: { |
| 56 | + keep_infinity: true, |
| 57 | + pure_getters: true, |
| 58 | + passes: 10, |
| 59 | + }, |
| 60 | + ecma: 5, |
| 61 | + toplevel: format === "cjs", |
| 62 | + }), |
| 63 | + ], |
| 64 | + }; |
| 65 | + }; |
| 66 | + |
| 67 | +function runRollup(config) { |
| 68 | + return rollup(config).then((r) => r.write(config.output)); |
| 69 | +} |
| 70 | + |
| 71 | +fs.rmdirSync("./dist", { recursive: true }); |
| 72 | +runRollup( |
| 73 | + makeConfig({ |
| 74 | + format: "esm", |
| 75 | + minify: false, |
| 76 | + types: true, |
| 77 | + css: process.argv.includes("--css"), |
| 78 | + }) |
| 79 | +).then( |
| 80 | + (r) => console.log("success!"), |
| 81 | + (e) => console.error(e) |
| 82 | +); |
0 commit comments