Skip to content

Commit

Permalink
Attach minor version to global vars and events
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Brain committed Feb 5, 2019
1 parent 675ddec commit 50f8f2b
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 22 deletions.
7 changes: 6 additions & 1 deletion globals.js
@@ -1,8 +1,13 @@
/* @flow */
/* eslint import/no-commonjs: 0 */

const { getCurrentVersion } = require('grumbler-scripts/config/webpack.config');

const pkg = require('./package.json');

module.exports = {
'__POST_ROBOT__': {
__POST_ROBOT__: {
__GLOBAL_KEY__: `__post_robot_${ getCurrentVersion(pkg) }__`,
__AUTO_SETUP__: true,
__IE_POPUP_SUPPORT__: true,
__GLOBAL_MESSAGE_SUPPORT__: true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -13,7 +13,7 @@
"babel": "babel src/ --out-dir dist/module",
"webpack": "babel-node --config-file ./node_modules/grumbler-scripts/config/.babelrc-node --plugins=transform-es2015-modules-commonjs ./node_modules/.bin/webpack --display-optimization-bailout --progress",
"test": "npm run lint && npm run flow-typed && npm run flow && npm run karma",
"build": "npm run test && npm run babel && npm run webpack",
"build": "npm run test && npm run babel && npm run webpack -- $@",
"release": "./publish.sh",
"release:patch": "./publish.sh patch",
"release:minor": "./publish.sh minor",
Expand Down
2 changes: 1 addition & 1 deletion publish.sh
Expand Up @@ -15,7 +15,7 @@ fi;
rm -rf node_modules/cross-domain-safe-weakmap node_modules/zalgo-promise node_modules/cross-domain-utils node_modules/belter
npm install cross-domain-safe-weakmap zalgo-promise cross-domain-utils belter

npm run build;
npm run build -- --level=${1-patch};

git add dist;
git commit -m "Dist" || echo "Nothing to distribute";
Expand Down
2 changes: 0 additions & 2 deletions src/conf/constants.js
Expand Up @@ -17,8 +17,6 @@ export const MESSAGE_NAME = {
OPEN_TUNNEL: ('postrobot_open_tunnel' : 'postrobot_open_tunnel')
};

export const __POST_ROBOT__ = ('__postRobot__' : '__postRobot__');

export const SEND_STRATEGY = {
POST_MESSAGE: ('postrobot_post_message' : 'postrobot_post_message'),
BRIDGE: ('postrobot_bridge' : 'postrobot_bridge'),
Expand Down
1 change: 1 addition & 0 deletions src/declarations.js
@@ -1,6 +1,7 @@
/* @flow */

declare var __POST_ROBOT__ : {
__GLOBAL_KEY__ : string,
__AUTO_SETUP__ : boolean,
__IE_POPUP_SUPPORT__ : boolean,
__GLOBAL_MESSAGE_SUPPORT__ : boolean
Expand Down
3 changes: 1 addition & 2 deletions src/drivers/receive/index.js
Expand Up @@ -3,7 +3,6 @@
import { isWindowClosed, type CrossDomainWindowType, getDomain, isSameTopWindow } from 'cross-domain-utils/src';
import { addEventListener, noop } from 'belter/src';

import { __POST_ROBOT__ } from '../../conf';
import { markWindowKnown, needsGlobalMessagingForBrowser } from '../../lib';
import { deserializeMessage } from '../../serialize';
import { getGlobal, globalStore } from '../../global';
Expand All @@ -29,7 +28,7 @@ function parseMessage(message : string, source : CrossDomainWindowType, origin :
return;
}

parsedMessage = parsedMessage[__POST_ROBOT__];
parsedMessage = parsedMessage[__POST_ROBOT__.__GLOBAL_KEY__];

if (!parsedMessage || typeof parsedMessage !== 'object' || parsedMessage === null) {
return;
Expand Down
3 changes: 1 addition & 2 deletions src/drivers/send/index.js
Expand Up @@ -3,7 +3,6 @@
import { isWindowClosed, type CrossDomainWindowType, type DomainMatcher } from 'cross-domain-utils/src';
import { uniqueID } from 'belter/src';

import { __POST_ROBOT__ } from '../../conf';
import { serializeMessage } from '../../serialize';
import type { Message } from '../types';
import type { OnType, SendType } from '../../types';
Expand All @@ -16,7 +15,7 @@ export function sendMessage(win : CrossDomainWindowType, domain : DomainMatcher,
}

const serializedMessage = serializeMessage(win, domain, {
[ __POST_ROBOT__ ]: {
[ __POST_ROBOT__.__GLOBAL_KEY__ ]: {
id: uniqueID(),
...message
}
Expand Down
10 changes: 4 additions & 6 deletions src/global.js
Expand Up @@ -4,13 +4,11 @@ import { type CrossDomainWindowType, type SameDomainWindowType } from 'cross-dom
import { WeakMap } from 'cross-domain-safe-weakmap/src';
import { getOrSet } from 'belter/src';

import { __POST_ROBOT__ } from './conf';

export function getGlobal(win : SameDomainWindowType = window) : Object {
if (win !== window) {
return win[__POST_ROBOT__];
return win[__POST_ROBOT__.__GLOBAL_KEY__];
}
const global : Object = win[__POST_ROBOT__] = win[__POST_ROBOT__] || {};
const global : Object = win[__POST_ROBOT__.__GLOBAL_KEY__] = win[__POST_ROBOT__.__GLOBAL_KEY__] || {};
return global;
}

Expand All @@ -30,7 +28,7 @@ type GlobalStore<T> = {|
|};

// $FlowFixMe
export function globalStore<T : mixed>(key? : string = __POST_ROBOT__, defStore? : ObjectGetter = getObj) : GlobalStore<T> {
export function globalStore<T : mixed>(key? : string = 'store', defStore? : ObjectGetter = getObj) : GlobalStore<T> {
return getOrSet(getGlobal(), key, () => {
let store = defStore();

Expand Down Expand Up @@ -80,7 +78,7 @@ type WindowStore<T> = {|
|};

// $FlowFixMe
export function windowStore<T>(key? : string = __POST_ROBOT__, defStore? : ObjectGetter = getObj) : WindowStore<T> {
export function windowStore<T>(key? : string = 'store', defStore? : ObjectGetter = getObj) : WindowStore<T> {
return globalStore('windowStore').getOrSet(key, () => {
const winStore = new WeakMap();

Expand Down
30 changes: 23 additions & 7 deletions webpack.config.js
@@ -1,13 +1,20 @@
/* @flow */
/* eslint import/no-nodejs-modules: off, import/no-default-export: off */
/* eslint import/no-nodejs-modules: off, import/no-default-export: off, import/default: off */

import { getWebpackConfig } from 'grumbler-scripts/config/webpack.config';
import { getWebpackConfig, getNextVersion } from 'grumbler-scripts/config/webpack.config';
import { argv } from 'yargs';

import pkg from './package.json';
import globals from './globals';

export const FILE_NAME = 'post-robot';
export const MODULE_NAME = 'postRobot';

const postRobotGlobals = {
...globals.__POST_ROBOT__,
__GLOBAL_KEY__: `__post_robot_${ getNextVersion(pkg, argv.level) }__`
};

export const WEBPACK_CONFIG = getWebpackConfig({
filename: `${ FILE_NAME }.js`,
modulename: MODULE_NAME,
Expand All @@ -16,7 +23,7 @@ export const WEBPACK_CONFIG = getWebpackConfig({
...globals,

__POST_ROBOT__: {
...globals.__POST_ROBOT__,
...postRobotGlobals,
__IE_POPUP_SUPPORT__: false,
__GLOBAL_MESSAGE_SUPPORT__: false
}
Expand All @@ -31,7 +38,7 @@ export const WEBPACK_CONFIG_MIN = getWebpackConfig({
...globals,

__POST_ROBOT__: {
...globals.__POST_ROBOT__,
...postRobotGlobals,
__IE_POPUP_SUPPORT__: false,
__GLOBAL_MESSAGE_SUPPORT__: false
}
Expand All @@ -42,21 +49,30 @@ export const WEBPACK_CONFIG_IE = getWebpackConfig({
filename: `${ FILE_NAME }.ie.js`,
modulename: MODULE_NAME,
minify: false,
vars: globals
vars: {
...globals,
__POST_ROBOT__: postRobotGlobals
}
});

export const WEBPACK_CONFIG_IE_MIN = getWebpackConfig({
filename: `${ FILE_NAME }.ie.min.js`,
modulename: MODULE_NAME,
minify: true,
vars: globals
vars: {
...globals,
__POST_ROBOT__: postRobotGlobals
}
});

export const WEBPACK_CONFIG_TEST = getWebpackConfig({
modulename: MODULE_NAME,
minify: false,
test: true,
vars: globals
vars: {
...globals,
__POST_ROBOT__: postRobotGlobals
}
});

export default [ WEBPACK_CONFIG, WEBPACK_CONFIG_MIN, WEBPACK_CONFIG_IE, WEBPACK_CONFIG_IE_MIN ];

0 comments on commit 50f8f2b

Please sign in to comment.