Provide Mixin via shared StencilJS library #6633
Replies: 11 comments 2 replies
-
|
hey! 👋 Stencil uses typescript as a transpiler where The good news is that Stencil outputs 'meta ready' versions of your components / mixins with everything needed via it's something like: |
Beta Was this translation helpful? Give feedback.
-
|
here's an example within our test suite: |
Beta Was this translation helpful? Give feedback.
-
|
@johnjenkins That's really helpful, thanks! The package.json was actually already set up to add the So the So a minimized example for the setup would be: // my-mixin.ts file inside of shared library
export const MyMixin = <T extends MixedInCtor>(Base: T) => {
class MyMixinClass extends Base implements MyInterface {
@Prop() myProp: string;
}
}
// customer component in separate project
import { MyMixin } from '@our/library/dist/collection/utils/mixins/my-mixin';
@Component({
tag: "my-component",
styleUrl: "my-component.scss",
shadow: true,
})
export class MyComponent extends Mixin(MyMixin) implements SomeLocalInterface {
render() {
return <div>{this.myProp}</div> // [ ERROR ] Property 'myProp' does not exist on type 'MyComponent'.
}
}When the mixin is placed inside of the customer project, everything works. Thanks for the help! |
Beta Was this translation helpful? Give feedback.
-
|
yeah - you'll need to tell typescript where to find the types via the 'core' "./collection/*": {
"types": "./dist/types/*.d.ts",
"import": "./dist/collection/*.js"
}Or an even nicer dx "./mixins/*": {
"types": "./dist/types/*.d.ts",
"import": "./dist/collection/*.js"
} |
Beta Was this translation helpful? Give feedback.
-
|
I'm going to move this to 'discussions' if that's ok? :) |
Beta Was this translation helpful? Give feedback.
-
|
@johnjenkins Thanks for your patience on this one. Even after a few years in web developing now, I feel like my journey is just starting sometimes. I'm afraid I just can't get this to work properly. Here's a basic excerpt from my library package.json: {
"name": "@our/library",
"version": "0.1.0",
"main": "./dist/index.cjs.js",
"module": "./dist/components/index.js",
"types": "./dist/components/index.d.ts",
"collection": "./dist/collection/collection-manifest.json",
"files": [
"dist/"
],
// the exports node did not exist at all, I've added it after your comment
"exports": {
".": {
"types": "./dist/components/index.d.ts",
"import": "./dist/components/index.js",
"require": "./dist/index.cjs.js"
},
"./mixins/*": {
"types": "./dist/types/*.d.ts",
"import": "./dist/collection/*.js"
}
},
"sideEffects": false,
[…],
}Now I can use an import like this: import { MyMixin } from '@our/library/mixins/utils/mixins/my-mixin';That makes the Property 'myProp' does not exist on type 'MyComponent'. error go away, but now everywhere where I use
It seems like now I have the type info, but the meta data is gone. When I look into export namespace Components {
[…]
interface MyComponent {
}
}
[…]I tried a few things (and AI tried to help) but didn't get anywhere. I must be doing something wrong here, but I have no idea where to start on finding out what. Thanks again for the quick responses on this! |
Beta Was this translation helpful? Give feedback.
-
|
no worries :) I setup a starter repo at https://github.com/johnjenkins/boilerplate-monorepo-stencil-mixin - lemme know if it makes sense (or doesn't)
then
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks! Could it be that the project is missing a dependency? @boilerplate-stencil/utilities? I'll check out the setup, and maybe there's already something in there that helps, without being able to actually run it. |
Beta Was this translation helpful? Give feedback.
-
|
I'm working on this right now too 😉 Currently, I'm struggling with the following issue: Two Stencil projects ( But sometimes you might only want one type. This currently limits its use when you have a core package and another one, and components from the core are supposed to be used. That’s why the https://github.com/ThornWalli/boilerplate-stencil/ currently includes the |
Beta Was this translation helpful? Give feedback.
-
|
Here is my PoC how to use |
Beta Was this translation helpful? Give feedback.
-
|
Btw. I have other issue that mixin only works in dev mode but not in production mode. Simply when serving files from |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone!
Thanks for finally being able to use extension patterns with components! That will make life a lot easier on a bunch of areas.
I'm not sure if this is a bug report, a feature request, or simply not possible, but we are using a library containing Stencil components to use within our customer projects, which themselves are providing their own Stencil components. So I thought using mixins would be a way to have some common logic provided via the shared library, and the custom components in the customer project would just need to implement the needed delta for that customer's concerns.
That shared logic would use Stencil decorators, like
@Propand@Stateand others.I think without those decorators, everything would probably work fine, but those decorators are not really transported when the library is being compiled and used as a dependency.
Is that just how it is? Is there any intention to have Mixins support such a setup?
Thanks for any info!
Beta Was this translation helpful? Give feedback.
All reactions