import postcss from 'postcss'
import { readFileSync as slurp } from 'fs'
import { resolve, isAbsolute } from 'path'
import { parse as parseUrl } from 'url'
import { default as rebaseUrl } from 'postcss-url'

export default function rebase(pkg, opts, filename) {
  return postcss([
      rebaseUrl({
        url: (url, decl, from, dirname, to, options, result) => {
          let parsed = parseUrl(url, true, true)
          
          if (parsed.host || isAbsolute(parsed.path)) {
            return url
          } else {
            let resolved = pkg.relative(resolve(pkg.root, opts.lib, parsed.path))
            return resolved
          }
        }
      })
    ]).process(slurp(filename, 'utf8'))
}
import postcss from 'postcss'
import { readFileSync as slurp } from 'fs'
import { resolve, isAbsolute } from 'path'
import { parse as parseUrl } from 'url'
import { default as rebaseUrl } from 'postcss-url'

export default function rebase(pkg, opts, filename) {
  return postcss([
      rebaseUrl({
        url: (url, decl, from, dirname, to, options, result) => {
          let parsed = parseUrl(url, true, true)
          
          if (parsed.host || isAbsolute(parsed.path)) {
            return url
          } else {
            let resolved = pkg.relative(resolve(pkg.root, opts.lib, parsed.path))
            return resolved.replace(/\\/g, '/')
          }
        }
      })
    ]).process(slurp(filename, 'utf8'))
}
Replace `\` with `/` in resolved path
Replace `\` with `/` in resolved path

This fixes url rebasing on windows. We probably should use something
other than the `path` module for resolving, but that can come at
a later time – this'll do for now.
