Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/migrate gulp to version 4 #633

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
355 changes: 160 additions & 195 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,201 +1,168 @@
var gulp = require('gulp');
var gulpLoadPlugins = require('gulp-load-plugins');
var browserSync = require('browser-sync');
var del = require('del');
var fs = require('fs');
var git = require('git-rev-sync');
var tryFn = require('nice-try');
var saveLicense = require('uglify-save-license');

var $ = gulpLoadPlugins();
var reload = browserSync.reload;

gulp.task('clean', del.bind(null, ['.tmp', 'dist']));

gulp.task('lint', function () {
return gulp.src([
'src/scripts/**/*.js'
]).pipe(reload({stream: true, once: true}))
.pipe($.eslint.format())
.pipe($.if(!browserSync.active, $.eslint.failAfterError()))
.pipe(gulp.dest('src/scripts'));
});

gulp.task('prepare-fonts', function () {
return gulp.src([
'node_modules/font-awesome/fonts/fontawesome-webfont.*'
]).pipe(gulp.dest('.tmp/fonts'));
});

gulp.task('process-fonts', ['prepare-fonts'], function () {
return gulp.src([
'.tmp/fonts/**/*'
]).pipe(gulp.dest('dist/fonts'));
});

gulp.task('prepare-langs', function () {
return gulp.src([
'src/langs/**/*'
]).pipe(gulp.dest('.tmp/langs'));
});

gulp.task('process-langs', ['prepare-langs'], function () {
return gulp.src([
'.tmp/langs/**/*'
]).pipe(gulp.dest('dist/langs'));
});

gulp.task('prepare-styles', function () {
return gulp.src([
'src/styles/**/*.css'
]).pipe($.autoprefixer())
.pipe(gulp.dest('.tmp/styles'))
.pipe(reload({stream: true}));
});

gulp.task('prepare-scripts', function () {
return gulp.src([
'src/scripts/**/*.js'
]).pipe($.plumber())
.pipe($.injectVersion({replace: '${ARIANG_VERSION}'}))
.pipe($.replace(/\${ARIANG_BUILD_COMMIT}/g, tryFn(git.short) || 'Local'))
.pipe(gulp.dest('.tmp/scripts'))
.pipe(reload({stream: true}));
});

gulp.task('prepare-views', function () {
return gulp.src([
'src/views/**/*.html'
]).pipe($.htmlmin({collapseWhitespace: true}))
.pipe($.angularTemplatecache({module: 'ariaNg', filename: 'views/templates.js', root: 'views/'}))
.pipe(gulp.dest('.tmp/scripts'));
});

