typefaces
typefaces copied to clipboard
Cannot use typeface in a webpack-with-sass-loader setup
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'
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;
}
I think I figured it out (also notice in my setup I use
MiniCssExtractPluginto separate CSS assets out of JS assets).
sass-loaderhas problems withurl(), thereforeresolve-url-loaderandfile-loaderare 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!