Webpack 2文件加载器返回stringified module.exports

时间:2017-05-11 09:16:43

标签: javascript webpack webpack-file-loader

当我尝试通过

导入一些资产(不是样式,它们正常工作)中的图像或字体
import Logo from 'img/logo.png'

我收到了奇怪的JSON.stringified字符串

module.exports = __webpack_public_path__ + "img/logo.png";

当我将publicPath设置为/时,我会收到:

module.exports = "img/logo.png";

这是我的Webpack配置。我做错了什么?

const isDevelopment = process.env.NODE_ENV!==' production';

const CONTEXT = 'frontend';
const alias = ['img', 'img/icons', '/scss', '/src', '/src/_common', '/src/_common/functions'];

const PLUGINS = [
  new ExtractTextPlugin({
    filename: (isDevelopment) ? '[name].css' : '[hash:12].css',
    disable: isDevelopment && !process.env.CSS
  }),
  new webpack.DefinePlugin({
    NODE_ENV: JSON.stringify(process.env.NODE_ENV)
  }),
  new HtmlWebpackPlugin({
    template: './index.html'
    // favicon: './favicon.ico'
  }),
  new webpack.optimize.CommonsChunkPlugin({
    name: 'vendor',
    minChunks: Infinity
  })
];

if (isDevelopment) {
  PLUGINS.push(new webpack.NamedModulesPlugin());
} else {
  PLUGINS.push(new webpack.optimize.UglifyJsPlugin());
  PLUGINS.push(new webpack.BannerPlugin({
    banner: `
    /* ${packageJSON.name} app v${packageJSON.version}.
    * Under ${packageJSON.license} protection.
    * Made by: ${packageJSON.author}.
    * ================================================================== */`,
    entryOnly: true,
    raw: true
  }));
}

const jsLoader = {
  test: /\.jsx?$/,
  exclude: /node_modules/,
  use: 'babel-loader'
};


const imageLoader = {
  test: /\.(png|jpe?g|gif|svg)?$/,
  exclude: /node_modules/,
  use: [{
    loader: 'file-loader',
    options: {
      publicPath: '',
      outputPath: '',
      name: (isDevelopment) ? '[path][name].[ext]' : '[hash:12].[ext]'
    }
  }]
};
if (!isDevelopment) {
  imageLoader.use.push({
    loader: 'image-webpack-loader',
    options: {

    }
  });
}

const fontLoader = {
  test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9#=&.]+)?$/,
  include: /font/,
  exclude: /(node_modules|img)/,
  use: {
    loader: 'file-loader',
    options: {
      name: (isDevelopment) ? '[path][name].[ext]' : '[hash:12].[ext]'
    }
  }
};

module.exports = {
  devtool: (isDevelopment) ? 'source-map' : false,
  context: path.resolve(__dirname, CONTEXT),
  entry: {
    vendor: Object.keys(packageJSON.dependencies),
    app: ['babel-polyfill', './src/app.js']
  },
  output: {
    path: path.resolve(__dirname, (isDevelopment) ? CONTEXT : 'build'),
    publicPath: '',
    filename: (isDevelopment) ? '[name].js' : '[chunkhash:12].js'
  },
  resolve: {
    alias: Array.prototype.reduce.call(alias, (acc, v) => {
      acc[v.split(/\/_?/).pop()] = path.join(__dirname, CONTEXT, v);
      return acc;
    }, {}),
    extensions: ['.js', '.jsx']
  },
  plugins: PLUGINS,
  module: {
    loaders: [jsLoader, sassLoader, svgLoader, imageLoader, fontLoader]
  }
};

0 个答案:

没有答案
相关问题