canjs / can-view-import

Import dependencies in CanJS views
https://canjs.com/doc/can-view-import.html
MIT License
3 stars 2 forks source link

Support importer function injection via module metadata #121

Open rjgotten opened 5 years ago

rjgotten commented 5 years ago

Currently processImport directly executes the importer function from the can-import-module package, passing it the target module name and the current (i.e. parent) module name:

https://github.com/canjs/can-view-import/blob/d1c77e7e8756b7bcc860550a653076c850c9205d/can-view-import.js#L25-L32

can-import-module does runtime module resolution and has zero support for systems that use build-time resolution, like Webpack.

If we look at e.g. the Webpack can-stache-loader, it already uses the intermediate language representation for stache files, and finalizes construction of the renderer by passing in the module:

if (moduleOptions.helpers) {
  moduleOptions.helpers = Object.assign({ module: module }, moduleOptions.helpers);
} else {
  moduleOptions.module = module;
}
return renderer( scope, moduleOptions, nodeList );

https://github.com/canjs/can-stache-loader/blob/0a17a7cff6673cd461b85c44eecd5a2684ca37fe/index.js#L31-L36 (Sorry... no support for cross-repository embedded code snippits on GitHub just yet...)

Why not add the possibility to specify a build-system specific importer on this module metadata? Systems like Webpack can then use this as an injection point where can-view-import can be made to operate off of a list of modules pre-imported via the build system.

E.g. https://gist.github.com/rjgotten/1bdc23e0f04a1c46af44eeb5fd08a063

matthewp commented 5 years ago

Sounds good to me!