Proper way to implement jwplayer in react component using webpack (react-starter-kit)

我只是一个虾纸丫 提交于 2019-12-04 05:29:42

I think this is what you need to do:

  1. Define window as external to the bundle so that references to it in other libraries are not mangled.
  2. Expose a global variable jwplayer so that you can attach your key
  3. (Optional) Create an alias to your jwplayer library

I've tested it and this configuration works for me, but only on the client and not on the server or isomorphically/universally.

webpack.config.js:

// Declare window as external
externals: {
    'window': 'Window'
},
// Create an easy binding so we can just import or require 'jwplayer'
resolve: {
    alias: {
        'jwplayer':'../path/to/jwplayer.js'
    }
},
// Expose jwplayer as a global variable so we can attach the key, etc.
module: {
    loaders: [
        { test: /jwplayer.js$/, loader: 'expose?jwplayer' }
    ]
}

Then you can import jwplayer from 'jwplayer' and require('jwplayer').

Probably an old question but I recently found a relatively stable solution.

I include the jwplayer in a folder called app/thirdparty/jwplayer-7.7.4. Next, add it to the exclude in the babel loader so it is not parsed.

{
  test: /\.jsx?$/,
  use: 'babel-loader',
  exclude: /(node_modules|thirdparty)/,
}

I then use dynamic import in order to bootstrap my component and load jwplayer.

async function bootstrap(Component: React.Element<*>) {
  const target = document.getElementById('root');
  const { render } = await import('react-dom');
  render(<Component />, target);
}

Promise.all([
  import('app/components/Root'),
  import('app/thirdparty/jwplayer-7.7.4/jwplayer.js'),
]).then(([ { default: Root } ]) => {
  window.jwplayer.key = "<your key>";
  bootstrap(Root);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!