forked from Digitro/web-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
144 lines (126 loc) · 4.41 KB
/
gulpfile.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
const gulp = require('gulp');
const gutil = require('gulp-util');
const replace = require('gulp-replace');
const clean = require('gulp-clean');
const runSequence = require('run-sequence');
const eslint = require('gulp-eslint');
const through = require('through2');
const cheerio = require('cheerio');
const htmlmin = require('gulp-html-minifier');
const jsonfile = require('jsonfile');
const coveralls = require('gulp-coveralls');
const codacy = require('gulp-codacy');
const paths = {
build: 'dist',
buildComponents: 'dist/web-components/',
polyfills: 'bower_components/webcomponentsjs/*.js',
polyfillsDist: 'dist/polyfills/',
node_modules_dependencies: 'node_modules/**',
components: 'src/**'
};
const packageJson = jsonfile.readFileSync('package.json');
gulp.task('clean-dist', function () {
gutil.log('Cleaning dist of the components');
return gulp.src([paths.build])
.pipe(clean());
});
gulp.task('copy-package-json', function () {
gutil.log('Copying package.json');
let newPackageJson = {
name: packageJson.name,
version: packageJson.version,
private: false,
dependencies: packageJson.dependencies
};
let distPackageJson = './' + paths.build + '/package.json';
jsonfile.writeFileSync(distPackageJson, newPackageJson, {spaces: 2});
return;
});
gulp.task('addVersion', function () {
let fileTypes = [
{
name: 'script',
srcAttribute: 'src'
},
{
name: 'link',
srcAttribute: 'href'
}
];
let versionGUID = 'v' + packageJson.version;
let addVersionToImports = through.obj(function (file, enc, cb) {
console.log('FILE', file.path);
let $ = cheerio.load(file.contents.toString());
for (let i = 0; i < fileTypes.length; i++) {
let attributes = fileTypes[i];
let $assets = $(attributes.name);
for (let j = 0; j < $assets.length; j++) {
let $asset = $assets.eq(j);
let src = $asset.attr(attributes.srcAttribute);
if (src && !src.match(/.*(\/\/).*/)) {
$asset.attr(attributes.srcAttribute, src + '?_v=' + versionGUID);
}
}
}
file.contents = new Buffer($.html());
this.push(file);
return cb();
});
return gulp.src(paths.buildComponents + '**/*.html')
.pipe(addVersionToImports)
.pipe(gulp.dest(paths.buildComponents));
});
gulp.task('package-components', function () {
gutil.log('Generating components');
return gulp.src(paths.components)
.pipe(replace(/((\.\.?\/?)*(bower_components|node_modules))/g, '../../lib', {
skipBinary: true
}))
.pipe(gulp.dest(paths.buildComponents));
});
gulp.task('copy-polyfills', function(){
gutil.log('Copying libs');
return gulp.src([paths.polyfills, "!**/gulpfile.js", "!**/custom-elements-es5-adapter.js"])
.pipe(gulp.dest(paths.polyfillsDist));
});
gulp.task('obfuscate', function () {
return gulp.src([paths.buildComponents + '**/dgt*.html'])
.pipe(htmlmin({
removeEmptyAttributes: true,
collapseWhitespace: true,
conservativeCollapse: true,
minifyJS: true,
minifyCSS: true,
removeComments: true,
removeCommentsFromCDATA: true,
removeCDATASectionsFromCDATA: true
}))
.pipe(gulp.dest(paths.buildComponents));
});
gulp.task('build', function (callback) {
runSequence('clean-dist', 'package-components', 'copy-polyfills', 'addVersion', 'copy-package-json', "obfuscate", callback);
});
gulp.task('lint', function () {
return gulp.src(['src/**/*.js', 'src/**/*.html', 'test/**/*.js', 'test/**/*.html']).pipe(eslint({
fix: false
}))
.pipe(eslint.result(function (result) {
gutil.log('ESLint result: ' + result.filePath);
for (let i = 0; i < result.messages.length; i++) {
gutil.log(result.messages[i]);
}
}))
.pipe(eslint.failAfterError());
});
gulp.task('coveralls', function() {
return gulp.src('coverage/**/lcov.info')
.pipe(coveralls());
});
gulp.task('codacy', function() {
return gulp.src('coverage/**/lcov.info')
.pipe(codacy({
token: '4f0175351baa40fc921127c5124552b8'
}));
});
// create a default task
gulp.task("default", ["build-zip"]);