Skip to content

SvgLoaderOptions

Defined in: types.ts:6

vite-awesome-svg-loader options

optional defaultImport: ImportType

Defined in: types.ts:245

Default import type when no import query is specified. Available values:

  1. source - SVG source code (default).
  2. url - URL pointing to the file.
  3. source-data-uri - SVG source code encoded in data URI.
  4. base64 - SVG source code encoded in base-64.
  5. base64-data-uri: SVG source code encoded in base-64 data URI.
"source"

optional preserveLineWidthList: FileMatchers

Defined in: types.ts:21

Files where line width should be preserved by adding vector-effect="non-scaling-stroke".

See FileMatchers for the available formats.

Alternative: Use import query: import image from "./image.svg?preserve-line-width".


optional replaceColorsList: MaybeArray<FileMatcher | ColorMapPerFiles | ColorMap>

Defined in: types.ts:141

Files where fill, stroke and <stop> colors should be replaced with currentColor (default) or custom colors.

What’s replaced:

  1. fill, stroke and stop-color attributes.
  2. CSS identifiers.

What’s untouched:

  1. none, transparent, and currentColor values.

Available formats in application priority (from highest to lowest):

  1. ColorMapPerFiles - a color to replacement map scoped to the files (see FileMatchers) specified in ColorMapPerFile.files property.

  2. ColorMap - a color to replacement map applied to all files.

  3. FileMatcher - files where all colors should be replaced with currentColor.

If multiple entries match the same file, replacements are merged by following rules:

  1. If two entries replace the same color or have ColorMapPerFiles.default set, the entry with highest priority wins.

  2. If, in previous case, both entries have the same priority, the entry with the lowest index in the original array wins.

Caveats:

Opacity set by initial colors, i.e. rgba() is lost when color is replaced. This is so because opacity retention would:

  1. Still bring confusion because one may rightfully expect that color replacement will completely replace colors and not correct them.

  2. Slow down build process.

  3. Impose high maintenance complexity.

Notes:

Setting currentColor can be done with an import: import imageSrc from "./path/to/image.svg?set-current-color".

Setting custom color is not possible with import queries.

viteAwesomeSvgLoader({
replaceColorsList: [
// FileMatcher: files where all colors should be replaced with `currentColor`
"some-file.svg",
new RegExp("\/some\/directory\/"),
(ctx) => ctx.relativePath.includes("/some/directory/") || ctx.fullPath.endsWith("-image.svg"),
// ColorMap: map of color replacements.
// Key is an original color, value is its replacement.
{
"#003147": "red",
"rgb(0, 49, 71)": "#003147",
"myCustomColor": "var(--some-color-var)",
},
// ColorMapPerFiles: a color to replacement map scoped to the specified files
{
// File matchers
files: ["vars.svg"],
// Replacements
replacements: {
red: "var(--primary-color)",
green: "var(--secondary-color)",
blue: "var(--tertiary-color)",
},
// A default color for unspecified colors
default: "red",
},
],
})

optional setCurrentColorList: FileMatchers

Defined in: types.ts:252


optional skipFilesList: FileMatchers

Defined in: types.ts:232

Files to bypass. They will be passed to another loader.

See FileMatchers for the available formats.


optional skipPreserveLineWidthList: FileMatchers

Defined in: types.ts:31

Files where line width should NOT be preserved.

Takes precedence over preserveLineWidthList and import queries (import image from "./image.svg?preserve-line-width").

See FileMatchers for the available formats.


optional skipPreserveLineWidthSelectors: CssSelectors

Defined in: types.ts:57

CSS selectors where line width should NOT be preserved. Use this to keep specific elements’ stroke widths unchanged.

See CssSelectors for the available formats and more configuration options.

Note: Unlike skipSetCurrentColorSelectors and skipTransformsSelectors, doesn’t impact build performance.

viteAwesomeSvgLoader({
skipPreserveLineWidthSelectors: [
// Global selector:
'*[data-original-line-width="true"], *[data-original-line-width="true"] *',
// File-scoped selector:
{
files: [new RegExp("\/some\\-file\\.svg"), new RegExp("\/other\\-file\\.svg")],
selectors: ['*[data-original-line-width="true"], *[data-original-line-width="true"] *'],
},
],
})

optional skipReplaceColorsList: FileMatchers

Defined in: types.ts:151

Files where colors should NOT be replaced.

Takes precedence over replaceColorsList and import queries (import image from "./image.svg?set-current-color").

See FileMatchers for the available formats.


optional skipReplaceColorsSelectors: CssSelectors

Defined in: types.ts:180

CSS selectors where colors should NOT be replaced. Use it to leave specific elements colors unchanged.

See CssSelectors for the available formats and more configuration options.

Warning: Heavy usage can significantly slow down build performance.

Limit selectors to specific files to improve performance.

Oe better, consider alternative approaches such as using color maps. See replaceColorsList, ColorMap, and ColorMapPerFiles) for the details.

viteAwesomeSvgLoader({
skipReplaceColorsSelectors: [
// Global selector:
'*[data-original-colors="true"], *[data-original-colors="true"] *',
// File-scoped selector:
{
files: [new RegExp("\/some\\-file\\.svg"), new RegExp("\/other\\-file\\.svg")],
selectors: ['*[data-original-colors="true"], *[data-original-colors="true"] *'],
},
],
})

optional skipSetCurrentColorList: FileMatchers

Defined in: types.ts:257


optional skipSetCurrentColorSelectors: CssSelectors

Defined in: types.ts:262


optional skipTransformsList: FileMatchers

Defined in: types.ts:192

Files to NOT transform (except SVGO).

See FileMatchers for the available formats.

Takes precedence over replaceColorsList, preserveLineWidthList, and import queries (import image from "./image.svg?set-current-color&preserve-line-width").

Alternative: Use import query: import image from "./image.svg?skip-transforms".


optional skipTransformsSelectors: CssSelectors

Defined in: types.ts:225

CSS selectors which should NOT be transformed. Use this to leave specific elements untouched.

See CssSelectors for the available formats and more configuration options.

Warning: Heavy usage can significantly slow down build performance.

Limit selectors to specific files to improve performance.

Or better, consider following approach:

  1. To avoid color replacement, consider alternative approaches such as using color maps. See replaceColorsList, ColorMap, and ColorMapPerFiles) for the details.

  2. To avoid line width preservation, just use skipPreserveLineWidthSelectors.

viteAwesomeSvgLoader({
skipTransformsSelectors: [
// Global selector:
'*[data-no-transforms="true"], *[data-no-transforms="true"] *',
// File-scoped selector:
{
files: [new RegExp("\/some\\-file\\.svg"), new RegExp("\/other\\-file\\.svg")],
selectors: ['*[data-no-transforms="true"], *[data-no-transforms="true"] *'],
},
],
})

optional tempDir: string

Defined in: types.ts:12

Temporary directory where SVG files will be stored in dev mode.

"temp"

optional urlImportsInLibraryMode: UrlImportsInLibraryMode

Defined in: types.ts:282

Behavior for ?url imports in library mode. Possible values:

  1. source-data-uri - source data URI will be exported, no files will be emitted.

  2. base-64-data-uri - base64 data URI will be exported, no files will be emitted.

  3. emit-files - files will be emitted, following exports will be produced:

    "" + new URL("file-name-[hash].svg", import.meta.url).href

    Warning: May cause issues when your library is consumed. Requires additional configuration from the users. Warning: This behavior is not aligned with the default Vite behavior.

"source-data-uri"