1 | import { basename } from './utils/path';
|
2 | import { writeFile } from 'sander';
|
3 | import { keys } from './utils/object';
|
4 | import SOURCEMAPPING_URL from './utils/sourceMappingURL';
|
5 | import Bundle from './Bundle';
|
6 |
|
7 | export function rollup ( options ) {
|
8 | if ( !options || !options.entry ) {
|
9 | throw new Error( 'You must supply options.entry to rollup' );
|
10 | }
|
11 |
|
12 | const bundle = new Bundle( options );
|
13 |
|
14 | return bundle.build().then( () => {
|
15 | return {
|
16 | imports: bundle.externalModules.map( module => module.id ),
|
17 | exports: keys( bundle.entryModule.exports ),
|
18 | modules: bundle.orderedModules.map( module => {
|
19 | return { id: module.id };
|
20 | }),
|
21 |
|
22 | generate: options => bundle.render( options ),
|
23 | write: options => {
|
24 | if ( !options || !options.dest ) {
|
25 | throw new Error( 'You must supply options.dest to bundle.write' );
|
26 | }
|
27 |
|
28 | const dest = options.dest;
|
29 | let { code, map } = bundle.render( options );
|
30 |
|
31 | let promises = [];
|
32 |
|
33 | if ( options.sourceMap ) {
|
34 | let url;
|
35 |
|
36 | if ( options.sourceMap === 'inline' ) {
|
37 | url = map.toUrl();
|
38 | } else {
|
39 | url = `${basename( dest )}.map`;
|
40 | promises.push( writeFile( dest + '.map', map.toString() ) );
|
41 | }
|
42 |
|
43 | code += `\n//# ${SOURCEMAPPING_URL}=${url}`;
|
44 | }
|
45 |
|
46 | promises.push( writeFile( dest, code ) );
|
47 | return Promise.all( promises );
|
48 | }
|
49 | };
|
50 | });
|
51 | }
|