I'm trying to migrate our project build system from gulp to webpack due to the need to eventually bundle our Javascript code.
The issue I've come across is that I Webpack doesn't seem to be able to read import the fonts from SASS.
The directory is pretty standard, somewhat like this:
application
|_assets
| |__fonts
| |__images
| |__scripts
| |__styles
|
|_public
| |__fonts
| |__images
| |__scripts
| |__styles
...
And in one of our .scss
files, I import the fonts like this:
@include font-face('Visuelt', '../fonts/visuelt-regular');
@include font-face('VisueltMedium', '../fonts/visuelt-medium');
@include font-face('VisueltBold', '../fonts/visuelt-bold');
@include font-face('VisueltLight', '../fonts/visuelt-light');
The problem is that when I run the webpack build, the following error is thrown for each of the fonts:
ERROR in ./assets/styles/main.scss (./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./assets/styles/main.scss) Module not found: Error: Can't resolve '../fonts/visuelt-bold.svg' in '/Users/jgarcia/Repositories/foo/assets/styles' @ ./assets/styles/main.scss (./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./assets/styles/main.scss) 7:4499-4535
I have noticed that if I change the path to /fonts/visuelt-regular
, webpack stops complaining. The problem is that then the fonts are not loaded when I run the application. In order for the bundled code to load the fonts properly, the bundled css must have ../fonts/visuelt-regular
as the path. I'm not really sure I understand what's going on.
I've tried multiple things, but I can't seem to get it to work. Any insight?
This is the webpack config:
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const buildPath = path.join(__dirname, 'public');
module.exports = {
mode: 'development',
entry: './assets/entry.js',
output: {
filename: 'assets/scripts/bundle.js',
path: buildPath,
publicPath: buildPath,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: { // TODO Potentially not necesary?
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-transform-runtime'],
},
},
},
{
test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
publicPath: 'assets/',
emitFile: false,
},
},
{
test: /\.scss$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '/assets/styles/[name].css',
chunkFilename: '[id].css',
}),
new CopyWebpackPlugin([
{ from: 'assets/fonts', to: 'assets/fonts' },
{ from: 'assets/images', to: 'assets/images' },
]),
],
};