react-async-script
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -0,1 +1,9 @@ | ||
v1.1.0 - Sun Jul 14 2019 10:19:00 PST | ||
-------------------------------------- | ||
- Switch to Jest [(#45)](https://github.com/dozoisch/react-async-script/pull/45) | ||
- Upgrade dependencies and add es module [(#44)](https://github.com/dozoisch/react-async-script/pull/44) | ||
v1.0.1 - Sun 17 Apr 2019 19:14:00 PST | ||
@@ -2,0 +10,0 @@ -------------------------------------- |
"use strict"; | ||
exports.__esModule = true; | ||
exports["default"] = makeAsyncScript; | ||
var _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; }; | ||
exports.default = makeAsyncScript; | ||
var _react = require("react"); | ||
var _propTypes = require("prop-types"); | ||
var _propTypes = _interopRequireDefault(require("prop-types")); | ||
var _propTypes2 = _interopRequireDefault(_propTypes); | ||
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics")); | ||
var _hoistNonReactStatics = require("hoist-non-react-statics"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics); | ||
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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var SCRIPT_MAP = {}; // A counter used to generate a unique id for each component that uses the 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 SCRIPT_MAP = {}; | ||
// A counter used to generate a unique id for each component that uses the function | ||
var idCount = 0; | ||
@@ -39,10 +29,11 @@ | ||
var AsyncScriptLoader = function (_Component) { | ||
_inherits(AsyncScriptLoader, _Component); | ||
var AsyncScriptLoader = | ||
/*#__PURE__*/ | ||
function (_Component) { | ||
_inheritsLoose(AsyncScriptLoader, _Component); | ||
function AsyncScriptLoader(props, context) { | ||
_classCallCheck(this, AsyncScriptLoader); | ||
var _this; | ||
var _this = _possibleConstructorReturn(this, _Component.call(this, props, context)); | ||
_this = _Component.call(this, props, context) || this; | ||
_this.state = {}; | ||
@@ -53,10 +44,13 @@ _this.__scriptURL = ""; | ||
AsyncScriptLoader.prototype.asyncScriptLoaderGetScriptLoaderID = function asyncScriptLoaderGetScriptLoaderID() { | ||
var _proto = AsyncScriptLoader.prototype; | ||
_proto.asyncScriptLoaderGetScriptLoaderID = function asyncScriptLoaderGetScriptLoaderID() { | ||
if (!this.__scriptLoaderID) { | ||
this.__scriptLoaderID = "async-script-loader-" + idCount++; | ||
} | ||
return this.__scriptLoaderID; | ||
}; | ||
AsyncScriptLoader.prototype.setupScriptURL = function setupScriptURL() { | ||
_proto.setupScriptURL = function setupScriptURL() { | ||
this.__scriptURL = typeof getScriptURL === "function" ? getScriptURL() : getScriptURL; | ||
@@ -66,3 +60,3 @@ return this.__scriptURL; | ||
AsyncScriptLoader.prototype.asyncScriptLoaderHandleLoad = function asyncScriptLoaderHandleLoad(state) { | ||
_proto.asyncScriptLoaderHandleLoad = function asyncScriptLoaderHandleLoad(state) { | ||
var _this2 = this; | ||
@@ -76,14 +70,17 @@ | ||
AsyncScriptLoader.prototype.asyncScriptLoaderTriggerOnScriptLoaded = function asyncScriptLoaderTriggerOnScriptLoaded() { | ||
_proto.asyncScriptLoaderTriggerOnScriptLoaded = function asyncScriptLoaderTriggerOnScriptLoaded() { | ||
var mapEntry = SCRIPT_MAP[this.__scriptURL]; | ||
if (!mapEntry || !mapEntry.loaded) { | ||
throw new Error("Script is not loaded."); | ||
} | ||
for (var obsKey in mapEntry.observers) { | ||
mapEntry.observers[obsKey](mapEntry); | ||
} | ||
delete window[options.callbackName]; | ||
}; | ||
AsyncScriptLoader.prototype.componentDidMount = function componentDidMount() { | ||
_proto.componentDidMount = function componentDidMount() { | ||
var _this3 = this; | ||
@@ -96,25 +93,27 @@ | ||
callbackName = _options.callbackName, | ||
scriptId = _options.scriptId; | ||
scriptId = _options.scriptId; // check if global object already attached to window | ||
// check if global object already attached to window | ||
if (globalName && typeof window[globalName] !== "undefined") { | ||
SCRIPT_MAP[scriptURL] = { loaded: true, observers: {} }; | ||
} | ||
SCRIPT_MAP[scriptURL] = { | ||
loaded: true, | ||
observers: {} | ||
}; | ||
} // check if script loading already | ||
// check if script loading already | ||
if (SCRIPT_MAP[scriptURL]) { | ||
var entry = SCRIPT_MAP[scriptURL]; | ||
// if loaded or errored then "finish" | ||
var entry = SCRIPT_MAP[scriptURL]; // if loaded or errored then "finish" | ||
if (entry && (entry.loaded || entry.errored)) { | ||
this.asyncScriptLoaderHandleLoad(entry); | ||
return; | ||
} | ||
// if still loading then callback to observer queue | ||
} // if still loading then callback to observer queue | ||
entry.observers[key] = function (entry) { | ||
return _this3.asyncScriptLoaderHandleLoad(entry); | ||
}; | ||
return; | ||
} | ||
/* | ||
@@ -125,6 +124,10 @@ * hasn't started loading | ||
*/ | ||
var observers = {}; | ||
observers[key] = function (entry) { | ||
return _this3.asyncScriptLoaderHandleLoad(entry); | ||
}; | ||
SCRIPT_MAP[scriptURL] = { | ||
@@ -134,7 +137,6 @@ loaded: false, | ||
}; | ||
var script = document.createElement("script"); | ||
script.src = scriptURL; | ||
script.async = true; | ||
if (scriptId) { | ||
@@ -165,2 +167,3 @@ script.id = scriptId; | ||
var mapEntry = SCRIPT_MAP[scriptURL]; | ||
if (mapEntry) { | ||
@@ -172,2 +175,3 @@ mapEntry.loaded = true; | ||
} | ||
observer(mapEntry); | ||
@@ -178,4 +182,6 @@ return true; | ||
}; | ||
script.onerror = function () { | ||
var mapEntry = SCRIPT_MAP[scriptURL]; | ||
if (mapEntry) { | ||
@@ -193,7 +199,9 @@ mapEntry.errored = true; | ||
AsyncScriptLoader.prototype.componentWillUnmount = function componentWillUnmount() { | ||
_proto.componentWillUnmount = function componentWillUnmount() { | ||
// Remove tag script | ||
var scriptURL = this.__scriptURL; | ||
if (options.removeOnUnmount === true) { | ||
var allScripts = document.getElementsByTagName("script"); | ||
for (var i = 0; i < allScripts.length; i += 1) { | ||
@@ -206,7 +214,10 @@ if (allScripts[i].src.indexOf(scriptURL) > -1) { | ||
} | ||
} | ||
// Clean the observer entry | ||
} // Clean the observer entry | ||
var mapEntry = SCRIPT_MAP[scriptURL]; | ||
if (mapEntry) { | ||
delete mapEntry.observers[this.asyncScriptLoaderGetScriptLoaderID()]; | ||
if (options.removeOnUnmount === true) { | ||
@@ -218,10 +229,9 @@ delete SCRIPT_MAP[scriptURL]; | ||
AsyncScriptLoader.prototype.render = function render() { | ||
var globalName = options.globalName; | ||
// remove asyncScriptOnLoad from childProps | ||
_proto.render = function render() { | ||
var globalName = options.globalName; // remove asyncScriptOnLoad from childProps | ||
var _props = this.props, | ||
asyncScriptOnLoad = _props.asyncScriptOnLoad, | ||
forwardedRef = _props.forwardedRef, | ||
childProps = _objectWithoutProperties(_props, ["asyncScriptOnLoad", "forwardedRef"]); // eslint-disable-line no-unused-vars | ||
var _this$props = this.props, | ||
asyncScriptOnLoad = _this$props.asyncScriptOnLoad, | ||
forwardedRef = _this$props.forwardedRef, | ||
childProps = _objectWithoutPropertiesLoose(_this$props, ["asyncScriptOnLoad", "forwardedRef"]); // eslint-disable-line no-unused-vars | ||
@@ -232,2 +242,3 @@ | ||
} | ||
childProps.ref = forwardedRef; | ||
@@ -238,5 +249,3 @@ return (0, _react.createElement)(WrappedComponent, childProps); | ||
return AsyncScriptLoader; | ||
}(_react.Component); | ||
// Note the second param "ref" provided by React.forwardRef. | ||
}(_react.Component); // Note the second param "ref" provided by React.forwardRef. | ||
// We can pass it along to AsyncScriptLoader as a regular prop, e.g. "forwardedRef" | ||
@@ -247,11 +256,12 @@ // And it can then be attached to the Component. | ||
var ForwardedComponent = (0, _react.forwardRef)(function (props, ref) { | ||
return (0, _react.createElement)(AsyncScriptLoader, _extends({}, props, { forwardedRef: ref })); | ||
return (0, _react.createElement)(AsyncScriptLoader, _extends({}, props, { | ||
forwardedRef: ref | ||
})); | ||
}); | ||
ForwardedComponent.displayName = "AsyncScriptLoader(" + wrappedComponentName + ")"; | ||
ForwardedComponent.propTypes = { | ||
asyncScriptOnLoad: _propTypes2.default.func | ||
asyncScriptOnLoad: _propTypes["default"].func | ||
}; | ||
return (0, _hoistNonReactStatics2.default)(ForwardedComponent, WrappedComponent); | ||
return (0, _hoistNonReactStatics["default"])(ForwardedComponent, WrappedComponent); | ||
}; | ||
} |
{ | ||
"name": "react-async-script", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A composition mixin for loading scripts asynchronously for React", | ||
"main": "lib/async-script-loader.js", | ||
"module": "lib/esm/async-script-loader.js", | ||
"scripts": { | ||
"build": "rm -rf lib && babel src --out-dir lib", | ||
"build": "rm -rf lib && npm run build:cjs && npm run build:esm", | ||
"build:cjs": "babel src --out-dir lib", | ||
"build:esm": "BABEL_ENV=esm babel src --out-dir lib/esm", | ||
"lint": "eslint ./", | ||
"pretty": "prettier --write src/*.js ./*.js test/*.js", | ||
"test": "karma start --single-run", | ||
"test-watch": "karma start", | ||
"test": "jest", | ||
"test-watch": "jest --watch", | ||
"prepare": "npm run build" | ||
@@ -30,34 +33,16 @@ }, | ||
"devDependencies": { | ||
"babel-cli": "^6.0.0", | ||
"babel-core": "^6.0.0", | ||
"babel-eslint": "^7.0.0", | ||
"babel-loader": "^6.0.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.23.0", | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-preset-es2015": "^6.22.0", | ||
"babel-preset-react": "^6.23.0", | ||
"babel-register": "^6.23.0", | ||
"babel-runtime": "^6.0.0", | ||
"chai": "^3.5.0", | ||
"es5-shim": "~4.1.3", | ||
"es6-shim": "^0.35.3", | ||
"eslint": "^5.3.0", | ||
"@babel/cli": "^7.4.4", | ||
"@babel/core": "^7.4.5", | ||
"@babel/preset-env": "^7.4.5", | ||
"@babel/preset-react": "^7.0.0", | ||
"@babel/register": "^7.4.4", | ||
"babel-eslint": "^10.0.2", | ||
"eslint": "^5.16.0", | ||
"eslint-config-prettier": "^3.0.1", | ||
"eslint-plugin-prettier": "^2.6.2", | ||
"karma": "~0.13.9", | ||
"karma-chai": "~0.1.0", | ||
"karma-chrome-launcher": "~0.2.0", | ||
"karma-cli": "~0.1.0", | ||
"karma-mocha": "~0.2.0", | ||
"karma-mocha-reporter": "~1.1.1", | ||
"karma-phantomjs-launcher": "^0.2.1", | ||
"karma-sourcemap-loader": "~0.3.5", | ||
"karma-webpack": "~1.7.0", | ||
"mocha": "~2.3.3", | ||
"phantomjs": "^2.0.0", | ||
"jest": "^24.8.0", | ||
"prettier": "^1.14.2", | ||
"react": "^16.4.2", | ||
"react-dom": "^16.4.2", | ||
"react-is": "^16.4.2", | ||
"webpack": "~1.14.0" | ||
"react-is": "^16.4.2" | ||
}, | ||
@@ -68,5 +53,5 @@ "peerDependencies": { | ||
"dependencies": { | ||
"hoist-non-react-statics": "^3.0.1", | ||
"hoist-non-react-statics": "^3.3.0", | ||
"prop-types": "^15.5.0" | ||
} | ||
} |
14
700
41249