3.0.1 • Published 3 years ago

gulp-memory-fs v3.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

gulp-memory-fs

中文文档

npm.io

gulp-memory-fs allows developers to use the memory file system ( memfs ) when building with gulp。

memory-fs is deprecated.

Start Using

const gulp = require('gulp');
const GulpMemoryFs = require('gulp-memory-fs');

const mfs = new GulpMemoryFs({
  dir: 'dist'
});

function build() {
  return gulp.src(path.join(__dirname, 'src/**/*.js'))
    .pipe(mfs.changed()) // or mfs.changed('dist')
    .pipe(mfs.dest());   // or mfs.dest('dist')
}

async function server() {
  await mfs.createServer();
}

function watch() {
  gulp.watch('src/**/*.js', build);
}

exports.default = gulp.series(
  build,
  gulp.parallel(watch, server)
);

Open the browser and type http://127.0.0.1:7777/ to start development.

API

GulpMemoryFs

ParameterTypeDescriptionDefault
portnumberService port number7777
dirstringDirectory of resources 
https{ key: string; cert: string; }Configure the file address of the https certificate, service enables https. 
reloadbooleanWhether the browser refreshes when the file is savedfalse
reloadTimenumberDelayed refresh time of the browser after the file is modified250
mock{ key: string: any | ((ctx: Context, next: Function) => void | Promise); }Configuring mock data 
proxy{ key: string: object; }Configuring the proxy 
mimeTypes{ key: string: string; }Configure mimeTypes 

GulpMemoryFs.prototype.changed & GulpMemoryFs.prototype.dest

Since it is a memory file system, you cannot use gulp-changed and use GulpMemoryFs.prototype.changed to compile only the modified file.

ParameterTypeDescription
outputstringOutput file directory

GulpMemoryFs.prototype.createServer

Start the service.

Mock

The mapping rules of mock are as follows:

const mock = {
  // How to use
  'GET /mock/data': { data: [1, 2] },

  // When the request method is omitted, the default request method is GET
  '/mock/data': { data: [1, 2] },

  // Support for custom functions, API reference koa and @koa/router
  'POST /mock/data': (ctx, next) => ctx.body = 'ok'
};

Proxy

The rules of the proxy are as follows:

const proxy = {
  '/proxy/raw/githubusercontent': {
    target: 'https://raw.githubusercontent.com/',
    changeOrigin: true,
    pathRewrite: {
      '^/proxy/raw/githubusercontent': ''
    }
  }
};

Proxy configuration reference http-proxy-middleware.

MimeTypes

const mimeTypes = {
  avif: 'image/avif'
};

Test

npm run example
npm run test