Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / build-scripts / style-builder.js
blobc3a21f63557e7a19c5da64b12e13d9ea2c1981d7
1 'use strict';
3 var gulp = require('gulp');
4 var sourcemaps = require('gulp-sourcemaps');
5 var less = require('gulp-less');
6 var Promise = require('bluebird');
7 var _ = require('lodash');
9 var lessWatcher = require('./less-watcher');
11 var defaults = {
12 dest: './',
13 sourceMapOptions: {},
14 // ... postcss
15 streamTransform: function(stream) {
16 return stream;
18 // Also see `LessWatcher` opts, `watchGlob`, `lessOptions`
21 module.exports = function(entryPoints, options) {
22 var opts = _.extend({}, defaults, options);
24 var myLessWatcher = lessWatcher(entryPoints, opts);
26 var buildStyles = function(entryPoints) {
27 return new Promise(function(resolve, reject) {
28 // We use gulp here for easy building
29 var stream = gulp
30 .src(entryPoints)
31 .pipe(sourcemaps.init())
32 .pipe(less(opts.lessOptions));
34 opts
35 .streamTransform(stream)
36 .pipe(sourcemaps.write(opts.sourceMapOptions.dest, opts.sourceMapOptions.options))
37 .pipe(gulp.dest(opts.dest))
38 .on('end', function() {
39 resolve();
41 .on('error', function(err) {
42 reject(err);
43 });
44 });
47 myLessWatcher.affectedEmitter.on('change', function(result) {
48 console.log('affected', result);
49 buildStyles(result).then(function() {
50 console.log('building done');
51 });
52 });
54 return {
55 build: function(force) {
56 var getTargetEntryPointsPromise = Promise.resolve(entryPoints);
57 if (!force) {
58 getTargetEntryPointsPromise = myLessWatcher.getDirtyEntryPoints();
61 return getTargetEntryPointsPromise
62 .then(function(targetEntryPoints) {
63 return buildStyles(targetEntryPoints);
65 .then(function() {
66 console.log('building done');
67 });
69 startWatching: myLessWatcher.startWatching.bind(myLessWatcher),
70 stopWatching: myLessWatcher.stopWatching.bind(myLessWatcher)