Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66cef58

Browse files
committed
Require Node.js 18 and move to ESM
1 parent 682984e commit 66cef58

File tree

5 files changed

+94
-111
lines changed

5 files changed

+94
-111
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 16
14-
- 14
15-
- 12
13+
- 20
14+
- 18
1615
steps:
17-
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v2
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-node@v4
1918
with:
2019
node-version: ${{ matrix.node-version }}
2120
- run: npm install

index.js

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,40 @@
1-
'use strict';
2-
const fancyLog = require('fancy-log');
3-
const PluginError = require('plugin-error');
4-
const through = require('through2');
5-
const applySourceMap = require('vinyl-sourcemaps-apply');
6-
const autoprefixer = require('autoprefixer');
7-
const postcss = require('postcss');
8-
9-
module.exports = options => {
10-
return through.obj((file, encoding, callback) => {
11-
if (file.isNull()) {
12-
callback(null, file);
13-
return;
14-
}
15-
16-
if (file.isStream()) {
17-
callback(new PluginError('gulp-autoprefixer', 'Streaming not supported'));
18-
return;
19-
}
20-
21-
(async () => {
22-
try {
23-
const result = await postcss(autoprefixer(options)).process(file.contents.toString(), {
24-
map: file.sourceMap ? {annotation: false} : false,
25-
from: file.path,
26-
to: file.path
27-
});
28-
29-
file.contents = Buffer.from(result.css);
30-
31-
if (result.map && file.sourceMap) {
32-
const map = result.map.toJSON();
33-
map.file = file.relative;
34-
map.sources = map.sources.map(() => file.relative);
35-
applySourceMap(file, map);
36-
}
37-
38-
const warnings = result.warnings();
39-
40-
if (warnings.length > 0) {
41-
fancyLog('gulp-autoprefixer:', '\n ' + warnings.join('\n '));
42-
}
43-
44-
setImmediate(callback, null, file);
45-
} catch (error) {
46-
const cssError = error.name === 'CssSyntaxError';
1+
import {Buffer} from 'node:buffer';
2+
import applySourceMap from 'vinyl-sourcemaps-apply';
3+
import autoprefixer from 'autoprefixer';
4+
import postcss from 'postcss';
5+
import {gulpPlugin} from 'gulp-plugin-extras';
6+
7+
export default function gulpAutoprefixer(options) {
8+
return gulpPlugin('gulp-autoprefixer', async file => {
9+
try {
10+
const result = await postcss(autoprefixer(options)).process(file.contents.toString(), {
11+
map: file.sourceMap ? {annotation: false} : false,
12+
from: file.path,
13+
to: file.path,
14+
});
15+
16+
file.contents = Buffer.from(result.css);
17+
18+
if (result.map && file.sourceMap) {
19+
const map = result.map.toJSON();
20+
map.file = file.relative;
21+
map.sources = map.sources.map(() => file.relative);
22+
applySourceMap(file, map);
23+
}
4724

48-
if (cssError) {
49-
error.message += error.showSourceCode();
50-
}
25+
const warnings = result.warnings();
26+
if (warnings.length > 0) {
27+
console.log('gulp-autoprefixer:', '\n ' + warnings.join('\n '));
28+
}
5129

52-
// Prevent stream unhandled exception from being suppressed by Promise
53-
setImmediate(callback, new PluginError('gulp-autoprefixer', error, {
54-
fileName: file.path,
55-
showStack: !cssError
56-
}));
30+
return file;
31+
} catch (error) {
32+
if (error.name === 'CssSyntaxError') {
33+
error.message += error.showSourceCode();
34+
error.isPresentable = true;
5735
}
58-
})();
36+
37+
throw error;
38+
}
5939
});
60-
};
40+
}

package.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Prefix CSS",
55
"license": "MIT",
66
"repository": "sindresorhus/gulp-autoprefixer",
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": ">=12"
16+
"node": ">=18"
1417
},
1518
"scripts": {
1619
"test": "xo && ava"
@@ -30,19 +33,17 @@
3033
"postcss-runner"
3134
],
3235
"dependencies": {
33-
"autoprefixer": "^10.2.6",
34-
"fancy-log": "^1.3.3",
35-
"plugin-error": "^1.0.1",
36-
"postcss": "^8.3.0",
37-
"through2": "^4.0.2",
36+
"autoprefixer": "^10.4.16",
37+
"gulp-plugin-extras": "^0.2.2",
38+
"postcss": "^8.4.31",
3839
"vinyl-sourcemaps-apply": "^0.2.1"
3940
},
4041
"devDependencies": {
41-
"ava": "^2.4.0",
42+
"ava": "^5.3.1",
4243
"gulp-sourcemaps": "^3.0.0",
43-
"p-event": "^4.2.0",
44-
"vinyl": "^2.2.1",
45-
"xo": "^0.39.0"
44+
"p-event": "^6.0.0",
45+
"vinyl": "^3.0.0",
46+
"xo": "^0.56.0"
4647
},
4748
"peerDependencies": {
4849
"gulp": ">=4"

readme.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77
## Install
88

9-
```
10-
$ npm install --save-dev gulp-autoprefixer
9+
```sh
10+
npm install --save-dev gulp-autoprefixer
1111
```
1212

1313
## Usage
1414

1515
```js
16-
const gulp = require('gulp');
17-
const autoprefixer = require('gulp-autoprefixer');
16+
import gulp from 'gulp';
17+
import autoprefixer from 'gulp-autoprefixer';
1818

19-
exports.default = () => (
19+
export default () => (
2020
gulp.src('src/app.css')
2121
.pipe(autoprefixer({
2222
cascade: false
@@ -40,12 +40,12 @@ See the Autoprefixer [options](https://github.com/postcss/autoprefixer#options).
4040
Use [gulp-sourcemaps](https://github.com/gulp-sourcemaps/gulp-sourcemaps) like this:
4141

4242
```js
43-
const gulp = require('gulp');
44-
const sourcemaps = require('gulp-sourcemaps');
45-
const autoprefixer = require('gulp-autoprefixer');
46-
const concat = require('gulp-concat');
43+
import gulp from 'gulp';
44+
import sourcemaps from 'gulp-sourcemaps';
45+
import concat from 'gulp-concat';
46+
import autoprefixer from 'gulp-autoprefixer';
4747

48-
exports.default = () => (
48+
export default () => (
4949
gulp.src('src/**/*.css')
5050
.pipe(sourcemaps.init())
5151
.pipe(autoprefixer())

test.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import path from 'path';
1+
import {Buffer} from 'node:buffer';
2+
import path from 'node:path';
3+
import {fileURLToPath} from 'node:url';
24
import test from 'ava';
35
import Vinyl from 'vinyl';
46
import sourceMaps from 'gulp-sourcemaps';
5-
import pEvent from 'p-event';
6-
import autoprefixer from '.';
7+
import {pEvent} from 'p-event';
8+
import autoprefixer from './index.js';
9+
10+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
711

812
test('autoprefix CSS', async t => {
913
const stream = autoprefixer();
@@ -13,7 +17,7 @@ test('autoprefix CSS', async t => {
1317
cwd: __dirname,
1418
base: path.join(__dirname, 'fixture'),
1519
path: path.join(__dirname, 'fixture', 'fixture.css'),
16-
contents: Buffer.from('::placeholder {\n\tcolor: gray;\n}')
20+
contents: Buffer.from('::placeholder {\n\tcolor: gray;\n}'),
1721
}));
1822

1923
const file = await data;
@@ -28,7 +32,7 @@ test('generate source maps', async t => {
2832

2933
init
3034
.pipe(autoprefixer({
31-
overrideBrowserslist: ['Firefox ESR']
35+
overrideBrowserslist: ['Firefox ESR'],
3236
}))
3337
.pipe(write);
3438

@@ -37,7 +41,7 @@ test('generate source maps', async t => {
3741
base: path.join(__dirname, 'fixture'),
3842
path: path.join(__dirname, 'fixture', 'fixture.css'),
3943
contents: Buffer.from('a {\n\tdisplay: flex;\n}'),
40-
sourceMap: ''
44+
sourceMap: '',
4145
}));
4246

4347
const file = await data;
@@ -48,36 +52,35 @@ test('generate source maps', async t => {
4852
});
4953

5054
test('read upstream source maps', async t => {
51-
let testFile;
5255
const stream = autoprefixer();
53-
const write = sourceMaps.write();
54-
const sourcesContent = [
55-
'a {\n display: flex;\n}\n',
56-
'a {\n\tdisplay: flex;\n}\n'
57-
];
56+
const finalStream = stream.pipe(sourceMaps.write());
57+
const data = pEvent(finalStream, 'data');
5858

59-
const data = pEvent(write, 'data');
59+
const testFile = new Vinyl({
60+
cwd: __dirname,
61+
base: path.join(__dirname, 'fixture'),
62+
path: path.join(__dirname, 'fixture', 'fixture.css'),
63+
contents: Buffer.from('a {\n\tdisplay: flex;\n}\n'),
64+
});
6065

61-
stream.pipe(write);
66+
testFile.sourceMap = {
67+
version: 3,
68+
sources: ['imported.less'],
69+
names: [],
70+
mappings: 'AAAA;EACC,aAAA',
71+
file: 'fixture.css',
72+
sourcesContent: ['a {\n display: flex;\n}\n'],
73+
};
6274

63-
stream.end(
64-
testFile = new Vinyl({
65-
cwd: __dirname,
66-
base: path.join(__dirname, 'fixture'),
67-
path: path.join(__dirname, 'fixture', 'fixture.css'),
68-
contents: Buffer.from('a {\n\tdisplay: flex;\n}\n')
69-
}),
70-
testFile.sourceMap = {
71-
version: 3,
72-
sources: ['imported.less'],
73-
names: [],
74-
mappings: 'AAAA;EACC,aAAA',
75-
file: 'fixture.css',
76-
sourcesContent: ['a {\n display: flex;\n}\n']
77-
}
78-
);
75+
stream.end(testFile);
7976

8077
const file = await data;
78+
79+
const sourcesContent = [
80+
'a {\n display: flex;\n}\n',
81+
'a {\n\tdisplay: flex;\n}\n',
82+
];
83+
8184
t.is(file.sourceMap.sourcesContent[0], sourcesContent[0]);
8285
t.is(file.sourceMap.sourcesContent[1], sourcesContent[1]);
8386
});

0 commit comments

Comments
 (0)