Fine Uploader with Webpack & ES6

◇◆丶佛笑我妖孽 提交于 2019-12-02 02:23:39

Figured it out!

First, I had to install the exports-loader for webpack. This loader will shim non-CommonJS/UMD/AMD bundles so you can require or import them (read more here). Then I had to edit my webpack.config.js to shim the qq namespace for the type of Fine Uploader I am using (S3 in this case):

webpack.config.js:

module: {
    loaders: [
        {
            test: /s3\.fine-uploader\.js/,
            loader: 'exports?qq'
        }
    ]
}

Now I am able to import the qq object from Fine Uploader and access it just as I would normally, except it's not on the global namespace. Win!

Note that I had to include a ref to the React-rendered DOM element so Fine Uploader knew where to attach itself. Also, note the use of componentDidMount to ensure the element has been rendered by React.

fine-uploader.jsx:

import React from 'react'

import qq from 'fine-uploader/s3.fine-uploader'

class FU extends React.Component {
  constructor (props) {
    super(props)
  }

  componentDidMount () {
    const fu = new qq.s3.FineUploaderBasic({
      button: this.refs.fu
    })
  }

  render () {
    return <div ref='fu'>Upload!</div>
  }
}

export default FU

I'll probably have more questions as I try and integrate this library with React and the modern Javascript ecosystem.

Just updated to NPM release 5.9.0 and can see they now export modules. This means you can import it into your component without having to use the exports loader in webpack.

import qq from 'fine-uploader/fine-uploader/fine-uploader.js';

also works with

import qq from 'fine-uploader/lib/traditional';

for the s3 loader use

import qq from 'fine-uploader/lib/s3';

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!