How to load libraries dynamically with require()


#1

Hi,

I am discovering Atom, and, of course, I am developing a package for it.
I’m trying to create a(nother) less transcoder package, but based on a configuration file in json (as with package.json, tsconfig.json, …).

In the config file (from the developed project), I would like to list less plugin to use (like less-plugin-clean-css, …).
ex:

{
  ...
  "plugins": {
    "less-plugin-clean-css": {
      "advanced": true
    }
  }
}

So I need to dynamically load this library (from the less transcoder package).

for (var prop in options.plugins) {
  var pluginClass = require(prop);
  var plugin = new pluginClass(options.plugins[prop]);
}

The issue I have is that less-plugin-clean-css has to be in the less transcoder package.
But I would like to load plugin from the projet’s package currently developed.

ps: project makes reference to a any project currently in development, less transcoder package makes reference to the package I am developing to transcode less files.


#2

I don’t think this is very reasonable from the perspective of a project developer. If I’m working on an app and I have a package or script that compiles my Less code for me, I don’t want to have to keep less-plugin-clean-css in my project directory. I want it to be stored in .atom/packages so that only the files relevant to my project are stored with my project.


#3

I agree with you but in this case, plugin are used by the developer who wants them in his project. My package has no need for this plugin.
I just want to create a convenient way for including desired plugin when less file are compiled.

For now the only solution I found, is to include every available less plugins in my package.
And I think this is not a good solution.