在 Webpack 3 中找不到静态资产

Static assets not found in Webpack 3

我习惯了 Gulp 并且我在 Webpack 和 Vue.js 中是全新的并且我迷失在所有必要的配置下

我无法在我的应用中使用字体图像等资产。我是在 .vue 组件中还是在我的 .scss 文件中调用它们。

这是我的项目结构:

这是我的 webpack.renderer.config.js:

let rendererConfig = {
  devtool: '#cheap-module-eval-source-map',
  entry: {
    renderer: path.join(__dirname, '../src/renderer/main.js')
  },
  externals: [
    ...Object.keys(dependencies || {}).filter(d => !whiteListedModules.includes(d))
  ],
  module: {
    rules: [
      {
        test: /\.(js|vue)$/,
        enforce: 'pre',
        exclude: /node_modules/,
        use: {
          loader: 'eslint-loader',
          options: {
            formatter: require('eslint-friendly-formatter')
          }
        }
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                sourceMap: true
              }
            } 
          ]
        })
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                sourceMap: true,
                importLoaders: 2
              }
            },
            {
              loader: 'postcss-loader',
              options: {
                sourceMap: 'inline'
              }
            },              
            {
              loader: 'sass-loader',
              options: {
                sourceMap: true
              }
            }
          ]
        }) 
      },
      {
        test: /\.html$/,
        use: 'vue-html-loader'
      },
      {
        test: /\.js$/,
        use: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.node$/,
        use: 'node-loader'
      },
      {
        test: /\.vue$/,
        use: {
          loader: 'vue-loader',
          options: {
            extractCSS: false,
            loaders: [
              'css-loader',
              {
                loader: 'postcss-loader',
                options: {
                  sourceMap: 'inline',
                  importLoaders: 1
                }
              },
              {
                loader: 'sass-loader',
                options: {
                  sourceMap: true
                }
              }
            ]
          }
        }
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        use: [
          {
            loader: 'url-loader',
            query: {
              // Images larger than 10 KB won’t be inlined
              limit: 10 * 1024,
              name: '[name].[hash:7].[ext]',
              outputPath: '../img/'
            }
          },
          {
            loader: 'img-loader',
            options: {
              // enabled: process.env.NODE_ENV === 'production',
              gifsicle: {
                interlaced: true
              },
              mozjpeg: {
                progressive: true,
                arithmetic: false
              },
              optipng: {
                optimizationLevel: 5
              },
              pngquant: {
                floyd: 0.5,
                speed: 2
              },
              svgo: {
                plugins: [
                  { removeViewBox: false }
                ]
              }
            }
          }
        ]
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        use: {
          loader: 'file-loader',
          query: {
            name: '../fonts/[name].[ext]'
          }
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: 'assets/media/[name]--[folder].[ext]'
        }
      }
    ]
  },
  node: {
    __dirname: process.env.NODE_ENV !== 'production',
    __filename: process.env.NODE_ENV !== 'production'
  },
  plugins: [
    new ExtractTextPlugin({
      filename: 'assets/styles/[name].css'
    }),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(__dirname, '../src/index.ejs'),
      minify: {
        collapseWhitespace: true,
        removeAttributeQuotes: true,
        removeComments: true
      },
      nodeModules: process.env.NODE_ENV !== 'production'
        ? path.resolve(__dirname, '../node_modules')
        : false
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  ],
  output: {
    filename: 'assets/scripts/[name].js',
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '../dist/electron')
  },
  resolve: {
    alias: {
      '@': path.join(__dirname, '../src/renderer'),
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['.js', '.vue', '.json', '.css', '.node']
  },
  target: 'electron-renderer'
}

我如何在 .scss 文件中加载图像和字体:

图片:

#wrapper {
  background-image: url('~@/assets/img/photo.jpeg');
  font-family: var(--RobotoRegular);
}

字体:

@font-face {
  font-family: "RobotoRegular";
  src: url("~@/assets/fonts/Roboto-Regular.woff2") format("woff2"),  
         url("~@/assets/fonts/Roboto-Regular.woff") format("woff"),
         url("~@/assets/fonts/Roboto-Regular.otf") format("opentype");  
}

我如何在 vue 组件中加载图像:

<img id="img" src="~@/assets/img/photo.jpeg" alt="img-test">

Webpack 似乎没问题:

但是服务器找不到文件,我不知道为什么

提前致谢

开发环境:

好吧,我所要做的就是在输出中指定一个 publicPath

output: {
    publicPath: '/',
    filename: 'assets/scripts/[name].js',
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '../dist/electron')
},

Whosebug 上的这个问题帮助我理解了:What does "publicPath" in Webpack do?