Files
postcss/gulpfile.js
T

186 lines
5.5 KiB
JavaScript

var gulp = require('gulp');
var fs = require('fs-extra');
// Build
gulp.task('build:clean', function (done) {
fs.remove(__dirname + '/build', done);
});
gulp.task('build:lib', ['build:clean'], function () {
var es6to5 = require('gulp-6to5');
return gulp.src('lib/*.js')
.pipe(es6to5())
.pipe(gulp.dest('build/lib'));
});
gulp.task('build:docs', ['build:clean'], function () {
var ignore = require('fs').readFileSync('.npmignore').toString()
.trim().split(/\n+/)
.concat(['.npmignore', 'index.js', 'package.json'])
.map(function (i) { return '!' + i; });
return gulp.src(['*'].concat(ignore))
.pipe(gulp.dest('build'));
});
gulp.task('build:package', ['build:clean'], function () {
var editor = require('gulp-json-editor');
gulp.src('./package.json')
.pipe(editor(function (json) {
json.main = 'lib/postcss';
json.devDependencies['6to5'] = json.dependencies['6to5'];
delete json.dependencies['6to5'];
return json;
}))
.pipe(gulp.dest('build'));
});
gulp.task('build', ['build:lib', 'build:docs', 'build:package']);
// Lint
gulp.task('lint', function () {
var jshint = require('gulp-jshint');
return gulp.src(['*.js',
'lib/*.js',
'test/*.js',
'tasks/*.js',
'benchmark/**/*.js'])
.pipe(jshint({ esnext: true, expr: true }))
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
});
// Benchmark
gulp.task('bench:clean', function (done) {
fs.remove(__dirname + '/benchmark/results', function () {
fs.remove(__dirname + '/benchmark/cache', done);
});
});
['tokenizer', 'parser'].forEach(function (type) {
gulp.task('bench:' + type, ['build:lib'], function() {
var compare = require('./tasks/compare');
var bench = require('gulp-bench');
var sh = require('execSync');
var status = sh.exec('git status --porcelain').stdout.trim();
var name = status === '' ? 'master' : 'current';
return gulp.src('./benchmark/' + type + '.js', { read: false })
.pipe(bench({ outputFormat: 'json', output: name + '.json' }))
.pipe(compare(name))
.pipe(gulp.dest('./benchmark/results'));
});
});
gulp.task('bench:bootstrap', function (done) {
if ( fs.existsSync('./benchmark/cache/bootstrap.css') ) return done();
var get = require('./tasks/get');
get('github:twbs/bootstrap:dist/css/bootstrap.css', function (css) {
fs.outputFile('./benchmark/cache/bootstrap.css', css, done);
});
});
gulp.task('bench:processors', ['build', 'bench:bootstrap'], function () {
var bench = require('gulp-bench');
var summary = require('gulp-bench-summary');
return gulp.src('./benchmark/processors.js', { read: false })
.pipe(bench())
.pipe(summary('PostCSS'));
});
gulp.task('bench:parsers', ['build', 'bench:bootstrap'], function () {
var bench = require('gulp-bench');
var summary = require('gulp-bench-summary');
return gulp.src('./benchmark/parsers.js', { read: false })
.pipe(bench())
.pipe(summary('PostCSS'));
});
// Tests
gulp.task('integration', ['build:lib'], function (done) {
var gutil = require('gulp-util');
var path = require('path');
var postcss = require('./build/lib/postcss');
var styles = require('./tasks/styles');
var error = function (message) {
done(new gutil.PluginError('integration', {
showStack: false,
message: message
}));
};
var sites = [
{ GitHub: 'https://github.com/' },
{ Twitter: 'https://twitter.com/' },
{ Bootstrap: 'github:twbs/bootstrap:dist/css/bootstrap.css' },
{ Habrahabr: 'http://habrahabr.ru/' },
{ Browserhacks: 'http://browserhacks.com/' }
];
styles(sites, {
site: function (name) {
gutil.log('Test ' + name + ' styles');
},
css: function (css, url) {
var processed;
try {
processed = postcss().process(css, {
map: { annotation: false },
safe: url.match('browserhacks.com')
}).css;
} catch (e) {
fs.writeFileSync('fail.css', css);
return error('Parsing error: ' + e.message);
}
if ( processed != css ) {
fs.writeFileSync('origin.css', css);
fs.writeFileSync('fail.css', processed);
return error('Output is not equal input');
}
gutil.log(' ' + gutil.colors.green(path.basename(url)));
return true;
},
done: done
});
});
gulp.task('test', function () {
require('./');
var mocha = require('gulp-mocha');
return gulp.src('test/*.js', { read: false }).pipe(mocha());
});
// Helpers
gulp.task('cases', function () {
var postcss = require('./');
var cases = __dirname + '/test/cases/';
fs.readdirSync(cases).forEach(function (file) {
if ( !file.match(/\.css$/) ) return;
var css = fs.readFileSync(cases + file);
var root = postcss.parse(css, { from: '/' + file });
var json = JSON.stringify(root, null, 4);
fs.writeFileSync(cases + file.replace(/\.css$/, '.json'), json + '\n');
});
});
// Common
gulp.task('clean', ['build:clean', 'bench:clean']);
gulp.task('default', ['lint', 'test', 'integration']);