Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving static assets in webpack dev server

I run webpack-dev-server from the root folder of my project. I have assets folder in /src/assets that is copied by CopyWebPackPlugin:

new CopyWebpackPlugin([ { from: 'src/assets', to: 'assets' } ]) 

If I put logo.png inside assets folder then After running webpack-dev-server I can't access http://localhost/assets/logo.png file, but can access http://localhost/src/assets/logo.png file. However if I run in production mode the situation turns upside down.

How to configure webpack server to make http://localhost/assets/logo.png file accessible in development mode?

like image 604
Roman Kolesnikov Avatar asked Jan 19 '16 07:01

Roman Kolesnikov


People also ask

Where does webpack-dev-server serve files from?

Content Base. The webpack-dev-server will serve the files in the current directory, unless you configure a specific content base. Using this config webpack-dev-server will serve the static files in your public folder. It'll watch your source files for changes and when changes are made the bundle will be recompiled.

Does webpack automatically minify?

Webpack v4+ will minify your code by default in production mode .

What is devServer?

A dev server is typically an internal web server used for testing and running code in development. It is a web server.


2 Answers

I would add that it was the opposite for me. I originally had my images and .obj/.mtl files in a public folder that existed at the root of my application. I moved them into an assets folder that was created in the app folder.

Performing an npm install --save-dev copy-webpack-plugin and adding the

new CopyWebpackPlugin([ { from: 'src/assets', to: 'assets' } ]) 

to the webpack.common.js file fixed my problem.

like image 169
Katana24 Avatar answered Sep 24 '22 08:09

Katana24


You can tell webpack to use a different path when loading from the browser.

In the output section of your webpack config file add a publicPath field pointing to your assets folder.

webpack.config.js

output: {   // your stuff   publicPath: '/assets/' } 
like image 43
dreyescat Avatar answered Sep 23 '22 08:09

dreyescat