gulp-useref
Advanced tools
+17
| 'use strict'; | ||
| module.exports = function () { | ||
| var _end; | ||
| return { | ||
| set: function (cb) { | ||
| _end = cb; | ||
| return _end; | ||
| }, | ||
| fn: function () { | ||
| return _end(); | ||
| } | ||
| }; | ||
| }; |
| 'use strict'; | ||
| var getPattern = require('./getPattern'); | ||
| module.exports = function (paths, files) { | ||
| var pattern, | ||
| matches, | ||
| glob = require('glob'), | ||
| searchPath = paths.searchPath, | ||
| name = paths.name, | ||
| cwd = paths.cwd, | ||
| basePath = paths.basePath, | ||
| filepath = paths.filepath, | ||
| transformPath = paths.transformPath; | ||
| if (searchPath && Array.isArray(searchPath)) { | ||
| searchPath = searchPath.length === 1 ? searchPath[0] : '{' + searchPath.join(',') + '}'; | ||
| } | ||
| pattern = getPattern(files, { | ||
| destPath: name, | ||
| searchPath: searchPath, | ||
| cwd: cwd, | ||
| basePath: basePath, | ||
| srcPath: filepath | ||
| }); | ||
| matches = glob.sync(pattern, { nosort: true }); | ||
| if (!matches.length) { | ||
| matches.push(pattern); | ||
| } | ||
| if (transformPath) { | ||
| matches[0] = transformPath(matches[0]); | ||
| } | ||
| return matches[0]; | ||
| }; |
| 'use strict'; | ||
| module.exports = function () { | ||
| var unprocessed = 0; | ||
| return { | ||
| get: function () { | ||
| return unprocessed; | ||
| }, | ||
| increment: function () { | ||
| return unprocessed++; | ||
| }, | ||
| decrement: function () { | ||
| return --unprocessed; | ||
| } | ||
| }; | ||
| }; |
+189
-6
| 'use strict'; | ||
| var through = require('through2'), | ||
| streamManager = require('./lib/streamManager'); | ||
| var path = require('path'), | ||
| gutil = require('gulp-util'), | ||
| es = require('event-stream'), | ||
| through = require('through2'), | ||
| useref = require('useref'), | ||
| getGlobs = require('./lib/getGlobs'), | ||
| addFilesFromExtStreams = require('./lib/addFilesFromExtStreams'), | ||
| addHtmlToStream = require('./lib/addHtmlToStream'), | ||
| unprocessedCounter = require('./lib/unprocessedCounter')(), | ||
| end = require('./lib/end')(), | ||
| additionalFiles = [], | ||
| transforms, | ||
| pluginOptions; | ||
| function handleAdditionalStreams(additionalStreams) { | ||
| var _additionalStreams = additionalStreams; | ||
| if (!Array.isArray(additionalStreams)) { | ||
| _additionalStreams = [ additionalStreams ]; | ||
| } | ||
| return _additionalStreams.map(function (stream) { | ||
| // filters stream to select needed files | ||
| return stream.pipe(es.through(function (file) { | ||
| additionalFiles.push(file); | ||
| })); | ||
| }); | ||
| } | ||
| function addAssetsToStream(paths, files) { | ||
| var self = this, | ||
| gulpif = require('gulp-if'), | ||
| concat = require('gulp-concat'), | ||
| isRelativeUrl = require('is-relative-url'), | ||
| vfs = require('vinyl-fs'), | ||
| src, | ||
| globs, | ||
| name = paths.name, | ||
| basePath = paths.basePath, | ||
| filepaths = files[name].assets, | ||
| options = pluginOptions, | ||
| gulpConcatOptions = {}; | ||
| if (!filepaths.length) { | ||
| return; | ||
| } | ||
| unprocessedCounter.increment(); | ||
| // Get relative file paths and join with search paths to send to vinyl-fs | ||
| globs = filepaths | ||
| .filter(isRelativeUrl) | ||
| .map(function (filepath) { | ||
| paths.filepath = filepath; | ||
| return getGlobs(paths, files); | ||
| }); | ||
| src = vfs.src(globs, { | ||
| base: basePath, | ||
| nosort: true | ||
| }) | ||
| .on('error', function (err) { | ||
| self.emit('error', new Error(err)); | ||
| }); | ||
| // add files from external streams | ||
| src = addFilesFromExtStreams.call(self, additionalFiles, globs, src); | ||
| // If any external transforms were included, pipe all files to them first | ||
| transforms.forEach(function (fn) { | ||
| src = src.pipe(fn(name)); | ||
| }); | ||
| // option for newLine in gulp-concat | ||
| if (options.hasOwnProperty('newLine')) { | ||
| gulpConcatOptions.newLine = options.newLine; | ||
| } | ||
| // Add assets to the stream | ||
| // If noconcat option is false, concat the files first. | ||
| src | ||
| .pipe(gulpif(!options.noconcat, concat(name, gulpConcatOptions))) | ||
| .pipe(through.obj(function (newFile, encoding, callback) { | ||
| // specify an output path relative to the cwd | ||
| if (options.base) { | ||
| newFile.path = path.join(options.base, name); | ||
| newFile.base = options.base; | ||
| } | ||
| // add file to the asset stream | ||
| self.push(newFile); | ||
| callback(); | ||
| })) | ||
| .on('finish', function () { | ||
| var unprocessed = unprocessedCounter.decrement(); | ||
| if (unprocessed === 0 && typeof end.fn === 'function') { | ||
| // end the asset stream | ||
| end.fn(); | ||
| } | ||
| }); | ||
| } | ||
| function processAssets(file, basePath, data) { | ||
| var self = this, | ||
| types = pluginOptions.types || [ 'css', 'js' ]; | ||
| types.forEach(function (type) { | ||
| var files = data[type], | ||
| name; | ||
| if (!files) { | ||
| return; | ||
| } | ||
| for (name in files) { | ||
| addAssetsToStream.call(self, { | ||
| name: name, | ||
| basePath: basePath, | ||
| searchPath: pluginOptions.searchPath, | ||
| cwd: file.cwd, | ||
| transformPath: pluginOptions.transformPath | ||
| }, files); | ||
| } | ||
| }); | ||
| } | ||
| module.exports = function (options) { | ||
| streamManager.transforms = Array.prototype.slice.call(arguments, 1); | ||
| streamManager.options = options || {}; | ||
| var opts = options || {}, | ||
| waitForAssets, | ||
| additionalStreams; | ||
| streamManager.additionalStreams(); | ||
| pluginOptions = opts; | ||
| transforms = Array.prototype.slice.call(arguments, 1); | ||
| return through.obj(streamManager.transformFunction, streamManager.flushFunction); | ||
| // If any external streams were included, add matched files to src | ||
| if (opts.additionalStreams) { | ||
| additionalStreams = handleAdditionalStreams(opts.additionalStreams); | ||
| // If we have additional streams, wait for them to run before continuing | ||
| waitForAssets = es.merge(additionalStreams).pipe(through.obj()); | ||
| } else { | ||
| // Else, create a fake stream | ||
| waitForAssets = through.obj(); | ||
| } | ||
| return through.obj(function (file, enc, cb) { | ||
| var self = this; | ||
| waitForAssets.pipe(es.wait(function () { | ||
| var output, | ||
| // Cache the file base path relative to the cwd | ||
| // Use later when it could be dropped | ||
| _basePath = path.dirname(file.path); | ||
| if (file.isNull()) { | ||
| return cb(null, file); | ||
| } | ||
| if (file.isStream()) { | ||
| return cb(new gutil.PluginError('gulp-useref', 'Streaming not supported')); | ||
| } | ||
| output = useref(file.contents.toString(), opts); | ||
| addHtmlToStream.call(self, file, output[0]); | ||
| if (!opts.noAssets) { | ||
| processAssets.call(self, file, _basePath, output[1]); | ||
| } | ||
| return cb(); | ||
| })); | ||
| // If no external streams were included, | ||
| // emit 'end' on the empty stream | ||
| if (!additionalStreams) { | ||
| waitForAssets.emit('end'); | ||
| } | ||
| }, function (cb) { | ||
| var unprocessed = unprocessedCounter.get(), | ||
| fn = function () {}; | ||
| end.set(cb); | ||
| if (unprocessed === 0) { | ||
| fn = cb; | ||
| } | ||
| return fn(); | ||
| }); | ||
| }; |
+4
-4
| { | ||
| "name": "gulp-useref", | ||
| "version": "3.1.0", | ||
| "version": "3.1.1", | ||
| "description": "Parse build blocks in HTML files to replace references to non-optimized scripts or stylesheets.", | ||
@@ -23,8 +23,8 @@ "main": "index.js", | ||
| "gulp": "^3.9.0", | ||
| "gulp-eslint": "^2.0.0", | ||
| "gulp-mocha": "^2.0.0", | ||
| "gulp-eslint": "^3.0.1", | ||
| "gulp-mocha": "^3.0.1", | ||
| "gulp-rename": "^1.2.2", | ||
| "mocha": "*", | ||
| "mock-gulp-dest": "^0.1.1", | ||
| "nyc": "^6.4.0", | ||
| "nyc": "^8.1.0", | ||
| "should": "*" | ||
@@ -31,0 +31,0 @@ }, |
+1
-1
@@ -68,3 +68,3 @@ # [gulp](https://github.com/gulpjs/gulp)-useref [](https://travis-ci.org/jonkemp/gulp-useref) [](https://coveralls.io/github/jonkemp/gulp-useref?branch=master) | ||
| uglify = require('gulp-uglify'), | ||
| minifyCss = require('gulp-minify-css'); | ||
| minifyCss = require('gulp-clean-css'); | ||
@@ -71,0 +71,0 @@ gulp.task('html', function () { |
| 'use strict'; | ||
| var exports = module.exports = (function () { | ||
| var gutil = require('gulp-util'), | ||
| through = require('through2'), | ||
| useref = require('useref'), | ||
| es = require('event-stream'), | ||
| path = require('path'), | ||
| isRelativeUrl = require('is-relative-url'), | ||
| vfs = require('vinyl-fs'), | ||
| getPattern = require('./getPattern'), | ||
| addFilesFromExtStreams = require('./addFilesFromExtStreams'), | ||
| addHtmlToStream = require('./addHtmlToStream'), | ||
| additionalFiles = [], | ||
| unprocessed = 0, | ||
| end; | ||
| return { | ||
| options: {}, | ||
| transforms: null, | ||
| getGlobs: function (file, paths, files) { | ||
| var pattern, | ||
| matches, | ||
| glob = require('glob'), | ||
| options = exports.options, | ||
| searchPath = options.searchPath, | ||
| name = paths.name, | ||
| basePath = paths.basePath, | ||
| filepath = paths.filepath; | ||
| if (searchPath && Array.isArray(searchPath)) { | ||
| searchPath = searchPath.length === 1 ? searchPath[0] : '{' + searchPath.join(',') + '}'; | ||
| } | ||
| pattern = getPattern(files, { | ||
| destPath: name, | ||
| searchPath: searchPath, | ||
| cwd: file.cwd, | ||
| basePath: basePath, | ||
| srcPath: filepath | ||
| }); | ||
| matches = glob.sync(pattern, { nosort: true }); | ||
| if (!matches.length) { | ||
| matches.push(pattern); | ||
| } | ||
| if (options.transformPath) { | ||
| matches[0] = options.transformPath(matches[0]); | ||
| } | ||
| return matches[0]; | ||
| }, | ||
| addAssetsToStream: function (file, paths, files) { | ||
| var self = this, | ||
| gulpif = require('gulp-if'), | ||
| concat = require('gulp-concat'), | ||
| src, | ||
| globs, | ||
| name = paths.name, | ||
| basePath = paths.basePath, | ||
| filepaths = files[name].assets, | ||
| options = exports.options, | ||
| gulpConcatOptions = {}; | ||
| if (!filepaths.length) { | ||
| return; | ||
| } | ||
| unprocessed++; | ||
| // Get relative file paths and join with search paths to send to vinyl-fs | ||
| globs = filepaths | ||
| .filter(isRelativeUrl) | ||
| .map(function (filepath) { | ||
| paths.filepath = filepath; | ||
| return exports.getGlobs(file, paths, files); | ||
| }); | ||
| src = vfs.src(globs, { | ||
| base: basePath, | ||
| nosort: true | ||
| }) | ||
| .on('error', function (err) { | ||
| self.emit('error', new Error(err)); | ||
| }); | ||
| // add files from external streams | ||
| src = addFilesFromExtStreams.call(self, additionalFiles, globs, src); | ||
| // If any external transforms were included, pipe all files to them first | ||
| exports.transforms.forEach(function (fn) { | ||
| src = src.pipe(fn(name)); | ||
| }); | ||
| // option for newLine in gulp-concat | ||
| if (options.hasOwnProperty('newLine')) { | ||
| gulpConcatOptions.newLine = options.newLine; | ||
| } | ||
| // Add assets to the stream | ||
| // If noconcat option is false, concat the files first. | ||
| src | ||
| .pipe(gulpif(!options.noconcat, concat(name, gulpConcatOptions))) | ||
| .pipe(through.obj(function (newFile, encoding, callback) { | ||
| // specify an output path relative to the cwd | ||
| if (options.base) { | ||
| newFile.path = path.join(options.base, name); | ||
| newFile.base = options.base; | ||
| } | ||
| // add file to the asset stream | ||
| self.push(newFile); | ||
| callback(); | ||
| })) | ||
| .on('finish', function () { | ||
| if (--unprocessed === 0 && end) { | ||
| // end the asset stream | ||
| end(); | ||
| } | ||
| }); | ||
| }, | ||
| processAssets: function (file, basePath, data) { | ||
| var self = this, | ||
| types = exports.options.types || [ 'css', 'js' ]; | ||
| types.forEach(function (type) { | ||
| var files = data[type], | ||
| name; | ||
| if (!files) { | ||
| return; | ||
| } | ||
| for (name in files) { | ||
| exports.addAssetsToStream.call(self, file, { | ||
| name: name, | ||
| basePath: basePath | ||
| }, files); | ||
| } | ||
| }); | ||
| }, | ||
| processFilesAndAssets: function (file, cb) { | ||
| var self = this, | ||
| output, | ||
| options = exports.options, | ||
| // Cache the file base path relative to the cwd | ||
| // Use later when it could be dropped | ||
| _basePath = path.dirname(file.path); | ||
| if (file.isNull()) { | ||
| return cb(null, file); | ||
| } | ||
| if (file.isStream()) { | ||
| return cb(new gutil.PluginError('gulp-useref', 'Streaming not supported')); | ||
| } | ||
| output = useref(file.contents.toString(), options); | ||
| addHtmlToStream.call(self, file, output[0]); | ||
| if (!options.noAssets) { | ||
| exports.processAssets.call(self, file, _basePath, output[1]); | ||
| } | ||
| return cb(); | ||
| }, | ||
| transformFunction: function (file, enc, cb) { | ||
| var self = this; | ||
| exports.waitForAssets.pipe(es.wait(function () { | ||
| exports.processFilesAndAssets.call(self, file, cb); | ||
| })); | ||
| // If no external streams were included, | ||
| // emit 'end' on the empty stream | ||
| if (!exports.options.additionalStreams) { | ||
| exports.waitForAssets.emit('end'); | ||
| } | ||
| }, | ||
| flushFunction: function (cb) { | ||
| end = cb; | ||
| if (unprocessed === 0) { | ||
| return cb(); | ||
| } | ||
| }, | ||
| additionalStreams: function () { | ||
| var options = exports.options; | ||
| // reinitialize global variable | ||
| unprocessed = 0; | ||
| // If any external streams were included, add matched files to src | ||
| if (options.additionalStreams) { | ||
| if (!Array.isArray(options.additionalStreams)) { | ||
| options.additionalStreams = [ options.additionalStreams ]; | ||
| } | ||
| options.additionalStreams = options.additionalStreams.map(function (stream) { | ||
| // filters stream to select needed files | ||
| return stream | ||
| .pipe(es.through(function (file) { | ||
| additionalFiles.push(file); | ||
| })); | ||
| }); | ||
| } | ||
| if (options.additionalStreams) { | ||
| // If we have additional streams, wait for them to run before continuing | ||
| exports.waitForAssets = es.merge(options.additionalStreams).pipe(through.obj()); | ||
| } else { | ||
| // Else, create a fake stream | ||
| exports.waitForAssets = through.obj(); | ||
| } | ||
| } | ||
| }; | ||
| }()); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
11
22.22%289
8.24%19849
-3.87%1
Infinity%