grainne6
Adding background image throws a module load error
When I add background-image: url(../images/my-image.png) to phoenix.css, the image displays correctly but the console shows an error
Uncaught Error: Module build failed: ModuleParseError: Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
at handleParseError (c:\projects\Phoenix\filmhub\assets\node_modules\webpack\lib\NormalModule.js:364:19)
This also happens with a brand new project so I know it’s not a change I made.
I also tried using background-image: url(“<%= Routes.static_path(@conn, “/images/my-image.png”) %>”); but that doesn’t work at all.
I am new to phoenix so I’m not sure what else to try, what is the correct way to display background images in phoenix? Thanks
Most Liked Responses
peerreynders
Just tried this on a vanilla Phoenix install.
/* This file is for your main application css. */
@import "./phoenix.css";
body {
background-image: url("/images/phoenix.png");
background-position: bottom;
background-repeat: no-repeat;
}
works without any change to the stock webpack config:
const path = require('path');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = (env, options) => ({
optimization: {
minimizer: [
new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),
new OptimizeCSSAssetsPlugin({})
]
},
entry: {
'./js/app.js': ['./js/app.js'].concat(glob.sync('./vendor/**/*.js'))
},
output: {
filename: 'app.js',
path: path.resolve(__dirname, '../priv/static/js')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
]
},
plugins: [
new MiniCssExtractPlugin({ filename: '../css/app.css' }),
new CopyWebpackPlugin([{ from: 'static/', to: '../' }])
]
});
So as suggested before - hand-type the image path into the app.css and see what happens.
package.json
{
"repository": {},
"license": "MIT",
"scripts": {
"deploy": "webpack --mode production",
"watch": "webpack --mode development --watch"
},
"dependencies": {
"phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"babel-loader": "^8.0.0",
"copy-webpack-plugin": "^4.5.0",
"css-loader": "^2.1.1",
"mini-css-extract-plugin": "^0.4.0",
"optimize-css-assets-webpack-plugin": "^4.0.0",
"uglifyjs-webpack-plugin": "^1.2.4",
"webpack": "4.4.0",
"webpack-cli": "^2.0.10"
}
}
adrianrl
Hi,
body {
background-image: url('/images/my-image.png');
}
<%= %> tags only works inside eex templates.
peerreynders
file-loader is the default fallback loader when the file exceeds the specified limit.
- install
file-loaderso that the fallback works - increase the specified
limitto be large enough so that your image will get processed (or remove it altogether).








