1

我有一个 movies.js 文件,其中包含:

images: {
        cover: require('assets/images/cover-bigbuckbunny.png')
      },

在组件中我动态使用它。一切正常。但是当我使用 webpack 进行构建时,最终看起来像这样:

建造

和 index.html 是

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <title>SCNSmartTV</title>
  <link href="./bundle.css" rel="stylesheet" />
</head>
<body>
  <div id="root"></div>
  <script src="./bundle.js"></script>
</body>
</html>

bundle js 引用了图片,然后我用编辑器在浏览器中打开 html 文件,控制台显示:

安慰

控制台没有显示任何错误,我看到捆绑包引用了控制台中的这个哈希,结果只是空白方块:

空白的

据我了解,图像被看到、加载、散列、引用......为什么不显示?

webpack 配置如下:

var fs = require('fs');
var path = require('path');
var webpack = require('webpack');
var CleanPlugin = require('clean-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var strip = require('strip-loader');

var root = path.resolve(__dirname, '../');

var babelrc = fs.readFileSync('./.babelrc');
var babelrcObject = {};

try {
  babelrcObject = JSON.parse(babelrc);
} catch (err) {
  console.error('==>     ERROR: Error parsing your .babelrc.');
  console.error(err);
}

var babelrcObjectDevelopment = babelrcObject.env && babelrcObject.env.development || {};

var combinedPlugins = babelrcObject.plugins || [];
combinedPlugins = combinedPlugins.concat(babelrcObjectDevelopment.plugins);

var babelLoaderQuery = Object.assign({}, babelrcObjectDevelopment, babelrcObject, {plugins: combinedPlugins});
delete babelLoaderQuery.env;

babelLoaderQuery.plugins = babelLoaderQuery.plugins || [];
var reactTransform = null;
for (var i = 0; i < babelLoaderQuery.plugins.length; ++i) {
  var plugin = babelLoaderQuery.plugins[i];
  if (Array.isArray(plugin) && plugin[0] === 'react-transform') {
    reactTransform = plugin;
  }
}

var WebpackIsomorphicToolsPlugin = require('webpack-isomorphic-tools/plugin');
var webpackIsomorphicToolsPlugin = new WebpackIsomorphicToolsPlugin(require('./webpack-isomorphic-tools'));

module.exports = {
  devtool: 'cheap-module-eval-source-map',
  entry: [
    './app/index'
  ],
  resolve: {
    root: [
      path.resolve('./app')
    ]
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/dist/'
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new ExtractTextPlugin( "bundle.css" ),
    webpackIsomorphicToolsPlugin
  ],
  module: {
    loaders: [
      { test: /\.jsx?$/, exclude: /node_modules/, loaders: ['babel?' + JSON.stringify(babelLoaderQuery), 'eslint-loader']},
      { test: /\.json$/, loader: 'json-loader' },
      { test: /\.less$/, loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=2&sourceMap!autoprefixer?browsers=last 2 version!less?outputStyle=expanded&sourceMap=true&sourceMapContents=true') },
      { test: /\.scss$/, loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=2&sourceMap!autoprefixer?browsers=last 2 version!sass?outputStyle=expanded&sourceMap=true&sourceMapContents=true') },
      { test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
      { test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
      { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream" },
      { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file" },
      { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml" },
      { test: /\.png$/, loader: "url-loader?limit=10000&mimetype=image/png" },
      { test: /\.gif$/, loader: "url-loader?limit=10000&mimetype=image/gif" },
      { test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader')},
      { test: webpackIsomorphicToolsPlugin.regular_expression('images'), loader: 'url-loader?limit=10240' }
    ]
  }
}

请帮忙,因为我尝试了很多配置和路由,但仍然没有成功并且没有想法。

顺便提一句。它可以静态工作吗?或者也许不可能在带有 index.html 和 ./bundle.js 的浏览器中启动它?还是我需要生成的任何其他必须包含在捆绑包中的额外映射文件?

4

2 回答 2

1

正如鲍勃海绵在他的评论中所写,我不得不

删除 png 和 gif 加载器,因为这种类型的文件由 webpackIsomorphicToolsPlugin.regular_expression('images') 处理

非常感谢 Bob Sponge 和 asdfasdfads。

于 2016-04-25T07:06:59.520 回答
0

由于您在 src 中有“数据:图像”,您可以尝试使用“url-loader?limit=0”,这样它就不会内联图像并引用文件 url。一旦你完成了那个报告。

于 2016-04-24T09:23:40.870 回答