Skip to content

Commit 38ce7d5

Browse files
committed
Require Node.js 12.20 and move to ESM
1 parent fd22068 commit 38ce7d5

File tree

10 files changed

+91
-116
lines changed

10 files changed

+91
-116
lines changed

.github/funding.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

.github/workflows/main.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
13+
- 16
1714
steps:
1815
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
2017
with:
2118
node-version: ${{ matrix.node-version }}
2219
- run: npm install

bench.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
'use strict';
21
/* global bench */
3-
const fs = require('fs');
4-
const stripCssComments = require('.');
2+
import fs from 'node:fs';
3+
import stripCssComments from './index.js';
54

65
const fixture = fs.readFileSync('fixture.css', 'utf8');
76

index.d.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
declare namespace stripCssComments {
2-
interface Options {
3-
/**
4-
- `true` - Preserve important comments `/*! *\/`.
5-
- `false` - Strip all comments.
6-
- `RegExp` - Preserve comments where the comment body matches a regular expression.
7-
- `Function` - Preserve comments for which a function returns `true`. The function is called on each comment, gets the comment body as the first argument, and is expected to return a boolean of whether to preserve the comment.
8-
9-
@default true
10-
*/
11-
readonly preserve?: boolean | RegExp | ((comment: string) => boolean);
12-
13-
/**
14-
Replace comments with whitespace instead of stripping them entirely.
15-
16-
@default true
17-
*/
18-
readonly whitespace?: boolean;
19-
}
1+
export interface Options {
2+
/**
3+
- `true` - Preserve important comments `/*! *\/`.
4+
- `false` - Strip all comments.
5+
- `RegExp` - Preserve comments where the comment body matches a regular expression.
6+
- `Function` - Preserve comments for which a function returns `true`. The function is called on each comment, gets the comment body as the first argument, and is expected to return a boolean of whether to preserve the comment.
7+
8+
@default true
9+
*/
10+
readonly preserve?: boolean | RegExp | ((comment: string) => boolean);
11+
12+
/**
13+
Replace comments with whitespace instead of stripping them entirely.
14+
15+
@default true
16+
*/
17+
readonly whitespace?: boolean;
2018
}
2119

2220
/**
@@ -26,7 +24,7 @@ Strip comments from CSS.
2624
2725
@example
2826
```
29-
import stripCssComments = require('strip-css-comments');
27+
import stripCssComments from 'strip-css-comments';
3028
3129
// By default important comments `/*!` are preserved
3230
stripCssComments('/*! <copyright> *\/ body { /* unicorns *\/color: hotpink; }');
@@ -56,9 +54,7 @@ stripCssComments(
5654
//=> '/*# preserved *\/ body { color: hotpink; }'
5755
```
5856
*/
59-
declare function stripCssComments(
57+
export default function stripCssComments(
6058
cssString: string,
61-
options?: stripCssComments.Options
59+
options?: Options
6260
): string;
63-
64-
export = stripCssComments;

index.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
1-
'use strict';
2-
const isRegExp = require('is-regexp');
1+
import isRegExp from 'is-regexp';
32

4-
module.exports = (cssString, options = {}) => {
5-
let preserveImportant = !(options.preserve === false || options.all === true);
6-
const stripWhitespace = options.whitespace === false;
3+
export default function stripCssComments(cssString, {preserve = true, whitespace = true, all} = {}) {
4+
if (all) {
5+
throw new Error('The `all` option is no longer supported. Use the `preserve` option instead.');
6+
}
77

8+
let preserveImportant = preserve;
89
let preserveFilter;
9-
if (typeof options.preserve === 'function') {
10+
if (typeof preserve === 'function') {
1011
preserveImportant = false;
11-
preserveFilter = options.preserve;
12-
} else if (isRegExp(options.preserve)) {
12+
preserveFilter = preserve;
13+
} else if (isRegExp(preserve)) {
1314
preserveImportant = false;
14-
preserveFilter = comment => options.preserve.test(comment);
15+
preserveFilter = comment => preserve.test(comment);
1516
}
1617

1718
let isInsideString = false;
1819
let currentCharacter = '';
1920
let comment = '';
2021
let returnValue = '';
2122

22-
for (let i = 0; i < cssString.length; i++) {
23-
currentCharacter = cssString[i];
23+
for (let index = 0; index < cssString.length; index++) {
24+
currentCharacter = cssString[index];
2425

25-
if (cssString[i - 1] !== '\\') {
26-
if (currentCharacter === '"' || currentCharacter === '\'') {
27-
if (isInsideString === currentCharacter) {
28-
isInsideString = false;
29-
} else if (!isInsideString) {
30-
isInsideString = currentCharacter;
31-
}
26+
if (cssString[index - 1] !== '\\' && (currentCharacter === '"' || currentCharacter === '\'')) {
27+
if (isInsideString === currentCharacter) {
28+
isInsideString = false;
29+
} else if (!isInsideString) {
30+
isInsideString = currentCharacter;
3231
}
3332
}
3433

3534
// Find beginning of `/*` type comment
36-
if (!isInsideString && currentCharacter === '/' && cssString[i + 1] === '*') {
35+
if (!isInsideString && currentCharacter === '/' && cssString[index + 1] === '*') {
3736
// Ignore important comment when configured to preserve comments using important syntax: /*!
38-
const isImportantComment = cssString[i + 2] === '!';
39-
let j = i + 2;
37+
const isImportantComment = cssString[index + 2] === '!';
38+
let index2 = index + 2;
4039

4140
// Iterate over comment
42-
for (; j < cssString.length; j++) {
41+
for (; index2 < cssString.length; index2++) {
4342
// Find end of comment
44-
if (cssString[j] === '*' && cssString[j + 1] === '/') {
43+
if (cssString[index2] === '*' && cssString[index2 + 1] === '/') {
4544
if ((preserveImportant && isImportantComment) || (preserveFilter && preserveFilter(comment))) {
4645
returnValue += `/*${comment}*/`;
47-
} else if (stripWhitespace) {
48-
if (cssString[j + 2] === '\n') {
49-
j++;
50-
} else if (cssString[j + 2] + cssString[j + 3] === '\r\n') {
51-
j += 2;
46+
} else if (!whitespace) {
47+
if (cssString[index2 + 2] === '\n') {
48+
index2++;
49+
} else if (cssString[index2 + 2] + cssString[index2 + 3] === '\r\n') {
50+
index2 += 2;
5251
}
5352
}
5453

@@ -58,11 +57,11 @@ module.exports = (cssString, options = {}) => {
5857
}
5958

6059
// Store comment text
61-
comment += cssString[j];
60+
comment += cssString[index2];
6261
}
6362

6463
// Resume iteration over CSS string from the end of the comment
65-
i = j + 1;
64+
index = index2 + 1;
6665

6766
continue;
6867
}
@@ -71,4 +70,4 @@ module.exports = (cssString, options = {}) => {
7170
}
7271

7372
return returnValue;
74-
};
73+
}

index.test-d.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import {expectType} from 'tsd';
2-
import stripCssComments = require('.');
2+
import stripCssComments from './index.js';
33

44
expectType<string>(
5-
stripCssComments('/*! <copyright> */ body { /* unicorns */color: hotpink; }')
5+
stripCssComments('/*! <copyright> */ body { /* unicorns */color: hotpink; }'),
66
);
77
expectType<string>(
88
stripCssComments(
99
'/*! <copyright> */ body { /* unicorns */color: hotpink; }', {
10-
preserve: false
11-
})
10+
preserve: false,
11+
}),
1212
);
1313
expectType<string>(
1414
stripCssComments('/*# preserved */ body { /* unicorns */color: hotpink; }', {
15-
preserve: /^#/
16-
})
15+
preserve: /^#/,
16+
}),
1717
);
1818
expectType<string>(
1919
stripCssComments('/*# preserved */ body { /* unicorns */color: hotpink; }', {
20-
preserve: comment => comment.charAt(0) === '#'
21-
})
20+
preserve: comment => comment.startsWith('#'),
21+
}),
2222
);
2323
expectType<string>(
2424
stripCssComments('/*# preserved */ body { /* unicorns */color: hotpink; }', {
25-
whitespace: false
26-
})
25+
whitespace: false,
26+
}),
2727
);

license

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Strip comments from CSS",
55
"license": "MIT",
66
"repository": "sindresorhus/strip-css-comments",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "[email protected]",
1011
"url": "https://sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=8"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1417
},
1518
"scripts": {
1619
"test": "xo && ava && tsd",
@@ -34,12 +37,12 @@
3437
"string"
3538
],
3639
"dependencies": {
37-
"is-regexp": "^2.1.0"
40+
"is-regexp": "^3.0.0"
3841
},
3942
"devDependencies": {
40-
"ava": "^2.4.0",
43+
"ava": "^3.15.0",
4144
"matcha": "^0.7.0",
42-
"tsd": "^0.11.0",
43-
"xo": "^0.25.3"
45+
"tsd": "^0.17.0",
46+
"xo": "^0.44.0"
4447
}
4548
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $ npm install strip-css-comments
1313
## Usage
1414

1515
```js
16-
const stripCssComments = require('strip-css-comments');
16+
import stripCssComments from 'strip-css-comments';
1717

1818
// By default important comments `/*!` are preserved
1919
stripCssComments('/*! <copyright> */ body { /* unicorns */color: hotpink; }');

test.js

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import stripCssComments from '.';
2+
import stripCssComments from './index.js';
33

44
test('main', t => {
55
t.is(stripCssComments('/*//comment*/body{}'), 'body{}');
@@ -27,22 +27,6 @@ test('main', t => {
2727
t.is(stripCssComments('body{/*!"\'"\'*/}'), 'body{/*!"\'"\'*/}');
2828
});
2929

30-
test('`all` option', t => {
31-
t.is(stripCssComments('/*!//comment*/body{}', {all: true}), 'body{}');
32-
t.is(stripCssComments('/*!//comment*/body{}', {all: true}), 'body{}');
33-
t.is(stripCssComments('/*!//"comment*/body{}', {all: true}), 'body{}');
34-
t.is(stripCssComments('/*!//\'comment*/body{}', {all: true}), 'body{}');
35-
t.is(stripCssComments('body{/*!comment*/}', {all: true}), 'body{}');
36-
t.is(stripCssComments('body{/*!\ncomment\n\\*/}', {all: true}), 'body{}');
37-
t.is(stripCssComments('body{content: "\'/*!ad*/\' \\""}', {all: true}), 'body{content: "\'/*!ad*/\' \\""}');
38-
t.is(stripCssComments('body{\r\n /*!\n\n\n\nfoo*/\n}', {all: true}), 'body{\r\n \n}');
39-
t.is(stripCssComments('body/*!foo*/{}', {all: true}), 'body{}');
40-
t.is(stripCssComments('body{/*!"*/}/*foo*/', {all: true}), 'body{}');
41-
t.is(stripCssComments('body{/*!\'*/}/*foo*/', {all: true}), 'body{}');
42-
t.is(stripCssComments('body{/*!"\'\\"*/}', {all: true}), 'body{}');
43-
t.is(stripCssComments('body{/*!"\'"\'*/}', {all: true}), 'body{}');
44-
});
45-
4630
test('`preserve` option', t => {
4731
t.is(stripCssComments('/*!//comment*/body{}', {preserve: false}), 'body{}');
4832
t.is(stripCssComments('/*!//"comment*/body{}', {preserve: false}), 'body{}');
@@ -66,39 +50,39 @@ test('`preserve` option', t => {
6650

6751
t.is(
6852
stripCssComments('body{/*##foo##*/}', {
69-
preserve: comment => comment.startsWith('##foo##')
70-
}), 'body{/*##foo##*/}'
53+
preserve: comment => comment.startsWith('##foo##'),
54+
}), 'body{/*##foo##*/}',
7155
);
7256

7357
t.is(
7458
stripCssComments('body{/*foo*/}', {
75-
preserve: comment => comment.startsWith('##foo##')
76-
}), 'body{}'
59+
preserve: comment => comment.startsWith('##foo##'),
60+
}), 'body{}',
7761
);
7862

7963
t.is(
8064
stripCssComments('body{/*##foo##*//*foo*/}', {
81-
preserve: comment => comment.startsWith('##foo##')
82-
}), 'body{/*##foo##*/}'
65+
preserve: comment => comment.startsWith('##foo##'),
66+
}), 'body{/*##foo##*/}',
8367
);
8468

8569
t.is(
8670
stripCssComments('body{/*##foo##*//*!foo*/}', {
87-
preserve: comment => comment.startsWith('##foo##')
88-
}), 'body{/*##foo##*/}'
71+
preserve: comment => comment.startsWith('##foo##'),
72+
}), 'body{/*##foo##*/}',
8973
);
9074

9175
t.is(
9276
stripCssComments('body{/*!##foo##*//*foo*/}', {
93-
preserve: comment => comment.startsWith('##foo##')
94-
}), 'body{}'
77+
preserve: comment => comment.startsWith('##foo##'),
78+
}), 'body{}',
9579
);
9680

