Hi everyone!
I am used to Gulp and I鈥檓 totally new in Webpack and Vue.js and I鈥檓 lost under all the necessary configuration 馃槙
I can鈥檛 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鈥檚 my project structure:

Here鈥檚 my webpack.renderer.config.js:
'use strict'
process.env.BABEL_ENV = 'renderer'
const path = require('path')
const { dependencies } = require('../package.json')
const webpack = require('webpack')
const BabiliWebpackPlugin = require('babili-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
/**
* List of node_modules to include in webpack bundle
*
* Required for specific packages like Vue UI libraries
* that provide pure *.vue files that need compiling
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/webpack-configurations.html#white-listing-externals
*/
let whiteListedModules = ['vue']
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
}
},
{
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'
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}
}
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
use: [
{
loader: 'url-loader',
query: {
// Images larger than 10 KB won鈥檛 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: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: 'assets/media/[name]--[folder].[ext]'
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
use: {
loader: 'file-loader',
query: {
name: '../fonts/[name].[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'
}
/**
* Adjust rendererConfig for development settings
*/
if (process.env.NODE_ENV !== 'production') {
rendererConfig.plugins.push(
new webpack.DefinePlugin({
'__static': `"${path.join(__dirname, '../static').replace(/\\/g, '\\\\')}"`
})
)
}
/**
* Adjust rendererConfig for production settings
*/
if (process.env.NODE_ENV === 'production') {
rendererConfig.devtool = ''
rendererConfig.plugins.push(
new BabiliWebpackPlugin(),
new CopyWebpackPlugin([
{
from: path.join(__dirname, '../static'),
to: path.join(__dirname, '../dist/electron/static'),
ignore: ['.*']
}
]),
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
)
}
module.exports = rendererConfig
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鈥檛 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: https://stackoverflow.com/questions/28846814/what-does-publicpath-in-webpack-do
Most helpful comment
Well all I had to do was to specify a
publicPathin theoutput.This question on StackOverflow helped me understand: https://stackoverflow.com/questions/28846814/what-does-publicpath-in-webpack-do