constitute icon indicating copy to clipboard operation
constitute copied to clipboard

Injecting libraries

Open rsumilang opened this issue 8 years ago • 0 comments

Hello,

I think it would be great to support libraries on the container that don't need to be instantiated and can be referenced by using the library itself. Currently I have to do this:

libraries.js:

import $ from 'jquery';
import { Value } from 'constitute';

export const jQuery = new Value($);

Then, I have to find wherever the libraries.js file is located relative to my current script and pass in the correct value.

test.js:

import constitute, { Dependencies, Value } from 'constitute';
import {jQuery} from '../some/path/to/lib/libraries.js';

@Dependencies(jQuery)
class Test {
   constructor($) {
    this.$ = $;
   }
}

While this isn't the worst thing in the world, I think it would be nice to be able to reference jQuery as a dependency and the system knows to treat it as a Value (aka library in my case).

config.js:

import _ from 'lodash';
import $ from 'jquery';
import constitute from 'constitute';

// configuration
constitute.addValues([_, $, ...etc]);

example.js:

import _ from 'lodash';
import $ from 'jquery';
import constitute, { Dependencies } from 'constitute';

@Dependencies(_, jQuery)
class Test {
   constructor(_, $) {
    this._ = _;
    this.$ = $;
   }
}

I think this way we could continue to import libraries as we normally do while letting constitute replace it where necessary if we overwrite it. I'm not sure how this works under the hood to know if it's even possible but I think it would make things a little more maintainable. Or is there perhaps a better way mockup when I did in the first scenario?

Thanks!

rsumilang avatar Mar 15 '16 20:03 rsumilang