代码拉取完成,页面将自动刷新
'use strict';
var fs = require('fs');
var path = require('path');
var Vinyl = require('vinyl');
var log = require('fancy-log');
var through = require('through2');
var colors = require('ansi-colors');
var PluginError = require('plugin-error');
var PLUGIN_NAME = 'gulp-file-assets';
var ASSETS_RE = /([^'"# ()?]+\.(EXT))\b/ig;
var defExts = [
'js', 'css', 'html', 'tpl',
'jpg', 'jpeg', 'png', 'gif', 'svg', 'webp',
'ttf', 'eot', 'otf', 'woff'
];
function getAvailableExts(includes, excludes) {
var exts = [];
for (var i = 0, l = includes.length; i < l; ++i) {
var ext = includes[i];
if (excludes.indexOf(ext) < 0) {
exts.push(ext);
}
}
return exts;
}
function getAbsolutePath(filename) {
if (!path.isAbsolute(filename)) {
filename = path.join(process.cwd(), filename);
}
return filename;
}
function isLocal(url) {
return !/^(https?:)?\/\//.test(url);
}
function isIgnored(ignores, filename) {
for (var i = 0, l = ignores.length; i < l; ++i) {
var item = ignores[i];
if (item instanceof RegExp) {
if (item.test(filename)) {
return true;
}
} else if (item === filename) {
return true;
}
}
return false;
}
function parseAssets(file, reference, curDepth, opts, push) {
var depth = opts['depth'];
var pattern = opts['pattern'];
var ignores = opts['ignores'];
var includeSrc = opts['includeSrc'];
var fileBase = file.base;
var filePath = file.path;
var relative = file.relative;
var contents = file.contents;
if (isIgnored(ignores, filePath)) {
return;
}
if (reference || includeSrc) {
log(PLUGIN_NAME + ':', 'Extract', (reference ? colors.green(reference) + ' -> ' : '') + colors.green(relative));
ignores.push(filePath);
push(file);
}
if (depth > 0 && curDepth >= depth) {
return;
}
var code = contents.toString();
if (Buffer.from(code).length === contents.length) {
code.replace(pattern, function ($, url) {
if (!isLocal(url)) {
return;
}
var files = [
getAbsolutePath(path.join(fileBase, url)),
getAbsolutePath(path.join(path.dirname(filePath), url))
];
if (files[0] === files[1]) {
files.pop();
}
for (var i = 0, l = files.length; i < l; ++i) {
var filename = files[i];
if (fs.existsSync(filename)) {
var asset = new Vinyl({
path: filename,
base: fileBase,
contents: fs.readFileSync(filename)
});
parseAssets(asset, relative, curDepth + 1, opts, push);
}
}
});
}
}
function fileAssets(opts) {
opts = opts || {};
var depth = opts['depth'] || null;
var exts = getAvailableExts(opts['exts'] || defExts, opts['excludes'] || []);
var pattern = new RegExp(ASSETS_RE.source.replace('EXT', exts.join('|')), 'ig');
var includeSrc = opts['includeSrc'] === undefined || opts['includeSrc'];
var ignores = opts['ignores'];
if (ignores) {
ignores = ignores.concat();
for (var i = 0, l = ignores.length; i < l; ++i) {
var item = ignores[i];
if (!(item instanceof RegExp)) {
ignores[i] = getAbsolutePath(item);
}
}
} else {
ignores = [];
}
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
return cb();
} else if (file.isStream()) {
cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
} else if (file.isBuffer()) {
parseAssets(file, null, 0, {
depth: depth,
pattern: pattern,
ignores: ignores,
includeSrc: includeSrc
}, this.push.bind(this));
cb();
}
});
}
fileAssets.defExts = defExts;
module.exports = fileAssets;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。