aurelia / webpack-plugin

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

Prevent ModuleConcatenationPlugin from inlining view models. #154

Closed shabalin closed 5 years ago

shabalin commented 5 years ago

I'm submitting a bug report

We were very excited with the news that there is no more need to PLATFORM.moduleName dialog view models, so we decided to give it a try.

https://aurelia.io/blog/2018/09/06/aurelia-release-notes-early-september-2018

Usage of Origin.get was removed; we now depend on the built-in support for class references from the CompositionEngine. What does this mean? Usage of class references for .viewModel should no longer cause issues with Webpack when PLATFORM.moduleName() is not used.

We removed all dialog VMs PLATFORM.moduleName() statements in our codebase, and were very happy to see dialogs working when we run app in webpack development mode. Unfortunately, the behavior is different in production mode. We prepared a repo based on Aurelia CLI app template that reproduces and issue.

Please tell us about your environment:

Current behavior: If i build a bundle with mode: "production" I am getting the following error on dialog opening.

aurelia-templating.js:792 Uncaught (in promise) Error: Cannot determine default view strategy for object.
    at e.getViewStrategy (aurelia-templating.js:792)
    at aurelia-templating.js:4802

in development mode dialog does open with no errors

I was able to reproduce the issue for the clean Aurelia App template (created using latest AURELIA CLI) https://github.com/shabalin/aurelia-dialog-issue-361


Initially we reported this problem to the Dialog repo, please read https://github.com/aurelia/dialog/issues/361#issuecomment-424613865

jods4 commented 5 years ago

@bigopon Can you have a look at this one? You created the new apis that don't rely on module and export names but it seems something still won't work if you hoist a VM as a child of another module.

bigopon commented 5 years ago

@shabalin I think its behavior is a bit unfortunately confusing. The mandatory usage of PLATFORM.moduleName was dropped for aurelia-dialog was mainly to leverage new features of aurelia-templating, which you can use static reference to a plain class, instead of a string as module name, to be the dialog view model. On the surface, there seems to be nothing has changed, but there are differences between v1 and v2.

So dropping PLATFORM.moduleName in the doc means drop the enforced magic, it does not really mean more magic.

bigopon commented 5 years ago

What we can do, to make it less surprising, is to add a warning inside aurelia-dialog to warn about this behavior specifically for webpack loader. Thoughts ? @StrahilKazlachev @jods4

jods4 commented 5 years ago

I am going to close this issue, because I don't think there's anything to be done about it on the webpack plugin side.

Preventing module concatenation is exactly what this plugin is about, through various means such as PLATFORM.moduleName or GlobDependenciesPlugin. So one solution is to use them.

If you want to go the pure JS route, we're adding APIs for that and the solution to your problem is to declare your view explicitly. Here's @StrahilKazlachev answer from the aurelia-dialog thread:

For this to work you will either need to pass the .view(moduleId or ViewStrategy) or use @view()/static $view on the view model class.

I agree the guidance could be better. At least the error message should lead you to the solution above (adding $view) rather than leaving you wondering what's wrong. This is best discussed in aurelia/dialog#361