1

我正在尝试在我的 ReactJS 应用程序中使用 GSAP 来制作客户端动画。如果我通过 npm 安装 gsap 模块,然后尝试在我的模块中使用 gsap,则会收到以下错误:

node_modules/gsap/src/uncompressed/TweenMax.js:2535
            _doc = document,
                   ^

ReferenceError: document is not defined
    at Function.<anonymous> (/Users/gcardella/Documents/TOG/SOLID/mockups/solid-website/node_modules/gsap/src/uncompressed/TweenMax.js:2535:11)
    at [object Object].check (/Users/gcardella/Documents/TOG/SOLID/mockups/solid-website/node_modules/gsap/src/uncompressed/TweenMax.js:5794:61)
    at new Definition (/Users/gcardella/Documents/TOG/SOLID/mockups/solid-website/node_modules/gsap/src/uncompressed/TweenMax.js:5811:10)
    at window._gsDefine (/Users/gcardella/Documents/TOG/SOLID/mockups/solid-website/node_modules/gsap/src/uncompressed/TweenMax.js:5816:12)
    at Array.<anonymous> (/Users/gcardella/Documents/TOG/SOLID/mockups/solid-website/node_modules/gsap/src/uncompressed/TweenMax.js:2489:11)
    at /Users/gcardella/Documents/TOG/SOLID/mockups/solid-website/node_modules/gsap/src/uncompressed/TweenMax.js:7570:9
    at Object.<anonymous> (/Users/gcardella/Documents/TOG/SOLID/mockups/solid-website/node_modules/gsap/src/uncompressed/TweenMax.js:7581:3)
    at Module._compile (module.js:435:26)
    at Module._extensions..js (module.js:442:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/gcardella/Documents/TOG/SOLID/mockups/solid-website/node_modules/babel-register/lib/node.js:138:7)

在 ReactJS 中有没有办法编码:如果它在服务器上呈现,没有文档所以不要加载 gsap,但是一旦下载了编译好的 bundle.js(我的客户端 react js 接管客户端),使用 gsap因为文件存在?

我目前正在使用的解决方法是将 gsap 嵌入 index.ejs 中:

<!DOCTYPE html>
<html>
  <head>
    <title>Solid Website</title>
  </head>

  <body>
    <section id="react-app" class="container-fluid">
      <%- markup %>
    </section>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.2/TweenMax.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.2/TimelineMax.min.js"></script>
    <script src="build.js"></script>
  </body>
</html>

更新:webpack.config.js

var path = require('path');

module.exports = {
  entry: path.join(process.cwd(), 'client-render.js'),
  output: {
    path: './public/',
    filename: 'build.js'
  },
  module: {
    loaders: [
      {
        loader: 'babel'
      }
    ]
  }
}
4

3 回答 3

3

在我的 webpack.config.js 中:

module.exports = {
  entry: path.join(process.cwd(), 'client/client-render.js'),
  output: {
    path: './public/',
    filename: 'build.js'
  },
  module: {
    loaders: [
      {
        test: /.js$/,
        loader: 'babel'
      }
    ]
  },
  /* this is new */
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production'),
        APP_ENV: JSON.stringify('browser')
      }
    })
  ]
}

然后在我的模块中:

if (process.env.APP_ENV === 'browser') {
  load_the_module_for_browser;
}
于 2016-02-02T15:49:33.727 回答
0

像这样在 webpackConfig 的 plugin 属性中注入 process.env:

plugins: [
  new webpack.DefinePlugin({
    'process.env': {
      NODE_ENV: JSON.stringify(isDevelopment ? 'development' : 'production'),
      IS_BROWSER: true
    }
  })
]
于 2016-02-04T14:26:22.323 回答
0

尝试在配置中添加 web 作为目标。https://webpack.github.io/docs/configuration.html#target它应该是默认的。

var path = require('path');

module.exports = {
  entry: path.join(process.cwd(), 'client-render.js'),
  output: {
    path: './public/',
    filename: 'build.js'
  },
  module: {
    loaders: [
      {
        loader: 'babel'
      }
    ]
  },
  target: "web",
}
于 2016-02-02T14:56:23.713 回答