Use stacktrace to get the plugin file name

=== EDIT === 

After better looking at the problem, I found an easier, less hacky crossbrowser solution, by using https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript

function getFileName(){ var srcParts = document.currentScript.src.split('/') return srcParts[srcParts.length-1].replace('.js', '')}var parameters = PluginManager.parameters(getFileName())=== Original post: don't use this, use solution above instead ===

If you want to get the filename of your plugin (for example to get your parameters independent of the file name)

you can throw and immediately catch an Error in your IIFE, and then use .stack to get the filename like this:

var path = require('path')try { throw new Error()} catch(e) { var fileName = e.stack.split('\n')[1].split(path.sep) fileName = /(.*)\.js:/.exec(fileName[fileName.length-1])[1]}var parameters = PluginManager.parameters(fileName)// same as: // var parameters = $plugins.filter(function(plugin){ return plugin.name === fileName })[0].parameters;if you are only interested in the parameter you can simplify it to:

Code:
var path = require('path')try {  throw new Error()} catch(e) {  var parameters = e.stack.split('\n')[1].split(path.sep)  parameters = PluginManager.parameters(/(.*)\.js:/.exec(parameters[parameters.length-1])[1])}
This is pretty nice.


I had saw the stacktrace before but didn't think of using it that way.


Does this work for all platforms/browsers?


MDN says the stack property is non-standard.
While this is a clever trick, I think our current solution is better and more reliable for getting the parameters.


One thing I noticed is that you used require, which is only available on windows and mac. But since you only used it to get the path separator, that can be ignored.


Another thing is that when I ran that code, my filename was in another position of the array.
While this is a clever trick, I think our current solution is better and more reliable for getting the parameters.One thing I noticed is that you used require, which is only available on windows and mac. But since you only used it to get the path separator, that can be ignored.Another thing is that when I ran that code, my filename was in another position of the array.
Ah, right didn't try to export as mobile, I may check what happens there
@hudell, @tsukihime, I updated my solution so now it's crossbrowser, works in nw.js as well, did not try exporting the project as mobile yet, if plugins are minified it may break, but it's addressable in the build task with a simple transform.
Ohh, I liked that solution!

You put a comma where it should be a semi-colon, but other than that, it's perfect and it should work on everything other than Internet Explorer and Opera Mobile.
Ohh, I liked that solution!

You put a comma where it should be a semi-colon, but other than that, it's perfect and it should work on everything other than Internet Explorer and Opera Mobile.
In IE the gl context needed by pixi is missing as well, should work on edge, the comma is my fault, I merged the last two line in one but I forgot to remove the comma, fixed now
This should be a working one-line version:

PluginManager.parameters(/\/(?!.+\/)(.+?)\.js/.exec(document.currentScript.src)[1]);It captures everything between a slash not followed by another slash (so, the last part of the file path) and ".js". Since you have to be inside the script to execute this code in the first place, the regex is guaranteed to match.
This should be a working one-line version:

PluginManager.parameters(/\/(?!.+\/)(.+?)\.js/.exec(document.currentScript.src)[1]);It captures everything between a slash not followed by another slash (so, the last part of the file path) and ".js". Since you have to be inside the script to execute this code in the first place, the regex is guaranteed to match.
if nested folder are allowed inside the plugin folder you need to add a end of string token ($) as anything.js is a valid folder
/edit: Sorry, i misread your post. You're right and i should modify it like this:

Code:
PluginManager.parameters(/\/(?!.+\/)(.+?)\.js$/.exec(document.currentScript.src)[1]);
That said, i never tried to add subfolders to the "plugins" folder, so if they would be part of the plugin name, you'd need to modify the regex to capture everything between "/js/plugins/" and ".js".
Not even sure it will work, so it may not be an issue; btw typing on a train to work is tricky
On second thought, the "$" isn't even necessary. Since i'm matching from the last slash, a path like this:


"/path/to/plugins.js/pluginName.js"


Will still get matched to "pluginName".
I remember now why I splitted, it's because that was part of the stacktrace solution which in nw.js returns the file: protocol full path, to avoid making the regex too verbose I figure out I could just split by the path separator

in the currentScript solution you should get what src is in the markup so script/*, also you're right your regex doesn't need the end of string for the reason you stated

Original thread: https://forums.rpgmakerweb.com/threads/use-stacktrace-to-get-the-plugin-file-name.49880/