Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use typeface in a webpack-with-sass-loader setup #199

Open
Abdull opened this issue Sep 3, 2020 · 2 comments
Open

Cannot use typeface in a webpack-with-sass-loader setup #199

Abdull opened this issue Sep 3, 2020 · 2 comments

Comments

@Abdull
Copy link

Abdull commented Sep 3, 2020

How can I use typeface-source-sans-pro in a webpack project that laverages sass-loader?

The current documentation is too sparse to make me understand what to do.

I did npm install --save typeface-source-sans-pro.

And then?

In my src/app.scss, I have added
@import 'typeface-source-sans-pro';

But then the webpack build fails with
Error: Can't resolve './files/source-sans-pro-300.woff2' in 'C:\Users\Abdull\git\webpack-demo\src'

@Abdull
Copy link
Author

Abdull commented Sep 3, 2020

I think I figured it out (also notice in my setup I use MiniCssExtractPlugin to separate CSS assets out of JS assets).

sass-loader has problems with url(), therefore resolve-url-loader and file-loader are required to be wired in.

npm install --save typeface-source-sans-pro
npm install resolve-url-loader file-loader --save-dev

webpack.config.js:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

// list of plugins active by default: https://webpack.js.org/configuration/mode/
module.exports = {
  mode: 'production',
  entry: './src/index.js',
  devtool: 'source-map',
  devServer: {
    contentBase: './dist',
  },
  output: {
    filename: 'app.js',
    library: 'app',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'app.css',
    }),
    new CleanWebpackPlugin({ cleanStaleWebpackAssets: false }),
    new HtmlWebpackPlugin({
      title: 'Output Management',
    }),
  ],
  module: {
    rules: [
      {
        test: /\.s[ac]ss$/i,
        use: [
          MiniCssExtractPlugin.loader,
          // Translates CSS into CommonJS
          'css-loader',
          'resolve-url-loader',
          // Compiles Sass to CSS
          'sass-loader'
        ]
      },
      { 
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [ 'file-loader' ]
      },
      {
        test: /\.m?js$/,
        // exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }
    ]
  }
};

app.scss:

@import 'normalize-scss/sass/normalize';
@include normalize();

@import 'typeface-source-sans-pro';

$body-color: red;
$gs_font_sans: "Source Sans Pro", Arial, Verdana, Helvetica, sans-serif;

body {
  color: $body-color;
  font-family: $gs_font_sans;
}

@andrew-chang-dewitt
Copy link

I think I figured it out (also notice in my setup I use MiniCssExtractPlugin to separate CSS assets out of JS assets).

sass-loader has problems with url(), therefore resolve-url-loader and file-loader are required to be wired in.

This solved my problem almost perfectly. I'd also been excluding the node_modules in my /\.s?[a|c]ss$/ tests in webpack, but allowing webpack to look in node_modules too was the final step in solving my issues.

Thanks for sharing your solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants