|
| 1 | +import replace from 'rollup-plugin-replace' |
| 2 | +import resolve from 'rollup-plugin-node-resolve' |
| 3 | +import commonjs from 'rollup-plugin-commonjs' |
| 4 | +import ts from 'rollup-plugin-typescript2' |
| 5 | +import alias from 'rollup-plugin-alias' |
| 6 | +import { terser } from 'rollup-plugin-terser' |
| 7 | +import path from 'path' |
| 8 | +import rimraf from 'rimraf' |
| 9 | +import pascalcase from 'pascalcase' |
| 10 | + |
| 11 | +const cwd = process.cwd() |
| 12 | +// eslint-disable-next-line |
| 13 | +const pkg = require(path.join(cwd, 'package.json')) |
| 14 | + |
| 15 | +rimraf.sync(path.join(cwd, './dist')) |
| 16 | + |
| 17 | +const banner = `/*! |
| 18 | + * ${pkg.name} v${pkg.version} |
| 19 | + * (c) ${new Date().getFullYear()} Eduardo San Martin Morote |
| 20 | + * @license MIT |
| 21 | + */` |
| 22 | + |
| 23 | +const exportName = pascalcase(pkg.name) |
| 24 | + |
| 25 | +function createEntry( |
| 26 | + { |
| 27 | + format, // Rollup format (iife, umd, cjs, es) |
| 28 | + external, // Rollup external option |
| 29 | + input = 'src/index.ts', // entry point |
| 30 | + env = 'development', // NODE_ENV variable |
| 31 | + minify = false, |
| 32 | + isBrowser = false, // produce a browser module version or not |
| 33 | + } = { |
| 34 | + input: 'src/index.ts', |
| 35 | + env: 'development', |
| 36 | + minify: false, |
| 37 | + isBrowser: false, |
| 38 | + } |
| 39 | +) { |
| 40 | + // force production mode when minifying |
| 41 | + if (minify) env = 'production' |
| 42 | + |
| 43 | + const config = { |
| 44 | + input, |
| 45 | + plugins: [ |
| 46 | + replace({ |
| 47 | + __VERSION__: pkg.version, |
| 48 | + 'process.env.NODE_ENV': `'${env}'`, |
| 49 | + }), |
| 50 | + alias({ |
| 51 | + resolve: ['.ts', '.js'], |
| 52 | + // entries: [{ find: 'firebase', replacement: path.join(__dirname, './stub') }], |
| 53 | + }), |
| 54 | + ], |
| 55 | + output: { |
| 56 | + banner, |
| 57 | + file: `dist/${pkg.name}.UNKNOWN.js`, |
| 58 | + format, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + if (format === 'iife') { |
| 63 | + config.output.file = pkg.unpkg |
| 64 | + config.output.name = exportName |
| 65 | + } else if (format === 'es') { |
| 66 | + config.output.file = isBrowser ? pkg.browser : pkg.module |
| 67 | + } else if (format === 'cjs') { |
| 68 | + config.output.file = pkg.main |
| 69 | + } |
| 70 | + |
| 71 | + if (!external) { |
| 72 | + config.plugins.push(resolve(), commonjs()) |
| 73 | + } else { |
| 74 | + config.external = external |
| 75 | + } |
| 76 | + |
| 77 | + config.plugins.push( |
| 78 | + ts({ |
| 79 | + // only check once, during the es version with browser (it includes external libs) |
| 80 | + check: format === 'es' && isBrowser && !minify, |
| 81 | + tsconfigOverride: { |
| 82 | + exclude: ['__tests__'], |
| 83 | + compilerOptions: { |
| 84 | + // same for d.ts files |
| 85 | + declaration: format === 'es' && isBrowser && !minify, |
| 86 | + target: format === 'es' && !isBrowser ? 'esnext' : 'es5', |
| 87 | + }, |
| 88 | + }, |
| 89 | + }) |
| 90 | + ) |
| 91 | + |
| 92 | + if (minify) { |
| 93 | + config.plugins.push( |
| 94 | + terser({ |
| 95 | + module: format === 'es', |
| 96 | + output: { |
| 97 | + preamble: banner, |
| 98 | + }, |
| 99 | + }) |
| 100 | + ) |
| 101 | + config.output.file = config.output.file.replace(/\.js$/i, '.min.js') |
| 102 | + } |
| 103 | + |
| 104 | + return config |
| 105 | +} |
| 106 | + |
| 107 | +const builds = [ |
| 108 | + createEntry({ format: 'cjs' }), |
| 109 | + createEntry({ format: 'es', isBrowser: true }), |
| 110 | +] |
| 111 | + |
| 112 | +if (pkg.unpkg) |
| 113 | + builds.push( |
| 114 | + createEntry({ format: 'iife' }), |
| 115 | + createEntry({ format: 'iife', minify: true }), |
| 116 | + createEntry({ format: 'es', isBrowser: true, minify: true }) |
| 117 | + ) |
| 118 | + |
| 119 | +export default builds |
0 commit comments