Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Mamaev committed Apr 2, 2019
1 parent 9851cbf commit 20f0708
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 143 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -19,10 +19,10 @@ module.exports = postcss.plugin('postcss-url', (options) => {
const to = opts.to ? path.dirname(opts.to) : from;

styles.walkDecls((decl) =>
promises.push(declProcessor(from, to, options, result, decl))
promises.push(declProcessor(from, to, options, result, decl))
);

return Promise.all(promises);
return Promise.all(promises);
};
});

Expand Down
1 change: 1 addition & 0 deletions src/lib/decl-processor.js
Expand Up @@ -96,6 +96,7 @@ const replaceUrl = (url, dir, options, result, decl) => {
};

const resultPromise;

if (Array.isArray(matchedOptions)) {
resultPromise = Promise.resolve();
matchedOptions.forEach((option) => {
Expand Down
77 changes: 39 additions & 38 deletions src/lib/get-file.js
Expand Up @@ -6,38 +6,38 @@ const mime = require('mime');
const getPathByBasePath = require('./paths').getPathByBasePath;

const readFileAsync = (filePath) => {
return new Promise((resolse, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) {
reject(err);
}
resolve(data);
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) {
reject(err);
}
resolve(data);
});
});
});
};

const existFileAsync = (filePath) => {
new Promise((resolve, reject) =>
fs.access(filePath, (err) => {
if (err) {
reject();
}
resolve(path);
})
)
new Promise((resolve, reject) =>
fs.access(filePath, (err) => {
if (err) {
reject();
}
resolve(filePath);
})
);
};

const oneSuccess = (promises) => {
return Promise.all(promises.map(p => {
return p.then(
val => Promise.reject(val),
err => Promise.resolve(err)
return Promise.all(promises.map((p) => {
return p.then(
(val) => Promise.reject(val),
(err) => Promise.resolve(err)
);
})).then(
(errors) => Promise.reject(errors),
(val) => Promise.resolve(val)
);
})).then(
errors => Promise.reject(errors),
val => Promise.resolve(val)
);
}
};

/**
*
Expand All @@ -48,21 +48,22 @@ const oneSuccess = (promises) => {
* @returns {Promise<PostcssUrl~File | Undefined>}
*/
const getFile = (asset, options, dir, warn) => {
const paths = options.basePath ?
getPathByBasePath(options.basePath, dir.from, asset.pathname) :
[asset.absolutePath];
const paths = options.basePath
? getPathByBasePath(options.basePath, dir.from, asset.pathname)
: [asset.absolutePath];

return oneSuccess(paths.map(path => existFileAsync(path)))
.then(path => readFileAsync(path))
.then(contents => ({
path: filePath,
contents: contents,
mimeType: mime.getType(filePath)
}))
.catch(() => {
warn(`Can't read file '${paths.join()}', ignoring`);
return;
});
return oneSuccess(paths.map((path) => existFileAsync(path)))
.then((path) => readFileAsync(path)
.then((contents) => ({
path,
contents,
mimeType: mime.getType(path)
})))
.catch(() => {
warn(`Can't read file '${paths.join()}', ignoring`);

return;
});
};

module.exports = getFile;
Expand Down
117 changes: 58 additions & 59 deletions src/type/copy.js
Expand Up @@ -13,45 +13,45 @@ const getAssetsPath = paths.getAssetsPath;
const normalize = paths.normalize;

const getHashName = (file, options) =>
(options && options.append ? (`${path.basename(file.path, path.extname(file.path))}_`) : '') +
calcHash(file.contents, options) +
path.extname(file.path);

