Skip to content

Lifecycle Method Issues with Nested Components when using single-export-module customElementsExportBehavior #22

@gaaral

Description

@gaaral

Problem

When rendering a nested component in Storybook, the inner component’s lifecycle method componentWillLoad (in my case) is not called unless I explicitly call defineCustomElements() in preview.ts.

Example:

//component-one.tsx
@Component({ tag: 'component-one', shadow: true })
export class ComponentOne {
  componentWillLoad() {
    console.log('componentWillLoad called'); // Only called when defineCustomElements() is used
  }

  render() {
    return <span>Component One</span>;
  }
}

//component-two.tsx
@Component({ tag: 'compnent-two', shadow: true })
export class ComponentTwo {
  render() {
    return <component-one></component-one>;
  }
}

Adding defineCustomElements() to preview.ts resolves the lifecycle issue, but introduces a Vite warning:

[vite] warning: 
/packages/my-component-library/dist/esm-es5/index-CB6EUl6g.js
1  |  import{__assign,__awaiter,__extends,__generator,__spreadArray}from"tslib";...

The above dynamic import cannot be analyzed by Vite.
See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations  for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.

Plugin: vite:import-analysis

stencil.config.ts Snippet

export const config: Config = {
...
    {
      type: 'dist',
      esmLoaderPath: '../loader',
      transformAliasedImportPathsInCollection: true,
    },
   {
      type: 'dist-custom-elements',
      customElementsExportBehavior: 'single-export-module',
      externalRuntime: false,
      includeGlobalScripts: false,
      minify: true,
    },
   angularOutputTarget({
      componentCorePackage: 'my-component-library',
      outputType: 'standalone',
      directivesProxyFile: '../angular/libs/core/src/libs/components.ts',
      directivesArrayFile: '../angular/libs/core/src/libs/index.ts',
    }),
...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions