This repository has been archived by the owner on Mar 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.js
70 lines (53 loc) · 1.52 KB
/
utils.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
import { encode as encodeSourceMap } from 'sourcemap-codec';
import { decode as decodeSourceMap } from 'sourcemap-codec';
function addNewLine( code, map, banner ) {
map = Object.assign( {}, map );
code = code.replace( banner, `${ banner }\n` );
const mappings = decodeSourceMap( map.mappings );
let codeStart = banner.match( /\n/g );
codeStart = codeStart ? codeStart.length + 1 : 1;
let whitespaceAtStart = code.replace( `${ banner }\n`, '' ).match( /^(\s)+/g );
whitespaceAtStart = whitespaceAtStart ? whitespaceAtStart.length : 0;
mappings.unshift( [] );
if ( Array.isArray( mappings[ codeStart ] ) && mappings[ codeStart ].length ) {
const offset = mappings[ codeStart ][ 0 ][ 0 ] - whitespaceAtStart;
mappings[ codeStart ].forEach( ( segment ) => {
segment[ 0 ] -= offset;
} );
}
map.mappings = encodeSourceMap( mappings );
return {
code,
map
};
}
function filterMinifyOptions( options ) {
const disallowedProperties = [
'banner',
'bannerNewLine',
'sourceMap',
'comments',
'plugins'
];
const minifyOptions = {};
Object.keys( options ).forEach( ( option ) => {
if ( disallowedProperties.indexOf( option ) === -1 ) {
minifyOptions[ option ] = options[ option ];
}
} );
return minifyOptions;
}
function isString( v ) {
return typeof v === 'string';
}
function isFn( v ) {
return typeof v === 'function';
}
function isFnOrString( v ) {
return isString( v ) || isFn( v );
}
export { addNewLine };
export { filterMinifyOptions };
export { isString };
export { isFn };
export { isFnOrString };