This repository has been archived by the owner on Mar 15, 2020. It is now read-only.
/
banner.js
253 lines (233 loc) · 6.44 KB
/
banner.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { expect } from 'chai';
import emitAssetPlugin from './helpers/emitAssetPlugin.js';
import createTransformTest from './helpers/createTransformTest.js';
import { defaultBabelOptions } from './helpers/createTransformTest.js';
import { defaultRollupOptions } from './helpers/createTransformTest.js';
import { defaultBundleOptions } from './helpers/createTransformTest.js';
import { assertTranspiled } from './helpers/createTransformTest.js';
import { getChunksNames } from './helpers/createTransformTest.js';
import { assertChunks } from './helpers/createTransformTest.js';
import { assertAssets } from './helpers/createTransformTest.js';
import plugin from '../src/index.js';
describe( 'banner and comments support', () => {
it( 'removes comments', () => {
return createTransformTest( {
babelOptions: Object.assign( {}, defaultBabelOptions, {
comments: false
} ),
rollupOptions: {
plugins: [
plugin( {
comments: false
} )
]
}
} ).then( assertTranspiled );
} );
it( 'adds banner even if comments are removed', () => {
return createTransformTest( {
rollupOptions: {
plugins: [
plugin( {
comments: false,
banner: '/* hublabubla */'
} )
]
}
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\// );
} );
} );
it( 'adds banner inherited from bundle.generate', () => {
return createTransformTest( {
bundleOptions: Object.assign( {}, defaultBundleOptions, {
banner: '/* hublabubla */'
} )
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\// );
} );
} );
// #146
it( 'adds banner inherited from bundle.generate only once when comments are enabled', () => {
const rollupOptions = Object.assign( {}, defaultRollupOptions );
// Plugin adding banner dynamically is used to ensure that test passes due to fix,
// not due to the fact that plugin does not insert banner on its own, causing that
// banner is inserted only once (by Rollup).
const bannerPlugin = {
outputOptions( outputOptions ) {
return Object.assign( {}, outputOptions, {
banner: '/* hublabubla */'
} );
}
};
rollupOptions.plugins = [ ... rollupOptions.plugins ];
rollupOptions.plugins.unshift( bannerPlugin );
return createTransformTest( {
rollupOptions,
bundleOptions: defaultBundleOptions
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\// );
expect( bundle.code ).not.to.match( /^(\/\* hublabubla \*\/\s*){2,}/ );
} );
} );
it( 'adds banner inherited from root configuration', () => {
const bannerOptions = {
output: {
banner: '/* hublabubla */'
}
};
const rollupOptions = Object.assign( defaultRollupOptions, bannerOptions );
const bundleOptions = Object.assign( {}, defaultBundleOptions, bannerOptions );
return createTransformTest( {
rollupOptions,
bundleOptions
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\// );
} );
} );
it ( 'adds banner as a result of call if plugin banner option is fn itself', () => {
return createTransformTest( {
rollupOptions: {
plugins: [
plugin( {
banner: () => {
return '/* hublabubla */';
}
} )
]
}
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\// );
} );
} );
it( 'preserves comments alongside banner if no comments option is passed', () => {
return createTransformTest( {
rollupOptions: {
plugins: [
plugin( {
banner: '/* hublabubla */'
} )
]
}
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\// );
expect( bundle.code ).to.match( /.+\/\* Simple comment \*\/.+/g );
} );
} );
it( 'preserves comments alongside banner if comments option is set to true', () => {
return createTransformTest( {
rollupOptions: {
plugins: [
plugin( {
comments: true,
banner: '/* hublabubla */'
} )
]
}
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\// );
expect( bundle.code ).to.match( /.+\/\* Simple comment \*\/.+/g );
} );
} );
// #16
it( 'adds new line after banner if appropriate option is passed', () => {
return createTransformTest( {
rollupOptions: {
plugins: [
plugin( {
banner: '/* hublabubla */',
bannerNewLine: true
} )
]
}
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\/\n[^\n]/ );
} );
} );
// #16
it( 'does not add new line after banner if appropriate option is not passed', () => {
return createTransformTest( {
rollupOptions: {
plugins: [
plugin( {
banner: '/* hublabubla */'
} )
]
}
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\/[^\n]/ );
} );
} );
// #16
it( 'does not add new line after banner if appropriate option is set to false', () => {
return createTransformTest( {
rollupOptions: {
plugins: [
plugin( {
banner: '/* hublabubla */',
bannerNewLine: false
} )
]
}
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\/[^\n]/ );
} );
} );
// #138
it ( 'works with other plugins', () => {
return createTransformTest( {
skipBabel: true,
fixture: 'asyncGenerators',
rollupOptions: {
plugins: [
plugin( {
plugins: [
'@babel/plugin-syntax-async-generators'
],
banner: '/* hublabubla */'
} )
]
}
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\// );
} );
} );
// #139, #144
it( 'adds banner to chunks', () => {
return createTransformTest( {
fixture: 'chunks',
skipBabel: true,
rollupOptions: {
plugins: [
plugin( {
banner: '/* hublabubla */'
} )
]
}
} ).then( ( { bundle: { code }, chunks } ) => {
const chunksNames = getChunksNames( code );
assertChunks( chunks, chunksNames );
chunks.forEach( ( { code } ) => {
expect( code ).to.match( /^\/\* hublabubla \*\// );
} );
} );
} );
// #139
it( 'does not add banner to assets', () => {
return createTransformTest( {
rollupOptions: {
plugins: [
emitAssetPlugin(),
plugin( {
banner: '/* hublabubla */'
} )
]
}
} ).then( ( { chunks } ) => {
assertAssets( chunks );
chunks.forEach( ( { code } ) => {
expect( code ).not.to.match( /^\/\* hublabubla \*\// );
} );
} );
} );
} );