Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

@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:

  1. ``` session
  2. npm install @rollup/stream --save-dev
  3. ```

Usage


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

  1. ``` js
  2. export default 'jingle bells, batman smells';
  3. ```

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

  1. ``` js
  2. import rollupStream from '@rollup/stream';

  3. const { log } = console;
  4. const options = {
  5.   input: 'src/index.js',
  6.   output: { format: 'cjs' },
  7. };
  8. const stream = rollupStream(options);
  9. let bundle = '';

  10. stream.on('data', (data) => (bundle += data));
  11. stream.on('end', () => log(bundle));
  12. ```

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 :

  1. ``` js
  2. import rollupStream from '@rollup/stream';
  3. import gulp from 'gulp';
  4. import terser from 'gulp-terser';
  5. import source from 'vinyl-source-stream';

  6. gulp.task('rollup', () => {
  7.   const options = { input: 'src/index.js' };
  8.   return rollupStream(options)
  9.     .pipe(source('bundle.js'))
  10.     .pipe(terser({ keep_fnames: true, mangle: false }))
  11.     .pipe(gulp.dest('dist'));
  12. });
  13. ```

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 :

  1. ``` js
  2. import rollupStream from '@rollup/stream';
  3. import buffer from 'vinyl-buffer';
  4. import gulp from 'gulp';
  5. import sourcemaps from 'gulp-sourcemaps';
  6. import source from 'vinyl-source-stream';

  7. gulp.task('rollup', () => {
  8.   const options = { input: 'src/index.js', output: { sourcemap: true } };
  9.   return rollupStream(options)
  10.     .pipe(source('bundle.js'))
  11.     .pipe(buffer())
  12.     .pipe(sourcemaps.init({ loadMaps: true }))
  13.     .pipe(sourcemaps.write('dist'))
  14.     .pipe(gulp.dest('dist'));
  15. });
  16. ```

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 :

  1. ``` js
  2. import rollupStream from '@rollup/stream';
  3. import buffer from 'vinyl-buffer';
  4. import gulp from 'gulp';
  5. import source from 'vinyl-source-stream';

  6. // declare the cache variable outside of task scopes
  7. let cache;

  8. gulp.task('rollup', () => {
  9.   return rollupStream({
  10.     input: 'src/index.js',
  11.     // define the cache in Rollup options
  12.     cache,
  13.   })
  14.     .on('bundle', (bundle) => {
  15.       // update the cache after every new bundle is created
  16.       cache = bundle;
  17.     })
  18.     .pipe(source('bundle.js'))
  19.     .pipe(buffer())
  20.     .pipe(gulp.dest('dist'));
  21. });

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

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

  26.   done();
  27. });
  28. ```

(Example based on the rollup-stream README )

Meta


CONTRIBUTING

LICENSE (MIT)
Last Updated: 2023-08-08 08:53:13