问题
I'm using gatsby-plugin-sass to include my sass files. I have a web-animations.sass
file that imports _typography.sass
. My _typography.sass
has a font-face declaration like this:
@font-face
font-family: 'BrandonPrinted'
src: url(../../global/fonts/brandonprinted-one-webfont.eot)
src: url(../../global/fonts/brandonprinted-one-webfont.eot?#iefix) format('embedded-opentype'), url(../../global/fonts/brandonprinted-one-webfont.woff) format('woff'), url(/src/page-assets/global/fonts/brandonprinted-one-webfont.ttf) format('truetype')
When I run gatsby develop
I get this error:
ERROR Failed to compile with 2 errors 19:51:15
These relative modules were not found:
* ../../global/fonts/brandonprinted-one-webfont.eot in ./~/css-loader!./~/sass-loader?{"plugins":[]}!./src/page-assets/work-ive-done/subpages/web-animations/styles/web-animations.sass
* ../../global/fonts/brandonprinted-one-webfont.woff in ./~/css-loader!./~/sass-loader?{"plugins":[]}!./src/page-assets/work-ive-done/subpages/web-animations/styles/web-animations.sass
It seems that the font url path is interpreted to be relative to web-animations.sass
instead of relative to _typography.sass
because if I move web-animations.sass
up two levels, then this error goes away.
Is there a way to have _typography.sass
look for the font files relative to its own location?
回答1:
Seems like this is resolved, but in case anyone runs across this in the future and wants an alternative solution I answered a similar question over here and included a repo with a deployed site demonstrating different ways of using static files with Sass.
回答2:
I got this working by including the resolve-url-loader Webpack loader.
Here's my gatsby-node.js
file:
"use strict";
var ExtractTextPlugin = require(`extract-text-webpack-plugin`);
var _require = require(`gatsby-1-config-css-modules`),
cssModulesConfig = _require.cssModulesConfig;
exports.modifyWebpackConfig = function (_ref, options) {
var config = _ref.config,
stage = _ref.stage;
var sassFiles = /\.s[ac]ss$/;
var sassModulesFiles = /\.module\.s[ac]ss$/;
options['sourceMap'] = 'sourceMap';
var sassLoader = `sass?${JSON.stringify(options)}`;
switch (stage) {
case `develop`:
{
config.loader(`sass`, {
test: sassFiles,
exclude: sassModulesFiles,
loaders: [`style`, `css`, 'resolve-url-loader', sassLoader]
});
return config;
}
case `build-css`:
{
config.loader(`sass`, {
test: sassFiles,
exclude: sassModulesFiles,
loader: ExtractTextPlugin.extract([`css?minimize`, 'resolve-url-loader', sassLoader])
});
config.loader(`sassModules`, {
test: sassModulesFiles,
loader: ExtractTextPlugin.extract(`style`, [cssModulesConfig(stage), 'resolve-url-loader', sassLoader])
});
return config;
}
case `develop-html`:
case `build-html`:
case `build-javascript`:
{
config.loader(`sass`, {
test: sassFiles,
exclude: sassModulesFiles,
loader: `null`
});
config.loader(`sassModules`, {
test: sassModulesFiles,
loader: ExtractTextPlugin.extract(`style`, [cssModulesConfig(stage), 'resolve-url-loader', sassLoader])
});
return config;
}
default:
{
return config;
}
}
};
来源:https://stackoverflow.com/questions/48189592/imported-sass-file-cant-find-font-file