5

在使用generator-react-server加载 SVG 文件(如 follow)生成的 react-server 项目中会触发反应错误。

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
}

这是我的反应组件:

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 
4

0 回答 0