gpt4 book ai didi

css - 无法使用 sass-loader (Webpack) 加载背景图像

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:49 24 4
gpt4 key购买 nike

我在尝试让浏览器在使用 webpack 和 sass-loader/style-loader/css-loader 时成功找到背景图片时遇到了问题。

路径似乎是正确的,因为每当我更改它时,编译过程都会失败,但实际上是可以的。

到目前为止我已经...

组件

import React from 'react'

const Image = () => (
<div className='sprite-container sprite-1'>
</div>
)

export default Image

CSS

.sprite-container {
width: 100px;
height: 100px;
border-radius: 100%;
border: 1px solid blue; // I put this just to confirm the container div is there, which it is.
background-image: url('/img/spritesheet.png');
background-repeat: no-repeat;
position: absolute;
top: 250px;
right: 20px;
}

.sprite-1 {
background-position: -100px, -100px;
}

事实上,div 是透明的。容器在那里,但背景图像加载失败。我刚开始在 Webpack 中编译 SASS,所以这可能与我的文件结构有关。

这是我的文件树的相关部分:

- src
- static (all static assets are served from this folder)
- img
-- spritesheet.png
- styles
-- app.scss
-- app-client.js (importing app.scss here)

我正在将 app.scss 导入我的主 js 文件 app-client.js(React 将其装载到应用程序)。

background-image css 属性中给出的路径是否需要相对于根目录或样式表?我假设是根目录 (/static)。

感谢任何帮助。


更新

文件树

- src
- static (all static assets are served from this folder)
- img
-- spritesheet.png
- js
-- bundle.js
- styles
-- app.scss
-- app-client.js (importing app.scss here)

webpack.config.js

const debug = process.env.NODE_ENV !== "production";

const webpack = require('webpack');
const path = require('path');

// const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
devtool: debug ? 'inline-sourcemap' : null,
entry: path.join(__dirname, 'src', 'app-client.js'),
devServer: {
inline: true,
port: 3333,
contentBase: "src/static/",
historyApiFallback: {
index: '/index-static.html'
}
},
output: {
path: path.join(__dirname, 'src', 'static', 'js'),
publicPath: "/js/",
filename: 'bundle.js'
},
module: {
loaders: [
{
test: path.join(__dirname, 'src'),
loader: ['babel-loader'],
query: {
cacheDirectory: 'babel_cache',
presets: debug ? ['react', 'es2015', 'react-hmre'] : ['react', 'es2015']
}
},
{
test: /\.scss$/,
loaders: [ 'style', 'css?sourceMap', 'sass?sourceMap' ]
// loader: ExtractTextPlugin.extract(
// 'style', // The backup style loader
// 'css?sourceMap!sass?sourceMap'
// )
},
{
test: /\.png$/,
loader: "url-loader?limit=10000&minetype=image/jpg"
}
]
},
plugins: debug ? [] : [
// new ExtractTextPlugin('styles.css'),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false },
mangle: true,
sourcemap: false,
beautify: false,
dead_code: true
}),
]
};

package.json

{
"name": "***",
"version": "1.0.0",
"description": "***",
"main": "src/server.js",
"repository": "**REPO**",
"scripts": {
"start": "NODE_ENV=production node_modules/.bin/babel-node --presets 'react,es2015' src/server.js",
"start-dev": "npm run start-dev-hmr",
"start-dev-single-page": "node_modules/.bin/http-server src/static",
"start-dev-hmr": "node_modules/.bin/webpack-dev-server --progress --inline --hot",
"build": "NODE_ENV=production node_modules/.bin/webpack -p"
},
"author": "***",
"license": "UNLICENSED",
"dependencies": {
"axios": "^0.15.3",
"babel-cli": "^6.11.4",
"babel-core": "^6.13.2",
"babel-loader": "^6.2.5",
"babel-plugin-react-html-attrs": "^2.0.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-object-rest-spread": "^6.22.0",
"babel-preset-es2015": "^6.13.2",
"babel-preset-react": "^6.11.1",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-2": "^6.22.0",
"ejs": "^2.5.1",
"express": "^4.14.0",
"react": "^15.3.1",
"react-dom": "^15.3.1",
"react-redux": "^5.0.2",
"react-router": "^2.6.1",
"redux": "^3.6.0",
"redux-logger": "^2.7.4",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"css-loader": "^0.26.1",
"file-loader": "^0.9.0",
"http-server": "^0.9.0",
"node-sass": "^4.3.0",
"react-hot-loader": "^1.3.0",
"sass-loader": "^4.1.1",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.14.1"
}
}

最佳答案

我遇到了同样的问题。我发现您可以在 css-loader 中包含 ?url=false 以禁用 url 处理。然后你可以在你的公用文件夹中放置一个图像以供 css 访问。该图像不会通过 webpack 运行,但它会让您克服 webpack 的编译错误并仍然允许访问图像。

所以这是我在 module.loaders 数组中的新对象:

{
test: /\.s?css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?url=false', 'sass-loader']
})
}

关于css - 无法使用 sass-loader (Webpack) 加载背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41783040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com