简体   繁体   English

如何防止外部脚本覆盖我的main.js配置

[英]How can I prevent an external script from overriding my main.js configuration

I'm building an application using require.js. 我正在使用require.js构建应用程序。 For a certain part I need to load a 3rd-Party script that is built using require.js as well (it's all minified and built into a single file using almond.js). 对于某个部分,我需要加载一个也使用require.js构建的第三方脚本(所有脚本都已缩小,并使用almond.js内置到单个文件中)。

For this I inject the script tag like: 为此,我注入脚本标签,例如:

var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.onload = function(){
    // remote widget script has loaded
    scriptDfd.resolve();
};
script.src = endpoints.widgetScript + '?appKey=' + auth.userAppKey + '&dev=1';
document.getElementsByTagName('head')[0].appendChild(script);

The script gets injected fine and is working, but from that point on I cannot use the module mapping defined in my own main.js anymore. 该脚本被注入并可以正常工作,但是从那以后,我不能再使用自己的main.js定义的模块映射了。

The error that is thrown when I try to access my modules is Uncaught TypeError: Cannot read property 'splice' of undefined which sounds like it's not able to find the module anymore (which is easily accessible before I inject the 3rd party script). 当我尝试访问我的模块时引发的错误是Uncaught TypeError: Cannot read property 'splice' of undefined这听起来好像它不再能够找到该模块(在注入第三者脚本之前可以轻松访问)。

Is there any feasible workaround for this? 有没有可行的解决方法?

So, as proposed in the comments I contacted the vendor of the external script (that is actually just dogfooding the dogfooding of another in-house product) and they used a custom namespace when running the r.js build script as described here . 所以,在意见建议我联系了外部脚本的供应商(即实际上只是内部测试的另一个内部产品的内部测试),并在运行时使用的是自定义空间r.js描述构建脚本这里

This creates two seperate require.js's that can be configured differently. 这将创建两个单独的require.js,可以对它们进行不同的配置。

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

相关问题 如何将指令从 main.js 移动到外部文件 Vue 3 - How can I move directives from main.js into an external file Vue 3 Webpack Babel 脚本添加到我的索引中。html 文件需要是 src="/main.js" 但是 src="main.js"。 如何正确配置它? - Webpack Babel script added to my index.html file needs to be src="/main.js" but is src="main.js". How can I configure this correctly? 如何在electronic中的main.js中从另一个脚本调用函数 - How to call a function in another script from main.js in electron 如何使用meteor 在服务器端main.js 上将数据插入到我的集合中? - How can I insert data to my collection on my server-side main.js with meteor? 如何将文件导入我的前端 main.js 文件? - How can I import files into my front end main.js files? VueJS:如何在模板中使用main.js中的数据? - VueJS: How do I use data from main.js in my templates? 如何从main.js调用供应商库? - How do I call a vendor library from my main.js? 我如何优化main.js的大小? - how can i optimize main.js size? 如何将列表从 other.js 文件导入到我的 main.js 文件? - How to import a list from other .js file to my main.js file? 如果我的Backbone main.js太大,我该怎么办? - What can I do if my Backbone main.js is too big?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM