Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueConverter registration in @casl/aurelia #619

Closed
rmja opened this issue May 3, 2022 · 6 comments
Closed

ValueConverter registration in @casl/aurelia #619

rmja opened this issue May 3, 2022 · 6 comments
Labels

Comments

@rmja
Copy link

rmja commented May 3, 2022

Describe the bug
The current minified build of @casl/aurelia does not retain the original class names which causes the name of the value converter to not be retained. For a value converter to be registered in aurelia its class name must end with ValueConverter which is not the case in the current minified build.

To Reproduce
Steps to reproduce the behavior:

Simply add aurelia.use.plugin(PLATFORM.moduleName("@casl/aurelia")); and see that the able value converter is not correctly registered. If this call is replaced with:

import { AbleValueConverter } from '@casl/aurelia';
aurelia.use.globalResources([AbleValueConverter]);

then the converter is registered correctly with the name able.

Expected behavior
The expected behavior is that aurelia.use.plugin(PLATFORM.moduleName("@casl/aurelia")); should correctly register the value converter. For this to work the minified build should have the following line:

t.globalResources([CanValueConverter, AbleValueConverter])

instead of the current:

t.globalResources([a,n]);
export{n as AbleValueConverter,a as CanValueConverter}

CASL Version

@casl/ability - v5.4.3
@casl/aurelia - v1.2.0

Environment:

@rmja rmja added the bug label May 3, 2022
@stalniy
Copy link
Owner

stalniy commented May 3, 2022

I thought nobody uses Aurelia)

but if you take a look at -> https://github.com/stalniy/casl/blob/master/packages/casl-aurelia/src/value-converter/can.ts#L28

you can see that class is properly marked. class names is just a one way to register resources. There is an explicit way to do this.

Anyway, I have not looked into Aurelia for a long time. So maybe you use incompatible version with casl.

@stalniy
Copy link
Owner

stalniy commented May 3, 2022

Additionally, I found converters to be less convenient/readable than pure props. At least in casl for Angular I plan to replace pipes with observables

@rmja
Copy link
Author

rmja commented May 3, 2022

I do and I love it:)

I think I found the issue. The problem was not with the value converter, but the PureAbility key for the DI container.
I register an AppAbility instance with the PureAbility key from my app as follows:

const ability = new AppAbility();
    aurelia.container.registerInstance(PureAbility, ability);
    aurelia.container.registerInstance(AppAbility, ability);

The problem is that the PureAbility key in the value converter instance is different as it uses the version from the @casl/aurelia umd output which is not the same as the one in the app. The PureAbility versions from the two different builds does not resolve to the same key, causing the manually configured PureAbility to not be the one that is actually used.

I have not completely found out how to solve this, but I will try and make a PR if I can fix the bug.

@stalniy
Copy link
Owner

stalniy commented May 3, 2022

Probably you register it too late or too early. So something overwrites container I guess.

I don’t think there is an issue with casl so I close this.

Feel free to reopen when you find the issue in casl

@stalniy stalniy closed this as completed May 3, 2022
@rmja
Copy link
Author

rmja commented May 5, 2022

You are right. This is not an issue with casl. It turns out that this is a webpack/aurelia-webpack-plugin issue.
I fixed it by adding the following line to my webpack.config.js file:

new NormalModuleReplacementPlugin(/^@casl\/ability$/, resolve("./node_modules/@casl/ability/dist/es6m/index.mjs")),

This ensures that only the es6m version is included in the build, instead of both the es6c and es6m versions.

When I do import { PureAbility } from '@casl/ability'; this imports the es6m version but aurelia.use.plugin(PLATFORM.moduleName("@casl/aurelia"), ability) seems to add a dependency to the es6c version, which has a different PureAbility implementation, hence the DI key for the two becomes different.

@rmja
Copy link
Author

rmja commented May 6, 2022

For reference, I have created a reproducing example here and submitted an issue over here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants