gulp-memory-fs
可以让开发者在使用gulp构建时也可以使用内存文件系统( memfs )。
memory-fs 已弃用。
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)
);
打开浏览器,输入http://127.0.0.1:7777/
,开始开发。
参数 | 类型 | 说明 | 默认值 |
---|---|---|---|
port | number | 服务的端口号 | 7777 |
dir | string | 资源的目录 |
|
https | { key: string; cert: string; } | 配置https证书的文件地址,服务启用https | |
reload | boolean | 文件保存时,浏览器是否刷新 | false |
reloadTime | number | 文件修改后,浏览器的延迟刷新时间 | 250 |
mock | { [key: string]: any | ((ctx: Context, next: Function) => void | Promise); } | 配置mock数据 | |
proxy | { [key: string]: object; } | 配置代理 | |
mimeTypes | { [key: string]: string; } | 配置mimeTypes |
由于是内存文件系统,无法使用gulp-changed
,使用GulpMemoryFs.prototype.changed
来只编译修改后的文件。
参数 | 类型 | 说明 |
---|---|---|
output | string | 输出文件的目录 |
启动服务。
mock的映射规则如下:
const mock = {
// 使用方法
'GET /mock/data': { data: [1, 2] },
// 省略请求方法时,默认的请求方法为GET
'/mock/data': { data: [1, 2] },
// 支持自定义函数,API 参考 koa 和 @koa/router
'POST /mock/data': (ctx, next) => ctx.body = 'ok'
};
proxy的规则如下:
const proxy = {
'/proxy/raw/githubusercontent': {
target: 'https://raw.githubusercontent.com/',
changeOrigin: true,
pathRewrite: {
'^/proxy/raw/githubusercontent': ''
}
}
};
proxy的配置参考http-proxy-middleware。
const mimeTypes = {
avif: 'image/avif'
};
npm run example
npm run test