-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
682984e
commit 66cef58
Showing
5 changed files
with
94 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,40 @@ | ||
'use strict'; | ||
const fancyLog = require('fancy-log'); | ||
const PluginError = require('plugin-error'); | ||
const through = require('through2'); | ||
const applySourceMap = require('vinyl-sourcemaps-apply'); | ||
const autoprefixer = require('autoprefixer'); | ||
const postcss = require('postcss'); | ||
|
||
module.exports = options => { | ||
return through.obj((file, encoding, callback) => { | ||
if (file.isNull()) { | ||
callback(null, file); | ||
return; | ||
} | ||
|
||
if (file.isStream()) { | ||
callback(new PluginError('gulp-autoprefixer', 'Streaming not supported')); | ||
return; | ||
} | ||
|
||
(async () => { | ||
try { | ||
const result = await postcss(autoprefixer(options)).process(file.contents.toString(), { | ||
map: file.sourceMap ? {annotation: false} : false, | ||
from: file.path, | ||
to: file.path | ||
}); | ||
|
||
file.contents = Buffer.from(result.css); | ||
|
||
if (result.map && file.sourceMap) { | ||
const map = result.map.toJSON(); | ||
map.file = file.relative; | ||
map.sources = map.sources.map(() => file.relative); | ||
applySourceMap(file, map); | ||
} | ||
|
||
const warnings = result.warnings(); | ||
|
||
if (warnings.length > 0) { | ||
fancyLog('gulp-autoprefixer:', '\n ' + warnings.join('\n ')); | ||
} | ||
|
||
setImmediate(callback, null, file); | ||
} catch (error) { | ||
const cssError = error.name === 'CssSyntaxError'; | ||
import {Buffer} from 'node:buffer'; | ||
import applySourceMap from 'vinyl-sourcemaps-apply'; | ||
import autoprefixer from 'autoprefixer'; | ||
import postcss from 'postcss'; | ||
import {gulpPlugin} from 'gulp-plugin-extras'; | ||
|
||
export default function gulpAutoprefixer(options) { | ||
return gulpPlugin('gulp-autoprefixer', async file => { | ||
try { | ||
const result = await postcss(autoprefixer(options)).process(file.contents.toString(), { | ||
map: file.sourceMap ? {annotation: false} : false, | ||
from: file.path, | ||
to: file.path, | ||
}); | ||
|
||
file.contents = Buffer.from(result.css); | ||
|
||
if (result.map && file.sourceMap) { | ||
const map = result.map.toJSON(); | ||
map.file = file.relative; | ||
map.sources = map.sources.map(() => file.relative); | ||
applySourceMap(file, map); | ||
} | ||
|
||
if (cssError) { | ||
error.message += error.showSourceCode(); | ||
} | ||
const warnings = result.warnings(); | ||
if (warnings.length > 0) { | ||
console.log('gulp-autoprefixer:', '\n ' + warnings.join('\n ')); | ||
} | ||
|
||
// Prevent stream unhandled exception from being suppressed by Promise | ||
setImmediate(callback, new PluginError('gulp-autoprefixer', error, { | ||
fileName: file.path, | ||
showStack: !cssError | ||
})); | ||
return file; | ||
} catch (error) { | ||
if (error.name === 'CssSyntaxError') { | ||
error.message += error.showSourceCode(); | ||
error.isPresentable = true; | ||
} | ||
})(); | ||
|
||
throw error; | ||
} | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters