Code Monkey home page Code Monkey logo

vue-svg-inline-loader's Introduction

vue-svg-inline-loader

npm npm npm paypal

Webpack loader used for inline replacement of SVG images with actual content of SVG files in Vue projects.

Loader parses only HTML template format.

Loader has built-in SVGO support for SVG optimization.

Sprite option works only with Vue Single File Component approach.


Notable changes

  • v 1.2.14
    • Added example for gridsome based projects
  • v1.2.11
    • Fixed a bug where original svg attributes were used by referencing svg rather than symbol itself. This may introduce breaking changes for developers who rely on this bugged behavior.
  • v1.2.6
    • Modified default value of svgo option to preserve viewBox attribute
    • Modified svgo option to accept true value as alias for default configuration
  • v1.2.5
    • Modified svgo option to accept null or false value for disabling SVG optimization
  • v1.2.3
    • Changed default value of md5 option to true
    • Added examples for webpack, vue-cli and nuxt based projects
  • v1.2.0
  • v1.1.3
    • Added transpiled version of loader
  • v1.1.0
    • Added new option: md5
  • v1.0.8
    • Options structure changed, deprecated options still get parsed to new ones
  • v1.0.0

Install

Via npm [package]:

$ npm install vue-svg-inline-loader --save-dev

Via yarn [package]:

$ yarn add vue-svg-inline-loader --dev

Usage

With webpack - webpack.config.js:

module.exports = {
	module: {
		rules: [
			{
				test: /\.vue$/,
				use: [
					{
						loader: "vue-loader",
						options: { /* ... */ }
					},
					{
						loader: "vue-svg-inline-loader",
						options: { /* ... */ }
					}
				]
			}
		]
	}
};

With vue-cli - vue.config.js:

module.exports = {
	chainWebpack: config => {
		config.module
			.rule("vue")
			.use("vue-svg-inline-loader")
				.loader("vue-svg-inline-loader")
				.options({ /* ... */ });
	}
};

It can also be used for Gridsome - gridsome.config.js.

With nuxt - nuxt.config.js:

module.exports = {
	build: {
		extend(config, { isDev, isClient }) {
			const vueRule = config.module.rules.find(rule => rule.test.test(".vue"));
			vueRule.use = [
				{
					loader: vueRule.loader,
					options: vueRule.options
				},
				{
					loader: "vue-svg-inline-loader",
					options: { /* ... */ }
				}
			];
			delete vueRule.loader;
			delete vueRule.options;
		}
	}
};

Basic inline SVG usage with svg-inline keyword directive:

<img svg-inline class="icon" src="./images/example.svg" alt="example" />

Which replaces into:

<svg svg-inline class="icon" focusable="false" role="presentation" xmlns="http://www.w3.org/2000/svg" viewBox="...">
	<path d="..."></path>
</svg>

Basic inline SVG sprite usage with svg-sprite keyword directive:

<img svg-inline svg-sprite class="icon" src="./images/example.svg" alt="example" />

Which replaces into:

<svg svg-inline svg-sprite class="icon" focusable="false" role="presentation" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
	<use xlink:href="#svg-sprite-md5hash" href="#svg-sprite-md5hash"></use>
</svg>
<!-- ... -->
<!-- will get injected right before root closing tag in Vue component -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none !important;">
	<symbol id="svg-sprite-md5hash" viewBox="...">
		<path d="..."></path>
	</symbol>
	<!-- ... -->
</svg>

Notice

Loader won't parse any images with Vue bindings used as src attribute [more info].

Configuration

Default options:

{
	inline: {
		keyword: "svg-inline",
		strict: true
	},
	sprite: {
		keyword: "svg-sprite",
		strict: true
	},
	dataAttributes: [],
	removeAttributes: ["alt", "src"],
	md5: true,
	xhtml: false,
	svgo: true
/* value true for svgo option is alias for:
	svgo: {
		plugins: [
			{
				removeViewBox: false
			}
		]
	}
*/
}

Explanation:

  • inline.keyword:
    Defines keyword, which marks images you want to replace with inline SVG. Keyword has to be wrapped with whitespace characters (e.g. space). In case of some conflicts, you can also use data version of your keyword (e.g. data-keyword).

  • inline.strict:
    In strict mode loader replaces only images with defined keyword. If strict mode is disabled, loader replaces all images.

  • sprite.keyword:
    Defines keyword, which marks images you want to replace with inline SVG using inline sprites. Keyword has to be wrapped with whitespace characters (e.g. space). In case of some conflicts, you can also use data version of your keyword (e.g. data-keyword).

  • sprite.strict:
    In strict mode loader replaces only images with defined keyword. If strict mode is disabled, loader replaces all images.

  • dataAttributes:
    Array of attributes which will be renamed to data-attributes.

  • removeAttributes:
    Array of attributes which will be removed.

  • md5:
    Use md5-encoded resource path as ID for inline SVG sprites instead of plaintext. Set it to false only for development purposes.

  • xhtml:
    In XHTML mode attribute minimization is forbidden. Empty attributes are filled with their names to be XHTML-compliant (e.g. disabled="disabled").

  • svgo:
    Pass SVGO configuration object (documentation can be found here) or true for default configuration. Pass null or false to disable SVG optimization.

Notice

User-defined options are deep-merged with default options. Arrays are not concatenated.


License

MIT

vue-svg-inline-loader's People

Contributors

oliverfindl avatar mechazawa avatar kaipaysen avatar niklong avatar

Watchers

James Cloos avatar Luca Ban avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.