swisnl / laravel-mix-svg-sprite Goto Github PK
View Code? Open in Web Editor NEWSVG sprite component for Laravel Mix
License: MIT License
SVG sprite component for Laravel Mix
License: MIT License
Extract mode for scss file is working when mix.options({ processCssUrls: true })
. But it is not working when setting the processCssUrls is false.
Would like to use relative url on my css.
Include as many relevant details about the environment you experienced the bug in and how to reproduce it.
// assets/styles/main.scss
body {
background-image: url(../sprites/email.svg);
}
// assets/scripts/main.js
/**
* Requires all directives from subdirectories.
* @param requireContext
* @returns {*}
*/
function requireAll(requireContext) {
return requireContext.keys().map(requireContext);
}
// Require all icons in the sprites folder.
requireAll(require.context('../../sprites', true, /\.svg$/))
// webpack.mix.js
mix
.options({ processCssUrls: false, })
.sass('assets/styles/main.scss', 'styles')
.js('assets/scripts/main.js', 'scripts')
.svgSprite('assets/sprites', 'sprite.svg')
;
When compiling with option of extract true; the expectation is that the compiled css asset urls for sprite assets resolve as the sprite file with sprite fragments identifiers for such file. Instead the a system path to the file is what is injected.
The expectations relates to https://github.com/JetBrains/svg-sprite-loader/tree/master/examples/extract-mode
.logo {background: url('./logo.svg')}
.logo {background: url('sprite.svg#logo-usage')}
.test {
background-image: url('./badge-steam.svg');
}
.test {
background-image: url(//home/user/localsites/site/app/public/wp-content/themes/test/assets/badge-steam.svg);
}
.test {
background-image: url(./sprite.svg#badge-steam);
}
const mix = require("laravel-mix");
require("laravel-mix-svg-sprite");
/**
* @see https://laravel-mix.com/docs/6.0/api
* @see https://laravel-mix.com/extensions/svg-sprite
* @see https://github.com/JetBrains/svg-sprite-loader/tree/master/examples/extract-mode
*/
mix
.options({
watchOptions: {
ignored: /node_modules/,
},
processCssUrls: true,
useRelativePaths: true,
publicPath: "dist",
})
.sass("assets/styles/app.scss", "styles")
.svgSprite("assets/svgs", "svg/sprite.svg", {
extract: true,
});
node v16.9.0
npm 7.22.0
OS Arch Linux x86_64
Kernel 5.13.13-arch-1-1
Hello, can you write an example please.
I wanted to inline my all SVG files but I couldn't
I want to use it in sass and html also)
I read that this package doesn't blob my folder and include all files but I don't understand how to do it.
Small example will help me and other guys.
Thanks in advance!
I tried to implement this package into my Laravel project, however, my svg sprite sheet is not being generated.
webpack.mix.js
let mix = require('laravel-mix');
require('laravel-mix-svg-sprite');
mix.js( 'resources/js/app.js', 'public/js' )
.sass( 'resources/sass/app.scss', 'public/css' )
.svgSprite(
'resources/sprite',
'public/sprite.svg',
)
.copyDirectory( 'resources/fonts', 'public/fonts' );
Contents of my package.json:
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"aos": "^2.3.4",
"axios": "^0.19",
"cross-env": "^7.0",
"jquery": "^3.6.0",
"laravel-echo": "^1.11.0",
"laravel-mix": "^5.0.1",
"laravel-mix-svg-sprite": "^2.0.0",
"lodash": "^4.17.13",
"magnific-popup": "^1.1.0",
"pusher-js": "^7.0.3",
"resolve-url-loader": "^3.1.0",
"sass": "^1.15.2",
"sass-loader": "^8.0.0",
"svg-spritemap-webpack-plugin": "^4.2.0",
"vue-template-compiler": "^2.6.14"
},
"dependencies": {
"@pusher/push-notifications-web": "^1.1.0",
"chart.js": "^3.5.0",
"puppeteer": "^5.5.0",
"vue": "^2.6.14"
}
}
When I run npm run watch
or npm run dev
next, nothing seems to be happening.
Am I missing any necessary steps?
Hello guys. I need your input please. The problem is that the svg file is not created.
Βelow you will see what I have done.
I would be grateful for any help.
webpack.config.js
module.exports = {
resolve: {
alias: {
'@createSvg': path.resolve(__dirname, './resources/img/icons')
}
}
};
webpack.mix.js
require('laravel-mix-svg-sprite');
Config.svgSprite = {
loaderOptions: {
extract: true
},
pluginOptions: {
plainSprite: true
}
};
mix.js('resources/js/index.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.svgSprite(
'@createSvg',
'public/img/sprite.svg'
)
.extract();
index.js (my app.js file)
function requireAll(r) {
r.keys().forEach(r);
}
requireAll(require.context('@createSvg', true, /\.svg$/));
I have a folder which contains svg-icons.
I try to generate sprite but nothing happens, mix completes and no sprite has been generated in dist folder.
const mix = require('laravel-mix');
require('laravel-mix-svg-sprite');
mix.svgSprite(
'resources/assets/svg',
'public/dist/front/sprite.svg',
{
extract: true
},
{
plainSprite: true
}
);
As I think the problem refers to this notice in plugin description
N.B. This component only adds a loader to the Webpack configuration, it doesn't glob your directory and include every file it finds. You need to require the icons from within your code (JS, CSS, etc.) just like other modules to have them added to the sprite!
But I dont know what I have to do with this.
I'm trying to do a project in Laravel coupling VueJS as an SPA and using your library, but something weird occur :
When i first run mix with browserSync, the website shows itself perfectly with all icons as svg, but when a change of code occurs, the website "hot reload" and all the icons generated by the library disappear !
And the same error occur when you open the website twice, the first tab could be perfect but the new one won't have any svg generated.
"laravel-mix": "^4.1.2", "laravel-mix-svg-sprite": "^1.0.0", "svg-spritemap-webpack-plugin": "^3.4.0", "svg4everybody": "^2.1.9", "vue": "^2.6.10", "vuex": "^3.1.1",
Mac OS
Webpack : https://gist.github.com/Epistol/127330e1e31fdd37b441b1dbd22025be
The SPA router of VueJS : https://gist.github.com/Epistol/227e8d55ff07a5ed898ba3f4bab6800f
app.js : https://gist.github.com/Epistol/d8eae61ee32ef49031574c13fd37ebce
And an exemple loading svg of my vue component page :
<svg class="fill-current w-full h-full">
<use xlink:href="/svg/sprite.svg#help"/>
</svg>
I believe this is possible with svgo but I'm not sure how to extend or change the config.
It is not possible to create multiple sprites. Only the last sprite is created.
I want to create multiple sprites.
Some fix is needed in the register function of the plugin to allow multiple sprites.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.