Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #87 from onigoetz/develop
Browse files Browse the repository at this point in the history
Update to PostCSS 8
  • Loading branch information
borodean committed May 13, 2021
2 parents 29b1b0f + 7aa58a0 commit 08c5016
Show file tree
Hide file tree
Showing 13 changed files with 5,211 additions and 255 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "airbnb-base/legacy",
"extends": "airbnb-base",
"root": true
}
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sudo: false
language: node_js
node_js:
- stable
- "6"
- "4"
- 14
- 12
- 10
after_success: npm run coveralls
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
environment:
matrix:
- nodejs_version: '' # is latest stable
- nodejs_version: 6
- nodejs_version: 4
- nodejs_version: 12
- nodejs_version: 10

# Install scripts (runs after repo cloning)
install:
Expand Down
4 changes: 2 additions & 2 deletions lib/__utils__/generateFileUniqueId.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require('fs');
const fs = require('fs');

module.exports = function generateFileUniqueId(resolvedPath) {
var mtime = fs.statSync(resolvedPath).mtime;
const { mtime } = fs.statSync(resolvedPath);
return mtime.getTime().toString(16);
};
104 changes: 51 additions & 53 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
var Assets = require('assets');
var dirname = require('path').dirname;
var functions = require('postcss-functions');
var postcss = require('postcss');
var quote = require('./quote');
var unescapeCss = require('./unescape-css');
var unquote = require('./unquote');
var util = require('util');
var Promise = require('bluebird');
var generateFileUniqueId = require('./__utils__/generateFileUniqueId');

var cachedDimensions = {};
const Assets = require('assets');
const { dirname } = require('path');
const functions = require('postcss-functions');
const util = require('util');
const quote = require('./quote');
const unescapeCss = require('./unescape-css');
const unquote = require('./unquote');
const generateFileUniqueId = require('./__utils__/generateFileUniqueId');

const cachedDimensions = {};

function formatUrl(url) {
return util.format('url(%s)', quote(url));
Expand All @@ -27,22 +25,19 @@ function formatHeight(measurements) {
return util.format('%dpx', measurements.height);
}

function plugin(options) {
var params = options || {};
var resolver;

module.exports = (params = {}) => {
if (params.relative === undefined) {
params.relative = false;
params.relative = false; // eslint-disable-line no-param-reassign
}

resolver = new Assets(options);
const resolver = new Assets(params);

function measure(path, density) {
var cached = null;
var id = '';
var getSizePromise = null;
let cached = null;
let id = '';
let getSizePromise = null;

return resolver.path(path).then(function measureSize(resolvedPath) {
return resolver.path(path).then((resolvedPath) => {
if (params.cache) {
cached = cachedDimensions[resolvedPath];
id = generateFileUniqueId(resolvedPath);
Expand All @@ -51,7 +46,7 @@ function plugin(options) {
if (cached && id && cached[id]) {
getSizePromise = Promise.resolve(cached[id]);
} else {
getSizePromise = resolver.size(path).then(function cacheSize(size) {
getSizePromise = resolver.size(path).then((size) => {
if (params.cache && id) {
cachedDimensions[resolvedPath] = {};
cachedDimensions[resolvedPath][id] = size;
Expand All @@ -60,61 +55,64 @@ function plugin(options) {
});
}

return getSizePromise.then(function correctDensity(size) {
return getSizePromise.then((size) => {
if (density !== undefined) {
return {
width: Number((size.width / density).toFixed(4)),
height: Number((size.height / density).toFixed(4))
height: Number((size.height / density).toFixed(4)),
};
}
return size;
});
});
}

return postcss()
.use(function appendInputDir(css) {
var inputDir;

if (css.source.input.file) {
inputDir = dirname(css.source.input.file);

resolver.options.loadPaths = resolver.options.loadPaths || [];
resolver.options.loadPaths.unshift(inputDir);

if (params.relative === true) {
resolver.options.relativeTo = inputDir;
}
}

if (typeof params.relative === 'string') {
resolver.options.relativeTo = params.relative;
}
})
.use(functions({
return {
// Initialize functions plugin as if it was this plugin
...functions({
functions: {
resolve: function resolve(path) {
var normalizedPath = unquote(unescapeCss(path));
const normalizedPath = unquote(unescapeCss(path));
return resolver.url(normalizedPath).then(formatUrl);
},
inline: function inline(path) {
var normalizedPath = unquote(unescapeCss(path));
const normalizedPath = unquote(unescapeCss(path));
return resolver.data(normalizedPath).then(formatUrl);
},
size: function size(path, density) {
var normalizedPath = unquote(unescapeCss(path));
const normalizedPath = unquote(unescapeCss(path));
return measure(normalizedPath, density).then(formatSize);
},
width: function width(path, density) {
var normalizedPath = unquote(unescapeCss(path));
const normalizedPath = unquote(unescapeCss(path));
return measure(normalizedPath, density).then(formatWidth);
},
height: function height(path, density) {
var normalizedPath = unquote(unescapeCss(path));
const normalizedPath = unquote(unescapeCss(path));
return measure(normalizedPath, density).then(formatHeight);
},
},
}),
// Override with our own features and name
postcssPlugin: 'postcss-assets',
Once(root) {
let inputDir;
if (root.source.input.file) {
inputDir = dirname(root.source.input.file);

resolver.options.loadPaths = resolver.options.loadPaths || [];
resolver.options.loadPaths.unshift(inputDir);

if (params.relative === true) {
resolver.options.relativeTo = inputDir;
}
}
}));
}

module.exports = postcss.plugin('postcss-assets', plugin);
if (typeof params.relative === 'string') {
resolver.options.relativeTo = params.relative;
}
},
};
};

module.exports.postcss = true;
6 changes: 3 additions & 3 deletions lib/quote.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* eslint quotes: 0 */

var util = require('util');
const util = require('util');

var R_QUOTES = /'/g;
const R_QUOTES = /'/g;

function escapeQuote(match, offset, string) {
if (string[offset - 1] === '\\') {
return match;
}
return '\\' + match;
return `\\${match}`;
}

module.exports = function quote(string) {
Expand Down
2 changes: 1 addition & 1 deletion lib/unescape-css.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var R_ESCAPE = /\\(?:([0-9a-f]{1,6} ?)|(.))/gi;
const R_ESCAPE = /\\(?:([0-9a-f]{1,6} ?)|(.))/gi;

function unescapeSequence(match, hex, char) {
if (hex) {
Expand Down
Loading

0 comments on commit 08c5016

Please sign in to comment.