9781
t.is(
9882
stripCssComments('body{/*!##foo*//*foo*/}', {
99-
preserve: comment => comment.endsWith('foo')
83+
preserve: comment => comment.endsWith('foo'),
10084
}),
101-
'body{/*!##foo*//*foo*/}'
85+
'body{/*!##foo*//*foo*/}',
10286
);
10387
});
10488

@@ -113,15 +97,15 @@ test('`whitespace` option', t => {
11397

11498
t.is(
11599
stripCssComments('body{/*!##foo*/\n/*foo*/}', {
116-
preserve: comment => comment.endsWith('foo'), whitespace: false
100+
preserve: comment => comment.endsWith('foo'), whitespace: false,
117101
}),
118-
'body{/*!##foo*/\n/*foo*/}'
102+
'body{/*!##foo*/\n/*foo*/}',
119103
);
120104

121105
t.is(
122106
stripCssComments('body{/*!##foo*/\r\n/*foo*/}', {
123-
preserve: comment => comment.endsWith('foo'), whitespace: false
107+
preserve: comment => comment.endsWith('foo'), whitespace: false,
124108
}),
125-
'body{/*!##foo*/\r\n/*foo*/}'
109+
'body{/*!##foo*/\r\n/*foo*/}',
126110
);
127111
});

0 commit comments

Comments
 (0)