2016-11-27 163 views
2

我有一些static-site-generator-webpack-plugin的问题。Webpack 2:static-site-generator-plugin“self is not defined”

在我的项目我使用:

  • 的WebPack V2.1.0-beta.27
  • 静态站点发电机的WebPack-插件 V3.1.0
  • 反应react-dom v15.4.1

这是一个单一的网页。

这里是webpack.config.babel.js文件的一部分:

import StaticSiteGeneratorPlugin from 'static-site-generator-webpack-plugin' 
 
    
 
export default { 
 
    entry: { 
 
     main: './components/Root/index.jsx', 
 
    }, 
 
    output: { 
 
     path: path.join(process.cwd(), './public'), 
 
     filename: '[name].[hash].js', 
 
     libraryTarget: 'umd', 
 
    }, 
 
    
 
     plugins: [ 
 
     // ... 
 
     new StaticSiteGeneratorPlugin('main', '/', {}), 
 
     ] 
 
     // ... 
 
    }

这是./components/Root/index.jsx文件:

import React from 'react' 
 
import ReactDOMServer from 'react-dom/server' 
 
import HTMLDocument from 'react-html-document' 
 

 
import App from '../App/index.jsx' 
 

 
export default function (locals, callback) { 
 
    const MyHtml = (
 
     <HTMLDocument 
 
      title='My Page' 
 
      metatags={[ 
 
       { name: 'description', content: 'My description' }, 
 
      ]} 
 
      scripts={[ `${locals.assets.main}` ]}> 
 
      <App /> 
 
     </HTMLDocument> 
 
    ) 
 

 
    const html = ReactDOMServer.renderToStaticMarkup(MyHtml, locals.assets) 
 
    callback(null, '<!doctype html>' + html) 
 
}

当我尝试使用它时,我看到错误消息:ERROR in ReferenceError: self is not defined。这是什么意思?

回答

3

我和webpack-dev-server发生了同样的错误。此错误的原因是在服务器上呈现。服务器没有对象self。您可以使用scope选项(https://github.com/markdalgleish/static-site-generator-webpack-plugin#scope)或者只是避免在服务器上使用static-site-generator-webpack-plugin。

如果使用的WebPack-DEV-服务器解决这个错误,你必须设置inline: falsehttps://github.com/webpack/docs/wiki/webpack-dev-server#inline-mode)。 如果您要在根网址中使用热重新加载,而不使用iframe,只需将<script src="http://localhost:8080/webpack-dev-server.js"></script>添加到您的网页(https://github.com/webpack/docs/wiki/webpack-dev-server#inline-mode-in-html)。

+0

friggin'inline:false'属性解决了它。不敢相信。你应该得到一个饼干。 – Birowsky

相关问题