I am new to the world of Node / NPM / Webpack, so I apologize if this is obvious.
I am trying to create a simple external project bundled with Webpack. I have node installed and the package.json file is configured. If I run "npm start" in my root directory, I get no errors from the console, and I can go to "localhost: 3000" in the browser and see my "hello world".
My next task is to include a stylesheet that contains a link to the image, for example:
.myimg {background: url(path/to/file.jpg);}
With settings like this, I can view the image through a webpack-dev server (by going to localhost: 3000 in a web browser), but if I create a project, the image path is wrong. I have no idea what I'm doing wrong, so I throw it at the Stackiverse in the hope that someone out there will find out what a stupid thing I'm doing.
Here is my package.json file:
{ "name": "webpack-test1", "version": "0.0.1", "description": "My project WTF.", "private": true, "scripts": { "start": "node server.js" }, "devDependencies": { "css-loader": "^0.15.2", "file-loader": "^0.8.4", "style-loader": "^0.12.3", "url-loader": "^0.5.6" }, "dependencies": { "webpack": "^1.9.6", "webpack-dev-server": "^1.8.2" } }
... and here is my webpack.config.js file:
var path = require('path'); var webpack = require('webpack'); module.exports = { entry: [ "./src/start.js" ], output: { filename: "bundle.js", path: path.join(__dirname, 'build'), publicPath: '/build/' }, module: { loaders: [ { test: /\.css$/, loader: 'style-loader!css-loader' }, { test: /\.(png|jpg)$/, loader: 'file-loader' } ] } };
... and then the index.html file:
<!doctype html> <html> <head> <title>Webpack Test</title> </head> <body> <div class="imgtest">hello, world</div> <script src="build/bundle.js"></script> </body> </html>
... file "start.js" specified in the configuration file:
require('./test.css'); console.log("yu no work?");
... and finally the contents of the css file itself:
.imgtest { background: url(img/volcano.jpg);}
As I said above, in the world of webpack-dev-server, the path to the image resolves correctly, and it appears as the background of the DOM element. In the published world, the browser tells me that it cannot find the file, and the wrong path to the file is clearly displayed in the Safari console:
http:
The correct local path is:
http:
Itโs clear that I am doing something wrong, but I canโt understand that. Any help or advice is appreciated.
Thank!