|
| 1 | +# Rollup with rollup-stream |
| 2 | + |
| 3 | +Like Browserify, [Rollup](http://rollupjs.org/) is a bundler and thus only fits naturally into gulp if it's at the start of the pipeline. Unlike Browserify, Rollup doesn't natively produce a stream as output and needs to be wrapped before it can take this position. [rollup-stream](https://github.com/Permutatrix/rollup-stream) does this for you, producing output just like that of Browserify's `bundle()` method—as a result, most of the Browserify recipes here will also work with rollup-stream. |
| 4 | + |
| 5 | +## Basic usage |
| 6 | +```js |
| 7 | +// npm install --save-dev rollup-stream vinyl-source-stream |
| 8 | +var gulp = require('gulp'); |
| 9 | +var rollup = require('rollup-stream'); |
| 10 | +var source = require('vinyl-source-stream'); |
| 11 | + |
| 12 | +gulp.task('rollup', function() { |
| 13 | + return rollup({ |
| 14 | + entry: './src/main.js' |
| 15 | + }) |
| 16 | + |
| 17 | + // give the file the name you want to output with |
| 18 | + .pipe(source('app.js')) |
| 19 | + |
| 20 | + // and output to ./dist/app.js as normal. |
| 21 | + .pipe(gulp.dest('./dist')); |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage with sourcemaps |
| 26 | +```js |
| 27 | +// npm install --save-dev rollup-stream gulp-sourcemaps vinyl-source-stream vinyl-buffer |
| 28 | +// optional: npm install --save-dev gulp-rename |
| 29 | +var gulp = require('gulp'); |
| 30 | +var rollup = require('rollup-stream'); |
| 31 | +var sourcemaps = require('gulp-sourcemaps'); |
| 32 | +//var rename = require('gulp-rename'); |
| 33 | +var source = require('vinyl-source-stream'); |
| 34 | +var buffer = require('vinyl-buffer'); |
| 35 | + |
| 36 | +gulp.task('rollup', function() { |
| 37 | + return rollup({ |
| 38 | + entry: './src/main.js', |
| 39 | + sourceMap: true |
| 40 | + }) |
| 41 | + |
| 42 | + // point to the entry file. |
| 43 | + .pipe(source('main.js', './src')) |
| 44 | + |
| 45 | + // buffer the output. most gulp plugins, including gulp-sourcemaps, don't support streams. |
| 46 | + .pipe(buffer()) |
| 47 | + |
| 48 | + // tell gulp-sourcemaps to load the inline sourcemap produced by rollup-stream. |
| 49 | + .pipe(sourcemaps.init({loadMaps: true})) |
| 50 | + |
| 51 | + // transform the code further here. |
| 52 | + |
| 53 | + // if you want to output with a different name from the input file, use gulp-rename here. |
| 54 | + //.pipe(rename('index.js')) |
| 55 | + |
| 56 | + // write the sourcemap alongside the output file. |
| 57 | + .pipe(sourcemaps.write('.')) |
| 58 | + |
| 59 | + // and output to ./dist/main.js as normal. |
| 60 | + .pipe(gulp.dest('./dist')); |
| 61 | +}); |
| 62 | +``` |
0 commit comments