-
Notifications
You must be signed in to change notification settings - Fork 310
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
feat: enable live code inclusion for consuming applications #2642
Conversation
Marks all packages as side effect free This allows bundlers to discard code not in the direct dependency tree of an import
Thank you for creating a Pull Request @gavinbarron. This is a checklist for the PR reviewer(s) to complete before approving and merging this PR:
|
The updated storybook is available here |
📖 The updated storybook is available here |
📖 The updated storybook is available here |
📖 The updated storybook is available here |
🚀 New react-contoso sample application deployed here |
📖 The updated storybook is available here |
🚀 New react-contoso sample application deployed here |
📖 The updated storybook is available here |
🚀 New react-contoso sample application deployed here |
📖 The updated storybook is available here |
🚀 New react-contoso sample application deployed here |
📖 The updated storybook is available here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me too. For the docs, we can add them on the READMEs and portal on a separate work item.
d405e0b
🚀 New react-contoso sample application deployed here |
📖 The updated storybook is available here |
…unction and setting up a side effect free path for importing the provider class
🚀 New react-contoso sample application deployed here |
📖 The updated storybook is available here |
|
Closes #1457
Closes #2656
PR Type
Design considerations
Currently bundlers like rollup, webpack, and parcel are unable to analyze the mgt packages and determine which parts of the these dependencies are live code to be included. This is often called "Tree Shaking", dead code removal. To enable this behavior our libraries must be side effect free, or refactor the side effects in such a way that they can be avoided by developers.
The current implementation of our
@microsoft/mgt-components
relies on the side effects of using the@customElement
decorator to register the custom elements with the browser as part of the import of any dependency from the library at the top level, i.e.import { xyz } from '@microsoft/mgt-components';
. This results in all of the custom elements being registered as soon as one top level import exists. Completely removing this side effect would be a breaking change and one that we want to avoid until we're ready for a v4 release.Given these constraints it is necessary to:
@customElement
decorator and report these as errors?@fluentui/web-component
approaches component registration.@microsoft/mgt-components
library still has a side effect of registering all custom elements.@microsoft/mgt-components
, e.g.import { MgtPersonCard } from '@microsoft/mgt-components/exports;
@microsoft/mgt-react
library such that it avoids the top level side effect in@microsoft/mgt-components
and allows correct tree shaking by bundlers.Description of the changes
Marks most libraries as being side effect free in package.json to allow bundlers to determine which code should be included.
Marks the root index file in mgt-components as having side effects.
Removes the use of the
@customElement
decorator as this is a side effect generating pattern that adds code which executes at import time to register the custom elements.Creates a new
register{{ComponentClass}}Component()
function per component to provide an imperative function based component mechanism for registering a custom element, including any nested custom element usages. Having a registration function that uses a naming convention is essential to allow the React shims to be generated based on metadata generated via custom-elements-mainifest/analyzer.Refactors React shim generation to be file per shim component and a barrel file, this ensures that bunders can include dependencies via React usage on a per-file basis and eliminates ambiguity as to which dependencies are required for a given React component.
Refactors a number of functions/classes/interfaces into separate files to reduce the number of circular dependencies down to two chains (mgt-person => mgt-person-card => mgt-person & mgt-person => mgt-person-card => mgt-organization => mgt-person)
Adds a plugin for custom-elements-mainifest/analyzer to correctly determine out custom element definitions at build time for use in generation of the react package and metadata used in storybook for docs pages, this is necessary as our components now use a custom function to perform the registration of the custom element which the custom-elements-mainifest/analyzer is unable to detect and understand natively.
Refactored the import of MgtPersonCard in MgtPerson to become a dynamic import.
PR checklist
yarn build
) and changes have been tested in at least two supported browsers (Edge + non-Chromium based browser)yarn setLicense
)Other information
Still to do, try to sort out dynamic imports in mgt-person so that we only pull in the mgt-person-card when needed.
BEGIN_COMMIT_OVERRIDE
feat!: enable live code inclusion for consuming applications (#2642)
Packages are marked as side effect free or have the files that include side effects listed in package.json. This allows bundlers to discard code not in the direct dependency tree of an import.
Adds functions for registering components
Moved to generating a single file per react wrpper component and a barrel file
Update tests to use registerComponent calls
fixing render problem when given and surname are null on personType objects
move mgt-components to module type es2020 for dynamic imports
refactored to only lazy load the person-card code when opening the flyout
update list of components registered by registerMgtComponents
move sample app to use lazy + Suspense to chunk up component usage
adding documentation for tree shaking support
BREAKING CHANGE: applications importing
@microsoft/mgt-react
but not using the wrapper components will not have components automatically registered in the browser. This leads to a breaking change when these applications emit raw web component markup rather than using the wrapper components.END_COMMIT_OVERRIDE