UNPKG

2.19 kBJavaScriptView Raw
1import Promise from 'es6-promise/lib/es6-promise/promise.js';
2import { basename } from './utils/path.js';
3import { writeFile } from './utils/fs.js';
4import { keys } from './utils/object.js';
5import validateKeys from './utils/validateKeys.js';
6import SOURCEMAPPING_URL from './utils/sourceMappingURL.js';
7import Bundle from './Bundle.js';
8
9export const VERSION = '<@VERSION@>';
10
11const ALLOWED_KEYS = [
12 'entry',
13 'dest',
14 'plugins',
15 'external',
16 'onwarn',
17 'indent',
18 'format',
19 'moduleName',
20 'sourceMap',
21 'intro',
22 'outro',
23 'banner',
24 'footer',
25 'globals',
26 'transform',
27 'load',
28 'resolveId',
29 'resolveExternal'
30];
31
32export function rollup ( options ) {
33 if ( !options || !options.entry ) {
34 return Promise.reject( new Error( 'You must supply options.entry to rollup' ) );
35 }
36
37 if ( options.transform || options.load || options.resolveId || options.resolveExternal ) {
38 return Promise.reject( new Error( 'The `transform`, `load`, `resolveId` and `resolveExternal` options are deprecated in favour of a unified plugin API. See https://github.com/rollup/rollup/wiki/Plugins for details' ) );
39 }
40
41 const error = validateKeys( options, ALLOWED_KEYS );
42
43 if ( error ) {
44 return Promise.reject( error );
45 }
46
47 const bundle = new Bundle( options );
48
49 return bundle.build().then( () => {
50 return {
51 imports: bundle.externalModules.map( module => module.id ),
52 exports: keys( bundle.entryModule.exports ),
53 modules: bundle.orderedModules.map( module => {
54 return { id: module.id };
55 }),
56
57 generate: options => bundle.render( options ),
58 write: options => {
59 if ( !options || !options.dest ) {
60 throw new Error( 'You must supply options.dest to bundle.write' );
61 }
62
63 const dest = options.dest;
64 let { code, map } = bundle.render( options );
65
66 let promises = [];
67
68 if ( options.sourceMap ) {
69 let url;
70
71 if ( options.sourceMap === 'inline' ) {
72 url = map.toUrl();
73 } else {
74 url = `${basename( dest )}.map`;
75 promises.push( writeFile( dest + '.map', map.toString() ) );
76 }
77
78 code += `\n//# ${SOURCEMAPPING_URL}=${url}`;
79 }
80
81 promises.push( writeFile( dest, code ) );
82 return Promise.all( promises );
83 }
84 };
85 });
86}