weixin_39523529
2020-12-02 10:30 阅读 138

Uncaught ReferenceError: THREE is not defined

...when I try to import this package using webpack.

I can see that there's an assumption that THREE is globally available. Wouldn't be possible to use require('three') and add it as peerDependency?

该提问来源于开源项目:gamestdio/three-text2d

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

11条回答 默认 最新

  • weixin_39955423 weixin_39955423 2020-12-02 10:30

    , you may already have a workaround, but here follows mine, in configuration.

    
    const webpack = require('webpack');
    
    var plugins = [
         new webpack.ProvidePlugin({
           THREE: "three"
             })
       ]
    
    module.exports = {
      ...
      plugins: plugins
    }
    
    点赞 评论 复制链接分享
  • weixin_40004960 weixin_40004960 2020-12-02 10:30

    Thanks for the workaround . It would be great to support webpack without having to expose THREE globally though.

    Do you guys know if there's a way to use peerDependency and be able to generate a standalone build file, without a pre-built THREE version on it, like this?

    Unfortunately not everybody uses modern build tools that relies on NPM. :(

    Cheers!

    点赞 评论 复制链接分享
  • weixin_39955423 weixin_39955423 2020-12-02 10:30

    , I'm still a webpack novice, so I don't know. Looks like peerDependency may be deprecated in favor of loader? Will look into it.

    点赞 评论 复制链接分享
  • weixin_39611037 weixin_39611037 2020-12-02 10:30

    I second pjanik's request to simply use const THREE = require('three'); (or something similar) wherever it is needed.

    Webpack can handle the rest itself, like it handles every other npm library. peerDependencies don't have much to do with webpack itself. They are not deprecated in the npm world (though they slightly changed with npm 3). You should probably add three as a peerDependency, though you should do that anyway despite of the changes we're proposing.

    I for one, use electron in my current project, which is why your workaround is not working for me (as I don't need to use webpack).

    点赞 评论 复制链接分享
  • weixin_40004960 weixin_40004960 2020-12-02 10:30

    could you use three-text2d in your project?

    点赞 评论 复制链接分享
  • weixin_39611037 weixin_39611037 2020-12-02 10:30

    : nope. that's why I am here ;) Not even sure how it could work in electron without a proper require in three-text2d as it is basically node.js. global could be a workaround, but why work around it if the actual fix is quite obvious :) or what is stopping you from using require('three')?

    点赞 评论 复制链接分享
  • weixin_40004960 weixin_40004960 2020-12-02 10:30

    I've published v0.2.3-rc1 with the peerDependencies. The only reason I was hesitant of doing it is because the file dist/three-text2d generated through browserify will now include THREE by default.

    Do you mind checking if it works in your environment ? Cheers!

    点赞 评论 复制链接分享
  • weixin_39611037 weixin_39611037 2020-12-02 10:30

    Thank you very much! I'll try and check it out next week. But looking at the commit, it should work.

    But may I ask why you are using browserify in this module? Shouldn't browserify (or webpack for that matter) rather be used in the "parent" project that uses three-text2d? Or is it just for your example in the index.html?

    点赞 评论 复制链接分享
  • weixin_40004960 weixin_40004960 2020-12-02 10:30

    thanks! Yes, it's for the example an also to people who wants to grab the library script in the old-fashion way. Maybe it doesn't make sense to support this sort of thing anymore, right?

    点赞 评论 复制链接分享
  • weixin_39611037 weixin_39611037 2020-12-02 10:30

    I don't think it is necessary anymore, but who knows. You could package an example zip and provide it as a download on github and leave it out of the repository.

    Concerning browserify including THREE by default: both browserify and webpack support externals (here and here)

    点赞 评论 复制链接分享
  • weixin_40004960 weixin_40004960 2020-12-02 10:30

    Thanks for the feedback everyone! I'll close this since it seems to be fine now. Version 0.3.0 has been released with a slight breaking change. Have a look at the changelog. Cheers!

    点赞 评论 复制链接分享

相关推荐