【发布时间】:2017-06-26 14:29:23
【问题描述】:
我尝试了几种不同的解决方案,但没有一个适合我。
我正在使用 .NET Core、最新的 Aurelia/Aurelia CLI 和使用 npm install font-awesome --save 安装的 Font-Awesome。
解决方案 1:
新文件:文件夹 \aurelia_project\tasks 中的 prepare-font-awesome.js
import gulp from 'gulp';
import merge from 'merge-stream';
import changedInPlace from 'gulp-changed-in-place';
import project from '../aurelia.json';
export default function prepareFontAwesome() {
const source = 'node_modules/font-awesome';
const taskCss = gulp.src(`${source}/css/font-awesome.min.css`)
.pipe(changedInPlace({ firstPass: true }))
.pipe(gulp.dest(`${project.platform.output}/css`));
const taskFonts = gulp.src(`${source}/fonts/*`)
.pipe(changedInPlace({ firstPass: true }))
.pipe(gulp.dest(`${project.platform.output}/fonts`));
return merge(taskCss, taskFonts);
}
更新了 build.js\aurelia_project\tasks
import prepareFontAwesome from './prepare-font-awesome'; // Our custom task
export default gulp.series(
readProjectConfiguration,
gulp.parallel(
transpile,
processMarkup,
processCSS,
prepareFontAwesome // Our custom task
),
writeBundles
);
在 html 中包含了很棒的字体
<link rel="stylesheet" href="scripts/css/font-awesome.min.css">
错误:
解决方案 2:
更新了 aurelia.json
{
"name": "font-awesome",
"path": "../node_modules/font-awesome/",
"main": "",
"resources": [
"css/font-awesome.min.css"
]
}
在 root/font-awesome/fonts 中添加字体文件
在 html 中包含了很棒的字体
<require from="font-awesome/css/font-awesome.min.css"></require>
错误:没有错误但图标不显示
解决方案 3:
更新 build.js:
import gulp from 'gulp';
import transpile from './transpile';
import processMarkup from './process-markup';
import processCSS from './process-css';
import { build } from 'aurelia-cli';
import project from '../aurelia.json';
import fs from 'fs';
import readline from 'readline';
import os from 'os';
export default gulp.series(
copyAdditionalResources,
readProjectConfiguration,
gulp.parallel(
transpile,
processMarkup,
processCSS
),
writeBundles
);
function copyAdditionalResources(done){
readGitIgnore();
done();
}
function readGitIgnore() {
let lineReader = readline.createInterface({
input: fs.createReadStream('./.gitignore')
});
let gitignore = [];
lineReader.on('line', (line) => {
gitignore.push(line);
});
lineReader.on('close', (err) => {
copyFiles(gitignore);
})
}
function copyFiles(gitignore) {
let stream,
bundle = project.build.bundles.find(function (bundle) {
return bundle.name === "vendor-bundle.js";
});
// iterate over all dependencies specified in aurelia.json
for (let i = 0; i < bundle.dependencies.length; i++) {
let dependency = bundle.dependencies[i];
let collectedResources = [];
if (dependency.path && dependency.resources) {
// run over resources array of each dependency
for (let n = 0; n < dependency.resources.length; n++) {
let resource = dependency.resources[n];
let ext = resource.substr(resource.lastIndexOf('.') + 1);
// only copy resources that are not managed by aurelia-cli
if (ext !== 'js' && ext != 'css' && ext != 'html' && ext !== 'less' && ext != 'scss') {
collectedResources.push(resource);
dependency.resources.splice(n, 1);
n--;
}
}
if (collectedResources.length) {
if (gitignore.indexOf(dependency.name)< 0) {
console.log('Adding line to .gitignore:', dependency.name);
fs.appendFile('./.gitignore', os.EOL + dependency.name, (err) => { if (err) { console.log(err) } });
}
for (let m = 0; m < collectedResources.length; m++) {
let currentResource = collectedResources[m];
if (currentResource.charAt(0) != '/') {
currentResource = '/' + currentResource;
}
let path = dependency.path.replace("../", "./");
let sourceFile = path + currentResource;
let destPath = './' + dependency.name + currentResource.slice(0, currentResource.lastIndexOf('/'));
console.log('Copying resource', sourceFile, 'to', destPath);
// copy files
gulp.src(sourceFile)
.pipe(gulp.dest(destPath));
}
}
}
}
}
function readProjectConfiguration() {
return build.src(project);
}
function writeBundles() {
return build.dest();
}
更新了 aurelia.json
{
"name": "font-awesome",
"main":"",
"path": "../node_modules/font-awesome",
"resources": [
"css/font-awesome.css",
"/fonts/fontawesome-webfont.woff2",
"/fonts/FontAwesome.otf",
"/fonts/fontawesome-webfont.eot",
"/fonts/fontawesome-webfont.svg",
"/fonts/fontawesome-webfont.ttf"
]
}
在 html 中包含了很棒的字体
<require from="font-awesome/css/font-awesome.css"></require>
错误:
得到: http://localhost:9000/font-awesome/fonts/fontawesome-webfont.woff2?v=4.7.0 (与 woff 和 ttf 相同)
真的很奇怪,因为文件被复制了,而且url是正确的..
文件夹结构:
测试了几个不同的来源
我错过了什么?
我更喜欢 less 实现,这样我就可以在我的 master less 文件中导入 Font-Awesome。
【问题讨论】:
-
确认一下,你的css文件被成功拉取了吗? CSS 实际上应该最有可能捆绑在您的
vendor-bundle中(它在您定义它的任何地方)。你能确认 font-awesome css 库在你的包中吗?它将被定义为一个模块,可能类似于font-awesome/css/font-awesome.css。 -
嗨,是的 font-awesome 是在 vendor-bundle 中定义的。
-
接下来要确认的是,当您需要 font awesome 捆绑包时(如您所描述,并确保“from”属性与供应商捆绑包中定义的名称相同),请执行字体很棒的样式被添加到
<head>? -
您丢失的部分可能是实际的字体文件本身 (github.com/FortAwesome/Font-Awesome/tree/master/fonts)。当您引入 CSS 时,您并没有引入字体。目前,您不能完全将字体与供应商捆绑包捆绑在一起,因此您必须创建一个 gulp 任务,将您的字体文件复制到您的输出目录(在 font-awesome.css 期望的位置)。查看此行以了解将字体文件放在何处:github.com/FortAwesome/Font-Awesome/blob/master/css/…
-
@Andrew 是的,字体很棒的样式被添加到 head 标签中,我可以看到样式被添加到图标中,但是字体没有加载.. 我添加了另一个解决方案, (3)。我的文件被复制到正确的文件夹(root/font-awesome/fonts),但我仍然收到错误消息:localhost:9000/font-awesome/fonts/…(与 woff 和 ttf 相同)
标签: asp.net-core font-awesome .net-core aurelia