Pony.fm/gulpfile.js

186 lines
6.4 KiB
JavaScript
Raw Normal View History

/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015 Peter Deltchev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2015-09-06 19:21:11 +02:00
var gulp = require("gulp"),
gutil = require("gulp-util"),
2015-09-06 19:21:11 +02:00
plug = require("gulp-load-plugins")(),
argv = require("yargs").argv,
header = require("gulp-header"),
webpack = require("webpack"),
WebpackDevServer = require("webpack-dev-server"),
webpackDevConfig = require("./webpack.dev.config.js"),
webpackProductionConfig = require("./webpack.production.config.js"),
webpackStream = require('webpack-stream'),
_ = require("underscore");
2015-09-06 19:21:11 +02:00
var plumberOptions = {
2015-10-25 03:43:37 +01:00
errorHandler: plug.notify.onError("Error: <%= error.message %>")
2015-09-06 19:21:11 +02:00
};
var licenseHeader = [
"/**",
"* Pony.fm - A community for pony fan music.",
2016-01-16 10:12:57 +01:00
"* Copyright (C) 2016 Peter Deltchev and others",
"*",
"* This program is free software: you can redistribute it and/or modify",
"* it under the terms of the GNU Affero General Public License as published by",
"* the Free Software Foundation, either version 3 of the License, or",
"* (at your option) any later version.",
"*",
"* This program is distributed in the hope that it will be useful,",
"* but WITHOUT ANY WARRANTY; without even the implied warranty of",
"* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
"* GNU Affero General Public License for more details.",
"*",
"* You should have received a copy of the GNU Affero General Public License",
"* along with this program. If not, see <http://www.gnu.org/licenses/>.",
"*/",
"",
""
2016-01-16 10:12:57 +01:00
].join('\n');
gulp.task("webpack-build", function() {
return gulp.src(_.values(webpackProductionConfig.entry))
.pipe(webpackStream(webpackProductionConfig))
.pipe(header(licenseHeader))
.pipe(gulp.dest('public'));
});
gulp.task("webpack-dev-server", function () {
// Starts a webpack-dev-server
var compiler = webpack(webpackDevConfig);
new WebpackDevServer(compiler, {
// server and middleware options, currently blank
}).listen(61999, "localhost", function (err) {
if (err)
throw new gutil.PluginError("webpack-dev-server", err);
// Server listening
gutil.log("[webpack-dev-server]", "http://localhost:61999/webpack-dev-server/index.html");
});
});
2015-10-25 03:43:37 +01:00
gulp.task("styles-app", function () {
var includedStyles = [
2016-08-05 05:31:58 +02:00
"node_modules/angular-material/angular-material.css",
2016-08-08 18:11:20 +02:00
"node_modules/ng-sortable/dist/ng-sortable.css",
2016-08-05 05:31:58 +02:00
"resources/assets/styles/app.less",
2015-10-25 03:43:37 +01:00
];
if (!argv.production) {
// we also want to add the embed stuff, since we're in development mode
// we want to watch embed files and re-compile them. However, we want
// to leave this path out in production so that embed files are not bloating
// the css file
2016-06-17 19:01:31 +02:00
includedStyles.push("resources/assets/styles/embed.less");
2015-10-25 03:43:37 +01:00
// Remove app.less from the cache so that it gets recompiled
var styleCache = plug.cached.caches.styles;
for (var file in styleCache) {
if (!styleCache.hasOwnProperty(file))
continue;
2016-08-08 18:11:20 +02:00
/*if (!endsWith(file, "app.less") && !endsWith(file, "angular-material.css"))
continue;*/
2015-10-25 03:43:37 +01:00
delete styleCache[file];
}
}
// note that we're not doing autoprefixer on dev builds for now to shave off roughly 600-700 milliseconds per
// build. It's already taking forever to recompile the less
2016-08-05 05:31:58 +02:00
if (argv.production) {
2015-10-25 03:43:37 +01:00
// Production pipeline
2016-08-05 05:31:58 +02:00
return gulp.src(includedStyles)
.pipe(plug.plumber(plumberOptions))
.pipe(plug.if(/\.less/, plug.less()))
.pipe(plug.autoprefixer({
browsers: ["last 2 versions"],
cascade: false
}))
.pipe(plug.concat("app.css"))
.pipe(plug.cleanCss())
.pipe(header(licenseHeader))
.pipe(gulp.dest("public/build/styles"));
} else {
2015-10-25 03:43:37 +01:00
// Development pipeline
2016-08-05 05:31:58 +02:00
return gulp.src(includedStyles)
.pipe(plug.plumber(plumberOptions))
.pipe(plug.cached("styles"))
.pipe(plug.sourcemaps.init())
.pipe(plug.if(/\.less/, plug.less()))
.pipe(plug.concat("app.css"))
.pipe(header(licenseHeader))
.pipe(plug.sourcemaps.write())
.pipe(gulp.dest("public/build/styles"))
.pipe(plug.livereload());
}
2015-09-06 19:21:11 +02:00
});
2015-10-25 03:43:37 +01:00
gulp.task("styles-embed", function () {
// note that this task should really only ever be invoked for production
// since development-mode watches and builds include the embed styles
// already
return gulp.src(["resources/assets/styles/embed.less"], {base: "resources/assets/styles"})
.pipe(plug.less())
.pipe(plug.autoprefixer({
browsers: ["last 2 versions"],
cascade: false
}))
2016-06-17 22:00:41 +02:00
.pipe(plug.concat("embed.css"))
.pipe(plug.cleanCss())
.pipe(header(licenseHeader))
2015-10-25 03:43:37 +01:00
.pipe(gulp.dest("public/build/styles"));
2015-09-06 19:21:11 +02:00
});
2015-10-25 03:43:37 +01:00
gulp.task('copy:templates', function () {
gulp.src([
'public/templates/**/*.html'
])
.pipe(plug.angularTemplatecache({
module: "ponyfm",
root: "/templates"
}))
.pipe(header(licenseHeader))
2015-10-25 03:43:37 +01:00
.pipe(gulp.dest('public/build/scripts'));
2015-09-06 19:21:11 +02:00
});
gulp.task('build', [
'webpack-build',
'copy:templates',
2015-10-25 03:43:37 +01:00
'styles-app',
'styles-embed'
2015-09-06 19:21:11 +02:00
]);
gulp.task("watch-legacy", ["build"], function () {
2015-10-25 03:43:37 +01:00
gulp.watch("resources/assets/styles/**/*.{css,less}", ["styles-app"]);
2015-09-06 19:21:11 +02:00
});
gulp.task("watch", ["webpack-dev-server", "watch-legacy"], function () {});
2015-09-06 19:21:11 +02:00
function endsWith(str, suffix) {
2015-10-25 03:43:37 +01:00
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}