const createDirAsync = (path) => {
return Promise((resolve, reject) => {
mkdirp(path, (err) => {
if (err) reject(err);
resolve();
})
})
(options && options.append ? (`${path.basename(file.path, path.extname(file.path))}_`) : '')
+ calcHash(file.contents, options)
+ path.extname(file.path);

const createDirAsync = (dirPath) => {
return new Promise((resolve, reject) => {
mkdirp(dirPath, (err) => {
if (err) reject(err);
resolve();
});
});
};

const writeFileAsync = (file, src) => {
return new Promise((resolve, reject) => {
fs.open(dest, 'wx', (err, fd) => {
if (err) {
if (err.code === 'EEXIST') {
resolve();
}
const writeFileAsync = (file, dest) => {
return new Promise((resolve, reject) => {
fs.open(dest, 'wx', (err, fd) => {
if (err) {
if (err.code === 'EEXIST') {
resolve();
}

reject(err);
}
reject(err);
}

resolve(fd);
})
resolve(fd);
});
})
.then(fd => {
if (!fd) return;

return new Promise((resolve, reject) => {
fs.writeFile(newAssetPath, file.contents, (err) => {
if (err) {
reject(err);
}
resolve();
.then((fd) => {
if (!fd) return;

return new Promise((resolve, reject) => {
fs.writeFile(dest, file.contents, (err) => {
if (err) {
reject(err);
}
resolve();
});
});
});
})
});
};

/**
Expand All @@ -74,31 +74,30 @@ const writeFileAsync = (file, src) => {
*/

module.exports = function processCopy(asset, dir, options, decl, warn, addDependency) {
if (!options.assetsPath && dir.from === dir.to) {
warn('Option `to` of postcss is required, ignoring');

return Promise.resolve();
}

const newRelativeAssetPath = generateNewPath(file, dir, options);

return getFile(asset, options, dir, warn)
.then(file => {
const assetRelativePath = options.useHash ?
getHashName(file, options.hashOptions) :
asset.relativePath;

const targetDir = getTargetDir(dir);
const newAssetBaseDir = getAssetsPath(targetDir, options.assetsPath);
const newAssetPath = path.join(newAssetBaseDir, assetRelativePath);
const newRelativeAssetPath = normalize(path.relative(targetDir, newAssetPath));

return createDirAsync(path.dirname(newAssetPath))
.then(() => writeFileAsync(file, newAssetPath))
.then(() => {
addDependency(file.path);
return `${newRelativeAssetPath}${asset.search}${asset.hash}`;
});
if (!options.assetsPath && dir.from === dir.to) {
warn('Option `to` of postcss is required, ignoring');

return Promise.resolve();
}

return getFile(asset, options, dir, warn)
.then((file) => {
const assetRelativePath = options.useHash
? getHashName(file, options.hashOptions)
: asset.relativePath;

const targetDir = getTargetDir(dir);
const newAssetBaseDir = getAssetsPath(targetDir, options.assetsPath);
const newAssetPath = path.join(newAssetBaseDir, assetRelativePath);
const newRelativeAssetPath = normalize(path.relative(targetDir, newAssetPath));

return createDirAsync(path.dirname(newAssetPath))
.then(() => writeFileAsync(file, newAssetPath))
.then(() => {
addDependency(file.path);

return `${newRelativeAssetPath}${asset.search}${asset.hash}`;
});
}
);
);
};
77 changes: 33 additions & 44 deletions src/type/inline.js
@@ -1,7 +1,5 @@
'use strict';

const fs = require('fs');

const processCopy = require('./copy');
const processRebase = require('./rebase');

Expand All @@ -27,32 +25,31 @@ const processFallback = (originUrl, dir, options) => {
default:
return Promise.resolve();
}
}
};

const inlineProcess = (file, asset, warn, options) => {
const isSvg = file.mimeType === 'image/svg+xml';
const defaultEncodeType = isSvg ? 'encodeURIComponent' : 'base64';
const encodeType = options.encodeType || defaultEncodeType;
const inlineProcess = (file, asset, warn, addDependency, options) => {
const isSvg = file.mimeType === 'image/svg+xml';
const defaultEncodeType = isSvg ? 'encodeURIComponent' : 'base64';
const encodeType = options.encodeType || defaultEncodeType;

// Warn for svg with hashes/fragments
if (isSvg && asset.hash && !options.ignoreFragmentWarning) {
// eslint-disable-next-line max-len
warn(`Image type is svg and link contains #. Postcss-url cant handle svg fragments. SVG file fully inlined. ${file.path}`);
}
// Warn for svg with hashes/fragments
if (isSvg && asset.hash && !options.ignoreFragmentWarning) {
// eslint-disable-next-line max-len
warn(`Image type is svg and link contains #. Postcss-url cant handle svg fragments. SVG file fully inlined. ${file.path}`);
}

addDependency(file.path);
addDependency(file.path);

const optimizeSvgEncode = isSvg && options.optimizeSvgEncode;
const encodedStr = encodeFile(file, encodeType, optimizeSvgEncode);
const resultValue = options.includeUriFragment && asset.hash
? encodedStr + asset.hash
: encodedStr;
const optimizeSvgEncode = isSvg && options.optimizeSvgEncode;
const encodedStr = encodeFile(file, encodeType, optimizeSvgEncode);
const resultValue = options.includeUriFragment && asset.hash
? encodedStr + asset.hash
: encodedStr;

// wrap url by quotes if percent-encoded svg
return isSvg && encodeType !== 'base64' ? `"${resultValue}"` : resultValue;
// wrap url by quotes if percent-encoded svg
return isSvg && encodeType !== 'base64' ? `"${resultValue}"` : resultValue;
};


/**
* Inline image in url()
*
Expand All @@ -70,33 +67,25 @@ const inlineProcess = (file, asset, warn, options) => {
// eslint-disable-next-line complexity
module.exports = function(asset, dir, options, decl, warn, addDependency) {
return getFile(asset, options, dir, warn)
.then(file => {
if (!file) return;

if (!file.mimeType) {
warn(`Unable to find asset mime-type for ${file.path}`);

return;
}

const maxSize = (options.maxSize || 0) * 1024;

if (maxSize) {
const size = Buffer.byteLength(file.contents);

if (stats.size >= maxSize) {
return processFallback.apply(this, arguments);
}
}

return inlineProcess(file, asset, warn, options);
});


.then((file) => {
if (!file) return;

if (!file.mimeType) {
warn(`Unable to find asset mime-type for ${file.path}`);

return;
}

const maxSize = (options.maxSize || 0) * 1024;

if (maxSize) {
const size = Buffer.byteLength(file.contents);

if (size >= maxSize) {
return processFallback.apply(this, arguments);
}
}

return inlineProcess(file, asset, warn, addDependency, options);
});
};

0 comments on commit 20f0708

Please sign in to comment.