|
| 1 | +import ts from 'typescript'; |
| 2 | +import path from 'path'; |
| 3 | +import external from 'rollup-plugin-peer-deps-external'; |
| 4 | +import json from '@rollup/plugin-json'; |
| 5 | +import replace from '@rollup/plugin-replace'; |
| 6 | +import typescript from 'rollup-plugin-typescript2'; |
| 7 | +import resolve from '@rollup/plugin-node-resolve'; |
| 8 | +import commonjs from '@rollup/plugin-commonjs'; |
| 9 | +import sourcemaps from 'rollup-plugin-sourcemaps'; |
| 10 | +import { terser } from 'rollup-plugin-terser'; |
| 11 | +import { safePackageName } from './safePackageName'; |
| 12 | +import { pascalcase } from './pascalcase'; |
| 13 | +import pkg from '../package.json'; |
| 14 | + |
| 15 | +export function createRollupConfig(options) { |
| 16 | + const name = options.name || safePackageName(pkg.name); |
| 17 | + const umdName = options.umdName || pascalcase(safePackageName(pkg.name)); |
| 18 | + const shouldMinify = options.minify || options.env === 'production'; |
| 19 | + const tsconfigPath = options.tsconfig || 'tsconfig.json'; |
| 20 | + const tsconfigJSON = ts.readConfigFile(tsconfigPath, ts.sys.readFile).config; |
| 21 | + const tsCompilerOptions = ts.parseJsonConfigFileContent( |
| 22 | + tsconfigJSON, |
| 23 | + ts.sys, |
| 24 | + './', |
| 25 | + ).options; |
| 26 | + |
| 27 | + const outputName = [ |
| 28 | + path.join(tsCompilerOptions.outDir, name), |
| 29 | + options.formatName || options.format, |
| 30 | + options.env, |
| 31 | + shouldMinify ? 'min' : '', |
| 32 | + 'js', |
| 33 | + ] |
| 34 | + .filter(Boolean) |
| 35 | + .join('.'); |
| 36 | + |
| 37 | + return { |
| 38 | + input: options.input, |
| 39 | + output: { |
| 40 | + file: outputName, |
| 41 | + format: options.format, |
| 42 | + name: umdName, |
| 43 | + sourcemap: true, |
| 44 | + globals: { |
| 45 | + react: 'React', |
| 46 | + 'react-dom': 'ReactDOM', |
| 47 | + 'react-hook-form': 'ReactHookForm', |
| 48 | + }, |
| 49 | + exports: 'named', |
| 50 | + }, |
| 51 | + plugins: [ |
| 52 | + external({ |
| 53 | + includeDependencies: options.format !== 'umd', |
| 54 | + }), |
| 55 | + json(), |
| 56 | + typescript({ |
| 57 | + tsconfig: options.tsconfig, |
| 58 | + clean: true, |
| 59 | + }), |
| 60 | + resolve(), |
| 61 | + options.format === 'umd' && |
| 62 | + commonjs({ |
| 63 | + include: /\/node_modules\//, |
| 64 | + }), |
| 65 | + options.env !== undefined && |
| 66 | + replace({ |
| 67 | + 'process.env.NODE_ENV': JSON.stringify(options.env), |
| 68 | + }), |
| 69 | + sourcemaps(), |
| 70 | + shouldMinify && |
| 71 | + terser({ |
| 72 | + output: { comments: false }, |
| 73 | + compress: { |
| 74 | + drop_console: true, |
| 75 | + }, |
| 76 | + }), |
| 77 | + options.plugins, |
| 78 | + ], |
| 79 | + }; |
| 80 | +} |
0 commit comments