[英]How can I use fontawesome icons in a laravel application using vite?
I have installed @fortawesome/fontawesome-free package using npm.我已经使用 npm 安装了@fortawesome/fontawesome-free包。 The latest Laravel application uses vite by default.
最新的 Laravel 应用默认使用 vite。 I am unable to solve this issue.
我无法解决这个问题。 Any help would be much appreciated.
任何帮助将非常感激。 My vite.config.js is
我的vite.config.js是
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import { viteStaticCopy } from 'vite-plugin-static-copy';
export default defineConfig({
plugins: [
laravel([
'resources/css/app.css',
'resources/js/app.js',
'resources/admin/css/app.css',
'resources/admin/js/app.js',
'resources/css/glide.css',
'resources/js/glide.js',
'resources/js/Sortable.js',
'resources/js/tinymce.js',
'resources/sass/app.scss',
'resources/admin/sass/app.scss',
]),
{
name: 'blade',
handleHotUpdate({ file, server }) {
if (file.endsWith('.blade.php')) {
server.ws.send({
type: 'full-reload',
path: '*',
});
}
},
},
viteStaticCopy({
targets: [
{
src: 'node_modules/@fortawesome/fontawesome-free/webfonts',
dest: '',
},
],
}),
],
});
I imported fontawesome scss files in app.scss .我在app.scss中导入了 fontawesome scss 文件。 My app.scss file contains
我的app.scss文件包含
@import "@fortawesome/fontawesome-free/scss/fontawesome";
@import "@fortawesome/fontawesome-free/scss/brands";
@import "@fortawesome/fontawesome-free/scss/regular";
@import "@fortawesome/fontawesome-free/scss/solid";
@import "@fortawesome/fontawesome-free/scss/v4-shims";
I tried using a third party library https://github.com/sapphi-red/vite-plugin-static-copy to copy webfonts of fontawesome package.我尝试使用第三方库https://github.com/sapphi-red/vite-plugin-static-copy来复制 fontawesome 包的 webfonts。 Is there a better way than this?
还有比这更好的方法吗?
It is quite easy if you are not adding it via npm.如果您不通过 npm 添加它,这很容易。 Copy entire fontawesome dir into resources dir
(/resources/fontawesome)
, then declare variable in your scss file like (assuming you are doing it in a file inside /resources/sass:将整个 fontawesome 目录复制到资源目录
(/resources/fontawesome)
中,然后在您的 scss 文件中声明变量,例如(假设您在 /resources/sass 中的文件中执行此操作:
$fa-font-path: '../fontawesome/webfonts';
and import fontawesome files:并导入 fontawesome 文件:
@import '../fontawesome/scss/brands';
@import '../fontawesome/scss/solid';
@import '../fontawesome/scss/light';
@import '../fontawesome/scss/fontawesome';
Build script will copy files to your /public/build/assets
dir and change urls and dev script will load them from your resources dir.构建脚本会将文件复制到您的
/public/build/assets
目录并更改 url,开发脚本将从您的资源目录加载它们。
I solved it by first installing sass pre-processor:我通过首先安装 sass 预处理器解决了这个问题:
npm install -D sass
after that I imported all fontawesome scss files into my app.js file:之后,我将所有 fontawesome scss文件导入到我的app.js文件中:
import './bootstrap';
import '@fortawesome/fontawesome-free/scss/fontawesome.scss';
import '@fortawesome/fontawesome-free/scss/brands.scss';
import '@fortawesome/fontawesome-free/scss/regular.scss';
import '@fortawesome/fontawesome-free/scss/solid.scss';
import '@fortawesome/fontawesome-free/scss/v4-shims.scss';
import Alpine from 'alpinejs';
window.Alpine = Alpine;
Alpine.start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.