Skip to content

Commit 391db29

Browse files
committed
Плагин написан с нуля и протестирован
0 parents  commit 391db29

35 files changed

+5078
-0
lines changed

.eslintrc.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"mocha": true,
5+
"node": true
6+
},
7+
"extends": ["eslint:recommended", "google"],
8+
"plugins": [
9+
"standard",
10+
"promise"
11+
]
12+
}

.gitignore

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Compiled binary addons (http://nodejs.org/api/addons.html)
27+
build/Release
28+
29+
# Dependency directories
30+
node_modules
31+
jspm_packages
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history
38+
39+
# IDE
40+
.idea
41+
42+
# Test artefacts
43+
test/cases/**/produced*

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- "6"
4+
- "7"

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 INTERVOLGA.RU
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.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# optimize-cssnano-plugin [![Build Status](https://travis-ci.org/intervolga/optimize-cssnano-plugin.svg?branch=master)](https://travis-ci.org/intervolga/optimize-cssnano-plugin)

index.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const cssnano = require('cssnano');
2+
3+
/**
4+
* Optimize cssnano plugin
5+
*
6+
* @param {Object} options
7+
*/
8+
function OptimizeCssnanoPlugin(options) {
9+
this.options = Object.assign({
10+
sourceMap: false,
11+
cssnanoOptions: {
12+
preset: 'default',
13+
map: false,
14+
},
15+
}, options);
16+
17+
if (this.options.sourceMap) {
18+
this.options.cssnanoOptions.map = Object.assign(
19+
{inline: false},
20+
this.options.cssnanoOptions.map || {});
21+
}
22+
}
23+
24+
OptimizeCssnanoPlugin.prototype.apply = function(compiler) {
25+
const self = this;
26+
27+
compiler.plugin('emit', function(compilation, callback) {
28+
// Search for CSS assets
29+
const assetsNames = Object.keys(compilation.assets)
30+
.filter((assetName) => {
31+
return /\.css$/i.test(assetName);
32+
});
33+
34+
let hasErrors = false;
35+
const promises = [];
36+
37+
// Generate promises for each minification
38+
assetsNames.forEach((assetName) => {
39+
// Original CSS
40+
const asset = compilation.assets[assetName];
41+
const originalCss = asset.source();
42+
// .replace(/\/\*# sourceMappingURL.+\*\//, '');
43+
44+
// Options for particalar cssnano call
45+
const options = JSON.parse(JSON.stringify(self.options.cssnanoOptions));
46+
options.to = assetName;
47+
48+
// Extract or remove previous map
49+
const mapName = assetName + '.map';
50+
if (options.map) {
51+
// Use previous map if exist...
52+
if (compilation.assets[mapName]) {
53+
const mapObject = JSON.parse(compilation.assets[mapName].source());
54+
55+
// ... and not empty
56+
if (mapObject.sources.length > 0 || mapObject.mappings.length > 0) {
57+
options.map.prev = compilation.assets[mapName].source();
58+
}
59+
}
60+
} else {
61+
delete compilation.assets[mapName];
62+
}
63+
64+
// Run minification
65+
const promise = cssnano.process(originalCss, options)
66+
.then((result) => {
67+
if (hasErrors) {
68+
return;
69+
}
70+
71+
// Extract CSS back to assets
72+
const processedCss = result.css;
73+
compilation.assets[assetName] = {
74+
source: function() {
75+
return processedCss;
76+
},
77+
size: function() {
78+
return processedCss.length;
79+
},
80+
};
81+
82+
// Extract map back to assets
83+
if (result.map) {
84+
const processedMap = result.map.toString();
85+
86+
compilation.assets[mapName] = {
87+
source: function() {
88+
return processedMap;
89+
},
90+
size: function() {
91+
return processedMap.length;
92+
},
93+
};
94+
// processedCss += `\n/*# sourceMappingURL=${assetName + '.map'} */`;
95+
}
96+
}
97+
).catch(function(err) {
98+
hasErrors = true;
99+
throw new Error('CSS minification error: ' + err.message +
100+
'. File: ' + assetName);
101+
}
102+
);
103+
promises.push(promise);
104+
});
105+
106+
Promise.all(promises)
107+
.then(function() {
108+
callback();
109+
})
110+
.catch(callback);
111+
});
112+
};
113+
114+
module.exports = OptimizeCssnanoPlugin;

0 commit comments

Comments
 (0)