aurelia / webpack-plugin

A plugin for webpack that enables bundling Aurelia applications.
MIT License
90 stars 36 forks source link

adding PLATFORM.moduleName - How it works to the wiki pages #146

Closed doktordirk closed 6 years ago

doktordirk commented 6 years ago

I had some struggle with PLATFORM.moduleName and hence i'd propose to add some explanation about the internals to the wiki pages Managing Dependencies

Until version 1.1.0, .feature("xxx") was tricky because it didn't take a module name but its parent folder and works by appending /index internally. Be sure to use aurelia-framework@1.1.0 or later, so that you can add the /index like so:

use.feature(PLATFORM.moduleName("controls/index"));

How it works

PLATFORM.moduleName is actually not used for runtime reference, but it gets statically analyzed by the aurelia-webpack-plugin at compile time to include your module in the build and make a reference database with the module name you use to the reference the aurelia-webpack-loader needs. Hence, you have to use string literals and following won't work:

let moduleName = 'my-resource';
// that will not register your resource and fail once you want to use the resource
aurelia.use.globalResources(PLATFORM.moduleName(moduleName));

In the rare case, you want to add a resource to aurelia using a variable, you can do that if you have registered it somewhere using PLATFORM.moduleName with a string literal. So following wouldn't be recommended, but does work:


let moduleName = 'my-resource';
// that works as the aurelia-webpack-plugin has made reference for 'my-resource' during compile time
aurelia.use.globalResources(moduleName);

// register it anywhere in your code using PLATFORM.moduleName with a string literal
PLATFORM.moduleName('my-resource');

HTML dependencies

A few Aurelia tags can dynamically load modules, like:

doktordirk commented 6 years ago

I now found that information in an older blog post. Had it been in the wiki it would have saved me some trouble.

Alexander-Taran commented 6 years ago

Would you like to provide a pull request for it @doktordirk ?

doktordirk commented 6 years ago

öhm, well, actually, i kinda tried, but was too stupid to do that for the wiki pages. seems tricky to do that, hence i just added it here, so eg you can add it. i don't care about 'authorship' too much

Alexander-Taran commented 6 years ago

@jods4 ?

jods4 commented 6 years ago

Thanks for your contribution, it's been added to the wiki.

I took the liberty to change the text a bit, especially the second code example which contained an unnecessary duplicated string.