I am used to Gulp and I’m totally new in Webpack and Vue.js and I’m lost under all the necessary configuration 😕
I can’t manage to use assets like images of fonts in my app. Whether I call them in a .vue
component or inside my .scss
files.
Here’s my webpack.renderer.config.js
:
let rendererConfig = {
devtool: '#cheap-module-eval-source-map',
entry: {
renderer: path.join(__dirname, '../src/renderer/main.js')
},
externals: [
...Object.keys(dependencies || {}).filter(d => !whiteListedModules.includes(d))
],
module: {
rules: [
{
test: /\.(js|vue)$/,
enforce: 'pre',
exclude: /node_modules/,
use: {
loader: 'eslint-loader',
options: {
formatter: require('eslint-friendly-formatter')
}
}
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
sourceMap: true
}
}
]
})
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 2
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: 'inline'
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
})
},
{
test: /\.html$/,
use: 'vue-html-loader'
},
{
test: /\.js$/,
use: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.node$/,
use: 'node-loader'
},
{
test: /\.vue$/,
use: {
loader: 'vue-loader',
options: {
extractCSS: false,
loaders: [
'css-loader',
{
loader: 'postcss-loader',
options: {
sourceMap: 'inline',
importLoaders: 1
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}
}
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
use: [
{
loader: 'url-loader',
query: {
// Images larger than 10 KB won’t be inlined
limit: 10 * 1024,
name: '[name].[hash:7].[ext]',
outputPath: '../img/'
}
},
{
loader: 'img-loader',
options: {
// enabled: process.env.NODE_ENV === 'production',
gifsicle: {
interlaced: true
},
mozjpeg: {
progressive: true,
arithmetic: false
},
optipng: {
optimizationLevel: 5
},
pngquant: {
floyd: 0.5,
speed: 2
},
svgo: {
plugins: [
{ removeViewBox: false }
]
}
}
}
]
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
use: {
loader: 'file-loader',
query: {
name: '../fonts/[name].[ext]'
}
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: 'assets/media/[name]--[folder].[ext]'
}
}
]
},
node: {
__dirname: process.env.NODE_ENV !== 'production',
__filename: process.env.NODE_ENV !== 'production'
},
plugins: [
new ExtractTextPlugin({
filename: 'assets/styles/[name].css'
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: path.resolve(__dirname, '../src/index.ejs'),
minify: {
collapseWhitespace: true,
removeAttributeQuotes: true,
removeComments: true
},
nodeModules: process.env.NODE_ENV !== 'production'
? path.resolve(__dirname, '../node_modules')
: false
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
],
output: {
filename: 'assets/scripts/[name].js',
libraryTarget: 'commonjs2',
path: path.join(__dirname, '../dist/electron')
},
resolve: {
alias: {
'@': path.join(__dirname, '../src/renderer'),
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['.js', '.vue', '.json', '.css', '.node']
},
target: 'electron-renderer'
}
How I load images and fonts in my .scss
files:
Images:
#wrapper {
background-image: url('~@/assets/img/photo.jpeg');
font-family: var(--RobotoRegular);
}
Fonts:
@font-face {
font-family: "RobotoRegular";
src: url("~@/assets/fonts/Roboto-Regular.woff2") format("woff2"),
url("~@/assets/fonts/Roboto-Regular.woff") format("woff"),
url("~@/assets/fonts/Roboto-Regular.otf") format("opentype");
}
How I load my images inside a vue component:
<img id="img" src="~@/assets/img/photo.jpeg" alt="img-test">
Which seems to be OK with Webpack:
However files are not found by the server and I don’t know why 😕
Thanks in advance 😃
Development environment:
Well all I had to do was to specify a publicPath
in the output.
output: {
publicPath: '/',
filename: 'assets/scripts/[name].js',
libraryTarget: 'commonjs2',
path: path.join(__dirname, '../dist/electron')
},
This question on StackOverflow helped me understand: What does "publicPath" in Webpack do?