This repository has been archived by the owner on Dec 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
117 lines (103 loc) · 3.6 KB
/
index.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
'use strict';
const PLUGIN_NAME = 'viur-ignite-css';
const path = require('path');
const isThere = require('is-there');
const gulp = require('gulp');
const gutil = require('gulp-util');
const rename = require('gulp-rename');
const clone = require('gulp-clone');
const gulpif = require('gulp-if');
const less = require('gulp-less');
const autoprefixer = require('gulp-autoprefixer');
const nano = require('gulp-cssnano');
const postcss = require('gulp-postcss');
const stylefmt = require('gulp-stylefmt');
const sourcemaps = require('gulp-sourcemaps');
const zindex = require('postcss-zindex');
const focus = require('postcss-focus');
const merge = require('merge-stream');
module.exports = {
build: function (options) {
// Set Default Options
var defaultOptions = {
src: path.join(__dirname, 'less/viur.less'), // source path of basic less file
dest: './appengine/static/css/', // destination path of css
sourceMap: true, // sourcemap for style.css
minSourceMap: false // sourcemap four style.min.css
};
if (typeof options === 'undefined') {
options = {};
}
for (var key in defaultOptions) {
if (typeof options[key] === 'undefined') {
options[key] = defaultOptions[key];
}
}
// Options for postcss
var processors = [
focus, // add focus to hover-states
zindex, // reduce z-index values
require('postcss-font-magician')({
hosted: path.join(options.dest, '..', 'fonts'), // import fonts
formats: 'local eot woff2'
})
];
var source = gulp.src(options.src, {
// base: path.join(options.src, '..')
})
.pipe(sourcemaps.init()) // initial sourcemap
.pipe(less({
paths: [path.join(__dirname, 'less', 'includes')] // need for relative @imports
})) // compile less to css
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
})) // add vendor prefixes
.pipe(postcss(processors)) // clean up css
.pipe(stylefmt()); // syntax formatting
var stylesPipe1 = source.pipe(clone()) // Create non-minified sourcemap
.pipe(rename('style.css')) // rename file -> style.css
.pipe(gulpif(options.sourceMap, sourcemaps.write('../map', {
includeContent: false,
sourceRoot: path.join('../../..', 'node_modules', 'viur-ignite-css', 'less') // #FIXME: this relative path problem sucks
}))) // create sourcemap
.pipe(gulp.dest(options.dest)); // save style.css
var stylesPipe2 = source.pipe(clone()) // Create minified sourcemap
.pipe(nano()) // minify css
.pipe(rename('style.min.css')) // rename -> style.min.css
.pipe(gulpif(options.minSourceMap, sourcemaps.write('../map', {
includeContent: false,
sourceRoot: path.join('../../..', 'node_modules', 'viur-ignite-css', 'less') // #FIXME: this relative path problem sucks
})))
.pipe(gulp.dest(options.dest)); // save style.min.css
return merge(stylesPipe1, stylesPipe2);
},
init: function (options) {
// Set Default Options
var defaultOptions = {
dest: './sources/less/style.less',
overwrite: false
};
if (typeof options === 'undefined') {
options = {};
}
for (var key in defaultOptions) {
if (typeof options[key] === 'undefined') {
options[key] = defaultOptions[key];
}
}
if (isThere(options.dest) && (options.overwrite === false || options.overwrite === 'false')) {
throw new gutil.PluginError(
PLUGIN_NAME,
'\'' + options.dest + '\' already exists\n\tcall function with option overwrite: true'
);
} else {
return copyPrototype(options.dest);
}
}
};
function copyPrototype(dest) {
return gulp.src(path.join(__dirname, 'prototype/style.less'))
.pipe(rename(path.basename(dest)))
.pipe(gulp.dest(path.dirname(dest)));
}