在打包的电子应用程序中找不到图像资产 - angular4 和 webpack [英] Image assets not found in packaged electron app - angular4 and webpack
问题描述
我正在使用 Angular 和 Webpack 编写一个电子应用程序,但我遇到了一个问题,即我的唯一一个图像引用在构建中以某种方式倾斜.我怀疑这是一个 webpack 配置错误,以某种方式与电子运行应用程序的操作有关 - 根据输出到 dist 时 .js 和 .png 文件所在的位置,实际结果 URL 指向正确的目录.
I'm writing an electron app using Angular and Webpack and I have an issue where my one and only reference to an image is getting skewed in the build somehow. I suspect it's a webpack config error somehow tied in to what electron does to run the app - given the actual resultant URL points to the correct directory according to where the .js and .png files sit when output to dist.
图片在 login.component.html 中引用
The image is reference in login.component.html
src 目录如下所示:
The the src directory looks like this:
src
├── app/
│ └── login/
│ ├── login.component.html
│ ├── login.component.ts
│ └── login.component.css
└── assets/
└── images/
└── someimage.png
dist 目录:
dist
├── assets/
│ └── images/
│ └── someimage.png
├── index.html
├── index.js
├── app.css
└── app.js
这是我的常用配置
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var helpers = require('./helpers');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
entry: {
'polyfills': './src/polyfills.ts',
'vendor': './src/vendor.ts',
'app': './src/app.ts',
},
resolve: {
extensions: ['.ts', '.js']
},
target: "electron-renderer",
module: {
rules: [
{
test: /\.ts$/,
loaders: [
{
loader: 'awesome-typescript-loader',
options: { configFileName: helpers.root('src', 'tsconfig.json') }
}, 'angular2-template-loader'
]
},
{
test: /\.html$/,
loader: 'html-loader'
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'file-loader?name=assets/[name].[hash].[ext]'
},
{
test: /\.scss$/,
exclude: helpers.root('src','app'),
use: [{
loader: ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css-loader?sourceMap' })
},
{
loader: 'css-loader',
options: {
sourceMaps: true
}
},
{
loader: "resolve-url-loader"
},
{
loader: "sass-loader",
options: {
sourceMaps: true
}
}]
},
{
test: /\.scss$/,
include: helpers.root('src','app'),
use: [
{
loader: 'raw-loader'
},
{
loader: "sass-loader"
}
]
}
]
},
plugins: [
// Workaround for angular/angular#11580
new webpack.ContextReplacementPlugin(
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
helpers.root('./src'), // location of your src
{} // a map of your routes
),
new webpack.optimize.CommonsChunkPlugin({
name: ['app', 'vendor', 'polyfills']
}),
new HtmlWebpackPlugin({
template: 'src/index.html'
}),
new CopyWebpackPlugin([
{from: helpers.root('./src/index.js'),to: helpers.root('./dist/index.js')},
{from: helpers.root('./package.json'),to: helpers.root('./dist/package.json')}
])
]
};
和我的产品与上述合并
const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const commonConfig = require('./webpack.common.js');
const helpers = require('./helpers');
const ElectronConnectWebpackPlugin = require('electron-connect-webpack-plugin');
const path = require('path');
const ENV = process.env.NODE_ENV = process.env.ENV = 'production';
module.exports = webpackMerge(commonConfig, {
output: {
path: helpers.root('dist'),
filename: '[name].js',
chunkFilename: '[id].[hash].chunk.js'
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new ExtractTextPlugin('[name].[hash].css'),
new webpack.DefinePlugin({
'process.env': {
'ENV': JSON.stringify(ENV)
}
}),
new webpack.LoaderOptionsPlugin({
htmlLoader: {
minimize: false // workaround for ng2
}
})
]
});
推荐答案
事实证明,index.html 缺少 <base href="./">
- 添加了这个并且网址正确解析
As it turns out, the index.html was missing a <base href="./">
- added this and the URLs resolved correctly
这篇关于在打包的电子应用程序中找不到图像资产 - angular4 和 webpack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!