Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,25 @@ See [PostCSS] docs for examples for your environment.

#### options.paths

Array of paths to resolve URL. Paths are tried in order, until an existing file is found.
Array of paths to resolve URL. Array paths are tried in order, until an existing file is found or will fallback to using the relative path.

Default: `false` - path will be relative to source file if it was specified.

#### options.resolve

Function to resolve URL. This option will override the default file id resolver that uses "paths" option.
Function recieves `file`, `url`, and `opts` arguments and expects to return an absolute path as a `string`.

For example

```js
function resolve(file, url, opts) {
return path.resolve('svg-folder', url);
}
```

Default: `function` - internal resolver using "paths" option and relative path.

#### options.removeFill

Default: `false` - with `true` removes all `fill` attributes before applying specified.
Expand Down
10 changes: 8 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ module.exports = postcss.plugin(
const { name, url } = parseRuleDefinition(node.params);
const { params, selectors } = getRuleParams(node);
const loader = {
id: resolveId(file, url, opts),
id:
typeof opts.resolve === "function"
? opts.resolve(file, url, opts)
: resolveId(file, url, opts),
parent: file,
params,
selectors,
Expand All @@ -68,7 +71,10 @@ module.exports = postcss.plugin(
statements.loaders.forEach(
({ url, params, valueNode, parsedValue }) => {
const loader = {
id: resolveId(file, url, opts),
id:
typeof opts.resolve === "function"
? opts.resolve(file, url, opts)
: resolveId(file, url, opts),
parent: file,
params,
selectors: {},
Expand Down
2 changes: 0 additions & 2 deletions lib/resolveId.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ module.exports = function resolveId(file, url, opts) {
return absolutePath;
}
}

return absolutePath;
}

if (file) {
Expand Down
39 changes: 39 additions & 0 deletions test/options.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env mocha */
const { resolve } = require("path");
const { compare } = require("./utils.js");

process.chdir(__dirname);
Expand Down Expand Up @@ -71,6 +72,44 @@ test('should prefer "paths" option over "from"', () => {
);
});

test('should fallback to relative option if "paths" option can\'t be resolved', () => {
return compare(
`
@svg-load icon url(fixtures/basic.svg) {}
background: svg-load('fixtures/basic.svg');
background: svg-inline(icon);
`,
`
background: url("data:image/svg+xml;charset=utf-8,<svg xmlns=\'http://www.w3.org/2000/svg\' id='basic'/>");
background: url("data:image/svg+xml;charset=utf-8,<svg xmlns=\'http://www.w3.org/2000/svg\' id='basic'/>");
`,
{
from: "input.css",
paths: ["./does_not_exist"],
encode: false
}
);
});

test('should resolve file path using the "resolve" option function', () => {
return compare(
`
@svg-load icon url(basic.svg) {}
background: svg-load('basic.svg');
background: svg-inline(icon);
`,
`
background: url("data:image/svg+xml;charset=utf-8,<svg xmlns=\'http://www.w3.org/2000/svg\' id='basic'/>");
background: url("data:image/svg+xml;charset=utf-8,<svg xmlns=\'http://www.w3.org/2000/svg\' id='basic'/>");
`,
{
from: "input.css",
resolve: (file, url, opts) => resolve("fixtures", url),
encode: false
}
);
});

test("should ignore xmlns absence", () => {
return compare(
`background: svg-load('fixtures/basic.svg');`,
Expand Down