gpt4 book ai didi

javascript - 使用 webpack 响应静态站点

转载 作者:搜寻专家 更新时间:2023-11-01 04:40:20 25 4
gpt4 key购买 nike

我正尝试按照本教程创建静态站点 http://jxnblk.com/writing/posts/static-site-generation-with-react-and-webpack/

我目前收到此错误:ReferenceError 中的错误:文档未定义

这是我的 app.jsx 文件:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router } from 'react-router';
import routes from './config/routes';



class App extends React.Component {
constructor(props) {
super(props);
this.state = {
}
}
render() {
return (
<Router>{routes}</Router>
);
}
}
export default App;
module.exports = function render(locals, callback) {
var html = React.renderToStaticMarkup(React.createElement(App, locals))
callback(null, '<!DOCTYPE html>' + html)
}

路线:

import React from 'react';
import { Route, IndexRoute } from 'react-router';
import Layout from '../components/layout';
import Home from '../components/home';

export default (
<Route path="/" component={Layout}>
<IndexRoute component={Home} />
</Route>
);

layout.jsx 组件:

import React from 'react';
import Nav from './nav';
import Footer from './footer';

import Styles from '../styles.scss';

class Layout extends React.Component {
constructor(props) {
super(props);
this.state = {

}
}
render() {
return (
<div id="layout" className={Styles.main}>
<Nav />
{this.props.children}
<Footer />
</div>
)
}

}

Layout.PropTypes = {
children: React.PropTypes.object.isRequired
}



export default Layout;

和我的 webpack.config.js 文件:

/* eslint-disable */
var path = require('path'),
webpack = require('webpack'),
autoprefixer = require('autoprefixer'),
OpenBrowserPlugin = require('open-browser-webpack-plugin');
StaticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin');
data = require('./data');

var configServe = {
port: 9100,
};


module.exports = {
devServer: {
hot: true,
inline: true,
historyApiFallback: true,
progress: true,
port: configServe.port,
},

entry: [
'webpack/hot/dev-server',
'webpack-dev-server/client?http://localhost:' + configServe.port,
path.resolve(__dirname, './src/app.jsx'),
],
output: {
path: __dirname,
filename: './dist/bundle.js',
libraryTarget: 'umd'
},
module: {
loaders: [
{
// JSX files :
test: /\.js[x]?$/,
include: path.resolve(__dirname, 'src'),
exclude: /node_modules/,
loader: 'babel-loader?presets[]=es2015&presets[]=react',
},
{
// CSS files :
test: /\.css?$/,
include: path.resolve(__dirname, 'src'),
loader: 'style-loader!css-loader!postcss-loader',
},
{
// SCSS files :
test: /\.scss?$/,
include: path.resolve(__dirname, 'src'),
loader: 'style-loader!css-loader!postcss-loader!sass',
},
{
test: /\.(png|jpg)$/,
include: path.resolve(__dirname, 'src'),
loader: 'file-loader'
},
{
test: /\.svg$/,
loader: 'svg-inline'
}
],
},
postcss: [
autoprefixer({ browsers: ['last 3 versions'] }),
],

plugins: [
// Avoid publishing files when compilation fails
new StaticSiteGeneratorPlugin('./dist/bundle.js', data.routes, data),
new webpack.NoErrorsPlugin(),
new webpack.HotModuleReplacementPlugin(),
new OpenBrowserPlugin({ url: 'http://localhost:' + configServe.port }),
],

resolve: {
extensions: ['', '.js', '.jsx'],
},

stats: {
// Nice colored output
colors: true,
},

// Create Sourcemaps for the bundle
devtool: 'source-map',
};

希望有人能帮我解决这个问题。我想最终以一种非固执己见的方式将这一切捆绑在一起,用 React 创建静态网页并发布到 github 上。

最佳答案

我猜是这一行导致了错误:

var html = React.renderToStaticMarkup(React.createElement(Root, locals))

您正在使用一些 Root 组件,但从您的代码得出的结论是它没有定义。它也在教程中定义(你已经指出)这一行:var Root = require('./components/Root.jsx')

关于javascript - 使用 webpack 响应静态站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36929680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com