UNPKG

2.15 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 'banner',
13 'dest',
14 'entry',
15 'external',
16 'footer',
17 'format',
18 'globals',
19 'indent',
20 'intro',
21 'moduleId',
22 'moduleName',
23 'onwarn',
24 'outro',
25 'plugins',
26 'sourceMap'
27];
28
29export function rollup ( options ) {
30 if ( !options || !options.entry ) {
31 return Promise.reject( new Error( 'You must supply options.entry to rollup' ) );
32 }
33
34 if ( options.transform || options.load || options.resolveId || options.resolveExternal ) {
35 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' ) );
36 }
37
38 const error = validateKeys( options, ALLOWED_KEYS );
39
40 if ( error ) {
41 return Promise.reject( error );
42 }
43
44 const bundle = new Bundle( options );
45
46 return bundle.build().then( () => {
47 return {
48 imports: bundle.externalModules.map( module => module.id ),
49 exports: keys( bundle.entryModule.exports ),
50 modules: bundle.orderedModules.map( module => {
51 return { id: module.id };
52 }),
53
54 generate: options => bundle.render( options ),
55 write: options => {
56 if ( !options || !options.dest ) {
57 throw new Error( 'You must supply options.dest to bundle.write' );
58 }
59
60 const dest = options.dest;
61 let { code, map } = bundle.render( options );
62
63 let promises = [];
64
65 if ( options.sourceMap ) {
66 let url;
67
68 if ( options.sourceMap === 'inline' ) {
69 url = map.toUrl();
70 } else {
71 url = `${basename( dest )}.map`;
72 promises.push( writeFile( dest + '.map', map.toString() ) );
73 }
74
75 code += `\n//# ${SOURCEMAPPING_URL}=${url}`;
76 }
77
78 promises.push( writeFile( dest, code ) );
79 return Promise.all( promises );
80 }
81 };
82 });
83}