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

fix(es/minifier): Don't inline functions used as arguments #3320

Merged
merged 13 commits into from
Jan 20, 2022

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Jan 20, 2022

Description:

swc_ecma_minifier:

  • Preserve a variable initialized with a function expression if it's used as an argument.

Related issue (if exists):


Investigation result

This is caused by the difference of the referential equality.

import { useCallback, useEffect, useState } from 'react'
import { jsx } from 'react/jsx-runtime'
import Form from '../components/Form';

function _defineProperty(obj, key, value) {
  return key in obj ? Object.defineProperty(obj, key, {
      value: value,
      enumerable: !0,
      configurable: !0,
      writable: !0
  }) : obj[key] = value, obj;
}

const Container = function (props) {
  return (0, jsx)(Form, function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = null != arguments[i] ? arguments[i] : {},
        ownKeys = Object.keys(source);
      "function" == typeof Object.getOwnPropertySymbols && (ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
        return Object.getOwnPropertyDescriptor(source, sym).enumerable;
      }))), ownKeys.forEach(function (key) {
        _defineProperty(target, key, source[key]);
      });
    }
    return target;
  }({}, props));
};

export default function PageWithSpread() {
  const [text, setText] = useState('');

  const handleChange = useCallback((e) => {
    setText(e.target.value);
  }, []);

  

  return (
    <Container
      onChange={handleChange}
      value={text}
    />
  )
}

works properly, but

import { useCallback, useEffect, useState } from 'react'
import { jsx } from 'react/jsx-runtime'
import Form from '../components/Form';

function _defineProperty(obj, key, value) {
  return key in obj ? Object.defineProperty(obj, key, {
      value: value,
      enumerable: !0,
      configurable: !0,
      writable: !0
  }) : obj[key] = value, obj;
}

export default function PageWithSpread() {
  const [text, setText] = useState('');

  const handleChange = useCallback((e) => {
    setText(e.target.value);
  }, []);

  const Container = function (props) {
    return (0, jsx)(Form, function (target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = null != arguments[i] ? arguments[i] : {},
          ownKeys = Object.keys(source);
        "function" == typeof Object.getOwnPropertySymbols && (ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
          return Object.getOwnPropertyDescriptor(source, sym).enumerable;
        }))), ownKeys.forEach(function (key) {
          _defineProperty(target, key, source[key]);
        });
      }
      return target;
    }({}, props));
  };

  return (
    <Container
      onChange={handleChange}
      value={text}
    />
  )
}

errors.

Only difference is the const Container

@kdy1 kdy1 added this to the v1.2.133 milestone Jan 20, 2022
@kdy1 kdy1 changed the title fix(es/minifier): Fix bug of spread fix(es/minifier): Don't inline function if it depends on referential equality Jan 20, 2022
@kdy1 kdy1 changed the title fix(es/minifier): Don't inline function if it depends on referential equality fix(es/minifier): Don't inline functions if it depends on referential equality Jan 20, 2022
@kdy1 kdy1 changed the title fix(es/minifier): Don't inline functions if it depends on referential equality fix(es/minifier): Don't inline functions used as arguemnts Jan 20, 2022
@kdy1 kdy1 changed the title fix(es/minifier): Don't inline functions used as arguemnts fix(es/minifier): Don't inline functions used as arguments Jan 20, 2022
@kdy1 kdy1 marked this pull request as ready for review January 20, 2022 05:29
@kdy1 kdy1 merged commit 57204e3 into swc-project:main Jan 20, 2022
@kdy1 kdy1 deleted the next-33458 branch January 20, 2022 07:06
@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

1 participant