├── .eslintrc ├── .gitignore ├── CHANGELOG.md ├── README.md ├── bin ├── handleError.js ├── help.md ├── rollup ├── runRollup.js └── showHelp.js ├── index.js └── package.json /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "indent": [ 2, "tab", { "SwitchCase": 1 } ], 4 | "quotes": [ 2, "single" ], 5 | "linebreak-style": [ 2, "unix" ], 6 | "semi": [ 2, "always" ], 7 | "space-after-keywords": [ 2, "always" ], 8 | "space-before-blocks": [ 2, "always" ], 9 | "space-before-function-paren": [ 2, "always" ], 10 | "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ], 11 | "no-cond-assign": [ 0 ] 12 | }, 13 | "env": { 14 | "es6": true, 15 | "browser": true, 16 | "mocha": true, 17 | "node": true 18 | }, 19 | "extends": "eslint:recommended", 20 | "ecmaFeatures": { 21 | "modules": true 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # rollup-babel 2 | 3 | ## 0.6.1 4 | 5 | * Pass through sourcemap option from CLI 6 | 7 | ## 0.6.0 8 | 9 | * Add CLI. Works exactly like the normal `rollup` cli, except with `rollupbabel`. 10 | 11 | ## 0.5.2 12 | 13 | * Append `sourceMappingURL` comment to output 14 | * Expose `bundle.imports`, `bundle.exports`, `bundle.modules` 15 | 16 | ## 0.5.1 17 | 18 | * Enforce sourceMap option 19 | 20 | ## 0.5.0 21 | 22 | * Update rollup version to 0.19 23 | * Sourcemap support 24 | 25 | ## 0.4.0 26 | 27 | * Update rollup version to 0.18 28 | 29 | ## 0.3.0 30 | 31 | * Update rollup version to 0.17 32 | 33 | ## 0.2.0 34 | 35 | * Downgrade rollup to 0.15.0, pending [rollup#132](https://github.com/rollup/rollup/issues/132) 36 | 37 | ## 0.1.5 38 | 39 | * Update rollup version to 0.16.0 40 | 41 | ## 0.1.4 42 | 43 | * Support `transform` option 44 | 45 | ## 0.1.3 46 | 47 | * Update rollup/babel-core versions 48 | 49 | ## 0.1.2 50 | 51 | * Update rollup version 52 | 53 | ## 0.1.1 54 | 55 | * Update rollup version 56 | 57 | ## 0.1.0 58 | 59 | * First release 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rollup-babel 2 | 3 | ## This project has been deprecated – please use [rollup-plugin-babel](https://github.com/rollup/rollup-plugin-babel) instead 4 | 5 | ---- 6 | 7 | ## What? 8 | 9 | A [Rollup](https://github.com/rollup/rollup)-[Babel](https://babeljs.io/) integration. 10 | 11 | ## Why? 12 | 13 | Using Rollup *then* using Babel slows down development, because Babel is much quicker dealing with small files than large bundles. But using Babel *then* Rollup often means you include helpers (e.g. `classCallCheck`) multiple times. 14 | 15 | Another problem: if you have external modules with a `jsnext:main` field, Rollup doesn't know that it needs to run them through Babel. 16 | 17 | This is an attempt to provide the best of both worlds – really fast transpilation, with really high-quality bundling. (Also, because Rollup doesn't include what it doesn't need, you often end up running much less code through Babel in the first place.) 18 | 19 | ## Usage 20 | 21 | Just like you'd use Rollup normally, except that the package name is `rollup-babel`. On the command line, use `rollupbabel`. 22 | 23 | ## Rough edges 24 | 25 | * There's no caching mechanism. We need that for fast incremental rebuilds (i.e. all files are transpiled with Babel each time, whether they've changed or not) 26 | * ~~No CLI~~ 27 | * ~~We're reimplementing a lot of stuff here. Rollup needs better hooks for module resolution, transformation, and manipulating the bundle (i.e. adding helpers)~~ 28 | * ~~Sourcemaps are not currently supported, because we're doing crude string manipulation and the map generated by Babel is discarded~~ 29 | 30 | ## License 31 | 32 | MIT 33 | -------------------------------------------------------------------------------- /bin/handleError.js: -------------------------------------------------------------------------------- 1 | var chalk = require( 'chalk' ); 2 | 3 | var handlers = { 4 | MISSING_INPUT_OPTION: function () { 5 | console.error( chalk.red( 'You must specify an --input (-i) option' ) ); 6 | }, 7 | 8 | MISSING_OUTPUT_OPTION: function () { 9 | console.error( chalk.red( 'You must specify an --output (-o) option when creating a file with a sourcemap' ) ); 10 | }, 11 | 12 | MISSING_NAME: function ( err ) { 13 | console.error( chalk.red( 'You must supply a name for UMD exports (e.g. `--name myModule`)' ) ); 14 | }, 15 | 16 | PARSE_ERROR: function ( err ) { 17 | console.error( chalk.red( 'Error parsing ' + err.file + ': ' + err.message ) ); 18 | }, 19 | 20 | ONE_AT_A_TIME: function ( err ) { 21 | console.error( chalk.red( 'rollup can only bundle one file at a time' ) ); 22 | }, 23 | 24 | DUPLICATE_IMPORT_OPTIONS: function ( err ) { 25 | console.error( chalk.red( 'use --input, or pass input path as argument' ) ); 26 | } 27 | }; 28 | 29 | module.exports = function handleError ( err ) { 30 | var handler; 31 | 32 | if ( handler = handlers[ err && err.code ] ) { 33 | handler( err ); 34 | } else { 35 | console.error( chalk.red( err.message || err ) ); 36 | 37 | if ( err.stack ) { 38 | console.error( chalk.grey( err.stack ) ); 39 | } 40 | } 41 | 42 | console.error( 'Type ' + chalk.cyan( 'rollup --help' ) + ' for help, or visit https://github.com/rollup/rollup/wiki' ); 43 | 44 | process.exit( 1 ); 45 | }; 46 | -------------------------------------------------------------------------------- /bin/help.md: -------------------------------------------------------------------------------- 1 | rollup-babel version <%= version %> 2 | ===================================== 3 | 4 | Usage: rollupbabel [options] 5 | 6 | Basic options: 7 | 8 | -v, --version Show version number 9 | -h, --help Show this help message 10 | -i, --input Input (alternative to ) 11 | -o, --output Output (if absent, prints to stdout) 12 | -f, --format [es6] Type of output (amd, cjs, es6, iife, umd) 13 | -e, --external Comma-separate list of module IDs to exclude 14 | -g, --globals Comma-separate list of `module ID:Global` pairs 15 | Any module IDs defined here are added to external 16 | -n, --name Name for UMD export 17 | -u, --id ID for AMD module (default is anonymous) 18 | -m, --sourcemap Generate sourcemap (`-m inline` for inline map) 19 | --no-strict Don't emit a `"use strict";` in the generated modules. 20 | --no-indent Don't indent result 21 | 22 | Examples: 23 | 24 | rollupbabel --format=cjs --output=bundle.js -- src/main.js 25 | 26 | rollupbabel -f iife --globals jquery:jQuery,angular:ng \ 27 | -i src/app.js -o build/app.js -m build/app.js.map 28 | 29 | Notes: 30 | 31 | * When piping to stdout, only inline sourcemaps are permitted 32 | 33 | For more information visit https://github.com/rollup/rollup-babel 34 | -------------------------------------------------------------------------------- /bin/rollup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var minimist = require( 'minimist' ), 4 | command; 5 | 6 | command = minimist( process.argv.slice( 2 ), { 7 | alias: { 8 | // Aliases 9 | strict: 'useStrict', 10 | 11 | // Short options 12 | d: 'indent', 13 | e: 'external', 14 | f: 'format', 15 | g: 'globals', 16 | h: 'help', 17 | i: 'input', 18 | m: 'sourcemap', 19 | n: 'name', 20 | o: 'output', 21 | u: 'id', 22 | v: 'version' 23 | } 24 | }); 25 | 26 | if ( command.help || ( process.argv.length <= 2 && process.stdin.isTTY ) ) { 27 | require( './showHelp' )(); 28 | } 29 | 30 | else if ( command.version ) { 31 | console.log( 'rollup version ' + require( '../package.json' ).version ); 32 | } 33 | 34 | else { 35 | require( './runRollup' )( command ); 36 | } 37 | -------------------------------------------------------------------------------- /bin/runRollup.js: -------------------------------------------------------------------------------- 1 | require( 'source-map-support' ).install(); 2 | 3 | var handleError = require( './handleError' ); 4 | var rollup = require( '../' ); 5 | 6 | module.exports = function ( options ) { 7 | if ( options._.length > 1 ) { 8 | handleError({ code: 'ONE_AT_A_TIME' }); 9 | } 10 | 11 | if ( options._.length === 1 ) { 12 | if ( options.input ) { 13 | handleError({ code: 'DUPLICATE_IMPORT_OPTIONS' }); 14 | } 15 | 16 | options.input = options._[0]; 17 | } 18 | 19 | var external = options.external ? options.external.split( ',' ) : []; 20 | 21 | if ( options.globals ) { 22 | var globals = Object.create( null ); 23 | 24 | options.globals.split( ',' ).forEach(function ( str ) { 25 | var names = str.split( ':' ); 26 | globals[ names[0] ] = names[1]; 27 | 28 | // Add missing Module IDs to external. 29 | if ( external.indexOf( names[0] ) === -1 ) { 30 | external.push( names[0] ); 31 | } 32 | }); 33 | 34 | options.globals = globals; 35 | } 36 | 37 | options.external = external; 38 | 39 | try { 40 | bundle( options ).catch( handleError ); 41 | } catch ( err ) { 42 | handleError( err ); 43 | } 44 | }; 45 | 46 | function bundle ( options ) { 47 | if ( !options.input ) { 48 | handleError({ code: 'MISSING_INPUT_OPTION' }); 49 | } 50 | 51 | return rollup.rollup({ 52 | entry: options.input, 53 | external: options.external, 54 | babel: { 55 | sourceMap: options.sourcemap 56 | } 57 | }).then( function ( bundle ) { 58 | var generateOptions = { 59 | dest: options.output, 60 | format: options.format, 61 | globals: options.globals, 62 | moduleId: options.id, 63 | moduleName: options.name, 64 | sourceMap: options.sourcemap, 65 | indent: options.indent !== false 66 | }; 67 | 68 | if ( options.output ) { 69 | return bundle.write( generateOptions ); 70 | } 71 | 72 | if ( options.sourcemap && options.sourcemap !== 'inline' ) { 73 | handleError({ code: 'MISSING_OUTPUT_OPTION' }); 74 | } 75 | 76 | var result = bundle.generate( generateOptions ); 77 | 78 | var code = result.code, 79 | map = result.map; 80 | 81 | if ( options.sourcemap === 'inline' ) { 82 | code += '\n//# sourceMappingURL=' + map.toUrl(); 83 | } 84 | 85 | process.stdout.write( code ); 86 | }); 87 | } 88 | -------------------------------------------------------------------------------- /bin/showHelp.js: -------------------------------------------------------------------------------- 1 | var fs = require( 'fs' ); 2 | var path = require( 'path' ); 3 | 4 | module.exports = function () { 5 | fs.readFile( path.join( __dirname, 'help.md' ), function ( err, result ) { 6 | var help; 7 | 8 | if ( err ) throw err; 9 | 10 | help = result.toString().replace( '<%= version %>', require( '../package.json' ).version ); 11 | console.log( '\n' + help + '\n' ); 12 | }); 13 | }; 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var path = require( 'path' ); 2 | var sander = require( 'sander' ); 3 | var rollup = require( 'rollup' ); 4 | var babel = require( 'babel-core' ); 5 | 6 | function extend ( target, source ) { 7 | Object.keys( source ).forEach( function ( key ) { 8 | target[ key ] = source[ key ]; 9 | }); 10 | return target; 11 | } 12 | 13 | function rollupBabel ( options ) { 14 | var babelOptions = options.babel || {}; 15 | var index; 16 | 17 | // ensure es6.modules are blacklisted 18 | if ( babelOptions.whitelist ) { 19 | index = babelOptions.whitelist.indexOf( 'es6.modules' ); 20 | if ( ~index ) babelOptions.whitelist.splice( index, 1 ); 21 | } 22 | 23 | if ( !babelOptions.blacklist ) babelOptions.blacklist = []; 24 | index = babelOptions.blacklist.indexOf( 'es6.modules' ); 25 | if ( !~index ) babelOptions.blacklist.push( 'es6.modules' ); 26 | 27 | babelOptions.externalHelpers = true; 28 | 29 | var usedHelpers = []; 30 | 31 | var transformers = Array.isArray( options.transform ) ? 32 | options.transform : 33 | options.transform ? [ options.transform ] : []; 34 | 35 | function rollupBabelTransformer ( code, id ) { 36 | var options = extend({ filename: id }, babelOptions ); 37 | 38 | var transformed = babel.transform( code, options ); 39 | 40 | transformed.metadata.usedHelpers.forEach( function ( helper ) { 41 | if ( !~usedHelpers.indexOf( helper ) ) usedHelpers.push( helper ); 42 | }); 43 | 44 | return { 45 | code: transformed.code, 46 | map: transformed.map 47 | }; 48 | } 49 | 50 | transformers.push( rollupBabelTransformer ); 51 | options.transform = transformers; 52 | 53 | // TODO need to create some hooks so we don't need to reimplement all this... 54 | return rollup.rollup( options ) 55 | .then( function ( bundle ) { 56 | var helpers = babel.buildExternalHelpers( usedHelpers, 'var' ) 57 | .replace( /var babelHelpers = .+/, '' ) 58 | .replace( /babelHelpers\.(\w+) = /g, 'var babelHelpers_$1 = ' ) 59 | .trim(); 60 | 61 | function generate ( options ) { 62 | options = extend( options, { 63 | intro: options.intro ? helpers + '\n\n' + options.intro : helpers + '\n' 64 | }); 65 | 66 | var generated = bundle.generate( options ); 67 | generated.code = generated.code.replace( /babelHelpers\./g, 'babelHelpers_' ); 68 | return generated; 69 | } 70 | 71 | return { 72 | imports: bundle.imports, 73 | exports: bundle.exports, 74 | modules: bundle.modules, 75 | 76 | generate: generate, 77 | write: function ( options ) { 78 | if ( !options || !options.dest ) { 79 | throw new Error( 'You must supply options.dest to bundle.write' ); 80 | } 81 | 82 | var dest = options.dest; 83 | var generated = generate( options ); 84 | 85 | var code = generated.code; 86 | 87 | var promises = []; 88 | 89 | if ( options.sourceMap ) { 90 | var url; 91 | 92 | if ( options.sourceMap === 'inline' ) { 93 | url = generated.map.toUrl(); 94 | } else { 95 | url = path.basename( dest ) + '.map'; 96 | promises.push( sander.writeFile( dest + '.map', generated.map.toString() ) ); 97 | } 98 | 99 | code += '\n//# sourceMappingURL=' + url; 100 | } 101 | 102 | promises.push( sander.writeFile( dest, code ) ); 103 | return sander.Promise.all( promises ); 104 | } 105 | }; 106 | }); 107 | } 108 | 109 | module.exports = { 110 | rollup: rollupBabel 111 | }; 112 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-babel", 3 | "description": "Rollup optimised for babel users", 4 | "version": "0.6.2", 5 | "author": "Rich Harris", 6 | "dependencies": { 7 | "babel-core": "^5.8.23", 8 | "chalk": "^1.1.1", 9 | "minimist": "^1.2.0", 10 | "rollup": "^0.19.2", 11 | "sander": "^0.4.0", 12 | "source-map-support": "^0.3.2" 13 | }, 14 | "devDependencies": { 15 | "eslint": "^1.7.3" 16 | }, 17 | "bin": { 18 | "rollupbabel": "./bin/rollup" 19 | }, 20 | "repository": "rollup/rollup-babel" 21 | } 22 | --------------------------------------------------------------------------------