Git Product home page Git Product logo

stream's Introduction

tests cover npm size libera manifesto

@rollup/stream

๐Ÿฃ Stream Rollup build results

This package exists to provide a streaming interface for Rollup builds. This is useful in situations where a build system is working with vinyl files, such as gulp.js.

Requirements

This plugin requires an LTS Node version (v8.0.0+) and Rollup v1.20.0+.

Install

Using npm:

npm install @rollup/stream --save-dev

Usage

Assume a src/index.js file exists and contains code like the following:

export default 'jingle bells, batman smells';

We can bundle src/index.js using streams such like:

import rollupStream from '@rollup/stream';

const { log } = console;
const options = {
  input: 'src/index.js',
  output: { format: 'cjs' },
};
const stream = rollupStream(options);
let bundle = '';

stream.on('data', (data) => (bundle += data));
stream.on('end', () => log(bundle));

The preceding code will concatenate each chunk (or asset) and output the entire bundle's content when Rollup has completed bundling and the stream has ended.

Options

All Rollup options are valid to pass as options to @rollup/stream.

Usage with Gulp

Using Gulp requires piping. Suppose one wanted to take the bundle content and run it through a minifier, such as terser:

import rollupStream from '@rollup/stream';
import gulp from 'gulp';
import terser from 'gulp-terser';
import source from 'vinyl-source-stream';

gulp.task('rollup', () => {
  const options = { input: 'src/index.js' };
  return rollupStream(options)
    .pipe(source('bundle.js'))
    .pipe(terser({ keep_fnames: true, mangle: false }))
    .pipe(gulp.dest('dist'));
});

Using Sourcemaps

Rollup can produce source maps by specifying the sourcemap output option. For example; to use the generated sourcemaps with Gulp and @rollup/stream:

import rollupStream from '@rollup/stream';
import buffer from 'vinyl-buffer';
import gulp from 'gulp';
import sourcemaps from 'gulp-sourcemaps';
import source from 'vinyl-source-stream';

gulp.task('rollup', () => {
  const options = { input: 'src/index.js', output: { sourcemap: true } };
  return rollupStream(options)
    .pipe(source('bundle.js'))
    .pipe(buffer())
    .pipe(sourcemaps.init({ loadMaps: true }))
    .pipe(sourcemaps.write('dist'))
    .pipe(gulp.dest('dist'));
});

Caching

The ability to cache a build is already built into Rollup, so users of @rollup/stream get that for free. Caching can be useful to reduce or optimize build times, and is often used when watching files that are part of a build. For example; to utilize caching with Gulp and @rollup/stream:

import rollupStream from '@rollup/stream';
import buffer from 'vinyl-buffer';
import gulp from 'gulp';
import source from 'vinyl-source-stream';

// declare the cache variable outside of task scopes
let cache;

gulp.task('rollup', () => {
  return rollupStream({
    input: 'src/index.js',
    // define the cache in Rollup options
    cache,
  })
    .on('bundle', (bundle) => {
      // update the cache after every new bundle is created
      cache = bundle;
    })
    .pipe(source('bundle.js'))
    .pipe(buffer())
    .pipe(gulp.dest('dist'));
});

gulp.task('watch', (done) => {
  gulp.watch('./src/**/*.js', gulp.series('rollup'));

  // or, with Gulp v3
  // gulp.watch('./src/**/*.js', ['rollup']);

  done();
});

(Example based on the rollup-stream README)

Meta

CONTRIBUTING

LICENSE (MIT)

stream's People

Contributors

dan503 avatar lukastaegert avatar notwoods avatar shellscape avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.