Webpack 2: 静态网站生成器插件 "self is not defined"

5
我遇到了 static-site-generator-webpack-plugin 的一些问题。
在我的项目中,我使用了:
  • webpack v2.1.0-beta.27
  • static-site-generator-webpack-plugin v3.1.0
  • reactreact-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。这是什么意思?
1个回答

6
我在使用 webpack-dev-server 时遇到了同样的错误。这个错误的原因是在服务器端渲染时缺少对象 self。您可以使用 scope 选项(https://github.com/markdalgleish/static-site-generator-webpack-plugin#scope) 或者避免在服务器上使用 static-site-generator-webpack-plugin。
如果您想要使用 webpack-dev-server 来解决此错误,您需要设置 inline: false (https://github.com/webpack/docs/wiki/webpack-dev-server#inline-mode)。
如果您想要在根URL中使用热重载而不使用iframe,请将 <script src="http://localhost:8080/webpack-dev-server.js"></script> 添加到您的页面中(https://github.com/webpack/docs/wiki/webpack-dev-server#inline-mode-in-html)。

1
一个该死的 inline: false 属性解决了它。简直不敢相信。你 Den 值得一块饼干。 - Daniel Birowsky Popeski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接