我正在尝试在我的 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'
}
]
}
}