Require.js: "Mismatched anonymous define() module" error - requirejs

I'm developing a JavaScript plugin for webshops which is module based and I'm using require.js as module loader to integrate it into the customer's HTML template.
The idea behind that is to let the customer put only one line of code into their template and everything is loaded automatically.
I get problems when the webshop has third party scripts which define themself anonymously as AMD module but are included by a separate <script> tag. In this case I get errors like:
Uncaught Error: Mismatched anonymous define() module:
[..]
http://requirejs.org/docs/errors.html#mismatch
There is no way to me to take influence on that. It is also not possible for me to influence where in the template my one-liner is put by our customers.
Is there a way to tell require.js to just focus on the modules I name in my code and to ignore any other referenced module-define JavaScripts?

I had the same problem. My workaround has been to put all the require/define/requirejs objects under another object such as foo.require, foo.define, foo.requirjs. I've now found in the require page something like what I did:
http://requirejs.org/docs/faq-advanced.html
Hope this can help you!

Related

Reusing a component in lit-element

Anyone that can point to any documentation on howto reuse code in lit-element.
The problem now is that if I declare an element, in my case a close-button and I want to reuse it by importing it into 2 or more lit-elements, there will be an error in the browser about the close-button being declared more than once.
Understandable enough, but how do I reuse a component, I could of course move the button to a separate file and add it to the document, but then there would be dependencies on that for other components to work.
Any suggestions
If close-button self-registers itself, with a call to customElements.define('close-button', ...), then you should be able to import its defining module and not have any errors due to the module caching behavior of JS.
You must have multiple customElements.define('close-button', ...) calls, so I'd make sure that 1) it's self-registering and you're not registering it again in each component that uses it, and 2) you're using standard JS modules.
After investigating a bit more, I concluded that sharing HTML templates might be the way to do it.

How to add code to every page from module

I am developing a plugin, that inserts some JS code into every page of OpenCart. This code needs some variables, that are set in admin part of module (this part is already finished). But now I need to modify template, that on every page there would be a HTML snippet with my code inserted.
I have gone through many tutorials (most of them were for OpenCart 1.X) and did not find anything about how to do this.
Also I would prefer not to change OpenCart's core code, like some modules do, if possible.

How to load CSS from library when using 'require'

I’m building an electron app. In it, I have a webview with a preload script. Inside said script, I’d like to use sweetalert.
I installed sweetalert with npm install --save sweetalert. Inside my script I load it with require('sweetalert') and call it with swal("Hello world!");. I now notice it doesn’t look right, as the alert is missing its required CSS file. But I’m loading it with require('sweetalert'), which is great since sweetalert can just remain in its directory inside node_modules and I don’t have to care for it, but its CSS is an integral part of it, and is not getting pulled the same way.
Now, what is the recommended way of solving this? Keep in mind I’m inside a javascript file and would like to remain that way. Do I really have to go get the CSS file and inject it in some way? And how would I do it correctly, since it is inside node_modules? After testing it, it seems like it can’t be done in this particular case due to Content Security Policy.
Either way, that seems so clunky in comparison to the require statement, it’d seem weird for a simpler solution to not be available.
You'll have to include it like you would normally do in a browser, for example in index.html. Copy it out of the module folder into your css folder if you have one and link it with the link tag. It depends on if you're using plain electron or some other boilerplate template with there is a gulp/grunt workflow on where to stick it but that's it really, electron is just a browser that's running your JS/html so it's really the exact same process. require only loads the JS module but not the styles.
if you wanted to include it dynamically you could use the same techniques as a regular browser for example (ex. document.write/create element).
I'm not familiar with sweetalert, but hopefully this helps.
Your syntax for require should be something similar to this.
var sweetalert = require('sweetalert')
You should then be able to access methods on the sweetalert object using the following syntax.
sweetalert.someMethod()
Remember requiring just returns a javascript object. Those objects usually have methods that will allow certain functionality. If you want to add sweetalert to your page, you will either need to inject it within the html, or the javascript within the sweetalert module will need to dynamically create html where the css is included. I hope that clarifies some things and helps you get a better sense of some of the inner workings.

require.js require inside a require

Why do we want to have another require structure inside a require structure?
like
require([mod1,mod2], function(m1, m2){
require([mod3], function(m3){
// and then will use m1 and m2 here as well
})
})
Why can't we just have one require structure? I want to understand the motivation between this setup.
Nested require isn't mandatory, and can easily be avoided if this don't fit your style.
Although, this can be useful to load submodules or conditional modules (like a polyfill).
In a more personal experience, I often use nested require inside my router controller in order to load certain page view when they're requested. This allow me to request only the dependencies of my router without loading the entire page collection of an app.
I also often find myself using nested require to manage some i18n aspect of some apps by loading conditional locale.
Last thing, I'd just remember that modules should be defined using define, not require. require function is really used to arbitrary load scripts if needed (and can be use once to bootstrap your app). So in most of the real use case, you'll have some nested require inside a define module definition.
Hope this help!

TypeScript extend object in module

What I want to do is really similar to this and this except I'm trying to figure out how to put an ArrayExtension inside a module.
I'm trying to get something similar to the way C# extension methods work, that way I can just import the module and I'll have my extra methods. The links I provided show how to extend an existing object, but I haven't been able to figure out how to encapsulate that into a module.
If you're targeting non-browser environments like node.js this will be possible because you will be able to pass references to your module's global members, such as Array, to other modules. Those other modules can then extend the passed in object and/or its prototype with extra functionality which will be only accessible by the calling module. Other modules would have to do the same in order to get these extensions; therefore, conflicts are minimized since imports are explicit.
However, in browser environments this is not the case since there is only one window object and any changes to its members are available everywhere. As soon as any of your modules extended Array those extensions would be available to all other modules -- increasing the possibility for conflicts and making the code harder to reason about.
With that said, there are patterns in JS, and therefore TypeScript, which should accomplish what you want. One such pattern is the 'mixin' pattern which allows you to add on extra functionality on an object instance basis. You could separate re-usable code into mixin modules which could then be applied to an object when needed, or even automatically in constructors. Take a look at this for a decent overview and implementation examples: http://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/
If you're trying to extend the built in Array type you can't do that within a module. You're extension will need to live in an ArrayEx.ts file and occur outside of any modules. The reason for that is that if you did it within a module you'd be extending the Foo.Array type which isn't the same as Array.
But you said you just want to be able import the module to have your extra methods show up and all you really need to do is add a /// <reference path='ArrayEx.ts' /> to any file you want the extension methods to be available to. This is essentially the same thing.

Resources