jQuery需要一个带有webpack文档的窗口

时间:2016-08-31 03:25:06

标签: reactjs webpack reactjs.net

我使用ReactJS.net(服务器端渲染),当我在Webpack中使用jquery时出现错误 这是我的错误

  

渲染时出错" Components.ToDoListSkeleton"至   " react_0LmYYfSk30qdrKJQe4McUQ":错误:jQuery需要一个带有窗口的窗口   文献       at module.exports(Script Document [5]:51:87) - > ?module.exports = global.document厂(全球,真):函数(W){如果(w.document!){抛出   新错误(" jQuery需要一个带有文档的窗口");}返回   factory(w);};} else {factory(global);} //如果不是窗口,则传递此项   定义了       在新的ToDoListSkeleton(脚本文档[5]:26:903)       在ReactCompositeComponentMixin._constructComponentWithoutOwner(Script Document [2]:8271:28)       在ReactCompositeComponentMixin._constructComponent(Script Document [2]:8253:22)       在ReactCompositeComponentMixin.mountComponent(Script Document [2]:8172:22)       在ReactReconciler.mountComponent(Script Document [2]:1977:36)       在Script Document [2]:19549:37       在Mixin.perform(脚本文档[2]:3788:21)       at renderToStringImpl(Script Document [2]:19544:25)       在renderToString(Script Document [2]:19574:11)       在脚本文件[7] [temp]:1:16行:19549栏:37

这是我的webpack配置

"use strict";

var path = require('path');
var WebpackNotifierPlugin = require('webpack-notifier');
var webpack = require("webpack");
module.exports = {
    context: path.join(__dirname, 'Content'),
    entry: {
        server: './server'
    },
    output: {
        path: path.join(__dirname, 'build'),
        filename: '[name].bundle.js'
    },
    module: {
        loaders: [
            // Transform JSX in .jsx files
            { test: /\.jsx$/, loader: 'jsx-loader?harmony' },
            { test: require.resolve("jquery"), loader: "expose?$!expose?jQuery" }
        ]
    },
    resolve: {
        // Allow require('./blah') to require blah.jsx
        extensions: ['', '.js', '.jsx']
    },
    externals: {
        //// Use external version of React (from CDN for client-side, or
        //// bundled with ReactJS.NET for server-side)
        react: "React"
    },
    plugins: [
      new WebpackNotifierPlugin(),
      new webpack.ProvidePlugin({
          $: "jquery", 
          jQuery: "jquery", 
          "window.jQuery": "jquery",    
      })
    ]
};

任何帮助或建议将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:-1)

jQuery仅设计用于浏览器,支持服务器端呈现。您需要从任何要在服务器端使用的代码中删除jQuery。