-
Notifications
You must be signed in to change notification settings - Fork 3
/
rollup.config.js
143 lines (137 loc) · 3.31 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require( 'fs' );
const typescript = require( 'rollup-plugin-typescript2' );
const terser = require( '@rollup/plugin-terser' );
const nodePolyfills = require( 'rollup-plugin-node-polyfills' );
const path = require( 'path' );
console.warn(
'Missing shims error is normal; index.js is not supposed to have bundled EventEmitter.'
);
function withMin( outputConfig ) {
const minConfig = Object.assign( {}, outputConfig );
minConfig.file = minConfig.file.replace( /\.js$/, '.min.js' );
minConfig.plugins = Array.from( outputConfig.plugins || [] );
minConfig.plugins.push( terser() );
return [ outputConfig, minConfig ];
}
function getHeader( withPolyfills = false ) {
let header = `
/*!
* # wikimedia-streams
* Receive events from Wikimedia wikis using the Wikimedia Event Platforms' EventStreams.
*
* @author Chlod Alejandro <chlod@chlod.net> and other contributors
* @license Apache-2.0
*/`;
if ( withPolyfills ) {
header += `
/*!
* This file includes code from the \`rollup-plugin-node-polyfills\` package.
* Its license has been attached below:
*
* ----------------------------------------------------------------------------
*
${
fs.readFileSync(
require.resolve( 'rollup-plugin-node-polyfills/LICENSE.md' ),
'utf8'
)
.trim()
.replace( /(?![^\n]{1,78}$)([^\n]{1,78})\s/g, '$1\n' )
.split( '\n' )
.map( line => ` * ${line}` )
.join( '\n' )
}
*/`;
}
header += '\n';
return header.trimStart();
}
/**
* @type {import('rollup').RollupOptions[]}
*/
module.exports = [
{
external: [ 'events', 'eventsource' ],
input: 'src/index.ts',
output: withMin( {
banner: getHeader(),
name: 'WikimediaStream',
format: 'umd',
file: 'dist/browser/index.js',
globals: {
events: 'window',
eventsource: 'EventSource'
},
exports: 'named'
} ),
plugins: [
typescript( {
tsconfig: 'tsconfig.browser.json'
} )
]
},
{
external: [ 'eventsource' ],
input: 'src/index.ts',
output: withMin( {
banner: getHeader( true ),
name: 'WikimediaStream',
format: 'umd',
file: 'dist/browser/bundle.js',
globals: {
eventsource: 'EventSource'
},
exports: 'named'
} ),
plugins: [
nodePolyfills(),
typescript( {
tsconfig: 'tsconfig.browser.json'
} )
]
},
{
external: [ 'eventsource' ],
input: 'src/index.ts',
output: withMin( {
banner: getHeader( true ),
format: 'cjs',
file: 'dist/browser/lib.js',
plugins: [ {
writeBundle( options, bundle ) {
for ( const [ fileName, chunk ] of Object.entries( bundle ) ) {
if ( !/lib(\.min)?\.js/.test( fileName ) ) {
return;
}
// Strip out EventSource import (is global in browser)
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.writeFileSync(
path.join( path.dirname( options.file ), fileName ),
chunk.code
.replace(
/import\s+\S*\s+from\s*["']eventsource["'];(\r\n|\n)*/,
''
)
.replace(
/var\s+\S*\s*=\s*require\s*\(\s*["']eventsource["']\s*\)\s*;(\r\n|\n)*/,
''
),
{ encoding: 'utf8' }
);
}
}
} ],
globals: {
eventsource: 'EventSource'
},
exports: 'named'
} ),
plugins: [
nodePolyfills(),
typescript( {
tsconfig: 'tsconfig.browser.json'
} )
]
}
];