【发布时间】:2020-03-04 07:29:44
【问题描述】:
在 NextJS 的开发模式下,我的 SASS 文件中的路径如下例所示:
background-image: url(/images/bg-header.png) !important;
但在生产模式下,我会将网站上传到域的子文件夹中,例如:
www.domain.com/subfolder/
我需要它像这样构建:
background-image: url(/subfolder/images/bg-header.png) !important;
我怎样才能得到它?
另外我在我的配置文件中使用了withCSS 和withSass,我尝试了几个next.config.js,但没有任何效果。
const config = {
....
webpack: (config, options) => {
// config.resolve.alias["@assets"] = '/subfolder/';
config.plugins.push(
new MiniCssExtractPlugin({
filename: 'static/css/[name].css',
chunkFilename: 'static/css/[name].chunk.css',
publicPath: '/subfolder/'
})
);
return config
},
};
module.exports = withSass(withCss(config));
我也试过了,还是不行。
config.module.rules.push({
test: /\.(png|jpg|gif|svg|ico)$/,
use: [
{
loader: "url-loader",
options: {
limit: 8192,
fallback: "file-loader",
publicPath: "/subfolder/",
outputPath: "/subfolder/",
name: "/subfolder/[name].[ext]"
}
}
]
});
【问题讨论】:
-
你也可以使用 postcss-cli 和 postcss-url 插件
-
非常感谢,由于您的回答,我已经搜索了资料,我已经能够解决了,我会发布答案。