UNPKG

845 BJavaScriptView Raw
1import Promise from 'es6-promise/lib/es6-promise/promise.js';
2
3export default function transform ( source, id, transformers ) {
4 let sourceMapChain = [];
5
6 if ( typeof source === 'string' ) {
7 source = {
8 code: source,
9 ast: null
10 };
11 }
12
13 let originalCode = source.code;
14 let ast = source.ast;
15
16 return transformers.reduce( ( promise, transformer ) => {
17 return promise.then( previous => {
18 return Promise.resolve( transformer( previous, id ) ).then( result => {
19 if ( result == null ) return previous;
20
21 if ( typeof result === 'string' ) {
22 result = {
23 code: result,
24 ast: null,
25 map: null
26 };
27 }
28
29 sourceMapChain.push( result.map );
30 ast = result.ast;
31
32 return result.code;
33 });
34 });
35
36 }, Promise.resolve( source.code ) )
37
38 .then( code => ({ code, originalCode, ast, sourceMapChain }) );
39}