gulp.task('prepare-html', ['prepare-styles', 'prepare-scripts', 'prepare-views'], function () {
return gulp.src([
'src/*.html'
]).pipe($.useref({searchPath: ['.tmp', 'src', '.']}))
.pipe($.if('js/*.js', $.replace(/\/\/# sourceMappingURL=.*/g, '')))
.pipe($.if('css/*.css', $.replace(/\/\*# sourceMappingURL=.* \*\/$/g, '')))
.pipe($.if(['js/moment-with-locales-*.min.js', 'js/plugins.min.js', 'js/aria-ng.min.js'], $.uglify({output: {comments: saveLicense}})))
.pipe($.if(['css/plugins.min.css', 'css/aria-ng.min.css'], $.cssnano({safe: true, autoprefixer: false})))
.pipe($.replace(/url\((\.\.\/fonts\/[a-zA-Z0-9\-]+\.woff2)(\?[a-zA-Z0-9\-_=.]+)?\)/g, function(match, fileName) {
return 'url(' + fileName + ')'; // remove version of woff2 file (woff2 file should be cached via application cache)
}))
.pipe($.if(['js/plugins.min.js', 'js/aria-ng.min.js', 'css/plugins.min.css', 'css/aria-ng.min.css'], $.rev()))
.pipe($.if('*.html', $.htmlmin({collapseWhitespace: true})))
.pipe($.revReplace())
.pipe(gulp.dest('.tmp'));
});

gulp.task('process-html', ['prepare-html'], function () {
return gulp.src([
'.tmp/*.html'
]).pipe($.replace(/<!-- AriaNg-Bundle:\w+ -->/, ''))
.pipe(gulp.dest('dist'));
});

gulp.task('process-assets', ['process-html'], function () {
return gulp.src([
'.tmp/css/**/*',
'.tmp/js/**/*'
],{ base: '.tmp' })
.pipe(gulp.dest('dist'));
});

gulp.task('prepare-assets-bundle', function () {
return gulp.src([
'src/favicon.png'
]).pipe(gulp.dest('.tmp'));
});
const gulp = require('gulp');
const gulpLoadPlugins = require('gulp-load-plugins');
const browserSync = require('browser-sync');
const del = require('del');
const fs = require('fs');
const git = require('git-rev-sync');
const tryFn = require('nice-try');
const saveLicense = require('uglify-save-license');

const $ = gulpLoadPlugins();
const reload = browserSync.reload;

gulp.task('clean', () => del(['.tmp', 'dist']));

gulp.task('lint', () => gulp.src([
'src/scripts/**/*.js'
]).pipe(reload({stream: true, once: true}))
.pipe($.eslint.format())
.pipe($.if(!browserSync.active, $.eslint.failAfterError()))
.pipe(gulp.dest('src/scripts')));

gulp.task('prepare-fonts', () => gulp.src([
'node_modules/font-awesome/fonts/fontawesome-webfont.*'
]).pipe(gulp.dest('.tmp/fonts')));

gulp.task('process-fonts', gulp.series('prepare-fonts', () => gulp.src([
'.tmp/fonts/**/*'
]).pipe(gulp.dest('dist/fonts'))));

gulp.task('prepare-langs', () => gulp.src([
'src/langs/**/*'
]).pipe(gulp.dest('.tmp/langs')));

gulp.task('process-langs', gulp.series('prepare-langs', () => gulp.src([
'.tmp/langs/**/*'
]).pipe(gulp.dest('dist/langs'))));

gulp.task('prepare-styles', () => gulp.src([
'src/styles/**/*.css'
]).pipe($.autoprefixer())
.pipe(gulp.dest('.tmp/styles'))
.pipe(reload({stream: true})));

gulp.task('prepare-scripts', () => gulp.src([
'src/scripts/**/*.js'
]).pipe($.plumber())
.pipe($.injectVersion({replace: '${ARIANG_VERSION}'}))
.pipe($.replace(/\${ARIANG_BUILD_COMMIT}/g, tryFn(git.short) || 'Local'))
.pipe(gulp.dest('.tmp/scripts'))
.pipe(reload({stream: true})));

gulp.task('prepare-views', () => gulp.src([
'src/views/**/*.html'
]).pipe($.htmlmin({collapseWhitespace: true}))
.pipe($.angularTemplatecache({module: 'ariaNg', filename: 'views/templates.js', root: 'views/'}))
.pipe(gulp.dest('.tmp/scripts')));

gulp.task('prepare-html', gulp.series('prepare-styles', 'prepare-scripts', 'prepare-views', () => gulp.src([
'src/*.html'
]).pipe($.useref({searchPath: ['.tmp', 'src', '.']}))
.pipe($.if('js/*.js', $.replace(/\/\/# sourceMappingURL=.*/g, '')))
.pipe($.if('css/*.css', $.replace(/\/\*# sourceMappingURL=.* \*\/$/g, '')))
.pipe($.if(['js/moment-with-locales-*.min.js', 'js/plugins.min.js', 'js/aria-ng.min.js'], $.uglify({output: {comments: saveLicense}})))
.pipe($.if(['css/plugins.min.css', 'css/aria-ng.min.css'], $.cssnano({safe: true, autoprefixer: false})))
.pipe($.replace(/url\((\.\.\/fonts\/[a-zA-Z0-9\-]+\.woff2)(\?[a-zA-Z0-9\-_=.]+)?\)/g, (match, fileName) => {
return 'url(' + fileName + ')'; // remove version of woff2 file (woff2 file should be cached via application cache)
}))
.pipe($.if(['js/plugins.min.js', 'js/aria-ng.min.js', 'css/plugins.min.css', 'css/aria-ng.min.css'], $.rev()))
.pipe($.if('*.html', $.htmlmin({collapseWhitespace: true})))
.pipe($.revReplace())
.pipe(gulp.dest('.tmp'))));

gulp.task('process-html', gulp.series('prepare-html', () => gulp.src([
'.tmp/*.html'
]).pipe($.replace(/<!-- AriaNg-Bundle:\w+ -->/, ''))
.pipe(gulp.dest('dist'))));

gulp.task('process-assets', gulp.series('process-html', () => gulp.src([
'.tmp/css/**/*',
'.tmp/js/**/*'
], {base: '.tmp'})
.pipe(gulp.dest('dist'))));

gulp.task('prepare-assets-bundle', () => gulp.src([
'src/favicon.png'
]).pipe(gulp.dest('.tmp')));

gulp.task('process-assets-bundle', gulp.series('prepare-fonts', 'prepare-langs', 'prepare-html', 'prepare-assets-bundle', () => gulp.src('.tmp/index.html')
.pipe($.replace(/<link rel="stylesheet" href="(css\/[a-zA-Z0-9\-_.]+\.css)">/g, (match, fileName) => {
const content = fs.readFileSync('.tmp/' + fileName, 'utf8');
return '<style type="text/css">' + content + '</style>';
}))
.pipe($.replace(/<script src="(js\/[a-zA-Z0-9\-_.]+\.js)"><\/script>/g, (match, fileName) => {
const content = fs.readFileSync('.tmp/' + fileName, 'utf8');
return '<script type="application/javascript">' + content + '</script>';
}))
.pipe($.replace(/url\(\.\.\/(fonts\/[a-zA-Z0-9\-]+\.woff)(\?[a-zA-Z0-9\-_=.]+)?\)/g, (match, fileName) => {
if (!fs.existsSync('.tmp/' + fileName)) {
return match;
}

gulp.task('process-assets-bundle', ['prepare-fonts', 'prepare-langs', 'prepare-html', 'prepare-assets-bundle'], function () {
return gulp.src('.tmp/index.html')
.pipe($.replace(/<link rel="stylesheet" href="(css\/[a-zA-Z0-9\-_.]+\.css)">/g, function(match, fileName) {
var content = fs.readFileSync('.tmp/' + fileName, 'utf8');
return '<style type="text/css">' + content + '</style>';
}))
.pipe($.replace(/<script src="(js\/[a-zA-Z0-9\-_.]+\.js)"><\/script>/g, function(match, fileName) {
var content = fs.readFileSync('.tmp/' + fileName, 'utf8');
return '<script type="application/javascript">' + content + '</script>';
}))
.pipe($.replace(/url\(\.\.\/(fonts\/[a-zA-Z0-9\-]+\.woff)(\?[a-zA-Z0-9\-_=.]+)?\)/g, function(match, fileName) {
if (!fs.existsSync('.tmp/' + fileName)) {
return match;
}
const contentBuffer = fs.readFileSync('.tmp/' + fileName);
const contentBase64 = contentBuffer.toString('base64');
return 'url(data:application/x-font-woff;base64,' + contentBase64 + ')';
}))
.pipe($.replace(/<link rel="icon" href="([a-zA-Z0-9\-_.]+\.png)">/g, (match, fileName) => {
if (!fs.existsSync('.tmp/' + fileName)) {
return match;
}

var contentBuffer = fs.readFileSync('.tmp/' + fileName);
var contentBase64 = contentBuffer.toString('base64');
return 'url(data:application/x-font-woff;base64,' + contentBase64 + ')';
}))
.pipe($.replace(/<link rel="icon" href="([a-zA-Z0-9\-_.]+\.png)">/g, function(match, fileName) {
if (!fs.existsSync('.tmp/' + fileName)) {
return match;
const contentBuffer = fs.readFileSync('.tmp/' + fileName);
const contentBase64 = contentBuffer.toString('base64');
return '<link rel="icon" href="data:image/png;base64,' + contentBase64 + '">';
}))
.pipe($.replace('<!-- AriaNg-Bundle:languages -->', () => {
const langDir = '.tmp/langs/';
let result = '';
const fileNames = fs.readdirSync(langDir, 'utf8');

if (fileNames.length > 0) {
result = '<script type="application/javascript">' +
'angular.module("ariaNg").config(["ariaNgAssetsCacheServiceProvider", () => {';

for (const fileName of fileNames) {
let content = fs.readFileSync(langDir + fileName, 'utf8');

const lastPointIndex = fileName.lastIndexOf('.');
const languageName = fileName.substr(0, lastPointIndex);

content = content.replace(/\\/g, '\\\\');
content = content.replace(/\r/g, '');
content = content.replace(/\n/g, '\\n');
content = content.replace(/"/g, '\\"');
result += 'e.setLanguageAsset(\'' + languageName + '\',"' + content + '");';
}

var contentBuffer = fs.readFileSync('.tmp/' + fileName);
var contentBase64 = contentBuffer.toString('base64');
return '<link rel="icon" href="data:image/png;base64,' + contentBase64 + '">';
}))
.pipe($.replace('<!-- AriaNg-Bundle:languages -->', function() {
var langDir = '.tmp/langs/';
var result = '';
var fileNames = fs.readdirSync(langDir, 'utf8');

if (fileNames.length > 0) {
result = '<script type="application/javascript">' +
'angular.module("ariaNg").config(["ariaNgAssetsCacheServiceProvider",function(e){';

for (var i = 0; i < fileNames.length; i++) {
var fileName = fileNames[i];
var content = fs.readFileSync(langDir + fileName, 'utf8');

var lastPointIndex = fileName.lastIndexOf('.');
var languageName = fileName.substr(0, lastPointIndex);

content = content.replace(/\\/g, '\\\\');
content = content.replace(/\r/g, '');
content = content.replace(/\n/g, '\\n');
content = content.replace(/"/g, '\\"');
result += 'e.setLanguageAsset(\'' + languageName + '\',"' + content + '");';
}

result += '}]);</script>';
}
result += '}]);</script>';
}

return result;
}))
.pipe($.replace(/<[a-z]+( [a-z\-]+="[a-zA-Z0-9\- ]+")* [a-z\-]+="((favicon.ico)|(tileicon.png)|(touchicon.png))"\/?>/g, ''))
.pipe(gulp.dest('dist'));
});
return result;
}))
.pipe($.replace(/<[a-z]+( [a-z\-]+="[a-zA-Z0-9\- ]+")* [a-z\-]+="((favicon.ico)|(tileicon.png)|(touchicon.png))"\/?>/g, ''))
.pipe(gulp.dest('dist'))));

gulp.task('process-full-extras', function () {
return gulp.src([
'LICENSE',
'src/*.*',
'!src/*.html'
], {
dot: true
}).pipe(gulp.dest('dist'));
});
gulp.task('process-full-extras', () => gulp.src([
'LICENSE',
'src/*.*',
'!src/*.html'
], {
dot: true
}).pipe(gulp.dest('dist')));

gulp.task('process-tiny-extras', function () {
return gulp.src([
'LICENSE'
]).pipe(gulp.dest('dist'));
});
gulp.task('process-tiny-extras', () => gulp.src([
'LICENSE'
]).pipe(gulp.dest('dist')));

gulp.task('info', function () {
return gulp.src([
'dist/**/*'
]).pipe($.size({title: 'build', gzip: true}));
});
gulp.task('info', () => gulp.src([
'dist/**/*'
]).pipe($.size({title: 'build', gzip: true})));

gulp.task('build', $.sequence('lint', 'process-fonts', 'process-langs', 'process-assets', 'process-full-extras', 'info'));
gulp.task('build', gulp.series('lint', 'process-fonts', 'process-langs', 'process-assets', 'process-full-extras', 'info'));

gulp.task('build-bundle', $.sequence('lint', 'process-assets-bundle', 'process-tiny-extras', 'info'));
gulp.task('build-bundle', gulp.series('lint', 'process-assets-bundle', 'process-tiny-extras', 'info'));

gulp.task('serve', ['prepare-styles', 'prepare-scripts', 'prepare-fonts'], function () {
gulp.task('serve', gulp.series('prepare-styles', 'prepare-scripts', 'prepare-fonts', () => {
browserSync({
notify: false,
port: 9000,
Expand All @@ -217,12 +184,12 @@ gulp.task('serve', ['prepare-styles', 'prepare-scripts', 'prepare-fonts'], funct
'.tmp/fonts/**/*'
]).on('change', reload);

gulp.watch('src/styles/**/*.css', ['prepare-styles']);
gulp.watch('src/scripts/**/*.js', ['prepare-scripts']);
gulp.watch('src/fonts/**/*', ['prepare-fonts']);
});
gulp.watch('src/styles/**/*.css', gulp.series('prepare-styles'));
gulp.watch('src/scripts/**/*.js', gulp.series('prepare-scripts'));
gulp.watch('src/fonts/**/*', gulp.series('prepare-fonts'));
}));

gulp.task('serve:dist', function () {
gulp.task('serve:dist', () => {
browserSync({
notify: false,
port: 9000,
Expand All @@ -232,6 +199,4 @@ gulp.task('serve:dist', function () {
});
});

gulp.task('default', ['clean'], function () {
gulp.start('build');
});
gulp.task('default', gulp.series('clean', 'build'));
Loading