├── .gitignore ├── art └── logo.png ├── LICENSE ├── package.json ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | *.log 3 | package-lock.json 4 | yarn.lock 5 | -------------------------------------------------------------------------------- /art/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danielstgt/laravel-mix-svg-vue/HEAD/art/logo.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Daniel Hartmann 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-mix-svg-vue", 3 | "version": "0.4.1", 4 | "description": "A Laravel Mix extension to inline SVG files with Vue.js and automatically optimize them with SVGO", 5 | "main": "index.js", 6 | "files": [ 7 | "index.js", 8 | "README.md", 9 | "package.json" 10 | ], 11 | "scripts": { 12 | "test": "echo \"Error: no test specified\" && exit 1" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/danielstgt/laravel-mix-svg-vue.git" 17 | }, 18 | "keywords": [ 19 | "laravel mix", 20 | "svg", 21 | "vue", 22 | "svgo" 23 | ], 24 | "author": "Daniel Hartmann", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/danielstgt/laravel-mix-svg-vue/issues" 28 | }, 29 | "homepage": "https://github.com/danielstgt/laravel-mix-svg-vue", 30 | "peerDependencies": { 31 | "laravel-mix": "^4.0.15 || ^5.0.0 || ^6.0.0" 32 | }, 33 | "dependencies": { 34 | "fs": "0.0.1-security", 35 | "path": "^0.12.7", 36 | "raw-loader": "^4.0.0", 37 | "svg-vue": "^0.2.0", 38 | "svg-vue3": "^0.2.1", 39 | "svgo": "^2.3.1", 40 | "svgo-loader": "^3.0.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | let mix = require('laravel-mix'); 2 | let path = require('path'); 3 | const { extendDefaultPlugins } = require('svgo'); 4 | 5 | class SvgVue { 6 | 7 | name() { 8 | return 'svgVue'; 9 | } 10 | 11 | dependencies() { 12 | return ['svgo-loader', 'raw-loader', 'fs', 'svg-vue', 'svg-vue3']; 13 | } 14 | 15 | register(options) { 16 | this.options = Object.assign({ 17 | svgPath: 'resources/svg', 18 | extract: false, 19 | svgoSettings: [ 20 | { removeTitle: true }, 21 | { removeViewBox: false }, 22 | { removeDimensions: true } 23 | ] 24 | }, options); 25 | 26 | this.includePath = path.resolve(__dirname, process.cwd() + '/' + this.options.svgPath); 27 | } 28 | 29 | boot() { 30 | Mix.listen('configReady', config => { 31 | config.module.rules.map(r => { 32 | if (this._isSvgRegExp(r.test) && ! this._isSvgVueRule(r)) { 33 | r.exclude = path.resolve(__dirname, process.cwd() + '/' + this.options.svgPath); 34 | } 35 | }); 36 | }); 37 | } 38 | 39 | webpackRules() { 40 | return { 41 | test: /\.svg$/, 42 | include: [ 43 | this.includePath 44 | ], 45 | rules: [ 46 | { 47 | loader: 'raw-loader' 48 | }, 49 | 50 | { 51 | loader: 'svgo-loader', 52 | options: { 53 | plugins: extendDefaultPlugins(this._convertSvgoOptions(this.options.svgoSettings)) 54 | } 55 | } 56 | ] 57 | } 58 | } 59 | 60 | webpackConfig(webpackConfig) { 61 | let fs = require('fs'); 62 | 63 | fs.mkdir(this.includePath, error => { 64 | if (error && error.code === 'EEXIST') return null; 65 | }); 66 | 67 | webpackConfig.resolve.alias['svg-files-path'] = this.includePath; 68 | 69 | if (this.options.extract) { 70 | let svgAssetsObj = { 71 | test: this.includePath, 72 | name: '/js/svg', 73 | chunks: 'all', 74 | enforce: true 75 | } 76 | 77 | if (webpackConfig.optimization.hasOwnProperty('splitChunks')) { 78 | webpackConfig.optimization.splitChunks.cacheGroups['svgAssets'] = svgAssetsObj; 79 | } else { 80 | webpackConfig.optimization = { 81 | splitChunks: { 82 | cacheGroups: { 83 | svgAssets: svgAssetsObj 84 | } 85 | } 86 | } 87 | } 88 | } 89 | } 90 | 91 | _isSvgRegExp(pattern) { 92 | let regExCheck = new RegExp(pattern); 93 | 94 | return regExCheck.test('.svg') || regExCheck.test('font.svg'); 95 | } 96 | 97 | _isSvgVueRule(rule) { 98 | if (rule.hasOwnProperty('include')) { 99 | return rule.include.includes(this.includePath); 100 | } 101 | 102 | return false; 103 | } 104 | 105 | _convertSvgoOptions(options) { 106 | let converted = []; 107 | 108 | options.forEach(option => { 109 | let settings = Object.keys(option); 110 | 111 | settings.forEach(setting => { 112 | converted.push({ 113 | name: setting, 114 | active: option[setting] 115 | }); 116 | }); 117 | }); 118 | 119 | return converted; 120 | } 121 | 122 | } 123 | 124 | mix.extend('svgVue', new SvgVue()); 125 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |
3 |
[{ removeTitle: true }, { removeViewBox: false }, { removeDimensions: true }] | SVGO settings
126 |
127 | ## Toggling or rendering icons inside lists
128 |
129 | Not really related to this extension, but when more than one `{{ item.title }}
151 |