get-file.js
946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
const fs = require('fs');
const mime = require('mime');
const getPathByBasePath = require('./paths').getPathByBasePath;
/**
*
* @param {PostcssUrl~Asset} asset
* @param {PostcssUrl~Options} options
* @param {PostcssUrl~Dir} dir
* @param {Function} warn
* @returns {PostcssUrl~File}
*/
const getFile = (asset, options, dir, warn) => {
const paths = options.basePath
? getPathByBasePath(options.basePath, dir.from, asset.pathname)
: [asset.absolutePath];
const filePath = paths.find(fs.existsSync);
if (!filePath) {
warn(`Can't read file '${paths.join()}', ignoring`);
return;
}
return {
path: filePath,
contents: fs.readFileSync(filePath),
mimeType: mime.lookup(filePath)
};
};
module.exports = getFile;
/**
* @typedef {Object} PostcssUrl~File
* @property {String} path
* @property {Buffer} contents
* @property {String} mimeType
*/