archi-scripting-plugin: CommonJS settings cause linked JS file to not load

Since jArchi 1.6 we have two new properties:

System.setProperty("polyglot.js.commonjs-require", "true");
System.setProperty("polyglot.js.commonjs-require-cwd", ArchiScriptPlugin.INSTANCE.getPreferenceStore().getString(IPreferenceConstants.PREFS_SCRIPTS_FOLDER));

However, some imported JS files no longer work.

  1. Add papaparse.js to a lib folder in your jArchi scripts folder papaparse.js.zip
  2. Run this simple script:
console.show();
console.clear();
load(__DIR__ + "lib/papaparse.js");
console.log("> Loaded Papa Parse");
console.log(Papa);
> Loaded Papa Parse
org.graalvm.polyglot.PolyglotException: ReferenceError: "Papa" is not defined

@jbsarrodie WDYT?

(PapaParse source - https://www.papaparse.com/)

papaparse.js.zip

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (18 by maintainers)

Most upvoted comments

And BTW, I’ll update the wiki page with more information soon too.

But let’s make sure CommonJS is enabled by default, and if needed, people will have to opt-out.

Done. 😉

This papaparse.js file needs to be treated like a CommonJS file.

Thank you for your analysis, you’re right. In fact, Papaparse authors tried to make their lib “intelligent”, and when the code is loaded, it tries to figure out in which context (browser, node.js or other) it has been executed. Unfortunately, there is no perfect method for that and their approach has some side effects in this case. So this is not an issue with jArchi itself, but a side effect of how this very specific javascript lib has been coded. And that’s the reason why I didn’t experienced this issue during my tests (I’m using several other libs with no issues, and I didn’t tested the one script I have which relies on Papaparse).

@jbsarrodie How to manage this situation?

I think the best option is to document our findings in a wiki page and update the Changelog to make sure people facing the issue understand it and find the quick solution.

Speaking about the solution…

  • Create a subfolder node_modules/papaparse
  • Copy papaparse.js into that folder and rename it to index.js
  • Replace load(__DIR__ + "lib/papaparse.js"); with const Papa = require("papaparse");
  • It works

There is an easier fix: require() can load modules using their absolute path. So no need for all these steps, simply replace

load(__DIR__ + "lib/papaparse.js");

by

const Papa = require(__DIR__ + "lib/papaparse.js");

I’ve added an option in preferences to enable/disable CommonJS in the dev branch.

IMHO, we don’t need to add such option only to solve an issue related to the way a specific lib has been written. I’d better keep jArchi 1.6 as it is and let people change one line their code.

I’ll look at it tonight.

JB