内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>gulp-if javascript file but not in specific directory for gulp-useref - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

gulp-if javascript file but not in specific directory for gulp-useref - Stack Overflow

programmeradmin0浏览0评论

I would like to run minifaction on javascript files that are not in the ./src/ponents directory. I tried this:

    gulp.task('index', function () {
    var assets = useref.assets();

    return gulp.src('src/index.html')
        .pipe(assets)
        .pipe(gulpif('!./src/ponents/** && *.js', uglify()))
        .pipe(gulpif('*.css', minifyCss()))
        .pipe(assets.restore())
        .pipe(useref())
        .pipe(gulp.dest('dist'));
});

but it does not seem to be working (i.e. it preforms minification on js files in ./src/ponents anyway).

All help is appreciated. Thanks in advance.

I would like to run minifaction on javascript files that are not in the ./src/ponents directory. I tried this:

    gulp.task('index', function () {
    var assets = useref.assets();

    return gulp.src('src/index.html')
        .pipe(assets)
        .pipe(gulpif('!./src/ponents/** && *.js', uglify()))
        .pipe(gulpif('*.css', minifyCss()))
        .pipe(assets.restore())
        .pipe(useref())
        .pipe(gulp.dest('dist'));
});

but it does not seem to be working (i.e. it preforms minification on js files in ./src/ponents anyway).

All help is appreciated. Thanks in advance.

Share Improve this question edited Apr 9, 2015 at 11:05 pQuestions123 asked Apr 8, 2015 at 13:28 pQuestions123pQuestions123 4,6116 gold badges30 silver badges60 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Update facing the real problem

Okay, that is a point. gulp-if uses gulp-match to evaluate conditions. There you can use an array of globs:

.pipe(gulpif(['*.js', '!./src/ponents/**'], uglify())

Still for others:

gulp-if might be the wrong approach. I'd suggest a stream queue.

var queue = require('streamqueue')

...

return queue.queue(
    // all the JS files without ponents
    gulp.src(['**/*.js', '!./src/ponents/**/'])
        .pipe(uglify()),
    // all JS files in ponents
    gulp.src('./src/ponents/**/*.js')
)
.done()
.pipe(doSomething())

This way you can do separate tasks on all your non-ponents, and afterwards execute mon processes.

Code's not tested, but you get the idea looking at the docs.

The real problem here I believe is that var assets = useref.assets(); stores the concatenated files in assets. Therefore, the paths of the individual files are lost and any gulp-if statement that uses the paths, useless.

There is an option for useref.assets(noconcat: true). I thought I could then use:

var assets = useref.assets(noconcat: true);

return gulp.src('src/index.html')
.pipe(assets)
.pipe(gulpif(['*.js', '!./src/ponents/**'], uglify())
.pipe...

but this didn't work either. I am not sure why (perhaps the full paths of the files are not stored?).

Regardless the solution I eventually went with was to use gulp-usemin. gulp-usemin can do eveything I am trying to acplish in my original post like so:

gulp.task('usemin', function() {
    return gulp.src(paths.index)
        .pipe(usemin({
            js1: [uglify(), 'concat'],
            js2: ['concat'],
            css: [minifyCss(), 'concat']
        }))
        .pipe(gulp.dest('dist/'));
});

where js1 and js2 are pipeline ids (specified in the html).

It is correct that useref.assets() stores the concatenated files in assets. But, there is now an option to pass the non-concatenated files down the stream. Not sure if this will fix the problem or not, but in the other example that was posted earlier, the options are not being passed in correctly. Options should be passed in as an object, so it should look like this:

var assets = useref.assets({ noconcat: true });

return gulp.src('src/index.html')
    .pipe(assets)
    .pipe(gulpif(['*.js', '!./src/ponents/**'], uglify())
    .pipe...

I will update the docs for that because I just noticed there is no example with the options in it.

发布评论

评论列表(0)

  1. 暂无评论