-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
transformBundle.js
39 lines (32 loc) · 1.06 KB
/
transformBundle.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
import { decode } from 'sourcemap-codec';
import error from './error.js';
export default function transformBundle ( code, plugins, sourceMapChain, options ) {
return plugins.reduce( ( promise, plugin ) => {
if ( !plugin.transformBundle ) return promise;
return promise.then( code => {
return Promise.resolve().then( () => {
return plugin.transformBundle( code, { format : options.format } );
}).then( result => {
if ( result == null ) return code;
if ( typeof result === 'string' ) {
result = {
code: result,
map: null
};
}
const map = typeof result.map === 'string' ? JSON.parse( result.map ) : result.map;
if ( map && typeof map.mappings === 'string' ) {
map.mappings = decode( map.mappings );
}
sourceMapChain.push( map );
return result.code;
}).catch( err => {
error({
code: 'BAD_BUNDLE_TRANSFORMER',
message: `Error transforming bundle${plugin.name ? ` with '${plugin.name}' plugin` : ''}: ${err.message}`,
plugin: plugin.name
});
});
});
}, Promise.resolve( code ) );
}