Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade clean css and add basic tests #37

Merged
merged 7 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# @see http://editorconfig.org/

# the buck stops here
root = true

# all files
[*]
end_of_line = LF
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{*.yml,*.yaml}]
indent_size = 2
21 changes: 21 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: build

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: setup node 20
uses: actions/setup-node@v3
with:
node-version: "20"
cache: 'npm'
- run: npm ci
- run: npm test
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,35 @@ Compresses the css output from less using [clean-css](https://github.com/jakubpa

## lessc usage

First of all install less via

```bash
npm install -g less
```

then install the `less-plugin-clean-css`

```bash
npm install -g less-plugin-clean-css
```

and then on the command line,

```
```bash
lessc file.less --clean-css="--s1 --advanced --compatibility=ie8"
```

See [clean-css](https://github.com/jakubpawlowicz/clean-css/tree/v3.0.1#how-to-use-clean-css-programmatically) for the available command options - the only differences are `advanced` and `rebase` which we default to false, because it is not always entirely safe.
See [clean-css](https://github.com/jakubpawlowicz/clean-css/tree/v3.0.1#how-to-use-clean-css-programmatically) for the
available command options - the only differences are `advanced` and `rebase` which we default to false, because it is
not always entirely safe.

## Programmatic usage

```js
var LessPluginCleanCSS = require('less-plugin-clean-css'),
cleanCSSPlugin = new LessPluginCleanCSS({advanced: true});
less.render(lessString, { plugins: [cleanCSSPlugin] })
.then(
less.render(lessString, {plugins: [cleanCSSPlugin]})
.then(
```

## Browser usage
Expand Down
20 changes: 10 additions & 10 deletions lib/clean-css-processor.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";

var CleanCSS = require("clean-css");
const CleanCSS = require("clean-css");

module.exports = function() {
module.exports = function () {
function CleanCSSProcessor(options) {
this.options = options || {};
}

CleanCSSProcessor.prototype = {
process: function (css, extra) {
var options = this.options,
let options = this.options,
sourceMap = extra.sourceMap,
sources,
sourcesContent;
Expand All @@ -18,7 +18,7 @@ module.exports = function() {
options.sourceMap = sourceMap.getExternalSourceMap();
if (options.sourceMap) {
options.sourceMap = options.sourceMap.toString();
var sourceMapObj = JSON.parse(options.sourceMap);
const sourceMapObj = JSON.parse(options.sourceMap);
if (sourceMapObj.sourcesContent) {
sourcesContent = sourceMapObj.sourcesContent;
sources = sourceMapObj.sources;
Expand All @@ -39,24 +39,24 @@ module.exports = function() {
options.advanced = false;
}

var output = new CleanCSS(options).minify(css);
const output = new CleanCSS(options).minify(css);

if (sourceMap) {
if (sourcesContent) {
for (var source = 0; source < sources.length; source++) {
for (let source = 0; source < sources.length; source++) {
output.sourceMap.setSourceContent(sources[source], sourcesContent[source]);
}
}
sourceMap.setExternalSourceMap(JSON.stringify(output.sourceMap));
}

var css = output.styles;
let outputCSS = output.styles;
if (sourceMap) {
var sourceMapURL = sourceMap.getSourceMapURL();
css += sourceMap.getCSSAppendage();
const sourceMapURL = sourceMap.getSourceMapURL();
outputCSS += sourceMap.getCSSAppendage();
}

return css;
return outputCSS;
}
};

Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

var getCleanCSSProcessor = require("./clean-css-processor"),
const getCleanCSSProcessor = require("./clean-css-processor"),
usage = require("./usage"),
parseOptions = require("./parse-options");

Expand All @@ -9,14 +9,14 @@ function LessPluginCleanCSS(options) {
}

LessPluginCleanCSS.prototype = {
install: function(less, pluginManager) {
var CleanCSSProcessor = getCleanCSSProcessor(less);
install: function (less, pluginManager) {
const CleanCSSProcessor = getCleanCSSProcessor(less);
pluginManager.addPostProcessor(new CleanCSSProcessor(this.options));
},
printUsage: function () {
usage.printUsage();
},
setOptions: function(options) {
setOptions: function (options) {
this.options = parseOptions(options);
},
minVersion: [2, 1, 0]
Expand Down
10 changes: 5 additions & 5 deletions lib/parse-options.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";

module.exports = function(options) {
module.exports = function (options) {
if (typeof options === "string") {
var cleanOptionArgs = options.split(" ");
const cleanOptionArgs = options.split(" ");
options = {};

for (var i = 0; i < cleanOptionArgs.length; i++) {
var argSplit = cleanOptionArgs[i].split("="),
for (let i = 0; i < cleanOptionArgs.length; i++) {
const argSplit = cleanOptionArgs[i].split("="),
argName = argSplit[0].replace(/^-+/, "");

switch (argName) {
Expand All @@ -21,7 +21,7 @@ module.exports = function(options) {
options.keepSpecialComments = 1;
break;
case "keepSpecialComments":
var specialCommentOption = argSplit[1];
let specialCommentOption = argSplit[1];
if (specialCommentOption !== "*") {
specialCommentOption = Number(specialCommentOption);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/usage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

module.exports = {
printUsage: function() {
printUsage: function () {
console.log("");
console.log("Clean CSS Plugin");
console.log("specify plugin with --clean-css");
Expand All @@ -13,7 +13,7 @@ module.exports = {
this.printOptions();
console.log("");
},
printOptions: function() {
printOptions: function () {
console.log("we support the following arguments... 'keep-line-breaks', 'b'");
console.log("'s0', 's1', 'advanced', 'rebase', 'keepSpecialComments', compatibility', 'rounding-precision'");
console.log("'skip-aggressive-merging', 'skip-shorthand-compacting'");
Expand Down
Loading