5

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.

user137369
  • 5,219
  • 5
  • 31
  • 54

2 Answers2

-1

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).

Eric Kelly
  • 435
  • 3
  • 8
-2

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.

user2263572
  • 5,435
  • 5
  • 35
  • 57
  • 1
    The syntax itself isn’t a problem. I can load and call `sweetalert` just fine, the problem is it doesn’t look right because it’s missing the CSS (I’m sure that is the case, since I tested it elsewhere). “the javascript within the sweetalert module will need to dynamically create html where the css is included” is my problem. `sweetalert` doesn’t do it itself, and so far I can’t do it as well due to the Content Security Policy. This may be undoable. – user137369 Nov 20 '15 at 21:58
  • This is a multifaceted question. No knowledge of the module being loaded is needed in order to provide value on the node require process. This answer may not satisfy all parts of the question, but overall does provide value to users using node modules. – user2263572 Apr 08 '16 at 21:59