david2tdw / blog

学习记录
1 stars 1 forks source link

[webpack] favicon打包到不同位置 #93

Open david2tdw opened 4 years ago

david2tdw commented 4 years ago
  1. 修改config/index.js文件

    build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/sysmanage/index.html'),
    
    // Paths
    assetsRoot: path.resolve(__dirname, '../dist/sysmanage'),
    assetsSubDirectory: 'static',
    
    /**
     * You can set by youself according to actual condition
     * You will need to set this if you plan to deploy your site under a sub path,
     * for example GitHub pages. If you plan to deploy your site to https://foo.github.io/bar/,
     * then assetsPublicPath should be set to "/bar/".
     * In most cases please use '/' !!!
     */
    assetsPublicPath: '/sysmanage/',

    index: 生成的index目录。 assetsRoot: 编译文件根目录。 assetsSubDirectory: 资源文件目录 assetsPublicPath: 打包后资源文件路径前缀。

  2. 修改webpack.prod.conf.js

    
    new HtmlWebpackPlugin({
      filename: config.build.index,
      template: 'index.html',
      inject: true,
      favicon: 'static/bb.ico',
      title: 'vue-admin-template',
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      }
      // default sort mode uses toposort which cannot handle cyclic deps
      // in certain cases, and in webpack 4, chunk order in HTML doesn't
      // matter anyway
    }),

HtmlWebpackPlugin的favicon属性会去相应路径寻找favicon的内容,然后打包到根目录。
3. favicon文件要放在static目录下。
![image](https://user-images.githubusercontent.com/24794474/70498069-347a8600-1b50-11ea-8468-3bdd345d30fc.png)

4. 打包后生成路径
![image](https://user-images.githubusercontent.com/24794474/70498095-4ceaa080-1b50-11ea-9418-4504002e2222.png)

5. index.html内容
![image](https://user-images.githubusercontent.com/24794474/70498157-7a374e80-1b50-11ea-84e7-73d5e97fc241.png)
david2tdw commented 4 years ago

其他方案