Skip to content

Commit 15b10c1

Browse files
committed
initial commit
0 parents  commit 15b10c1

File tree

6 files changed

+267
-0
lines changed

6 files changed

+267
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.idea
3+
package-lock.json
4+
yarn.lock

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.gitignore
2+
.DS_Store
3+
._*
4+
node_modules
5+
npm-debug.log
6+
test
7+
yarn.lock
8+
package-lock.json

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Marat
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

index.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
let Plugin = require('gulp-query').Plugin
2+
, node_path = require('path')
3+
, glob = require('glob')
4+
, gulp = require('gulp')
5+
, gulpif = require('gulp-if')
6+
, concat = require("gulp-concat")
7+
, cssnano = require("gulp-cssnano")
8+
, sourcemaps = require('gulp-sourcemaps')
9+
;
10+
11+
class StylesPlugin extends Plugin {
12+
13+
static method() {
14+
return 'styles';
15+
}
16+
17+
/**
18+
*
19+
* @param {GulpQuery} GulpQuery
20+
* @param configs
21+
*/
22+
constructor(GulpQuery, configs) {
23+
super(GulpQuery, configs);
24+
25+
/**
26+
* @type {Object.<String,{src: Array, to: String, completeAmount: Number}>}
27+
* @private
28+
*/
29+
this._reportList = {};
30+
}
31+
32+
run(task_name, config, callback) {
33+
34+
let full = ('full' in config ? config['full'] : false);
35+
let path_to = this.path(config.to);
36+
let parent_folder = 'parent_folder' in config ? config.parent_folder : null;
37+
38+
let concat_name, copy_to;
39+
if (!node_path.extname(path_to)) {
40+
concat_name = null;
41+
copy_to = path_to;
42+
} else {
43+
concat_name = node_path.basename(path_to);
44+
copy_to = node_path.dirname(path_to) + '/'
45+
}
46+
47+
if (!Array.isArray(config.from)) {
48+
config.from = [config.from];
49+
}
50+
51+
let from = [];
52+
config.from.forEach((path) => {
53+
54+
path = this.path(parent_folder ? (parent_folder + path) : path);
55+
56+
if (node_path.basename(path).indexOf('*') !== -1) {
57+
glob.sync(path).forEach((f) => {
58+
from.push(f);
59+
});
60+
} else {
61+
from.push(path);
62+
}
63+
64+
});
65+
66+
let sourceMap = ('source_map' in config ? config['source_map'] : true);
67+
let sourceMapType = ('source_map_type' in config ? config['source_map_type'] : 'inline');
68+
sourceMapType = sourceMapType === 'inline' ? 'inline-source-map' : 'source-map';
69+
70+
if (this.isProduction()) {
71+
sourceMap = false;
72+
}
73+
74+
let list = [];
75+
76+
if (concat_name) {
77+
list.push('Concat');
78+
}
79+
80+
if (this.isProduction() && !full) {
81+
list.push('Compress');
82+
}
83+
84+
if (sourceMap) {
85+
if (sourceMapType === 'source-map') {
86+
list.push('Source map: file');
87+
} else {
88+
list.push('Source map: inline');
89+
}
90+
}
91+
92+
let _src = from;
93+
let _dest = copy_to + (concat_name ? concat_name : '');
94+
let reportFunc;
95+
96+
if (!concat_name) {
97+
this._reportList[_src] = {
98+
from: _src,
99+
to: _dest,
100+
completeAmount: 0
101+
};
102+
103+
reportFunc = this.reportList.bind(this, task_name, _src);
104+
} else {
105+
reportFunc = this.report.bind(this, task_name, _src, _dest, true, list);
106+
}
107+
108+
return gulp.src(from)
109+
.pipe(this.plumber(this.reportError.bind(this, task_name, _src, _dest)))
110+
.pipe(gulpif(sourceMap, sourcemaps.init()))
111+
.pipe(gulpif(!!concat_name, concat(concat_name || 'empty')))
112+
.pipe(gulpif(
113+
!full && this.isProduction(),
114+
cssnano({
115+
autoprefixer: {
116+
browsers: ["> 1%", "last 2 versions"],
117+
add: true
118+
}
119+
})
120+
))
121+
.pipe(gulpif(sourceMap, sourcemaps.write(
122+
(sourceMapType === 'inline-source-map' ? null : '.'),
123+
{includeContent: (sourceMapType === 'inline-source-map')}
124+
)))
125+
.pipe(gulp.dest(copy_to))
126+
.pipe(this.notify(reportFunc))
127+
;
128+
129+
}
130+
131+
reportList(task_name, from) {
132+
133+
if (!(from in this._reportList)) {
134+
return;
135+
}
136+
137+
++this._reportList[from].completeAmount;
138+
139+
if (this._reportList[from].completeAmount >= this._reportList[from].from.length) {
140+
this.report(
141+
task_name,
142+
this._reportList[from].from,
143+
this._reportList[from].to,
144+
true
145+
);
146+
147+
delete this._reportList[from];
148+
}
149+
}
150+
}
151+
152+
module.exports = StylesPlugin;

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "gulp-query-css",
3+
"version": "1.0.1",
4+
"description": "This plugin provides automatic source maps, copy, combine and rename CSS, autoprefixing and minification.",
5+
"main": "index.js",
6+
"author": "Marat Nuriev",
7+
"email": "nurieff@gmail.com",
8+
"license": "MIT",
9+
"repository": "https://github.com/gulp-query/gulp-query-css",
10+
"keywords": [
11+
"css",
12+
"autoprefixer",
13+
"cssnano",
14+
"gulp",
15+
"gulp-query",
16+
"gulp-query-plugin"
17+
],
18+
"dependencies": {
19+
"gulp-query": "^1.0.0",
20+
"gulp": "^3.9.1",
21+
"gulp-concat": "^2.6.1",
22+
"gulp-cssnano": "^2.1.2",
23+
"gulp-if": "^2.0.2",
24+
"gulp-sourcemaps": "^2.6.1"
25+
}
26+
}

readme.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## gulp-query-css
2+
Plugin for [gulp-query](https://github.com/gulp-query/gulp-query)
3+
4+
Uses [cssnano](http://cssnano.co/) with autoprefixer for optimization
5+
6+
This plugin provides automatic source maps, copy, combine and rename CSS, autoprefixing and minification.
7+
Write your CSS rules without vendor prefixes — autoprefixer will do everything itself
8+
9+
```
10+
npm install gulp-query gulp-query-css
11+
```
12+
13+
### Example
14+
Paste the code into your `gulpfile.js` and configure it
15+
```javascript
16+
let build = require('gulp-query')
17+
, css = require('gulp-query-css')
18+
;
19+
cocktail(function (query) {
20+
query.plugins([css])
21+
.css(['1.css','2.css'],'css/12.css','onetwo')
22+
.css('css_source/*.css','css/')
23+
24+
.css({
25+
from: 'css_source/*.css',
26+
to: 'css/big.css',
27+
name: 'big'
28+
})
29+
;
30+
});
31+
```
32+
And feel the freedom
33+
```
34+
gulp
35+
gulp --production // For production
36+
gulp watch // Watching change
37+
gulp css // Only for css
38+
gulp css:onetwo
39+
gulp css:big watch
40+
...
41+
```
42+
43+
### Options
44+
```javascript
45+
.css({
46+
name: "task_name", // For gulp css:task_name
47+
from: 'css_source/*.css', // ['1.css','2.css']
48+
to: "css/",
49+
source_map: true,
50+
source_map_type: 'inline',
51+
full: false, // if set true is without compress in prod
52+
autoprefixer: {
53+
browsers: ["> 1%", "last 2 versions"],
54+
}
55+
})
56+
```

0 commit comments

Comments
 (0)