简体   繁体   English

在React Web App中使用createjs-soundjs

[英]Using createjs-soundjs in react web app

I would like to use https://www.npmjs.com/package/createjs-soundjs to play sounds on a react web app. 我想用https://www.npmjs.com/package/createjs-soundjs对一个反应web应用程序播放声音。

I installed the package normally with npm install createjs-soundjs and it's showing in the packages list. 我通常使用npm install createjs-soundjs安装该软件包,它显示在软件包列表中。 How should I include this to my project, so that I could use it? 我应该如何包括这对我的项目,这样我就可以使用它吗?

I have tried the following: 我尝试了以下方法:

import React from 'react';
import classNames from 'classnames';
import createjs from 'createjs';
import SoundJS from 'createjs-soundjs'; // this line causes an error

The error message in console: soundjs-0.6.2.min.js:17 Uncaught ReferenceError: createjs is not defined. 控制台中的错误消息:soundjs-0.6.2.min.js:17 Uncaught ReferenceError:未定义createjs。 This error message is very clear, but I have no idea where to start. 此错误消息是很清楚,但我不知道从哪里开始。 Am I missing something fundamental? 我缺少基本的东西吗?

The SoundJS core bundle is not in the proper CommonJS or ES6 format, it's a bundle that makes strong assumption that it's loaded as a top-level script in the browser. SoundJS核心捆绑软件的格式不符合正确的CommonJS或ES6格式,它是一个强有力的假设,将其作为浏览器中的顶级脚本加载。 So it first tries to create the global value by assigning it to a property on this (which it assumes to be the window) and then tries to access this global values as just createjs . 所以,它首先试图通过将其分配给一个属性创建全球价值this (这假设是窗口),然后尝试为刚刚访问此全局值createjs So obviously this doesn't work in the module context. 因此,显然这在模块上下文中不起作用。

There's a workaround though, the code is for Webpack 2 and based on this comment (which is for Webpack 1): 但是,有一种解决方法,该代码适用于Webpack 2,并基于此注释 (适用于Webpack 1):

module: {
  rules: [
    // ...
    {
      test: /createjs/,
      use: [
        'imports-loader?this=>window',
        'exports-loader?createjs'
      ]
    },
    // ...
  ]
},

plugins: [
  // ...
  new webpack.ProvidePlugin({
    'createjs': 'createjs',
  }),
  // ...
],

resolve: {
  alias: {
    'createjs': path.resolve(__dirname, '../lib/soundjs-0.6.2.combined'),
  }
},

The 3rd entry (resolve.alias) maps the imports of createjs to the file I've downloaded and placed into my lib folder (which is not as elegant as using something from npm, but now I'm sure what I get. npm version might work as well). 第三个条目( createjs )将createjs的导入createjs到我下载的文件中,并将其放置到我的lib文件夹中(这不像使用npm中的内容那样优雅,但是现在我确定可以得到什么。)可能也可以)。

The first entry (module.rules) tells Webpack to first substitute this with window , and then to take the createjs instance from the global (window) context and make it the module export. 第一个条目(module.rules)告诉Webpack首先用window代替this ,然后从全局(窗口)上下文中获取createjs实例,并将其作为模块导出。

Finally, the 2nd entry (ProvidePlugin) precedes all requests for global createjs (in other modules) with const createjs = require('createjs') . 最后,第二个条目(ProvidePlugin)在所有对全局createjs请求(在其他模块中)之前带有const createjs = require('createjs')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM