2016-01-29 01:27:15 +03:00
|
|
|
/*
|
2016-02-04 17:03:20 +03:00
|
|
|
* Copyright (c) 2016 Martin Donath <martin.donath@squidfunk.com>
|
2016-01-29 01:27:15 +03:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Imports
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
var gulp = require('gulp');
|
|
|
|
var addsrc = require('gulp-add-src');
|
|
|
|
var args = require('yargs').argv;
|
2016-02-11 14:09:58 +03:00
|
|
|
var autoprefix = require('autoprefixer');
|
2016-01-29 01:27:15 +03:00
|
|
|
var child = require('child_process');
|
|
|
|
var clean = require('del');
|
|
|
|
var collect = require('gulp-rev-collector');
|
|
|
|
var compact = require('gulp-remove-empty-lines');
|
|
|
|
var concat = require('gulp-concat');
|
|
|
|
var ignore = require('gulp-ignore');
|
|
|
|
var gulpif = require('gulp-if');
|
2016-02-11 14:09:58 +03:00
|
|
|
var mincss = require('gulp-cssnano');
|
2016-01-29 01:27:15 +03:00
|
|
|
var minhtml = require('gulp-htmlmin');
|
|
|
|
var minimage = require('gulp-image-optimization');
|
|
|
|
var modernizr = require('gulp-modernizr');
|
|
|
|
var mqpacker = require('css-mqpacker');
|
|
|
|
var notifier = require('node-notifier');
|
2016-08-07 19:01:56 +03:00
|
|
|
var path = require('path');
|
2016-01-29 01:27:15 +03:00
|
|
|
var plumber = require('gulp-plumber');
|
|
|
|
var postcss = require('gulp-postcss');
|
|
|
|
var rev = require('gulp-rev');
|
|
|
|
var sass = require('gulp-sass');
|
|
|
|
var sourcemaps = require('gulp-sourcemaps');
|
2016-08-07 19:01:56 +03:00
|
|
|
var stream = require('webpack-stream');
|
2016-01-29 01:27:15 +03:00
|
|
|
var uglify = require('gulp-uglify');
|
|
|
|
var util = require('gulp-util');
|
|
|
|
var vinyl = require('vinyl-paths');
|
2016-08-07 19:01:56 +03:00
|
|
|
var webpack = require('webpack');
|
2016-01-29 01:27:15 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Locals
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
2016-02-11 14:09:58 +03:00
|
|
|
/* MkDocs server */
|
2016-01-29 01:27:15 +03:00
|
|
|
var server = null;
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Overrides
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Override gulp.src() for nicer error handling.
|
|
|
|
*/
|
|
|
|
var src = gulp.src;
|
|
|
|
gulp.src = function() {
|
|
|
|
return src.apply(gulp, arguments)
|
|
|
|
.pipe(
|
|
|
|
plumber(function(error) {
|
|
|
|
util.log(util.colors.red(
|
|
|
|
'Error (' + error.plugin + '): ' + error.message
|
|
|
|
));
|
2016-08-07 19:01:56 +03:00
|
|
|
var file = error.relativePath.split('/').pop();
|
2016-01-29 01:27:15 +03:00
|
|
|
notifier.notify({
|
2016-08-07 19:01:56 +03:00
|
|
|
title: 'Error (' + error.plugin + '): ' + file,
|
|
|
|
message: error.messageOriginal
|
2016-01-29 01:27:15 +03:00
|
|
|
});
|
|
|
|
this.emit('end');
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Asset pipeline
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build stylesheets from SASS source.
|
|
|
|
*/
|
|
|
|
gulp.task('assets:stylesheets', function() {
|
|
|
|
return gulp.src('src/assets/stylesheets/*.scss')
|
|
|
|
.pipe(gulpif(args.sourcemaps, sourcemaps.init()))
|
|
|
|
.pipe(
|
|
|
|
sass({
|
|
|
|
includePaths: [
|
2016-09-02 01:33:45 +03:00
|
|
|
'node_modules/modularscale-sass/stylesheets',
|
|
|
|
'node_modules/material-design-color',
|
|
|
|
'node_modules/material-shadows'
|
2016-01-29 01:27:15 +03:00
|
|
|
]
|
|
|
|
}))
|
|
|
|
.pipe(
|
|
|
|
postcss([
|
|
|
|
autoprefix(),
|
|
|
|
mqpacker
|
|
|
|
]))
|
|
|
|
.pipe(gulpif(args.sourcemaps, sourcemaps.write()))
|
|
|
|
.pipe(gulpif(args.production, mincss()))
|
2016-08-07 19:01:56 +03:00
|
|
|
.pipe(gulp.dest('material/assets/stylesheets'));
|
2016-01-29 01:27:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
2016-08-07 19:01:56 +03:00
|
|
|
* Build javascripts by transpiling ES6 with babel.
|
2016-01-29 01:27:15 +03:00
|
|
|
*/
|
|
|
|
gulp.task('assets:javascripts', function() {
|
2016-08-07 19:01:56 +03:00
|
|
|
return gulp.src('src/assets/javascripts/**/*.js')
|
|
|
|
.pipe(
|
|
|
|
stream({
|
|
|
|
entry: 'application.js',
|
|
|
|
output: {
|
|
|
|
filename: 'application.js'
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
loaders: [{
|
|
|
|
loader: 'babel-loader',
|
|
|
|
test: path.join(__dirname, 'src/assets/javascripts'),
|
|
|
|
query: {
|
|
|
|
presets: 'es2015'
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
new webpack.NoErrorsPlugin(),
|
|
|
|
new webpack.ResolverPlugin(
|
|
|
|
new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin(
|
|
|
|
'.bower.json', ['main']
|
|
|
|
)
|
|
|
|
)
|
|
|
|
].concat(
|
|
|
|
args.production ? [
|
|
|
|
new webpack.optimize.UglifyJsPlugin({
|
|
|
|
compress: {
|
|
|
|
warnings: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
] : []),
|
|
|
|
stats: {
|
|
|
|
colors: true
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
modulesDirectories: [
|
|
|
|
'src/assets/javascripts',
|
2016-09-02 01:33:45 +03:00
|
|
|
'node_modules'
|
2016-08-07 19:01:56 +03:00
|
|
|
],
|
|
|
|
extensions: [
|
|
|
|
'', '.js'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
devtool: args.sourcemaps ? 'source-map' : ''
|
|
|
|
}))
|
|
|
|
.pipe(gulp.dest('material/assets/javascripts'));
|
2016-01-29 01:27:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a customized modernizr build.
|
|
|
|
*/
|
|
|
|
gulp.task('assets:modernizr', [
|
|
|
|
'assets:stylesheets',
|
|
|
|
'assets:javascripts'
|
|
|
|
], function() {
|
|
|
|
return gulp.src([
|
2016-08-07 19:01:56 +03:00
|
|
|
'material/assets/stylesheets/*.css',
|
|
|
|
'material/assets/javascripts/*.js'
|
2016-01-29 01:27:15 +03:00
|
|
|
]).pipe(
|
|
|
|
modernizr({
|
|
|
|
options: [
|
|
|
|
'addTest', /* Add custom tests */
|
|
|
|
'fnBind', /* Use function.bind */
|
|
|
|
'html5printshiv', /* HTML5 support for IE */
|
|
|
|
'setClasses', /* Add CSS classes to root tag */
|
|
|
|
'testProp' /* Test for properties */
|
|
|
|
]
|
|
|
|
}))
|
|
|
|
.pipe(concat('modernizr.js'))
|
|
|
|
.pipe(gulpif(args.production, uglify()))
|
2016-02-09 14:58:55 +03:00
|
|
|
.pipe(gulp.dest('material/assets/javascripts'));
|
2016-01-29 01:27:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy static assets like images and webfonts.
|
|
|
|
*/
|
|
|
|
gulp.task('assets:static', function() {
|
2016-02-09 14:58:55 +03:00
|
|
|
return gulp.src('src/assets/{fonts,images}/*.{jpg,png,gif}')
|
2016-01-29 01:27:15 +03:00
|
|
|
.pipe(gulpif(args.production,
|
|
|
|
minimage({
|
|
|
|
optimizationLevel: 5,
|
|
|
|
progressive: true,
|
|
|
|
interlaced: true
|
|
|
|
})))
|
2016-02-09 14:58:55 +03:00
|
|
|
.pipe(addsrc.append('src/assets/{fonts,images}/*.{ico,eot,svg,ttf,woff}'))
|
2016-08-07 19:01:56 +03:00
|
|
|
.pipe(gulp.dest('material/assets'));
|
2016-01-29 01:27:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Minify views.
|
|
|
|
*/
|
|
|
|
gulp.task('assets:views', args.production ? [
|
2016-02-04 17:03:20 +03:00
|
|
|
'assets:modernizr',
|
2016-01-29 01:27:15 +03:00
|
|
|
'assets:revisions:clean',
|
|
|
|
'assets:revisions'
|
|
|
|
] : [], function() {
|
|
|
|
return gulp.src([
|
2016-02-04 17:03:20 +03:00
|
|
|
'src/*.html'
|
2016-01-29 01:27:15 +03:00
|
|
|
]).pipe(
|
|
|
|
minhtml({
|
|
|
|
collapseBooleanAttributes: true,
|
|
|
|
removeComments: true,
|
|
|
|
removeScriptTypeAttributes: true,
|
|
|
|
removeStyleLinkTypeAttributes: true
|
|
|
|
}))
|
|
|
|
.pipe(compact())
|
|
|
|
.pipe(gulpif(args.production,
|
|
|
|
addsrc.append([
|
2016-02-09 14:58:55 +03:00
|
|
|
'material/manifest.json',
|
|
|
|
'material/**/*.css'
|
2016-01-29 01:27:15 +03:00
|
|
|
])))
|
|
|
|
.pipe(gulpif(args.production, collect()))
|
|
|
|
.pipe(ignore.exclude(/manifest\.json$/))
|
2016-02-09 14:58:55 +03:00
|
|
|
.pipe(gulp.dest('material'));
|
2016-01-29 01:27:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean outdated revisions.
|
|
|
|
*/
|
|
|
|
gulp.task('assets:revisions:clean', function() {
|
2016-02-09 14:58:55 +03:00
|
|
|
return gulp.src(['material/**/*.{ico,css,js,png,jpg,gif}'])
|
2016-02-11 14:09:58 +03:00
|
|
|
.pipe(ignore.include(/-[a-f0-9]{8,}\.(ico|css|js|png|jpg|gif)$/))
|
2016-01-29 01:27:15 +03:00
|
|
|
.pipe(vinyl(clean));
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Revision assets after build.
|
|
|
|
*/
|
|
|
|
gulp.task('assets:revisions', [
|
|
|
|
'assets:revisions:clean',
|
|
|
|
'assets:stylesheets',
|
|
|
|
'assets:javascripts',
|
|
|
|
'assets:static'
|
|
|
|
], function() {
|
2016-02-09 14:58:55 +03:00
|
|
|
return gulp.src(['material/**/*.{ico,css,js,png,jpg,gif}'])
|
2016-02-11 14:09:58 +03:00
|
|
|
.pipe(ignore.exclude(/-[a-f0-9]{8,}\.(css|js|png|jpg|gif)$/))
|
2016-01-29 01:27:15 +03:00
|
|
|
.pipe(rev())
|
2016-02-09 14:58:55 +03:00
|
|
|
.pipe(gulp.dest('material'))
|
2016-01-29 01:27:15 +03:00
|
|
|
.pipe(rev.manifest('manifest.json'))
|
2016-02-09 14:58:55 +03:00
|
|
|
.pipe(gulp.dest('material'));
|
2016-01-29 01:27:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build assets.
|
|
|
|
*/
|
|
|
|
gulp.task('assets:build', [
|
|
|
|
'assets:stylesheets',
|
|
|
|
'assets:javascripts',
|
|
|
|
'assets:modernizr',
|
|
|
|
'assets:static',
|
|
|
|
'assets:views'
|
|
|
|
]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Watch assets for changes and rebuild on the fly.
|
|
|
|
*/
|
|
|
|
gulp.task('assets:watch', function() {
|
|
|
|
|
|
|
|
/* Rebuild stylesheets */
|
|
|
|
gulp.watch([
|
|
|
|
'src/assets/stylesheets/**/*.scss'
|
|
|
|
], ['assets:stylesheets']);
|
|
|
|
|
|
|
|
/* Rebuild javascripts */
|
|
|
|
gulp.watch([
|
|
|
|
'src/assets/javascripts/**/*.js',
|
|
|
|
'bower.json'
|
|
|
|
], ['assets:javascripts']);
|
|
|
|
|
|
|
|
/* Copy static assets */
|
|
|
|
gulp.watch([
|
2016-02-27 14:19:18 +03:00
|
|
|
'src/assets/{fonts,images}/*'
|
2016-01-29 01:27:15 +03:00
|
|
|
], ['assets:static']);
|
|
|
|
|
|
|
|
/* Minify views */
|
|
|
|
gulp.watch([
|
2016-02-04 17:03:20 +03:00
|
|
|
'src/*.html'
|
2016-01-29 01:27:15 +03:00
|
|
|
], ['assets:views']);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Application server
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
2016-02-11 14:09:58 +03:00
|
|
|
* Build documentation.
|
2016-01-29 01:27:15 +03:00
|
|
|
*/
|
2016-02-11 14:09:58 +03:00
|
|
|
gulp.task('mkdocs:build', [
|
2016-01-29 01:27:15 +03:00
|
|
|
'assets:build'
|
|
|
|
], function() {
|
|
|
|
return child.spawnSync('mkdocs', ['build']);
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
2016-02-11 14:09:58 +03:00
|
|
|
* Restart MkDocs server.
|
2016-01-29 01:27:15 +03:00
|
|
|
*/
|
2016-02-11 14:09:58 +03:00
|
|
|
gulp.task('mkdocs:serve', function() {
|
2016-01-29 01:27:15 +03:00
|
|
|
if (server)
|
|
|
|
server.kill();
|
|
|
|
|
2016-02-11 14:09:58 +03:00
|
|
|
/* Spawn MkDocs server */
|
2016-01-29 01:27:15 +03:00
|
|
|
server = child.spawn('mkdocs', ['serve', '-a', '0.0.0.0:8000']);
|
|
|
|
|
|
|
|
/* Pretty print server log output */
|
|
|
|
server.stdout.on('data', function(data) {
|
|
|
|
var lines = data.toString().split('\n')
|
|
|
|
for (var l in lines)
|
|
|
|
if (lines[l].length)
|
|
|
|
util.log(lines[l]);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Print errors to stdout */
|
|
|
|
server.stderr.on('data', function(data) {
|
|
|
|
process.stdout.write(data.toString());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Interface
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
2016-02-11 14:09:58 +03:00
|
|
|
* Build assets and documentation.
|
2016-01-29 01:27:15 +03:00
|
|
|
*/
|
|
|
|
gulp.task('build', [
|
2016-02-11 14:09:58 +03:00
|
|
|
'assets:build'
|
|
|
|
].concat(args.mkdocs
|
|
|
|
? 'mkdocs:build'
|
|
|
|
: []));
|
2016-01-29 01:27:15 +03:00
|
|
|
|
|
|
|
/*
|
2016-02-11 14:09:58 +03:00
|
|
|
* Start asset and MkDocs watchdogs.
|
2016-01-29 01:27:15 +03:00
|
|
|
*/
|
|
|
|
gulp.task('watch', [
|
|
|
|
'assets:build',
|
|
|
|
], function() {
|
|
|
|
return gulp.start([
|
2016-02-11 14:09:58 +03:00
|
|
|
'assets:watch'
|
|
|
|
].concat(args.mkdocs
|
|
|
|
? 'mkdocs:serve'
|
|
|
|
: []));
|
2016-01-29 01:27:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build assets by default.
|
|
|
|
*/
|
|
|
|
gulp.task('default', ['build']);
|