webpack的file-loader没有将src文件夹图像文件移动到dist文件夹

时间:2017-03-09 22:24:38

标签: webpack webpack-2 webpack-file-loader

{
        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;

3 个答案:

答案 0 :(得分:0)

它在示例中说明了您:

  

您现在可以使用require('./src/image_big.jpg');

来要求任何图像文件

如果你不在某处导入图片,webpack就不会复制它们,因为它只会将file-loader应用于它看到的导入并符合你的规则。您也可以使用url在Sass中导入它们,例如:

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

.png.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超过配置的限制。

答案 1 :(得分:0)

使用以下代码段要求src/images/main.js下的所有文件(或图片所在的位置):

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

这将使Webpack处理该文件夹中的所有文件。

答案 2 :(得分:0)

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

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

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

关于您的问题,上面示例指南中的图像是通过html文件通过<img>标记引用的。这个<img>引用由webpack获取,图像文件放在已在输出&#34; dist /&#34;中创建的资产目录中。文件夹:

&#34; app.component.html&#34;:

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

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

&#34; webpack.conf.js&#34;:

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'
        })
    ]
};