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

TreeShaking 表现问题 #3355

Closed
zombieJ opened this issue Oct 8, 2019 · 8 comments
Closed

TreeShaking 表现问题 #3355

zombieJ opened this issue Oct 8, 2019 · 8 comments

Comments

@zombieJ
Copy link
Contributor

zombieJ commented Oct 8, 2019

创建一个页面如下:

import React from 'react';
import Input from 'antd/es/input';
import 'antd/dist/antd.min.css';

export default () => <Input />;

修改 antd/es/config-provider/index.js 为:

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

// TODO: remove this lint
// SFC has specified a displayName, but not worked.

/* eslint-disable react/display-name */
import * as React from 'react';
import createReactContext from '@ant-design/create-react-context';
import defaultRenderEmpty from './renderEmpty';
import LocaleProvider, { ANT_MARK } from '../locale-provider';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
export var configConsumerProps = ['getPopupContainer', 'rootPrefixCls', 'getPrefixCls', 'renderEmpty', 'csp', 'autoInsertSpaceInButton', 'locale'];
var ConfigContext = createReactContext({
  // We provide a default function for Context without provider
  getPrefixCls: function getPrefixCls(suffixCls, customizePrefixCls) {
    if (customizePrefixCls) return customizePrefixCls;
    return "ant-".concat(suffixCls);
  },
  renderEmpty: defaultRenderEmpty
});
export var ConfigConsumer = ConfigContext.Consumer;

var ConfigProvider =
/*#__PURE__*/
function (_React$Component) {
  _inherits(ConfigProvider, _React$Component);

  function ConfigProvider() {
    var _this;

    _classCallCheck(this, ConfigProvider);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(ConfigProvider).apply(this, arguments));

    _this.getPrefixCls = function (suffixCls, customizePrefixCls) {
      var _this$props$prefixCls = _this.props.prefixCls,
          prefixCls = _this$props$prefixCls === void 0 ? 'ant' : _this$props$prefixCls;
      if (customizePrefixCls) return customizePrefixCls;
      return suffixCls ? "".concat(prefixCls, "-").concat(suffixCls) : prefixCls;
    };

    _this.renderProvider = function (context, legacyLocale) {
      var _this$props = _this.props,
          children = _this$props.children,
          getPopupContainer = _this$props.getPopupContainer,
          renderEmpty = _this$props.renderEmpty,
          csp = _this$props.csp,
          autoInsertSpaceInButton = _this$props.autoInsertSpaceInButton,
          locale = _this$props.locale;

      var config = _extends(_extends({}, context), {
        getPrefixCls: _this.getPrefixCls,
        csp: csp,
        autoInsertSpaceInButton: autoInsertSpaceInButton
      });

      if (getPopupContainer) {
        config.getPopupContainer = getPopupContainer;
      }

      if (renderEmpty) {
        config.renderEmpty = renderEmpty;
      }

      // return React.createElement(ConfigContext.Provider, {
      //   value: config
      // }, React.createElement(LocaleProvider, {
      //   locale: locale || legacyLocale,
      //   _ANT_MARK__: ANT_MARK
      // }, children));
      return React.createElement(ConfigContext.Provider, {
        value: config
      }, children);
    };

    return _this;
  }

  _createClass(ConfigProvider, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      // return React.createElement(LocaleReceiver, null, function (_, __, legacyLocale) {
      //   return React.createElement(ConfigConsumer, null, function (context) {
      //     return _this2.renderProvider(context, legacyLocale);
      //   });
      // });
      return React.createElement(ConfigConsumer, null, function (context) {
        return _this2.renderProvider(context);
      });
    }
  }]);

  return ConfigProvider;
}(React.Component);

export function withConfigConsumer(config) {
  return function withConfigConsumerFunc(Component) {
    // Wrap with ConfigConsumer. Since we need compatible with react 15, be care when using ref methods
    var SFC = function SFC(props) {
      return React.createElement(ConfigConsumer, null, function (configProps) {
        var basicPrefixCls = config.prefixCls;
        var getPrefixCls = configProps.getPrefixCls;
        var customizePrefixCls = props.prefixCls;
        var prefixCls = getPrefixCls(basicPrefixCls, customizePrefixCls);
        return React.createElement(Component, _extends({}, configProps, props, {
          prefixCls: prefixCls
        }));
      });
    };

    var cons = Component.constructor;
    var name = cons && cons.displayName || Component.name || 'Component';
    SFC.displayName = "withConfigConsumer(".concat(name, ")");
    return SFC;
  };
}
export default ConfigProvider;
//# sourceMappingURL=index.js.map

其中 locale-provider 代码没有被引用到,但是没有被 shaking。

@xiaohuoni
Copy link
Member

标记一下 bug 提一下 P 级

@sorrycc sorrycc added type(bug) and removed pr(bug) labels Oct 23, 2019
@xiaohuoni
Copy link
Member

xiaohuoni commented Oct 28, 2019

疑惑: antd 按需引入无效?

引入Button的单页demo

import { Button } from 'antd';
export default () => (
  <div>
    <Button>Index Page</Button>
  </div>
);

image

pro项目的分析

image

看起来两个包引入的大小是一样的。
@zombieJ 豆酱,你怎么看?

@zombieJ
Copy link
Contributor Author

zombieJ commented Oct 29, 2019

看一下 moment,只用了 Button 是不应该加载 moment 的。另外 antd 内部改了依赖逻辑,把 LocaleProvider 抽出来了。要用旧版的测试一下:https://github.com/ant-design/ant-design/blob/master/components/config-provider/context.tsx

ref:

@xiaohuoni
Copy link
Member

更新了一下,上面的配图。

@EliazTray
Copy link

这里 preset 的 modules 设置成了 commonjs,或许和 webpack 文档 中提到的第二点矛盾?

@cixiu
Copy link

cixiu commented Nov 28, 2019

只用Button,依然会把 moment 给打包进去

@stale
Copy link

stale bot commented Jan 27, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jan 27, 2020
@stale stale bot closed this as completed Feb 3, 2020
@atzcl
Copy link
Contributor

atzcl commented Mar 6, 2020

有任何进展?

@xiaohuoni xiaohuoni reopened this Mar 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants