webpack文件加载器输出损坏的图像

时间:2018-04-24 18:38:52

标签: javascript webpack webpack-file-loader

使用最新的file-loader。它会像指定的那样将文件输出到images/css-urls,但它们会损坏图像。

配置正在搜索sass文件并用文件加载器输出的新文件替换url。该部分有效,但文件已损坏。

const ExtractTextPlugin = require("extract-text-webpack-plugin");
const extractSass = new ExtractTextPlugin("./bundle.css");

var mainConfig = {
  entry: {
    "app": [
      "./app/main.ts",
      "./static/scss/main.scss",
    ],
    "vendor": "./app/vendor.ts",
    "polyfills": "./app/polyfills.ts",
  },
  output: {
    path: path.resolve(__dirname, "static"),
    filename: "[name].js"
  },
  resolve: {
    extensions: [".ts", ".js"],
  },
  devtool: "source-map",
  module: {
    rules: [{
      test: /.*\.(gif|png|jpe?g|svg)$/i,
      use: [
        {
          loader: 'file-loader',
          options: {
            name: '[name].[hash].[ext]',
            outputPath: 'images/css-urls/',
          }
        },
      ]
    }, {
      test: /\.(scss|css)$/,
      use: extractSass.extract({
        use: [{
          loader: "css-loader",
        }, {
          loader: "sass-loader",
        }],
        // use style-loader in development
        fallback: "style-loader"
      })
    }, {
      test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
      loader: "url-loader",
    }, {
      test: /\.ts$/,
      loaders: [
        {
          loader: "awesome-typescript-loader",
          options: { configFileName: root("./", "tsconfig.json") }
        } , "angular2-template-loader"
      ]
    }, {
      test: /\.html$/,
      loader: "html-loader"
    }]
  },
  plugins: [
    extractSass,

    new webpack.ContextReplacementPlugin(
      // The (\\|\/) piece accounts for path separators in *nix and Windows
      /angular(\\|\/)core(\\|\/)@angular/,
      root("./app"), // location of your src
      {} // a map of your routes
    ),

    new webpack.optimize.CommonsChunkPlugin({
      name: ["app", "vendor", "polyfills"]
    }),

  ]
}

1 个答案:

答案 0 :(得分:0)

我通过删除文件加载器规则并用此

替换url-loader规则来实现它
{
  test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
  loader: "url-loader",
  options: {
    limit: 8192,
    fallback: "file-loader",

    // fallback options
    name: '[name].[hash].[ext]',
    outputPath: 'images/css-urls/',
  },
}