简体   繁体   English

用覆盖模块变量的库链接imports-loader和exports-loader

[英]chaining imports-loader and exports-loader with a library that overwrites the module variable

I'm trying to load an audio encoding library called libvorbis.js which is an emscripten compiled version of vorbis. 我正在尝试加载名为libvorbis.js的音频编码库,这是vorbis的脚本编译版本。

libvorbis.js requires to be executed with the window variable in it's scope and leaves the classes exposed as a var without an exports declaration. libvorbis.js要求在其作用域内使用window变量执行,并且使类暴露为var,而无需导出声明。

Normally it would be easy to chain imports-loader and exports-loader to fetch such a file. 通常,将imports-loader和exports-loader链接起来以获取此类文件很容易。 Using- Using-

require 'imports-loader?this=>window!exports-loader?VorbisMediaRecorder!libvorbis.js/build/libvorbis.js'

which will expose the window object as 'this' inside libvorbis.js and take VorbisMediaRecorder out as the return to this which is needed by my other modules/code to be run later. 它将在libvorbis.js中将窗口对象公开为“ this”,并将VorbisMediaRecorder排除在外,这是我其他模块/代码稍后需要运行的返回值。

HOWEVER, libvorbis.js itself overwrites the 'module' variable which screws up exports-loader from exporting 但是,libvorbis.js本身会覆盖“模块”变量,从而使exports-loader无法导出

libvorbis.js has this check in it's code libvorbis.js在代码中有此检查

// node.js Environment
var module;
if (module && module.exports) {
    makeVorbisEncoderModule({}, module);
}

// Web Worker Environment
if (!module && this.document === undefined) {
    VorbisWorkerScript.main(this);
}

this overwrites module which causes webpack to throw this error 此覆盖模块导致webpack抛出此错误

commons.js:142 TypeError: Cannot set property 'exports' of undefined

as it's trying to run 当它试图运行

/*** EXPORTS FROM exports-loader ***/
module.exports = VorbisMediaRecorder;

Any ideas on how to resolve this issue? 关于如何解决此问题的任何想法? I really don't want to fork libvorbis and modify it's source. 我真的不想分叉libvorbis并修改它的来源。

The libvorbis.js library was written to be included as a script on the page. libvorbis.js库被编写为作为脚本包含在页面上。

So I solved my particular problem with using file-loader. 因此,我使用文件加载器解决了我的特殊问题。 It will load the script exactly like a file with a src etc... hence it can be run without issues. 它会像使用src等文件一样加载脚本,因此可以正常运行。

# libvorbis must be resolved in window scope, use the file-loader
require 'file-loader?name=[name].[ext]!libvorbis.js/build/libvorbis.js'

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

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