【发布时间】:2018-08-20 08:50:03
【问题描述】:
我使用 webpack 3 构建了我的 wordpress 主题。一切正常,但我的 sass 构建只写入了缩小的 css 文件的相对路径:
.selector { background-image: url(/img/layout/bg-contact.jpg); }
我需要以下路径:
.selector { background-image: url(wp-content/themes/themename/img/layout/bg-contact.jpg); }
我的 webpack.config.js 的相关部分:
module.exports = {
entry: {
app: [
bootstrapConfig,
'./resources/assets/scripts/main.js'
]
},
output: {
path: path.resolve(__dirname, 'wp-content/themes/themename'),
// path: path.resolve(__dirname, 'themename'),
filename: 'assets/main.min.js',
publicPath: '/'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
'file-loader?name=img/[folder]/[name].[ext]',
'image-webpack-loader'
]
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: [
{
loader: "css-loader",
options: {
sourceMap: true,
minimize: true,
modules: true,
importLoaders: 1
}
},
{
loader: "postcss-loader",
options: {
sourceMap: true,
path: './postcss.config.js'
}
},
{
loader: "sass-loader",
options: {
sourceMap: true,
includePaths: [dirAssets]
}
}
],
fallback: "style-loader",
publicPath: './'
})
},
{
test:/bootstrap-sass[\/\\]assets[\/\\]/,
use: [
{
loader: 'imports-loader'
}
]
},
{
test: /\.[ot]tf$/,
loader: 'url-loader',
options: {
limit: 50000,
mimetype: 'application/octet-stream',
name: './fonts/[name].[ext]',
publicPath: './'
}
},
{
test: /\.eot$/,
loader: 'url-loader',
options: {
limit: 50000,
mimetype: 'application/vnd.ms-fontobject',
name: './fonts/[name].[ext]',
publicPath: './'
}
},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader',
options: {
limit: 50000,
mimetype: 'application/font-woff',
name: './fonts/[name].[ext]',
publicPath: './'
}
}
]
},
resolve: {
modules: [
dirNode,
dirAssets,
path.join(__dirname, "resources")
],
extensions: ['.js', '.json', '.html', '.php', '.scss', '.css']
},
plugins: [
[ ... ]
new ExtractTextPlugin({
filename: "assets/main.min.css",
allChunks: true
}),
[ ... ]
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
Tether: "tether",
"window.Tether": "tether"
})
]
};
如何解决我的背景图像在 main.min.css 中的“/img/layout/bg-contact.jpg”之前作为路径“wp-content/themes/themename”?
【问题讨论】: