导入的Sass文件无法找到字体文件

时间:2018-01-10 14:27:14

标签: sass gatsby

我使用gatsby-plugin-sass来包含我的sass文件。我有一个导入web-animations.sass的{​​{1}}文件。我的_typography.sass有一个像这样的font-face声明:

_typography.sass

当我运行@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') 时,我收到此错误:

gatsby develop

似乎字体网址路径被解释为相对于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 ,而不是相对于web-animations.sass,因为如果我将_typography.sass向上移动两个级别,则此错误会消失。< / p>

有没有办法让web-animations.sass查找相对于自己位置的字体文件?

2 个答案:

答案 0 :(得分:1)

似乎已解决,但万一将来有人遇到这种情况,并希望有一个替代解决方案I answered a similar question over here,并且包括一个已部署站点的回购协议,展示了在Sass中使用静态文件的不同方式。

答案 1 :(得分:0)

我通过加入resolve-url-loader Webpack loader来实现这一目标。

这是我的gatsby-node.js文件:

"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;
        }
    }
};