无法让 gulp-rev-replace 与 gulp-useref 一起使用

时间:2023-03-19
本文介绍了无法让 gulp-rev-replace 与 gulp-useref 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

继续我之前的 问题 - 但这次是下一步:让文件修订工作.

Continuing my previous question - but this time is the next step: getting file revisions to work.

我正在学习 johnpapa 的 Gulp 自动化课程,但似乎遇到了另一面墙(当您尝试将简明课程调整为不同的文件结构时,您会遇到这种情况:P).

I'm working through johnpapa's course on automation with Gulp and seem to hit another wall (that's what you get when you try to adapt a concise course to a different file structure :P ).

基本上,问题是我确实得到了带有修订名称的文件,但是这些名称没有进入最终结果 test.jsp,我不知道为什么...

Basically, the issue is that I do get files named with a revision, but those names do not get into the end-result test.jsp, and I can't figure out why...

这是任务(为简洁起见省略了缩小,但它适用于文件修订):

This is the task (the minification is omitted for brevity, but it works fine with the file revisioning):

gulp.task('build-dev', ['inject'], function () {

    var assets = $.useref.assets({searchPath: ''});

    return gulp
        .src(config.indexFile)
        .pipe($.rename('test.jsp'))
        .pipe($.plumber())
        .pipe(assets)
        .pipe($.rev())
        .pipe(assets.restore())
        .pipe($.useref())
        .pipe($.revReplace({modifyUnreved: replaceDirectory, modifyReved: replaceDirectory}))
        .pipe(gulp.dest(config.indexLocation))
        .pipe($.rev.manifest())
        .pipe(gulp.dest(config.indexLocation))
        ;
});

inject 是将css和js引用注入索引文件的任务(正常工作),$require('gulp-load-plugins')({lazy: true})config.indexFileindex.jsp.

inject is the task that injects css and js references to the index file (works correctly), $ is require('gulp-load-plugins')({lazy: true}) and config.indexFile is index.jsp.

replaceDirectory 函数是(使用因为 rev-manifest 生成完整路径名):

The replaceDirectory function is (used since the rev-manifest generates full pathnames):

function replaceDirectory(path) {

    var strToReplace = '../..';
    var strToReplaceWith = process.cwd().replace(/\/g, '/') + '/WebContent';

    if (path) {
        return path.replace(strToReplace, strToReplaceWith);
    } else {
        return path;
    }
}

我的文件结构(与课程中的不同)是:

My file structure (unlike the one in the course) is:

- ModuleDir
    - dist
        - css
            - lib.css
            - app.css
        - fonts
        - images
        - js
            - lib.js
            - app.js
    - css
    - js
    - web-app
        - InnerDir
            - index.jsp
            - test.jsp
    - package.json, bower.json, etc. (all the required files)

基本上,index.jsp 是针对 CSS 和 JS 库和应用程序资产进行处理的,这些资产被缩小并连接成 lib.css、lib.js、app.css 和 app.js.之后,所有这些都被注入到 index.jsp 的副本中,该副本称为 test.jsp.

Basically, index.jsp is processed for CSS and JS library and application assets, which are minified and concatenated into lib.css, lib.js, app.css and app.js. Later all these are injected into a copy of index.jsp which is called test.jsp.

资产收集、连接、注入工作和磁盘上的文件修改工作出色.test.jsp中文件名的更新——没那么多...

The asset gathering, concatenation, injection works and file revisions on disk work splendidly. The update of file names in test.jsp- not so much...

任何想法或指针将不胜感激.

Any ideas or pointers will be appreciated.

推荐答案

您必须将 replaceInExtensions: '.jsp' 添加到 revReplace() 的选项中.

You have to add replaceInExtensions: '.jsp' to your options for revReplace().

在我查看插件代码并弄清楚之前,我遇到了这个问题一天半.我正在使用 .php 文件.文档确实说您需要这样做,但很容易错过.

I had this problem for a day and a half before I looked at the plugin code and figured it out. I'm using .php files. The documentation does say you need to do this but it's easily missed.

希望对你有帮助.

这篇关于无法让 gulp-rev-replace 与 gulp-useref 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Node.js 事件流:在哪里设置 MaxListeners? 下一篇:使用 gulp-nodemon & 时无法使用 Ctrl+C 停止 Gulpgulp.watch 一起看

相关文章