我已经习惯了Gulp而且我在Webpack和Vue.js中都是新手,我在所有必要的配置下都迷失了
我无法在我的应用中使用像字体图像这样的资源。我是在.vue
组件中调用它们还是在.scss
个文件中调用它们。
这是我的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'
}
如何在.scss
文件中加载图片和字体:
图片:
#wrapper {
background-image: url('~@/assets/img/photo.jpeg');
font-family: var(--RobotoRegular);
}
字体:
@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");
}
如何在vue组件中加载图片:
<img id="img" src="~@/assets/img/photo.jpeg" alt="img-test">
Webpack似乎没问题:
然而,服务器找不到文件,我不知道为什么
提前致谢
开发环境:
答案 0 :(得分:1)
我所要做的就是在输出中指定publicPath
。
output: {
publicPath: '/',
filename: 'assets/scripts/[name].js',
libraryTarget: 'commonjs2',
path: path.join(__dirname, '../dist/electron')
},
StackOverflow上的这个问题帮助我理解:What does "publicPath" in Webpack do?