Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add favicon to /dist? #128

Closed
jforaker opened this issue May 2, 2016 · 17 comments
Closed

How to add favicon to /dist? #128

jforaker opened this issue May 2, 2016 · 17 comments

Comments

@jforaker
Copy link
Contributor

jforaker commented May 2, 2016

Seems trivial but is difficult. How to add a favicon.ico to the npm run build task?

@coryhouse
Copy link
Owner

Great question. Follow the same pattern used by tools/buildHTML. Just copy the file from the root of /src to the root of /dist

@coryhouse
Copy link
Owner

Ah, multiple ways to do that, but here's an easy way with Node: http://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js

@jforaker
Copy link
Contributor Author

jforaker commented May 2, 2016

Hmm that is one way. Here's how I did it in case anyone lands on this thread:

  1. require my favicon in my main App.js entry file
    require('../assets/images/favicon.ico')

  2. Added a loader configuration that does not scramble the image file names in the build task (per this thread Not able to bundle favicon.ico into build folder webpack/webpack#1336 (comment))

          {
              test: /\.jpe?g$|\.ico$|\.gif$|\.png$|\.svg$|\.woff$|\.ttf$|\.wav$|\.mp3$/,
              loader: 'file-loader?name=[name].[ext]'  // <-- retain original file name
          }
    
  3. Add the standard <link rel="shortcut icon" href="favicon.ico"> in src/index.html

Now npm run build will output the image/assets names as their original name, hence giving you the ability to href="favicon.ico" right in your index.html

@coryhouse
Copy link
Owner

Nice! That solution is clearly better than my idea. :) I just tried adding this to Slingshot, but I'm getting an error. Would you be willing to submit a pull request? The only change I'd suggest from your snippet is to write a specific test for only the .ico file so that other filetypes can continue to be handled separately.

@jforaker
Copy link
Contributor Author

jforaker commented May 3, 2016

Sure --> #129 😄 . Added specific test as you mentioned: https://github.com/coryhouse/react-slingshot/pull/129/files#diff-b83f3305d04bb1c8d674be30b5f9e1efR32 Tested and works well.

@chookie
Copy link

chookie commented Nov 28, 2016

If anyone is having trouble now I followed as per @jforaker but changed slightly for ejs and es6 import.

Add your favicon.ico to wherever, in my case in src/images

In index.js
import './images/favicon.ico';

In webpack.config.*.js files

new HtmlWebpackPlugin({   
    favicon: 'src/images/favicon.ico'

and
{test: /\.(jpe?g|png|gif|ico)$/i, loader: 'file?name=[name].[ext]'},

@ryangpts
Copy link

ryangpts commented Apr 4, 2017

I only needed this:

In webpack.config.*.js files

new HtmlWebpackPlugin({   
    favicon: 'src/images/favicon.ico'

@mikedevita
Copy link
Contributor

@ryangpts thanks, this is what I needed as well...

coryhouse pushed a commit that referenced this issue May 1, 2017
* include favicon in HtmlWebpackPlugin()

this helps to resolve #128, prod build wasn't copying the favicon.ico to dist/

* Update index.ejs

* Revert "Update index.ejs"

This reverts commit fdc4a2f.
@LiamKlyneker
Copy link

@ryangpts legend!!!

@StamatisDeli
Copy link

my favicon is in png format. it makes sense that it would work, but it's not

@nickytonline
Copy link
Collaborator

png should be supported. Please see https://www.w3.org/2005/10/howto-favicon @StamatisDeli.

@StamatisDeli
Copy link

StamatisDeli commented Sep 29, 2018

@nickytonline I had to do this in plugins:
new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html",
favicon: './src/images/favicon-16x16.png'
}),

@croraf
Copy link

croraf commented Oct 13, 2018

my favicon is in png format. it makes sense that it would work, but it's not

It does work in png!

@js2me
Copy link

js2me commented Nov 8, 2018

Works for me

   {
        test: /\.html$/,
        loader: 'html-loader',
        options: {
          attrs: [':data-image', ':src', 'link:href'],
        },
      },
      {
        test: /\.(png|jpg|jpeg|svg)$/,
        use: 'file-loader?name=[hash:base64:7].[ext]',
      },
      {
        test: /favicon\.png$/,
        use: 'file-loader?name=[name].[ext]',
      },

@Jackieriel
Copy link

You still do it manually
after build simply copy your favicon to dist/static/img then edit dist/index.html
simply include the favicon tag.

this works perfectly for me

@lucassalazar
Copy link

lucassalazar commented Jun 19, 2020

If u are using the hash to name the file, and using the <link/> tag to add a static favicon. Here is another solution:

In index.js
import './images/favicon.png';

In webpack.config.*.js files

            {
                test: /\.(svg|png|jpg|gif)$/,
                use: {
                    loader: "file-loader",
                    options: {
                        name (resourcePath) {
                            if (/favicon/.test(resourcePath)) {
                                return "[name].[ext]";
                            } else {
                                return "[name].[hash].[ext]";
                            }
                        },
                        publicPath: "images",
                        outputPath: "images"
                    }
                }
            },

@PerezCode
Copy link

Year 2021, that's how it worked for me:

This is the directory of my app
Captura de pantalla 2021-06-20 221337

  • First I added <link rel="shortcut icon" href="./favicon.png" type="image/png" /> to my index.html.
  • Second, I added the following code line to my webpack.config.js and webpack.config.dev.js
    Captura de pantalla 2021-06-20 221337

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests