forked from postcss/postcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
148 lines (122 loc) · 4.29 KB
/
gulpfile.babel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import gulp from 'gulp';
import path from 'path';
import fs from 'fs-extra';
gulp.task('clean', (done) => {
fs.remove(path.join(__dirname, 'postcss.js'), () => {
fs.remove(path.join(__dirname, 'build'), done);
});
});
// Build
gulp.task('build:lib', ['clean'], () => {
let babel = require('gulp-babel');
return gulp.src('lib/*.js')
.pipe(babel({ loose: 'all' }))
.pipe(gulp.dest('build/lib'));
});
gulp.task('build:docs', ['clean'], () => {
let ignore = require('fs').readFileSync('.npmignore').toString()
.trim().split(/\n+/)
.concat(['.npmignore', 'index.js', 'package.json'])
.map( i => '!' + i );
return gulp.src(['*'].concat(ignore))
.pipe(gulp.dest('build'));
});
gulp.task('build:package', ['clean'], () => {
let editor = require('gulp-json-editor');
gulp.src('./package.json')
.pipe(editor( (p) => {
p.main = 'lib/postcss';
p.devDependencies.babel = p.dependencies.babel;
delete p.dependencies.babel;
return p;
}))
.pipe(gulp.dest('build'));
});
gulp.task('build', ['build:lib', 'build:docs', 'build:package']);
// Lint
gulp.task('lint', () => {
let eslint = require('gulp-eslint');
return gulp.src(['*.js', 'lib/*.js', 'test/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('spellcheck', (done) => {
let gutil = require('gulp-util');
let run = require('gulp-run');
run('yaspeller .').exec()
.on('error', (err) => {
done(new gutil.PluginError('spellcheck', {
showStack: false,
message: err.message
}));
})
.on('finish', done);
});
// Tests
gulp.task('integration', ['build:lib', 'build:package'], (done) => {
let gutil = require('gulp-util');
let load = require('load-resources');
let postcss = require('./build/lib/postcss');
let error = (url, message) => {
gutil.log(gutil.colors.red('Fail on ' + url));
done(new gutil.PluginError('integration', {
showStack: false,
message: message
}));
};
let 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/'
};
let urls = Object.keys(sites).map( i => sites[i] );
let lastDomain = false;
let siteIndex = -1;
load(urls, '.css', (css, url, last) => {
postcss().process(css, {
map: { annotation: false },
safe: url.match('browserhacks.com')
}).catch( (e) => {
fs.writeFileSync('fail.css', css);
return error(url, 'Parsing error: ' + e.message + e.stack);
}).then( (result) => {
if ( !result ) return;
if ( result.css !== css ) {
fs.writeFileSync('origin.css', css);
fs.writeFileSync('fail.css', result.css);
error(url, 'Output is not equal input');
return;
}
let domain = url.match(/https?:\/\/[^\/]+/)[0];
if ( domain !== lastDomain ) {
lastDomain = domain;
siteIndex += 1;
gutil.log('Test ' + Object.keys(sites)[siteIndex] + ' styles');
}
gutil.log(' ' + gutil.colors.green(path.basename(url)));
if ( last ) done();
}).catch(done);
});
});
gulp.task('test', () => {
let mocha = require('gulp-mocha');
return gulp.src('test/*.js', { read: false }).pipe(mocha());
});
// Helpers
gulp.task('cases', () => {
let postcss = require('./lib/postcss');
let cases = path.join(__dirname, 'test', 'cases');
fs.readdirSync(cases).forEach( (name) => {
if ( !name.match(/\.css$/) ) return;
let css = fs.readFileSync(path.join(cases, name));
let root = postcss.parse(css, { from: '/' + name });
let json = JSON.stringify(root, null, 4);
let file = path.join(cases, name.replace(/\.css$/, '.json'));
fs.writeFileSync(file, json + '\n');
});
});
// Common
gulp.task('default', ['lint', 'spellcheck', 'test', 'integration']);