Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
react-loadable flushModuleIds / flushServerSideRequires
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.default = Loadable;
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var isWebpack = typeof __webpack_require__ !== "undefined";
var requireFn = isWebpack ? __webpack_require__ : module.require.bind(module);
var babelInterop = function babelInterop(obj) {
// $FlowIgnore
return obj && obj.__esModule ? obj.default : obj;
};
let moduleIds = [];
function Loadable(opts) {
var loader = opts.loader;
var LoadingComponent = opts.LoadingComponent;
var delay = opts.delay || 200;
var serverSideRequirePath = opts.serverSideRequirePath;
var webpackRequireWeakId = opts.webpackRequireWeakId;
var resolveModuleFn = opts.resolveModule ? opts.resolveModule : babelInterop;
var isLoading = false;
var outsideComponent = null;
var outsidePromise = null;
var outsideError = null;
var tryRequire = function tryRequire(pathOrId) {
try {
// $FlowIgnore
return resolveModuleFn(requireFn(pathOrId));
} catch (err) {}
return null;
};
if (!isWebpack && serverSideRequirePath) {
outsideComponent = tryRequire(serverSideRequirePath);
}
var load = function load() {
if (!outsidePromise) {
isLoading = true;
outsidePromise = loader().then(function (Component) {
isLoading = false;
outsideComponent = resolveModuleFn(Component);
}).catch(function (error) {
isLoading = false;
outsideError = error;
});
}
return outsidePromise;
};
return function (_React$Component) {
_inherits(Loadable, _React$Component);
_createClass(Loadable, null, [{
key: "preload",
value: function preload() {
load();
}
}]);
function Loadable(props) {
_classCallCheck(this, Loadable);
var _this = _possibleConstructorReturn(this, (Loadable.__proto__ || Object.getPrototypeOf(Loadable)).call(this, props));
// TODO: could this happen in below condition?
moduleIds.push(webpackRequireWeakId());
if (!outsideComponent && isWebpack && webpackRequireWeakId) {
var weakId = webpackRequireWeakId();
if (__webpack_modules__[weakId]) {
// if it's not in webpack modules, we wont be able
// to load it. If we attempt to, we mess up webpack's
// internal state, so only tryRequire if it's already
// in webpack modules.
outsideComponent = tryRequire(weakId);
}
}
_this.state = {
error: outsideError,
pastDelay: false,
Component: outsideComponent
};
return _this;
}
_createClass(Loadable, [{
key: "componentWillMount",
value: function componentWillMount() {
var _this2 = this;
this._mounted = true;
if (this.state.Component) {
return;
}
this._timeout = setTimeout(function () {
_this2.setState({ pastDelay: true });
}, delay);
load().then(function () {
if (!_this2._mounted) return;
clearTimeout(_this2._timeout);
_this2.setState({
error: outsideError,
pastDelay: false,
Component: outsideComponent
});
});
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this._mounted = false;
clearTimeout(this._timeout);
}
}, {
key: "render",
value: function render() {
var _state = this.state,
pastDelay = _state.pastDelay,
error = _state.error,
Component = _state.Component;
if (isLoading || error) {
return _react2.default.createElement(LoadingComponent, {
isLoading: isLoading,
pastDelay: pastDelay,
error: error
});
} else if (Component) {
return _react2.default.createElement(Component, this.props);
} else {
return null;
}
}
}]);
return Loadable;
}(_react2.default.Component);
}
Loadable.flushModuleIds = () => {
const ret = moduleIds;
moduleIds = [];
return ret;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.