-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
31 lines (24 loc) · 809 Bytes
/
index.js
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
'use strict' // needed for caller-path
const { readFileSync } = require('fs')
const { join, dirname } = require('path')
const styleResolve = require('style-resolve')
const isWindows = require('is-windows')
const slash = require('slash')
const caller = require('caller-path')
const cssUrlRegex = require('./util/css-url-regex')
const dotSlashRegex = /^[./]*/
module.exports = requireStyle
function requireStyle (name) {
const path = styleResolve.sync(name, {
basedir: dirname(caller())
})
var css = readFileSync(path, 'utf8')
// resolve any relative paths to absolute paths
css = css.replace(cssUrlRegex, (_, _2, url) => {
url = url
.replace(dotSlashRegex, match => join(dirname(path), match))
if (isWindows()) url = slash(url)
return `url('${url}')`
})
return css
}