• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

backflip/gulp-resolve-dependencies: Gulp plugin to resolve asset dependencies

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

backflip/gulp-resolve-dependencies

开源软件地址(OpenSource Url):

https://github.com/backflip/gulp-resolve-dependencies

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

gulp-resolve-dependencies

Resolve dependency directives in assets, e.g. @depend (Juicer) or //= require (Sprockets)). Inspired by grunt-concat-in-order. Useful in combination with gulp-concat.

Usage

First, install gulp-resolve-dependencies as a development dependency:

npm install --save-dev gulp-resolve-dependencies

Then, add it to your gulpfile.js (probably together with gulp-concat):

var resolveDependencies = require('gulp-resolve-dependencies');
var concat = require('gulp-concat');

gulp.task('js', function(){
  gulp.src(['app/assets/js/main.js'])
    .pipe(resolveDependencies({
      pattern: /\* @requires [\s-]*(.*\.js)/g
    }))
        .on('error', function(err) {
            console.log(err.message);
        })
    .pipe(concat())
    .pipe(gulp.dest('dest/assets/js/'));
});

And use the directives in your JS files (dependencies can be nested, they are handled recursively):

/**
 * @requires libs/jquery/jquery.js
 * @requires ../modules/slideshow/slideshow.js
 */

(function(window, document, $, undefined) {
    'use strict';

    $(document).on('ready', function() {
        $('.slideshow').slideshow();
    });

})(window, document, jQuery);

Warning: This might not be very efficient (especially in case of nested dependencies). Some kind of caching mechanism could come in handy.

Circular dependencies are either silently ignored or emit an error. See options.ignoreCircularDependencies below.

gulp.src will not necessarily return files in a deterministic way. If this turns out to be an issue, using a plugin like gulp-sort or gulp-order right after gulp.src might be an option for you. Thanks to @fabiospampinato for pointing this out.

API

resolveDependencies(options)

options.pattern

Type: RegExp

The matching pattern (defaults to /\* @requires [\s-]*(.*?\.js)/g).

options.resolvePath

Type: Function

Resolver for matched paths. Default:

function(match, targetFile) {
    return path.join(path.dirname(targetFile.path), match);
}

Parameters:

  • match {String} Matched file path (in the example above this would be libs/jquery/jquery.js and ../modules/slideshow/slideshow.js, respectively)
  • targetFile {Vinyl file object} Currently parsed file (where the matches were found)

The path package is available in this context.

Returning a falsy value will ignore the resolved path:

function(match, targetFile) {
    // Ignore `/lib.js`
    if (match.match(/\/lib\.js$/)) {
        return null;
    }

    return path.join(path.dirname(targetFile.path), match);
}

options.log

Type: Boolean

Whether to log the resolved dependencies (defaults to false).

options.ignoreCircularDependencies

Type: Boolean

Whether to just continue instead of emitting an error if circular dependencies are detected (defaults to false).

options.include

Type: Array

Only dependencies matching this array of absolute paths will be included (defaults to []).

options.exclude

Type: Array

Dependencies matching this array of absolute paths will be excluded (defaults to []).

Contributors




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap