Skip to content

Commit 3a0a9b4

Browse files
committed
init
0 parents  commit 3a0a9b4

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
*.log
3+
package-lock.json
4+
yarn.lock

index.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
let mix = require('laravel-mix');
2+
3+
class SvgVue {
4+
5+
name() {
6+
return 'svgVue';
7+
}
8+
9+
dependencies() {
10+
return ['img-loader', 'imagemin-svgo', 'raw-loader', 'fs', 'svg-vue'];
11+
}
12+
13+
register(options) {
14+
this.options = Object.assign({
15+
extract: false,
16+
path: './resources/svg/',
17+
svgoSettings: [
18+
{ removeTitle: true },
19+
{ removeViewBox: false },
20+
{ removeDimensions: true }
21+
]
22+
}, options);
23+
}
24+
25+
boot() {
26+
Mix.listen('configReady', config => {
27+
let search = [
28+
/(\.(png|jpe?g|gif|webp)$|^((?!font).)*\.svg$)/.toString(),
29+
/(\.(woff2?|ttf|eot|otf)$|font.*\.svg$)/.toString()
30+
];
31+
32+
config.module.rules.map(r => {
33+
if (search.includes(r.test.toString())) r.exclude = /\.svg$/;
34+
});
35+
});
36+
}
37+
38+
webpackRules() {
39+
return {
40+
test: /\.svg$/,
41+
loaders: [
42+
{
43+
loader: 'raw-loader'
44+
},
45+
46+
{
47+
loader: 'img-loader',
48+
options: {
49+
plugins: [
50+
require('imagemin-svgo')({ plugins: this.options.svgoSettings })
51+
]
52+
}
53+
}
54+
]
55+
}
56+
}
57+
58+
webpackConfig(webpackConfig) {
59+
let fs = require('fs');
60+
let svgPath = path.resolve(__dirname, process.cwd() + '/' + this.options.path);
61+
62+
fs.mkdir(svgPath, error => {
63+
if (error && error.code === 'EEXIST') return null;
64+
});
65+
66+
webpackConfig.resolve.alias['svg-files-path'] = svgPath;
67+
68+
if (this.options.extract) {
69+
let svgAssetsObj = {
70+
test: svgPath,
71+
name: '/js/svg',
72+
chunks: 'all',
73+
enforce: true
74+
}
75+
76+
if (webpackConfig.optimization.hasOwnProperty('splitChunks')) {
77+
webpackConfig.optimization.splitChunks.cacheGroups['svgAssets'] = svgAssetsObj;
78+
} else {
79+
webpackConfig.optimization = {
80+
splitChunks: {
81+
cacheGroups: {
82+
svgAssets: svgAssetsObj
83+
}
84+
}
85+
}
86+
}
87+
}
88+
}
89+
90+
}
91+
92+
mix.extend('svgVue', new SvgVue());

package.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "laravel-mix-svg-vue",
3+
"version": "0.1.0",
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+
"package.json"
9+
],
10+
"scripts": {
11+
"test": "echo \"Error: no test specified\" && exit 1"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/danielstgt/laravel-mix-svg-vue.git"
16+
},
17+
"keywords": [
18+
"laravel mix",
19+
"svg",
20+
"vue",
21+
"svgo"
22+
],
23+
"author": "Daniel Hartmann",
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/danielstgt/laravel-mix-svg-vue/issues"
27+
},
28+
"homepage": "https://github.com/danielstgt/laravel-mix-svg-vue",
29+
"peerDependencies": {
30+
"laravel-mix": "^4.0.15"
31+
},
32+
"dependencies": {
33+
"fs": "0.0.1-security",
34+
"imagemin-svgo": "^7.0.0",
35+
"img-loader": "^3.0.1",
36+
"raw-loader": "^2.0.0",
37+
"svg-vue": "^0.1.1"
38+
}
39+
}

0 commit comments

Comments
 (0)