From 25f430716793917f0bc45095f9a191deec6d8c19 Mon Sep 17 00:00:00 2001 From: Ivan Kravchenko Date: Fri, 16 Dec 2016 19:30:29 -0500 Subject: [PATCH] 0.8.2 precompile --- .babelrc | 4 + .npmignore | 2 + build/dispatchTrackingEvent.js | 18 +++++ build/index.js | 25 ++++++ build/makeClassMemberDecorator.js | 73 +++++++++++++++++ build/trackEventMethodDecorator.js | 32 ++++++++ build/withTrackingComponentDecorator.js | 100 ++++++++++++++++++++++++ package.json | 18 ++++- 8 files changed, 269 insertions(+), 3 deletions(-) create mode 100644 .babelrc create mode 100644 .npmignore create mode 100644 build/dispatchTrackingEvent.js create mode 100644 build/index.js create mode 100644 build/makeClassMemberDecorator.js create mode 100644 build/trackEventMethodDecorator.js create mode 100644 build/withTrackingComponentDecorator.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..53e4a51 --- /dev/null +++ b/.babelrc @@ -0,0 +1,4 @@ +{ + "plugins": ["transform-runtime", "transform-decorators-legacy"], + "presets": ["es2015", "stage-0", "react"] +} diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..ba49d74 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +src +node_modules diff --git a/build/dispatchTrackingEvent.js b/build/dispatchTrackingEvent.js new file mode 100644 index 0000000..13f8ecc --- /dev/null +++ b/build/dispatchTrackingEvent.js @@ -0,0 +1,18 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = dispatchTrackingEvent; + +var _customEvent = require('custom-event'); + +var _customEvent2 = _interopRequireDefault(_customEvent); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function dispatchTrackingEvent(data) { + document.dispatchEvent(new _customEvent2.default('FirehoseTrackingEvent', { + detail: data + })); +} \ No newline at end of file diff --git a/build/index.js b/build/index.js new file mode 100644 index 0000000..44511ac --- /dev/null +++ b/build/index.js @@ -0,0 +1,25 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _withTrackingComponentDecorator = require('./withTrackingComponentDecorator'); + +Object.defineProperty(exports, 'withTracking', { + enumerable: true, + get: function get() { + return _interopRequireDefault(_withTrackingComponentDecorator).default; + } +}); + +var _trackEventMethodDecorator = require('./trackEventMethodDecorator'); + +Object.defineProperty(exports, 'trackEvent', { + enumerable: true, + get: function get() { + return _interopRequireDefault(_trackEventMethodDecorator).default; + } +}); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } \ No newline at end of file diff --git a/build/makeClassMemberDecorator.js b/build/makeClassMemberDecorator.js new file mode 100644 index 0000000..d86c9d4 --- /dev/null +++ b/build/makeClassMemberDecorator.js @@ -0,0 +1,73 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _defineProperty = require('babel-runtime/core-js/reflect/define-property'); + +var _defineProperty2 = _interopRequireDefault(_defineProperty); + +var _apply = require('babel-runtime/core-js/reflect/apply'); + +var _apply2 = _interopRequireDefault(_apply); + +exports.default = makeClassMemberDecorator; + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * Helper to decorate class member + * Supports class plain methods, field syntax and lazy methods + * @param {Function} decorate Actual decorator function. + * Example: + * decoratedFn => function () { + * // do stuff... + * return Reflect.apply(decoratedFn, this, arguments); + * } + * @returns {Function} Class member decorator ((target, name, descriptor) => newDescriptor) + */ +function makeClassMemberDecorator(decorate) { + return function (target, name, descriptor) { + var configurable = descriptor.configurable, + enumerable = descriptor.enumerable, + value = descriptor.value, + _get = descriptor.get, + initializer = descriptor.initializer; + + + if (value) { + return { + configurable: configurable, + enumerable: enumerable, + value: decorate(value) + }; + } + + // support lazy initializer + if (_get || initializer) { + return { + configurable: configurable, + enumerable: enumerable, + get: function get() { + // This happens if someone accesses the + // property directly on the prototype + if (this === target) { + return null; + } + + var resolvedValue = initializer ? (0, _apply2.default)(initializer, this, []) : (0, _apply2.default)(_get, this, []); + var decoratedValue = decorate(resolvedValue).bind(this); + (0, _defineProperty2.default)(this, name, { + configurable: configurable, + enumerable: enumerable, + value: decoratedValue + }); + return decoratedValue; + } + }; + } + + throw new Error('called makeClassMemberDecorator on unsupported descriptor'); + }; +} \ No newline at end of file diff --git a/build/trackEventMethodDecorator.js b/build/trackEventMethodDecorator.js new file mode 100644 index 0000000..0a655b1 --- /dev/null +++ b/build/trackEventMethodDecorator.js @@ -0,0 +1,32 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _apply = require('babel-runtime/core-js/reflect/apply'); + +var _apply2 = _interopRequireDefault(_apply); + +exports.default = trackEventMethodDecorator; + +var _makeClassMemberDecorator = require('./makeClassMemberDecorator'); + +var _makeClassMemberDecorator2 = _interopRequireDefault(_makeClassMemberDecorator); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function trackEventMethodDecorator() { + var trackingData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; + + return (0, _makeClassMemberDecorator2.default)(function (decoratedFn) { + return function () { + if (this.props && typeof this.props.trackEvent === 'function') { + var thisTrackingData = typeof trackingData === 'function' ? trackingData(this.props, arguments) : trackingData; + this.props.trackEvent(thisTrackingData); + } + + return (0, _apply2.default)(decoratedFn, this, arguments); + }; + }); +} \ No newline at end of file diff --git a/build/withTrackingComponentDecorator.js b/build/withTrackingComponentDecorator.js new file mode 100644 index 0000000..0149552 --- /dev/null +++ b/build/withTrackingComponentDecorator.js @@ -0,0 +1,100 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _extends2 = require('babel-runtime/helpers/extends'); + +var _extends3 = _interopRequireDefault(_extends2); + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +exports.default = withTrackingComponentDecorator; + +var _react = require('react'); + +var _react2 = _interopRequireDefault(_react); + +var _dispatchTrackingEvent = require('./dispatchTrackingEvent'); + +var _dispatchTrackingEvent2 = _interopRequireDefault(_dispatchTrackingEvent); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function withTrackingComponentDecorator() { + var trackingContext = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; + + return function (DecoratedComponent) { + var _class, _temp; + + var decoratedComponentName = DecoratedComponent.displayName || DecoratedComponent.name || 'Component'; + + return _temp = _class = function (_Component) { + (0, _inherits3.default)(WithTracking, _Component); + + function WithTracking(props) { + (0, _classCallCheck3.default)(this, WithTracking); + + var _this = (0, _possibleConstructorReturn3.default)(this, (WithTracking.__proto__ || (0, _getPrototypeOf2.default)(WithTracking)).call(this, props)); + + _this.trackEvent = _this.trackEvent.bind(_this); + return _this; + } + + (0, _createClass3.default)(WithTracking, [{ + key: 'getChildContext', + value: function getChildContext() { + var thisTrackingContext = typeof trackingContext === 'function' ? trackingContext(this.props) : trackingContext; + return { + tracking: (0, _extends3.default)({}, this.context.tracking, thisTrackingContext) + }; + } + }, { + key: 'trackEvent', + value: function trackEvent(data) { + (0, _dispatchTrackingEvent2.default)((0, _extends3.default)({}, this.getChildContext().tracking, data)); + } + }, { + key: 'componentDidMount', + value: function componentDidMount() { + if (trackingContext.page) { + this.trackEvent({ + action: 'pageview' + }); + } + } + }, { + key: 'render', + value: function render() { + return _react2.default.createElement(DecoratedComponent, (0, _extends3.default)({}, this.props, { + trackEvent: this.trackEvent + })); + } + }]); + return WithTracking; + }(_react.Component), _class.displayName = 'WithTracking(' + decoratedComponentName + ')', _class.contextTypes = { + tracking: _react.PropTypes.object + }, _class.childContextTypes = { + tracking: _react.PropTypes.object + }, _temp; + }; +} \ No newline at end of file diff --git a/package.json b/package.json index 6cf14dc..8306286 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "nyt-react-tracking", - "version": "0.8.1", + "version": "0.8.2", "description": "Experimental \"tracking layer\" implementation for React. The core idea is to provide declarative way to add tracking for React applications.", - "main": "src/index.js", + "main": "build/index.js", "repository": { "type": "git", "url": "git+https://github.com/nytm/nyt-react-tracking.git" @@ -20,6 +20,18 @@ "custom-event": "^1.0.1" }, "peerDependencies": { - "react": "^15.0.0" + "react": "^15.0.0", + "babel-runtime": "^6.20.0" + }, + "devDependencies": { + "babel-cli": "^6.18.0", + "babel-plugin-transform-decorators-legacy": "^1.3.4", + "babel-plugin-transform-runtime": "^6.15.0", + "babel-preset-es2015": "^6.18.0", + "babel-preset-react": "^6.16.0", + "babel-preset-stage-0": "^6.16.0" + }, + "scripts": { + "build": "babel --out-dir build src" } }