简体   繁体   English

RequireJS 配置与不支持 AMD 的模块

[英]RequireJS configuration with modules that have no AMD support

So I'm trying to read the Require JS API -documentation to figure out how this should be done properly, but I've been unable to load a library so it doesn't get defined as undefined.因此,我正在尝试阅读Require JS API 文档以弄清楚应该如何正确完成此操作,但我一直无法加载库,因此它不会被定义为未定义。 I'm using this through NPM-WebJars, though I guess it shouldn't matter how you define it.我通过 NPM-WebJars 使用它,但我想你如何定义它并不重要。

Basically, I'm trying to get use of the react-bootstrap-switch -library working in my project.基本上,我正在尝试使用在我的项目中工作的 react-bootstrap-switch -library Here's JavaScript-file source in Pastebin as it might change in the master-branch.这是Pastebin 中JavaScript 文件源,因为它可能会在主分支中发生变化。 react-bootstrap-switch defines the library with exports.default = Switch. react-bootstrap-switch 使用exports.default = Switch 定义库。 I have several other modules defined using RequireJS, which have AMD-support and are working.我有几个使用 RequireJS 定义的其他模块,它们具有 AMD 支持并且正在工作。

Here is how I'm using RequireJS这是我如何使用 RequireJS

requirejs.config("paths":{"react-bootstrap-switch":["@routes.WebJarAssets.at("react-bootstrap-switch/15.0.0/dist/js/index")"]}}); // the file location

using the library使用图书馆

define(['react', 'react-bootstrap-switch'], function(React, Switch){
     console.log(Switch); // <- undefined
} 

So with just standard requirejs.config -import it doesn't work, because lack of AMD support.因此,仅使用标准的 requirejs.config -import 就行不通,因为缺少 AMD 支持。 So I guess I should use shim, but how do I do that?所以我想我应该使用垫片,但我该怎么做呢? I've been reading into it a bit, but I fail to properly import the library using it.我一直在阅读它,但我无法正确导入使用它的库。 It probably should be something pretty easy, but it doesn't seem to be working.它可能应该很容易,但它似乎不起作用。 For instance例如

 requirejs.config({
             "paths":{"react-bootstrap-switch":["@routes.WebJarAssets.at("react-bootstrap-switch/15.0.0/dist/js/index")"]},
             "shim": {
                     "react-bootstrap-switch": {
                          exports: 'Switch'
                      }}});

leaves it undefined.让它未定义。 I've been trying to get in the between with init-functions, but I can't locate the correct namespace inside the window.我一直在尝试使用 init 函数介于两者之间,但我无法在窗口中找到正确的命名空间。 Also tried to switch the order of shimming before the paths, trying 'default' instead of 'Switch', and some other tricks, but it just doesn't seem to import it properly as it gets stuck to undefined.还尝试在路径之前切换填充顺序,尝试使用“默认”而不是“切换”,以及其他一些技巧,但它似乎无法正确导入,因为它卡在未定义状态。

Last thing I could try to is to get the code locally and change to something else instead of exports.default or just code my own function for that, but I would like to get this working for future reference.我可以尝试的最后一件事是在本地获取代码并更改为其他内容而不是exports.default 或者只是为此编写我自己的函数,但我想让它工作以供将来参考。

It is clear what you are trying to load is a file that was transpiled and had for target a CommonJS environment.很明显,您尝试加载的是一个经过编译并以 CommonJS 环境为目标的文件。 Such files cannot be loaded as-is by RequireJS. RequireJS 无法按原样加载此类文件。

You'll have at a minimum to wrap its code in define(function (require, exports, module) { at the start, and });您至少需要将其代码包装在define(function (require, exports, module) {开头和}); at the end.在末尾。 Once you do that, it may work.一旦你这样做了,它可能会起作用。 You can use r.js to do the wrapping, or any other tool.您可以使用r.js或任何其他工具进行包装。

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

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