1
+ "use strict" ;
2
+
3
+ /**
4
+ * The MIT License (MIT)
5
+ *
6
+ * Copyright (c) 2016-2019 Mickael Jeanroy
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ */
26
+ var path = require ( 'path' ) ;
27
+
28
+ var rollupPluginUtils = require ( 'rollup-pluginutils' ) ;
29
+
30
+ var extractBanner = require ( 'extract-banner' ) ;
31
+
32
+ var MagicString = require ( 'magic-string' ) ;
33
+
34
+ module . exports = function ( ) {
35
+ var options = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { } ;
36
+ var filter = rollupPluginUtils . createFilter ( options . include , options . exclude ) ;
37
+ return {
38
+ transform : function transform ( code , id ) {
39
+ if ( ! filter ( id ) ) {
40
+ return ;
41
+ } // Remove banner for JS files only
42
+
43
+
44
+ var ext = path . extname ( id ) . toLowerCase ( ) ;
45
+
46
+ if ( ext !== '.js' && ext !== '.jsx' && ext !== '.ts' && ext !== '.tsx' ) {
47
+ return ;
48
+ }
49
+
50
+ var banner = extractBanner ( code ) ;
51
+
52
+ if ( ! banner ) {
53
+ return ;
54
+ } // Use a magicString: it will generate the sourceMap at the end.
55
+
56
+
57
+ var magicString = new MagicString ( code ) ;
58
+ var pos = code . indexOf ( banner ) ; // Remove the banner with the magicString instance.
59
+
60
+ magicString . remove ( pos , pos + banner . length ) ; // Trim left to remove blank spaces.
61
+
62
+ magicString . trimStart ( ) ;
63
+ return {
64
+ code : magicString . toString ( ) ,
65
+ map : magicString . generateMap ( {
66
+ hires : true
67
+ } )
68
+ } ;
69
+ }
70
+ } ;
71
+ } ;
0 commit comments