Kok*_*oko 6 image webpack vue.js
我正在使用 webpackfile-loader在我的构建中包含图像。Webpack 编译良好并将图像复制dev/assets到dist/images
但是生成的 HTML 中的标签是错误的:
<img src="/dist/images/triangle.svg">
Run Code Online (Sandbox Code Playgroud)
它应该是:
<img src="./images/triangle.svg">
Run Code Online (Sandbox Code Playgroud)
对于文件加载器,我的 webpack 配置是否错误?
.vue 文件:
<div>
<img src='./images/thing.png'>
</div>
.style {
background: url('./images/anotherthing.png');
}
Run Code Online (Sandbox Code Playgroud)
Webpack 配置(简化)
module.exports = {
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'images/'
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
webpack 配置(完整)
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, './dist/'),
publicPath: './',
filename: 'js/build.js'
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
'scss': 'vue-style-loader!css-loader!sass-loader',
'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax',
}
}
},
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
appendTsSuffixTo: [/\.vue$/],
}
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath:'./images/'
}
},
{
test: /\.(json)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: './data/'
}
}
]
},
resolve: {
extensions: ['.ts', '.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
},
devServer: {
contentBase: './dist',
historyApiFallback: true,
noInfo: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
Run Code Online (Sandbox Code Playgroud)
您可能只想设置publicPath: '/'.
可以将其视为publicPath相对于面向公众的域的根目录的资产目录。如果您的服务器正在提供从/dist到 的文件example.com,则意味着可以/dist/images在 处公开访问资产example.com/images(正如您所见,file-loader已经添加了该位)。/images
| 归档时间: |
|
| 查看次数: |
1922 次 |
| 最近记录: |