1 | 'use strict';
|
2 |
|
3 | var fs = require('fs-extra'),
|
4 | path = require('path'),
|
5 | yaml = require('js-yaml'),
|
6 | less = require('gulp-less'),
|
7 | rename = require('gulp-rename'),
|
8 | changed = require('gulp-changed'),
|
9 | lazypipe = require('lazypipe'),
|
10 | autoprefixer = require('gulp-autoprefixer'),
|
11 | sourcemaps = require('gulp-sourcemaps');
|
12 |
|
13 | function is_file(fullpath) {
|
14 | return fs.existsSync(fullpath) && fs.statSync(fullpath).isFile();
|
15 | }
|
16 |
|
17 | function is_dir(fullpath) {
|
18 | return fs.existsSync(fullpath) && fs.statSync(fullpath).isDirectory();
|
19 | }
|
20 |
|
21 | module.exports = function(options) {
|
22 | var src = path.join(options.paths.styles.cwd, options.paths.styles.glob),
|
23 | dest = path.join(options.paths.dest, options.paths.styles.dest);
|
24 |
|
25 | var bower_dir = path.resolve(options.paths.bower, 'bower_components');
|
26 |
|
27 | function compile(stream, onError) {
|
28 | var config = [],
|
29 | semantic = [];
|
30 |
|
31 | var env_vars = is_file(options.paths.env) ? yaml.load(fs.readFileSync(options.paths.env)) : {},
|
32 | theme_dir = path.join(path.dirname(options.paths.env), '_site'),
|
33 | semantic_src = path.join(bower_dir, 'semantic-ui/src'),
|
34 | semantic_less = path.join(semantic_src, 'semantic.less'),
|
35 | semantic_config = path.join(semantic_src, 'theme.config');
|
36 |
|
37 | if (is_dir(semantic_src) && (env_vars && typeof env_vars.semantic === 'object')) {
|
38 | for (var group in env_vars.semantic) {
|
39 | for (var kind in env_vars.semantic[group]) {
|
40 | semantic.push('& { @import "definitions/' + [group, kind].join('/') + '"; }');
|
41 | config.push('@' + kind + ': "' + env_vars.semantic[group][kind] + '";');
|
42 | }
|
43 | }
|
44 |
|
45 | if (is_dir(theme_dir)) {
|
46 | config.push('@siteFolder: "' + theme_dir + '";');
|
47 | } else {
|
48 | config.push('@siteFolder: "_site";');
|
49 | }
|
50 |
|
51 | config.push('@definitionsFolder: "definitions";');
|
52 | config.push('@themesFolder: "themes";');
|
53 | config.push('@import "theme.less";');
|
54 | }
|
55 |
|
56 | fs.outputFileSync(semantic_less, semantic.join('\n'));
|
57 | fs.outputFileSync(semantic_config, config.join('\n'));
|
58 |
|
59 | var doLess = function() {
|
60 | return less({
|
61 | paths: [bower_dir, process.cwd(), dest]
|
62 | })
|
63 | .on('error', onError);
|
64 | };
|
65 |
|
66 | var partialPipe = lazypipe()
|
67 | .pipe(doLess)
|
68 | .pipe(autoprefixer, options.paths.styles.autoprefixer);
|
69 |
|
70 | console.log('Autoprefixer: ', options.paths.styles.autoprefixer);
|
71 |
|
72 | stream = options.env.debug !== true ? stream.pipe(partialPipe()) : stream
|
73 | .pipe(sourcemaps.init())
|
74 | .pipe(partialPipe())
|
75 | .pipe(sourcemaps.write());
|
76 |
|
77 | if (options.bundle && options.bundle.compact) {
|
78 | return stream.pipe(rename(function(file) {
|
79 | var fixed_name = path.basename(file.dirname);
|
80 |
|
81 | file.dirname = file.dirname.substr(0, file.dirname.length - fixed_name.length);
|
82 | file.basename = fixed_name || file.basename;
|
83 | file.extname = '.css';
|
84 | }));
|
85 | }
|
86 |
|
87 | return stream;
|
88 | }
|
89 |
|
90 | function ifChanged(stream, onError) {
|
91 | return stream.pipe(changed(dest, {
|
92 | extension: '.css',
|
93 | hasChanged: function(stream, cb, sourceFile, targetPath) {
|
94 | if (options.bundle && options.bundle.compact) {
|
95 | targetPath = targetPath.replace(/\/?index(\.\w+)$/, '$1');
|
96 | }
|
97 |
|
98 | changed.compareLastModifiedTime(stream, cb, sourceFile, targetPath);
|
99 | }
|
100 | })).on('error', onError);
|
101 | }
|
102 |
|
103 | return {
|
104 | src: src,
|
105 | dest: dest,
|
106 | pipe: compile,
|
107 | check: ifChanged
|
108 | };
|
109 | };
|