Using local web fonts with webpack
Is it possible to just always reference the original fonts? They don't appear to get changed by file-loader anyways.
File structure
APP
├───build
│ │ build.js
│ │ build.min.js
│ │
│ └───fonts
│ allthefonts.woff
│
├───css
│ main.css
│
├───fonts
│ allthefonts.woff
│
└───js
main.js
main.css
@font-face {
font-family: All-The-Fonts;
src: url('../fonts/allthefonts.woff') format('woff');
}
webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
...
output: {
path: path.resolve(__dirname, 'build'),
filename: "[name].js",
globalObject: 'this'
},
module: {
rules: [
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: './fonts/' //dont actually use these fonts but still need to process them
}
}]
}
]
},
...
};
a better approach would be to use 'url-loader' and add the following line in loaders.
{
test: /\.(jpe?g|png|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
loader: 'url-loader?limit=100000'
}
I got it working thanks to this article: https://www.robinwieruch.de/webpack-font
Got a working solution thanks to @omerts in this thread. Solution involved using publicPath. I had been trying to use it as an option in module.exports with the fonts file-loader, not the output.
Updated webpack.config.js:
const webpack = require('webpack');
const PROD = JSON.parse(process.env.PROD_ENV || '0');
const path = require('path');
const PATHS = {
build: path.join(__dirname, './src/public')
};
module.exports = {
entry: './src/app/App.jsx',
output: {
path: PATHS.build,
filename: PROD ? 'bundle.min.js' : 'bundle.js',
publicPath: PATHS.build
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: '/node_modules/',
query: {
presets: ['es2015', 'react', 'stage-1']
}
},
{
test: /\.s?css$/,
loaders: ['style', 'css', 'sass']
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader?name=/fonts/[name].[ext]'
},
{
test: /\.(jpg|png)$/,
loader: 'file-loader?name=/fonts/[name].[ext]'
}
]
},
plugins: PROD ? [
new webpack.optimize.UglifyJsPlugin({
beautify: false,
comments: false,
compress: {
warnings: false,
drop_console: true
},
mangle: {
except: ['$'],
screw_ie8: true,
keep_fnames: false
}
})
] : []
};