Created
April 25, 2020 17:05
-
-
Save Margino/b426aaac356a8e105e40a8f90a3d3971 to your computer and use it in GitHub Desktop.
Версирование css и js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // gulp-rev 1) добавляем хэш в имя файла, прописываем их в manifest | |
| // gulp-rev-collector переименовываем ссылки на хэшированные файлы. 1) указываем файлы в которых нужно изменить ссылки 2) указываем путь к манифестам | |
| // gul-rev-outdated удаляем старые хэшированные файлы, для этого 1) устанавливаем кучу плагинов: npm i gulp-util rimraf gulp-rev-outdated path through2 2) добавляем функцию cleaner | |
| const rev = require('gulp-rev'); | |
| const revCollector = require('gulp-rev-collector'); | |
| const gutil = require('gulp-util'); | |
| const rimraf = require('rimraf'); | |
| const revOutdated = require('gulp-rev-outdated'); | |
| const path = require('path'); | |
| const through = require('through2'); | |
| gulp.task('rev:css', () => { | |
| return gulp.src([ | |
| `./_app/css/*.css` | |
| ]) | |
| .pipe(rev()) | |
| .pipe(gulp.dest(`./jekyll/css`)) | |
| .pipe(rev.manifest({})) | |
| .pipe(gulp.dest(`./jekyll/_manifest/css`)) | |
| }); | |
| gulp.task('rev:js', () => { | |
| return gulp.src([ | |
| `./_app/js/*.js`, | |
| ]) | |
| .pipe(rev()) | |
| .pipe(gulp.dest(`./jekyll/js`)) | |
| .pipe(rev.manifest()) | |
| .pipe(gulp.dest('./jekyll/_manifest/js')) | |
| }); | |
| gulp.task('revCollector', () => { | |
| return gulp.src([ | |
| `./jekyll/**/*.js`, | |
| `./jekyll/**/*.html`, | |
| `./jekyll/_manifest/**/*.json` | |
| ]) | |
| .pipe(revCollector({replaceReved: true})) | |
| .pipe(gulp.dest((file) => { | |
| return file.base; | |
| })); | |
| }); | |
| function cleaner() { | |
| return through.obj(function(file, enc, cb){ | |
| rimraf( path.resolve( (file.cwd || process.cwd()), file.path), function (err) { | |
| if (err) { | |
| this.emit('error', new gutil.PluginError('Cleanup old files', err)); | |
| } | |
| this.push(file); | |
| cb(); | |
| }.bind(this)); | |
| }); | |
| } | |
| gulp.task('rev:clean', function() { | |
| return gulp.src( [ | |
| `./jekyll/css/*.*`, | |
| `./jekyll/js/*.*`, | |
| `./jekyll/*.html`, | |
| ], {read: false}) | |
| .pipe(revOutdated(1)) // количество ревизий | |
| .pipe(cleaner()); | |
| return; | |
| }); | |
| gulp.task('rev:all', gulp.series( | |
| 'rev:css', | |
| 'rev:js', | |
| 'revCollector', | |
| 'rev:clean' | |
| ) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment