11import babel from '@rollup/plugin-babel' ;
22import replace from '@rollup/plugin-replace' ;
33import ignore from 'rollup-plugin-ignore' ;
4- import { terser } from 'rollup-plugin-terser' ;
4+ import alias from '@rollup/plugin-alias' ;
5+ import nodePolyfills from 'rollup-plugin-polyfill-node' ;
6+ import nodeResolve from '@rollup/plugin-node-resolve' ;
7+ import pkg from './package.json' ;
8+ import commonjs from '@rollup/plugin-commonjs'
59
610const cjs = {
711 exports : 'named' ,
@@ -15,81 +19,72 @@ const esm = {
1519const getCJS = override => Object . assign ( { } , cjs , override ) ;
1620const getESM = override => Object . assign ( { } , esm , override ) ;
1721
18- const configBase = {
19- input : 'src/index.js' ,
20- external : [ 'zlib' ] ,
21- plugins : [
22- babel ( {
23- babelrc : false ,
24- babelHelpers : 'runtime' ,
25- exclude : 'node_modules/**' ,
26- presets : [
27- [
28- '@babel/preset-env' ,
29- {
30- loose : true ,
31- modules : false ,
32- targets : { node : '12' , browsers : 'last 2 versions' } ,
33- } ,
34- ] ,
35- ] ,
36- plugins : [ [ '@babel/plugin-transform-runtime' , { version : '^7.16.4' } ] ] ,
37- } ) ,
38- ] ,
39- } ;
22+ const input = 'src/index.js' ;
4023
41- const serverConfig = Object . assign ( { } , configBase , {
42- output : [
43- getESM ( { file : 'lib/png-js.es.js' } ) ,
44- getCJS ( { file : 'lib/png-js.cjs.js' } ) ,
45- ] ,
46- plugins : configBase . plugins . concat (
47- replace ( {
48- preventAssignment : true ,
49- values : {
50- BROWSER : JSON . stringify ( false ) ,
24+ const babelConfig = ( { browser } ) => ( {
25+ babelrc : false ,
26+ babelHelpers : 'runtime' ,
27+ exclude : 'node_modules/**' ,
28+ presets : [
29+ [
30+ '@babel/preset-env' ,
31+ {
32+ loose : true ,
33+ modules : false ,
34+ targets : { node : '12' , browsers : 'last 2 versions' } ,
5135 } ,
52- } ) ,
53- ) ,
54- external : [ 'fs' ] ,
36+ ] ,
37+ ] ,
38+ plugins : [ [ '@babel/plugin-transform-runtime' , { version : '^7.16.4' } ] ] ,
5539} ) ;
5640
57- const serverProdConfig = Object . assign ( { } , serverConfig , {
41+ const getExternal = ( { browser } ) => [
42+ ...( browser ? [ ] : [ 'fs' ] ) ,
43+ ...( Object . keys ( pkg . dependencies ) . filter ( dep => ! browser || 'browserify-zlib' !== dep ) ) ,
44+ ] ;
45+
46+ const getPlugins = ( { browser } ) => [
47+ ...( browser ? [
48+ ignore ( [ 'fs' ] ) ,
49+ alias ( {
50+ entries : [
51+ { find : 'zlib' , replacement : 'browserify-zlib' } ,
52+ ]
53+ } ) ,
54+ commonjs ( ) ,
55+ nodeResolve ( { browser, preferBuiltins : ! browser } ) ,
56+ nodePolyfills ( { include : [ / n o d e _ m o d u l e s \/ .+ \. j s / ] } ) ,
57+ ] : [ ] ) ,
58+ babel ( babelConfig ( { browser } ) ) ,
59+ replace ( {
60+ preventAssignment : true ,
61+ values : {
62+ BROWSER : JSON . stringify ( browser ) ,
63+ } ,
64+ } ) ,
65+ ] ;
66+
67+ const serverConfig = {
68+ input,
5869 output : [
59- getESM ( { file : 'lib/png-js.es.min. js' } ) ,
60- getCJS ( { file : 'lib/png-js.cjs.min. js' } ) ,
70+ getESM ( { file : 'lib/png-js.es.js' } ) ,
71+ getCJS ( { file : 'lib/png-js.cjs.js' } ) ,
6172 ] ,
62- plugins : serverConfig . plugins . concat ( terser ( ) ) ,
63- } ) ;
73+ external : getExternal ( { browser : false } ) ,
74+ plugins : getPlugins ( { browser : false } ) ,
75+ } ;
6476
65- const browserConfig = Object . assign ( { } , configBase , {
77+ const browserConfig = {
78+ input,
6679 output : [
6780 getESM ( { file : 'lib/png-js.browser.es.js' } ) ,
6881 getCJS ( { file : 'lib/png-js.browser.cjs.js' } ) ,
6982 ] ,
70- plugins : configBase . plugins . concat (
71- replace ( {
72- preventAssignment : true ,
73- values : {
74- BROWSER : JSON . stringify ( true ) ,
75- 'png-js' : 'png-js/png.js' ,
76- } ,
77- } ) ,
78- ignore ( [ 'fs' ] ) ,
79- ) ,
80- } ) ;
81-
82- const browserProdConfig = Object . assign ( { } , browserConfig , {
83- output : [
84- getESM ( { file : 'lib/png-js.browser.es.min.js' } ) ,
85- getCJS ( { file : 'lib/png-js.browser.cjs.min.js' } ) ,
86- ] ,
87- plugins : browserConfig . plugins . concat ( terser ( ) ) ,
88- } ) ;
83+ external : getExternal ( { browser : true } ) ,
84+ plugins : getPlugins ( { browser : true } ) ,
85+ } ;
8986
9087export default [
9188 serverConfig ,
92- serverProdConfig ,
9389 browserConfig ,
94- browserProdConfig ,
9590] ;
0 commit comments