我正在爲我的PHP和React項目使用webpack。我想使用webpack文件加載器從我的.scss文件加載背景圖像,但由於某些原因,我不知道,img
-文件夾未被複制/導出到我的dist
-文件夾。下面是webpack.config.js:Webpack文件加載器無法導出圖像
var webpack = require("webpack");
var path = require("path");
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var WatchLiveReloadPlugin = require('webpack-watch-livereload-plugin');
var DIST_DIR = path.resolve(__dirname, "dist");
var SRC_DIR = path.resolve(__dirname, "src");
var extractPlugin = new ExtractTextPlugin({
filename: 'main.css'
});
module.exports = {
entry: ['babel-polyfill', SRC_DIR + "/app/index.js"],
output: {
path: DIST_DIR + "/app",
filename: "bundle.js",
publicPath: "/dist"
},
watch: true,
module: {
rules: [
{
test: /\.js$/,
include: SRC_DIR,
loader: "babel-loader",
exclude: /node_modules/,
query: {
presets: ["react", "es2015", "stage-2"], plugins: ["transform-decorators-legacy", "transform-class-properties"]
}
},
{
test: /\.scss$/,
use: extractPlugin.extract({
fallback: "style-loader",
use: ["css-loader", "sass-loader", "resolve-url-loader"]
})
},
{
test: /\.css$/,
use: ["css-loader", "sass-loader", "resolve-url-loader"]
},
{
test: /\.(jpg|png)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'img/',
publicPath: 'img/'
}
}
]
}
]
},
plugins: [
extractPlugin,
new WatchLiveReloadPlugin({
port: 'localhost',
files: [
'./dist/app/*.css',
'./dist/**/*.js',
'./src/app/**/*.png',
'./src/app/**/*.jpg',
'./src/app/**/.*.scss',
'./src/**/*.php',
'./src//*.js'
]
})
]
};
我也試過loader: 'file-loader?name=/dist/img/[name].[ext]',
,但沒有運氣。
我的文件結構是這樣的:
-- dist
-- app
bundle.js
main.css
-- src
-- app
-- css
main.scss
-- img
someimage.jpg
然後在我的.scss
我想這:
background-image: url('/img/someimage.jpg');
有沒有人有一個想法有什麼不對嗎?
確定這工作到目前爲止,但如果該文件夾中有多個圖像呢?我不想導入每個單一的圖像文件 – Steve
https://stackoverflow.com/questions/37313954/how-to-url-loader-multiple-images-in-webpack還沒有嘗試過,但它看起來很有希望。 –
哦,男人認真嗎?爲什麼webpack使事情變得如此複雜?!?無論如何,感謝您的建議:-) – Steve