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

Tree shake class components #3591

Merged
merged 4 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "preact",
"amdName": "preact",
"version": "10.5.13",
"version": "11.0.0-beta",
"private": false,
"description": "Fast 3kb React-compatible Virtual DOM library.",
"main": "dist/preact.js",
Expand All @@ -21,6 +21,13 @@
"Samsung>=8.2",
"not dead"
],
"sideEffects": [
"./compat/**/*",
"./debug/**/*",
"./devtools/**/*",
"./hooks/**/*",
"./test-utils/**/*"
],
"exports": {
".": {
"types": "./src/index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { patch } from './diff/patch';
import { DIRTY_BIT, FORCE_UPDATE, MODE_UNMOUNTING } from './constants';
import { getParentContext, getParentDom } from './tree';

export let ENABLE_CLASSES = false;

/**
* The render queue
* @type {import('./internal').RendererState}
Expand All @@ -23,6 +25,7 @@ export const rendererState = {
* getChildContext
*/
export function Component(props, context) {
ENABLE_CLASSES = true;
this.props = props;
this.context = context;
}
Expand Down
5 changes: 3 additions & 2 deletions src/diff/catch-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
MODE_PENDING_ERROR,
TYPE_COMPONENT
} from '../constants';
import { ENABLE_CLASSES } from '../component';

/**
* Find the closest error boundary to a thrown error and call it
Expand All @@ -19,13 +20,13 @@ export function _catchError(error, internal) {
~internal.flags & MODE_RERENDERING_ERROR
) {
try {
if (internal.type.getDerivedStateFromError) {
if (ENABLE_CLASSES && internal.type.getDerivedStateFromError) {
internal._component.setState(
internal.type.getDerivedStateFromError(error)
);
}

if (internal._component.componentDidCatch) {
if (ENABLE_CLASSES && internal._component.componentDidCatch) {
internal._component.componentDidCatch(error);
}

Expand Down
40 changes: 21 additions & 19 deletions src/diff/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { normalizeToVNode, Fragment } from '../create-element';
import { setProperty } from './props';
import { createInternal } from '../tree';
import options from '../options';
import { rendererState } from '../component';
import { ENABLE_CLASSES, rendererState } from '../component';
/**
* Diff two virtual nodes and apply proper changes to the DOM
* @param {import('../internal').Internal} internal The Internal node to mount
Expand Down Expand Up @@ -340,26 +340,28 @@ function mountComponent(internal, startDom) {
if (!c.state) c.state = {};
if (c._nextState == null) c._nextState = c.state;

if (type.getDerivedStateFromProps != null) {
if (c._nextState == c.state) {
c._nextState = Object.assign({}, c._nextState);
}
if (ENABLE_CLASSES) {
if (type.getDerivedStateFromProps != null) {
if (c._nextState == c.state) {
c._nextState = Object.assign({}, c._nextState);
}

Object.assign(
c._nextState,
type.getDerivedStateFromProps(newProps, c._nextState)
);
}
Object.assign(
c._nextState,
type.getDerivedStateFromProps(newProps, c._nextState)
);
}

if (type.getDerivedStateFromProps == null && c.componentWillMount != null) {
c.componentWillMount();
}
if (type.getDerivedStateFromProps == null && c.componentWillMount != null) {
c.componentWillMount();
}

if (c.componentDidMount != null) {
// If the component was constructed, queue up componentDidMount so the
// first time this internal commits (regardless of suspense or not) it
// will be called
internal._commitCallbacks.push(c.componentDidMount.bind(c));
if (c.componentDidMount != null) {
// If the component was constructed, queue up componentDidMount so the
// first time this internal commits (regardless of suspense or not) it
// will be called
internal._commitCallbacks.push(c.componentDidMount.bind(c));
}
}

c.context = componentContext;
Expand All @@ -375,7 +377,7 @@ function mountComponent(internal, startDom) {
while (counter++ < 25) {
internal.flags &= ~DIRTY_BIT;
if (renderHook) renderHook(internal);
if (internal.flags & TYPE_CLASS) {
if (ENABLE_CLASSES && internal.flags & TYPE_CLASS) {
renderResult = c.render(c.props, c.state, c.context);
// note: disable repeat render invocation for class components
break;
Expand Down
85 changes: 45 additions & 40 deletions src/diff/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { getDomSibling } from '../tree';
import { mountChildren } from './mount';
import { Fragment } from '../create-element';
import { rendererState } from '../component';
import { ENABLE_CLASSES, rendererState } from '../component';

/**
* Diff two virtual nodes and apply proper changes to the DOM
Expand Down Expand Up @@ -225,41 +225,44 @@ function patchComponent(internal, newVNode) {
c._nextState = c.state;
}

if (type.getDerivedStateFromProps != null) {
if (c._nextState == c.state) {
c._nextState = Object.assign({}, c._nextState);
}

Object.assign(
c._nextState,
type.getDerivedStateFromProps(newProps, c._nextState)
);
}

let oldProps = c.props;
let oldState = c.state;

if (
type.getDerivedStateFromProps == null &&
newProps !== oldProps &&
c.componentWillReceiveProps != null
) {
c.componentWillReceiveProps(newProps, componentContext);
}
if (ENABLE_CLASSES) {
if (type.getDerivedStateFromProps != null) {
if (c._nextState == c.state) {
c._nextState = Object.assign({}, c._nextState);
}

if (
!(internal.flags & FORCE_UPDATE) &&
c.shouldComponentUpdate != null &&
c.shouldComponentUpdate(newProps, c._nextState, componentContext) === false
) {
c.props = newProps;
c.state = c._nextState;
internal.flags |= SKIP_CHILDREN;
return;
}
Object.assign(
c._nextState,
type.getDerivedStateFromProps(newProps, c._nextState)
);
}

if (c.componentWillUpdate != null) {
c.componentWillUpdate(newProps, c._nextState, componentContext);
if (
type.getDerivedStateFromProps == null &&
newProps !== oldProps &&
c.componentWillReceiveProps != null
) {
c.componentWillReceiveProps(newProps, componentContext);
}

if (
!(internal.flags & FORCE_UPDATE) &&
c.shouldComponentUpdate != null &&
c.shouldComponentUpdate(newProps, c._nextState, componentContext) ===
false
) {
c.props = newProps;
c.state = c._nextState;
internal.flags |= SKIP_CHILDREN;
return;
}

if (c.componentWillUpdate != null) {
c.componentWillUpdate(newProps, c._nextState, componentContext);
}
}

c.context = componentContext;
Expand All @@ -274,7 +277,7 @@ function patchComponent(internal, newVNode) {
while (counter++ < 25) {
internal.flags &= ~DIRTY_BIT;
if (renderHook) renderHook(internal);
if (internal.flags & TYPE_CLASS) {
if (ENABLE_CLASSES && internal.flags & TYPE_CLASS) {
renderResult = c.render(c.props, c.state, c.context);
// note: disable repeat render invocation for class components
break;
Expand All @@ -297,15 +300,17 @@ function patchComponent(internal, newVNode) {
);
}

if (c.getSnapshotBeforeUpdate != null) {
snapshot = c.getSnapshotBeforeUpdate(oldProps, oldState);
}
if (ENABLE_CLASSES) {
if (c.getSnapshotBeforeUpdate != null) {
snapshot = c.getSnapshotBeforeUpdate(oldProps, oldState);
}

// Only schedule componentDidUpdate if the component successfully rendered
if (c.componentDidUpdate != null) {
internal._commitCallbacks.push(() => {
c.componentDidUpdate(oldProps, oldState, snapshot);
});
// Only schedule componentDidUpdate if the component successfully rendered
if (c.componentDidUpdate != null) {
internal._commitCallbacks.push(() => {
c.componentDidUpdate(oldProps, oldState, snapshot);
});
}
}

if (renderResult == null) {
Expand Down
3 changes: 2 additions & 1 deletion src/diff/unmount.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MODE_UNMOUNTING, TYPE_DOM, TYPE_ROOT } from '../constants';
import { unsubscribeFromContext } from '../create-context';
import options from '../options';
import { applyRef } from './refs';
import { ENABLE_CLASSES } from '../component';

/**
* Unmount a virtual node from the tree and apply DOM changes
Expand All @@ -24,7 +25,7 @@ export function unmount(internal, parentInternal, skipRemove) {
if ((r = internal._component)) {
unsubscribeFromContext(internal);

if (r.componentWillUnmount) {
if (ENABLE_CLASSES && r.componentWillUnmount) {
try {
r.componentWillUnmount();
} catch (e) {
Expand Down