|
| 1 | +/* rollup.config.js - Rollup configuration. |
| 2 | + * Copyright (c) 2019 - 2021 Richard Huang <rickypc@users.noreply.github.com> |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 7 | + |
| 8 | +import babel from '@rollup/plugin-babel'; |
| 9 | +import copy from 'rollup-plugin-copy'; |
| 10 | +import del from 'rollup-plugin-delete'; |
| 11 | +import fs from 'fs'; |
| 12 | +import path from 'path'; |
| 13 | +import { terser } from 'rollup-plugin-terser'; |
| 14 | +import pkg from './package.json'; |
| 15 | + |
| 16 | +const banner = `/*! ${pkg.name} v${pkg.version} | (c) ${pkg.author.name} <${pkg.author.email}> | ${pkg.license} */`; |
| 17 | +const external = [ |
| 18 | + 'react', |
| 19 | + 'react/jsx-runtime', |
| 20 | +]; |
| 21 | +const globals = { |
| 22 | + react: 'React', |
| 23 | + 'react/jsx-runtime': 'jsxRuntime', |
| 24 | +}; |
| 25 | +const input = ['./src/index.js']; |
| 26 | +const writePackage = (json, to) => ({ |
| 27 | + generateBundle() { |
| 28 | + const { |
| 29 | + devDependencies, |
| 30 | + eslintConfig, |
| 31 | + jest, |
| 32 | + scripts, |
| 33 | + ...output |
| 34 | + } = json; |
| 35 | + delete output.babel; |
| 36 | + fs.writeFileSync(path.resolve(to), JSON.stringify(output)); |
| 37 | + }, |
| 38 | + name: 'write-package', |
| 39 | +}); |
| 40 | + |
| 41 | +export default [ |
| 42 | + // CJS and ESM. |
| 43 | + { |
| 44 | + external, |
| 45 | + input, |
| 46 | + onwarn: (warning, next) => { |
| 47 | + if (warning.code === 'UNUSED_EXTERNAL_IMPORT') return; |
| 48 | + next(warning); |
| 49 | + }, |
| 50 | + output: [ |
| 51 | + { |
| 52 | + banner, |
| 53 | + exports: 'named', |
| 54 | + file: `dist/${pkg.main}`, |
| 55 | + format: 'cjs', |
| 56 | + globals, |
| 57 | + sourcemap: true, |
| 58 | + strict: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + banner, |
| 62 | + exports: 'named', |
| 63 | + file: `dist/${pkg.module}`, |
| 64 | + format: 'esm', |
| 65 | + globals, |
| 66 | + sourcemap: true, |
| 67 | + strict: false, |
| 68 | + }, |
| 69 | + ], |
| 70 | + plugins: [ |
| 71 | + // Begin run once. |
| 72 | + del({ |
| 73 | + targets: 'dist/*', |
| 74 | + }), |
| 75 | + copy({ |
| 76 | + targets: [ |
| 77 | + { src: ['example.js', 'README.md'], dest: 'dist' }, |
| 78 | + ], |
| 79 | + }), |
| 80 | + writePackage(pkg, 'dist/package.json'), |
| 81 | + // End run once. |
| 82 | + babel({ |
| 83 | + babelrc: false, |
| 84 | + babelHelpers: 'bundled', |
| 85 | + exclude: 'node_modules/**', |
| 86 | + presets: [ |
| 87 | + '@babel/flow', |
| 88 | + [ |
| 89 | + '@babel/react', |
| 90 | + { |
| 91 | + runtime: 'automatic', |
| 92 | + }, |
| 93 | + ], |
| 94 | + ], |
| 95 | + }), |
| 96 | + terser({ |
| 97 | + compress: false, |
| 98 | + mangle: false, |
| 99 | + }), |
| 100 | + ], |
| 101 | + }, |
| 102 | + // UMD. |
| 103 | + { |
| 104 | + external, |
| 105 | + input, |
| 106 | + output: [ |
| 107 | + { |
| 108 | + banner, |
| 109 | + esModule: false, |
| 110 | + exports: 'named', |
| 111 | + file: 'dist/index.umd.js', |
| 112 | + format: 'umd', |
| 113 | + globals, |
| 114 | + name: 'reactStoreContextHooks', |
| 115 | + sourcemap: true, |
| 116 | + }, |
| 117 | + ], |
| 118 | + plugins: [ |
| 119 | + babel({ |
| 120 | + babelrc: false, |
| 121 | + babelHelpers: 'bundled', |
| 122 | + exclude: 'node_modules/**', |
| 123 | + presets: [ |
| 124 | + [ |
| 125 | + '@babel/preset-env', |
| 126 | + { |
| 127 | + exclude: ['transform-typeof-symbol'], |
| 128 | + targets: { |
| 129 | + browsers: [ |
| 130 | + 'last 2 versions', |
| 131 | + '> 5%', |
| 132 | + ], |
| 133 | + }, |
| 134 | + }, |
| 135 | + ], |
| 136 | + '@babel/flow', |
| 137 | + '@babel/react', |
| 138 | + ], |
| 139 | + }), |
| 140 | + terser(), |
| 141 | + ], |
| 142 | + }, |
| 143 | +]; |
0 commit comments