This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Showing
12 changed files
with
221 additions
and
196 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,4 @@ | ||
{ | ||
"extends": "eslint:recommended", | ||
"env": { | ||
"node": true | ||
}, | ||
"rules": { | ||
"indent": [2, 2], | ||
"linebreak-style": [2, "unix"], | ||
"no-console": [0], | ||
"quotes": [2, "single"], | ||
"semi": [2, "always"], | ||
"space-before-blocks": [2, "always"], | ||
"space-before-function-paren": [2, { | ||
"anonymous": "always", | ||
"named": "never" | ||
}], | ||
"space-before-keywords": [2, "always"] | ||
}, | ||
"extends": "airbnb/legacy", | ||
"root": true | ||
} |
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,82 +1,94 @@ | ||
var Assets = require('assets'); | ||
var dirname = require('path').dirname; | ||
var functions = require('postcss-functions'); | ||
var path = require('path'); | ||
var postcss = require('postcss'); | ||
var quote = require('./lib/quote'); | ||
var unescapeCss = require('./lib/unescape-css'); | ||
var unquote = require('./lib/unquote'); | ||
var util = require('util'); | ||
|
||
module.exports = postcss.plugin('postcss-assets', function (options) { | ||
options = options || {}; | ||
function formatUrl(url) { | ||
return util.format('url(%s)', quote(url)); | ||
} | ||
|
||
if (options.relative === undefined) { | ||
options.relative = false; | ||
function formatSize(measurements) { | ||
return util.format('%dpx %dpx', measurements.width, measurements.height); | ||
} | ||
|
||
function formatWidth(measurements) { | ||
return util.format('%dpx', measurements.width); | ||
} | ||
|
||
function formatHeight(measurements) { | ||
return util.format('%dpx', measurements.height); | ||
} | ||
|
||
function plugin(options) { | ||
var params = options || {}; | ||
var resolver; | ||
|
||
if (params.relative === undefined) { | ||
params.relative = false; | ||
} | ||
|
||
var resolver = Assets(options); | ||
resolver = new Assets(options); | ||
|
||
function measure(path, density) { | ||
return resolver.size(path) | ||
.then(function (size) { | ||
.then(function correctDensity(size) { | ||
if (density !== undefined) { | ||
size.width = Number((size.width / density).toFixed(4)); | ||
size.height = Number((size.height / density).toFixed(4)); | ||
return { | ||
width: Number((size.width / density).toFixed(4)), | ||
height: Number((size.height / density).toFixed(4)) | ||
}; | ||
} | ||
return size; | ||
}); | ||
} | ||
|
||
return postcss() | ||
.use(function (css) { | ||
.use(function appendInputDir(css) { | ||
var inputDir; | ||
|
||
if (css.source.input.file) { | ||
var inputDir = path.dirname(css.source.input.file); | ||
inputDir = dirname(css.source.input.file); | ||
|
||
resolver.options.loadPaths = resolver.options.loadPaths || []; | ||
resolver.options.loadPaths.unshift(inputDir); | ||
|
||
if (options.relative) { | ||
if (params.relative === true) { | ||
resolver.options.relativeTo = inputDir; | ||
} | ||
} | ||
|
||
if (typeof params.relative === 'string') { | ||
resolver.options.relativeTo = params.relative; | ||
} | ||
}) | ||
.use(functions({ | ||
functions: { | ||
resolve: function (path) { | ||
path = unquote(unescapeCss(path)); | ||
return resolver.url(path) | ||
.then(function (url) { | ||
return util.format('url(%s)', quote(url)); | ||
}); | ||
resolve: function resolve(path) { | ||
var normalizedPath = unquote(unescapeCss(path)); | ||
return resolver.url(normalizedPath).then(formatUrl); | ||
}, | ||
inline: function (path) { | ||
path = unquote(unescapeCss(path)); | ||
return resolver.data(path) | ||
.then(function (data) { | ||
return util.format('url(%s)', quote(data)); | ||
}); | ||
inline: function inline(path) { | ||
var normalizedPath = unquote(unescapeCss(path)); | ||
return resolver.data(normalizedPath).then(formatUrl); | ||
}, | ||
size: function (path, density) { | ||
path = unquote(unescapeCss(path)); | ||
return measure(path, density) | ||
.then(function (size) { | ||
return util.format('%dpx %dpx', size.width, size.height); | ||
}); | ||
size: function size(path, density) { | ||
var normalizedPath = unquote(unescapeCss(path)); | ||
return measure(normalizedPath, density).then(formatSize); | ||
}, | ||
width: function (path, density) { | ||
path = unquote(unescapeCss(path)); | ||
return measure(path, density) | ||
.then(function (size) { | ||
return util.format('%dpx', size.width); | ||
}); | ||
width: function width(path, density) { | ||
var normalizedPath = unquote(unescapeCss(path)); | ||
return measure(normalizedPath, density).then(formatWidth); | ||
}, | ||
height: function (path, density) { | ||
path = unquote(unescapeCss(path)); | ||
return measure(path, density) | ||
.then(function (size) { | ||
return util.format('%dpx', size.height); | ||
}); | ||
height: function height(path, density) { | ||
var normalizedPath = unquote(unescapeCss(path)); | ||
return measure(normalizedPath, density).then(formatHeight); | ||
} | ||
} | ||
})); | ||
}); | ||
} | ||
|
||
module.exports = postcss.plugin('postcss-assets', plugin); |
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,10 +1,12 @@ | ||
var R_ESCAPE = /\\(?:([0-9a-f]{1,6} ?)|(.))/gi; | ||
|
||
module.exports = function (string) { | ||
return string.replace(R_ESCAPE, function (match, hex, char) { | ||
if (hex) { | ||
return String.fromCharCode(parseInt(hex, 16)); | ||
} | ||
return char; | ||
}); | ||
function unescapeSequence(match, hex, char) { | ||
if (hex) { | ||
return String.fromCharCode(parseInt(hex, 16)); | ||
} | ||
return char; | ||
} | ||
|
||
module.exports = function unescapeCss(string) { | ||
return string.replace(R_ESCAPE, unescapeSequence); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "airbnb/base" | ||
} |
Oops, something went wrong.