0
{
        test: /\.(png|jpg|gif)$/,
        loader: 'file-loader?name=img/img-[hash:6].[ext]',
      },

我将此配置添加到通过将上述规则添加到webpack.config.js中来使用file-loader

module: {
    rules: [

部分,但没有图像文件移动到dist/文件夹,即使我将图像文件放入src/images/. 有什么我错过的东西需要添加到 webpack 配置中吗?

我正在遵循https://julienrenaux.fr/2015/03/30/introduction-to-webpack-with-practical-examples/中的示例

顺便说一句,我正在使用 webpack 2... 现在有不同的方式来配置文件加载器吗?

这是整个 webpack 配置文件

const { resolve } = require('path');

const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');

const config = {
  devtool: 'cheap-module-eval-source-map',

  entry: [
    'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    './main.js',
    './assets/scss/main.scss',
  ],

  output: {
    filename: 'bundle.js',
    path: resolve(__dirname, 'dist'),
    publicPath: '/',
  },

  context: resolve(__dirname, 'app'),

  devServer: {
    hot: true,
    contentBase: resolve(__dirname, 'dist'),
    publicPath: '/',
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        loaders: [
          'babel-loader',
        ],
        exclude: /node_modules/,
      },
      {
        test: /\.scss$/,
        exclude: /node_modules/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            'css-loader',
            {
              loader: 'sass-loader',
              query: {
                sourceMap: false,
              },
            },
          ],
        }),
      },
      { test: /\.(png|jpg)$/, use: 'url-loader?limit=15000' },
      { test: /\.eot(\?v=\d+.\d+.\d+)?$/, use: 'file-loader' },
      { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: 'url-loader?limit=10000&mimetype=application/font-woff' },
      { test: /\.[ot]tf(\?v=\d+.\d+.\d+)?$/, use: 'url-loader?limit=10000&mimetype=application/octet-stream' },
      { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, use: 'url-loader?limit=10000&mimetype=image/svg+xml' },
      {
        test: /\.(png|jpg|gif)$/,
        loader: 'file-loader?name=img/img-[hash:6].[ext]',
      },
    ],
  },

  plugins: [
    new ExtractTextPlugin({ filename: 'style.css', disable: false, allChunks: true }),
    new CopyWebpackPlugin([{ from: 'vendors', to: 'vendors' }]),
    new OpenBrowserPlugin({ url: 'http://localhost:8080' }),
    new webpack.HotModuleReplacementPlugin(),
  ],
};

module.exports = config;
4

3 回答 3

4

使用以下代码段要求src/images/内部(或您的图像所在的任何位置)下的所有文件main.js

// load assets
function requireAll(r) { r.keys().forEach(r); }
requireAll(require.context('src/images/', true));

这将使 Webpack 处理位于该文件夹内的所有文件。

于 2017-03-10T03:52:34.537 回答
0

它在您遵循的示例中说:

require('./src/image_big.jpg')您现在可以使用;要求任何图像文件

如果你没有在某处导入图像,webpack 不会复制它们,因为它只会将 应用于file-loader它看到的导入并匹配你的规则。你也可以在 Sass 中导入它们,url例如:

background-image: url('./src/image.jpg');

.png对于and ,您还有两条规则.jpg

{ test: /\.(png|jpg)$/, use: 'url-loader?limit=15000' },
{
  test: /\.(png|jpg|gif)$/,
  loader: 'file-loader?name=img/img-[hash:6].[ext]',
},

选择其中之一。因为现在它会同时应用两者,但只有其中一个真正被使用,所以当图像大于 15kb 时你最终会得到两个图像,因为当文件大于配置的限制时url-loader会回退到。file-loader

于 2017-03-10T01:52:57.220 回答
0

参考:https ://angular.io/docs/ts/latest/guide/webpack.html

我建议尝试上面引用的指南。请注意,您需要运行以下命令才能使其正常工作:

npm install --save-dev karma-jasmine-html-reporter

关于您的问题,上述示例指南中的图像是<img>通过 html 文件的标签引用的。这个<img>引用被 webpack 拾取,并且图像文件被放置在一个资产目录中,该目录已经在输出“dist/”文件夹中创建:

“app.component.html”:

<main>
    <h1>Hello from Angular App with Webpack</h1>
    <img src="../assets/images/angular.png">
</main>

这是他们的 webpack 配置文件和他们的加载器:

“webpack.conf.js”:

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var helpers = require('./helpers');

module.exports = {
    entry: {
        'polyfills': './src/polyfills.ts',
        'vendor': './src/vendor.ts',
        'app': './src/main.ts'
    },

    resolve: {
        extensions: ['.ts', '.js']
    },

    module: {
        rules: [
            {
                test: /\.ts$/,
                loaders: [
                    {
                        loader: 'awesome-typescript-loader',
                        options: { configFileName: helpers.root('src', 'tsconfig.json') }
                    } , 'angular2-template-loader'
                ]
            },
            {
                test: /\.html$/,
                loader: 'html-loader'
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: 'file-loader?name=assets/[name].[hash].[ext]'
            },
            {
                test: /\.css$/,
                exclude: helpers.root('src', 'app'),
                loader: ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css-loader?sourceMap' })
            },
            {
                test: /\.css$/,
                include: helpers.root('src', 'app'),
                loader: 'raw-loader'
            }
        ]
    },

    plugins: [
        // Workaround for angular/angular#11580
        new webpack.ContextReplacementPlugin(
            // The (\\|\/) piece accounts for path separators in *nix and Windows
            /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
            helpers.root('./src'), // location of your src
            {} // a map of your routes
        ),

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

        new HtmlWebpackPlugin({
            template: 'src/index.html'
        })
    ]
};
于 2017-03-14T20:01:16.213 回答