需要SVG导致react-server错误:“React未定义”

时间:2016-10-28 19:46:25

标签: reactjs svg webpack react-server

在使用generator-react-server生成的反应服务器项目中加载SVG文件,如跟踪会触发反应错误。

const fmc = require('../images/logo.svg');

给出错误

Failed to load page at "../pages/home.js" ReferenceError: React is not defined
at Object.<anonymous> (/Users/me/Projects/myProject/dev/images/logo.svg:1:1)
at Module._compile (module.js:541:32)
at loader (/Users/svasseur/Projects/delateurs/dev/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/svasseur/Projects/delateurs/dev/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/me/Projects/myProject/dev/components/footer.js:5:13)

我尝试为SVG加载程序指定webpack文件加载器,就像我在之前的项目中所做的那样,但它没有用。以下是我在 .reactserverrc 中定位的 webpack.conf.js 文件"webpack-config": "./webpack.config.js"

export default (webpackConfig) => {
  webpackConfig.module.loaders.splice(0, 0,
    { test: /\.woff(2)?(\?v=\d+\.\d+\.\d+)?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
    { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url-loader?limit=10000&mimetype=application/octet-stream" },
    { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader" },
    { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=image/svg+xml" },
    {
      test: /\.(png|jpg|gif)$/,
      loader: 'url-loader?limit=8192'
    },
    {
      test: /\.(mp4|ogg)$/,
      loader: 'file-loader'
    }
  )
  return webpackConfig
}

这是我的React组件:

import React from 'react';
require('../styles/footer.scss');

const myLogo = require('../images/logo.svg');

export default class Footer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  render() {
    return (
        <div>
            <nav className="flexcontainer">
                <div className="flexitem"><img src={myLogo} className="footer-logo"/></div>
            </nav>
        </div>
    );
  }
}

那就是说,要求jsx中的SVG像这样有效:

<img src={require('../images/logo.svg')} />

我正在使用节点v6.2.2和以下模块

└── react@0.14.8
└── react-server@0.4.8 
└─┬ react-server-cli@0.4.9
  └── webpack@1.13.2 

0 个答案:

没有答案