1+
12import typescript from '@rollup/plugin-typescript'
3+ import commonjs from '@rollup/plugin-commonjs'
4+ import nodeResolve from '@rollup/plugin-node-resolve'
25import replace from '@rollup/plugin-replace'
36import fs from 'fs'
47
@@ -10,59 +13,96 @@ const defaultOptions = () => ({
1013 external : [ ] ,
1114 // the entry point for the bundle
1215 input : undefined ,
13- // output directory for the bundle
14- output : undefined
16+ // additional rollup plugins
17+ plugins : [ ]
1518} )
1619
17- export const sharedOutput = {
18- dir : 'dist' ,
19- generatedCode : {
20- preset : 'es2015' ,
21- }
22- }
23-
2420function createRollupConfig ( options = defaultOptions ( ) ) {
2521 const packageJson = JSON . parse ( fs . readFileSync ( `${ process . cwd ( ) } /package.json` ) )
2622
27- return {
28- input : options . input || 'src/index.ts' ,
29- output : options . output || [
30- {
31- ...sharedOutput ,
23+ return [
24+ // ESM build
25+ {
26+ input : options . input || './src/index.ts' ,
27+ output : {
28+ dir : 'dist/esm' ,
3229 entryFileNames : '[name].js' ,
33- format : 'cjs'
34- } ,
35- {
36- ...sharedOutput ,
37- preserveModules : true ,
38- entryFileNames : '[name].mjs' ,
39- format : 'esm'
40- }
41- ] ,
42- external : options . external ,
43- plugins : [
44- replace ( {
45- preventAssignment : true ,
46- values : {
47- __VERSION__ : packageJson . version ,
48- ...options . additionalReplacements ,
30+ format : 'es' ,
31+ sourcemap : true ,
32+ generatedCode : {
33+ preset : 'es2015' ,
4934 }
50- } ) ,
51- typescript ( {
52- removeComments : true ,
53- // don't output anything if there's a TS error
54- noEmitOnError : true ,
55- // turn on declaration files and declaration maps
56- compilerOptions : {
57- declaration : true ,
58- declarationMap : true ,
59- emitDeclarationOnly : true ,
60- declarationDir : 'dist/types' ,
61- }
62- } ) ,
63- ...( options . plugins ?? [ ] )
64- ]
65- }
35+ } ,
36+ external : options . external ,
37+ plugins : [
38+ replace ( {
39+ preventAssignment : true ,
40+ values : {
41+ __BUGSNAG_NOTIFIER_VERSION__ : packageJson . version ,
42+ ...options . additionalReplacements ,
43+ }
44+ } ) ,
45+ nodeResolve ( {
46+ preferBuiltins : true
47+ } ) ,
48+ commonjs ( ) ,
49+ typescript ( {
50+ tsconfig : './tsconfig.json' ,
51+ declarationDir : 'dist/esm' ,
52+ outDir : 'dist/esm' ,
53+ noEmitOnError : true
54+ } ) ,
55+ ...( options . plugins ?? [ ] )
56+ ]
57+ } ,
58+ // CJS build
59+ {
60+ input : options . input || './src/index.ts' ,
61+ output : {
62+ dir : 'dist/cjs' ,
63+ entryFileNames : '[name].js' ,
64+ format : 'cjs' ,
65+ sourcemap : true ,
66+ generatedCode : {
67+ preset : 'es2015' ,
68+ } ,
69+ exports : 'named' ,
70+ interop : 'auto' ,
71+ footer : 'module.exports = Object.assign(exports.default || {}, exports);'
72+ } ,
73+ external : options . external ,
74+ plugins : [
75+ replace ( {
76+ preventAssignment : true ,
77+ values : {
78+ __BUGSNAG_NOTIFIER_VERSION__ : packageJson . version ,
79+ ...options . additionalReplacements ,
80+ }
81+ } ) ,
82+ nodeResolve ( {
83+ preferBuiltins : true
84+ } ) ,
85+ commonjs ( ) ,
86+ typescript ( {
87+ tsconfig : './tsconfig.json' ,
88+ declarationDir : 'dist/cjs' ,
89+ outDir : 'dist/cjs' ,
90+ noEmitOnError : true
91+ } ) ,
92+ {
93+ name : 'emit-cjs-package-json' ,
94+ generateBundle ( ) {
95+ this . emitFile ( {
96+ type : 'asset' ,
97+ fileName : 'package.json' ,
98+ source : JSON . stringify ( { type : 'commonjs' } , null , 2 )
99+ } ) ;
100+ }
101+ } ,
102+ ...( options . plugins ?? [ ] )
103+ ]
104+ }
105+ ]
66106}
67107
68- export default createRollupConfig
108+ export default createRollupConfig
0 commit comments