shonny-ua / gulp-rev-collector

Static asset revision data collector from manifests, with was generated from different streams and replace they's links in html template.
MIT License
150 stars 41 forks source link

support concat static asserts rev #44

Open Froguard opened 6 years ago

Froguard commented 6 years ago

index.html

<!--
<script src ="/js/com/a.js"></script>
<script src ="/js/com/b.js"></script>
<script src ="/js/com/c.js"></script>
...
-->

<!-- optimize -->
<script src ="/concat_prefix/com/a.js,/com/b.js,/com/c.js"></script>
<!-- '/concat_prefix' map '/js', multi src split by char ','-->

Example:

In this case, the concat_prefix is '/js/??', it map to '/js'

origin: /js/??/com/a.js,/com/b.js,/com/c.js after: /js/??/com/a-[revcode].js,/com/b-[revcode].js,/com/c-[revcode].js

gulpfile.js

gulp.task('afterRev', () => {
    return gulp.src([
            './demo/input/*.json', 
            './demo/input/**/*.html'
        ])
        .pipe(revCollector({
            concatPrefixes: {
                '/js/??': '/js',
                '/css/??': '/css'
            }
        }))
        .pipe(gulp.dest('./demo/output'));
});

You can see PR43 :smiley:

more about concat static asserts : nginx-http-concat

shonny-ua commented 6 years ago

Oh... Why do you need to use modconcat, if you know how to use gulp? What about gulp-concat?

shonny-ua commented 6 years ago

You don't need any pool requests. See next test:

it('should replace parts of concated links in .html file ', function (cb) {
    var stream = revCollector({
    });
    var fileCount = 0;
    var revisionMap = {
        "a.js": "a-17b72c16ec.js",
        "b.js": "b-2c0d21e40c.js",
        "c.js": "c-ffffffffff.js"
    };

    var htmlFileBody        = '<html><head><script src="/js/??/com/a.js,/com/b.js,/com/c.js"></head><body><img src="cdn/image.gif" /></body></html>';
    var htmlRevedFileBody   = '<html><head><script src="/assets/js/com-mon-2c0d21e40c.js"><link rel="stylesheet" href="/assets/css/a-c-12345678.css" /></head><body><img src="cdn/image.gif" /></body></html>';

    stream.write(new gutil.File({
        path: 'rev/css/rev-manifest.json',
        contents: new Buffer(JSON.stringify(revisionMap))
    }));

    stream.write(new gutil.File({
        path: 'index.html',
        contents: new Buffer(htmlFileBody)
    }));

    stream.on('data', function (file) {
        var fpath = file.path;
        var contents = file.contents.toString('utf8');
        var ext = path.extname(file.path);

        assert.equal(ext, '.html', 'Only html files should pass through the stream');

        assert(
            !/\/js\/\?\?\/com\/a\.js,\/com\/b\.js,\/com\/c\.js/.test(contents),
            'The JS file name should be replaced'
        );

        assert(
            /\/js\/\?\?\/com\/a-17b72c16ec\.js,\/com\/b-2c0d21e40c\.js,\/com\/c-ffffffffff\.js/.test(contents),
            'The JS file name should be correct replaced'
        );

        fileCount++;
    });

    stream.on('end', function() {
        assert.equal(fileCount, 1, 'Only one file should pass through the stream');
        cb();
    });

    stream.end();
});

It works using gulp-rev-collector v1.2.2

Froguard commented 6 years ago

how about the files with the same name,if rev by only name without necesary path.

发自网易邮箱大师 On 11/13/2017 19:31, Oleksandr Ovcharenko wrote:

You don't need any pool requests. See next test:

it('should replace parts of concated links in .html file ', function (cb) { var stream = revCollector({ }); var fileCount = 0; var revisionMap = { "a.js": "a-17b72c16ec.js", "b.js": "b-2c0d21e40c.js", "c.js": "c-ffffffffff.js" };

var htmlFileBody        = '<html><head><script src="/js/??/com/a.js,/com/b.js,/com/c.js"></head><body><img src="cdn/image.gif" /></body></html>';
var htmlRevedFileBody   = '<html><head><script src="/assets/js/com-mon-2c0d21e40c.js"><link rel="stylesheet" href="/assets/css/a-c-12345678.css" /></head><body><img src="cdn/image.gif" /></body></html>';

stream.write(new gutil.File({
    path: 'rev/css/rev-manifest.json',
    contents: new Buffer(JSON.stringify(revisionMap))
}));

stream.write(new gutil.File({
    path: 'index.html',
    contents: new Buffer(htmlFileBody)
}));

stream.on('data', function (file) {
    var fpath = file.path;
    var contents = file.contents.toString('utf8');
    var ext = path.extname(file.path);

    assert.equal(ext, '.html', 'Only html files should pass through the stream');

    assert(
        !/\/js\/\?\?\/com\/a\.js,\/com\/b\.js,\/com\/c\.js/.test(contents),
        'The JS file name should be replaced'
    );

    assert(
        /\/js\/\?\?\/com\/a-17b72c16ec\.js,\/com\/b-2c0d21e40c\.js,\/com\/c-ffffffffff\.js/.test(contents),
        'The JS file name should be correct replaced'
    );

    fileCount++;
});

stream.on('end', function() {
    assert.equal(fileCount, 1, 'Only one file should pass through the stream');
    cb();
});

stream.end();

});

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

shonny-ua commented 6 years ago

neural network will help You in this case.

var revisionMap = {
        "a.js": "a-17b72c16ec.js",
        "a.js": "a-aaaaaaaaaa.js",
        "b.js": "b-2c0d21e40c.js",
        "c.js": "c-ffffffffff.js"
    };

console.log(revisionMap); - ???