From f3bd9c4dd3a711c934cff305d6cb644dad77c50b Mon Sep 17 00:00:00 2001 From: Giancarlo Anemone Date: Mon, 29 Jan 2018 12:28:56 -0800 Subject: [PATCH] Fix tree shaking (#57) https://github.com/fusionjs/fusion-plugin-universal-events/pull/57 --- package.json | 6 +++--- src/browser.js | 14 +++++++------ src/server.js | 55 +++++++++++++++++++++++++------------------------- yarn.lock | 26 ++++++++++++------------ 4 files changed, 52 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 44ff430..4584ac9 100644 --- a/package.json +++ b/package.json @@ -27,17 +27,17 @@ }, "devDependencies": { "babel-eslint": "8.2.1", - "create-universal-package": "3.2.6", + "create-universal-package": "3.3.0", "eslint": "4.16.0", "eslint-config-fusion": "0.2.1", "eslint-plugin-cup": "1.0.0", "eslint-plugin-flowtype": "2.42.0", - "eslint-plugin-import": "2.8.0", + "eslint-plugin-import": "^2.8.0", "eslint-plugin-prettier": "2.5.0", "eslint-plugin-react": "7.6.0", "flow-bin": "0.64.0", "fusion-core": "0.3.0-4", - "fusion-test-utils": "0.4.1", + "fusion-test-utils": "0.4.2", "fusion-tokens": "0.0.5", "nyc": "11.4.1", "prettier": "1.10.2", diff --git a/src/browser.js b/src/browser.js index 5a6a556..621553d 100644 --- a/src/browser.js +++ b/src/browser.js @@ -52,11 +52,13 @@ class UniversalEmitter extends Emitter { } } -const plugin = createPlugin({ - deps: {fetch: FetchToken}, - provides: ({fetch}) => { - return new UniversalEmitter(fetch); - }, -}); +const plugin = + __BROWSER__ && + createPlugin({ + deps: {fetch: FetchToken}, + provides: ({fetch}) => { + return new UniversalEmitter(fetch); + }, + }); export default plugin; diff --git a/src/server.js b/src/server.js index c55f2c6..6b29528 100644 --- a/src/server.js +++ b/src/server.js @@ -58,32 +58,33 @@ class ScopedEmitter extends Emitter { teardown() {} } -export default createPlugin({ - provides: () => new GlobalEmitter(), - middleware: (deps, globalEmitter) => { - const bodyParser = require('koa-bodyparser'); - const parseBody = bodyParser(); - return async function universalEventsMiddleware(ctx, next) { - const emitter = globalEmitter.from(ctx); - if (ctx.method === 'POST' && ctx.path === '/_events') { - await parseBody(ctx, async () => {}); - const {items} = ctx.request.body; - if (items) { - for (let index = 0; index < items.length; index++) { - const {type, payload} = items[index]; - emitter.emit(type, payload); +export default __NODE__ && + createPlugin({ + provides: () => new GlobalEmitter(), + middleware: (deps, globalEmitter) => { + const bodyParser = require('koa-bodyparser'); + const parseBody = bodyParser(); + return async function universalEventsMiddleware(ctx, next) { + const emitter = globalEmitter.from(ctx); + if (ctx.method === 'POST' && ctx.path === '/_events') { + await parseBody(ctx, async () => {}); + const {items} = ctx.request.body; + if (items) { + for (let index = 0; index < items.length; index++) { + const {type, payload} = items[index]; + emitter.emit(type, payload); + } + ctx.status = 200; + } else { + ctx.status = 400; } - ctx.status = 200; - } else { - ctx.status = 400; } - } - // awaiting next before registering `then` on ctx.timing.end to try and get as much as possible - // into the event batch flush. - await next(); - ctx.timing.end.then(() => { - emitter.flush(); - }); - }; - }, -}); + // awaiting next before registering `then` on ctx.timing.end to try and get as much as possible + // into the event batch flush. + await next(); + ctx.timing.end.then(() => { + emitter.flush(); + }); + }; + }, + }); diff --git a/yarn.lock b/yarn.lock index 5edb909..95e01fd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -667,8 +667,8 @@ resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-0.3.29.tgz#7f2ad7ec55f914482fc9b1ec4bb1ae6028d46066" "@types/node@^9.3.0": - version "9.3.0" - resolved "https://registry.yarnpkg.com/@types/node/-/node-9.3.0.tgz#3a129cda7c4e5df2409702626892cb4b96546dd5" + version "9.4.0" + resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.0.tgz#b85a0bcf1e1cc84eb4901b7e96966aedc6f078d1" "@types/rimraf@^0.0.28": version "0.0.28" @@ -1456,9 +1456,9 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4: safe-buffer "^5.0.1" sha.js "^2.4.8" -create-universal-package@3.2.6: - version "3.2.6" - resolved "https://registry.yarnpkg.com/create-universal-package/-/create-universal-package-3.2.6.tgz#8269a7ac8ad5d1e13139a36f18d139fb48000483" +create-universal-package@3.3.0: + version "3.3.0" + resolved "https://registry.yarnpkg.com/create-universal-package/-/create-universal-package-3.3.0.tgz#69c31d117705f72b8c418c1140e4cf051160ef9d" dependencies: "@babel/core" "^7.0.0-beta.36" "@babel/plugin-transform-flow-strip-types" "^7.0.0-beta.36" @@ -1654,8 +1654,8 @@ doctrine@^2.0.2, doctrine@^2.1.0: esutils "^2.0.2" domain-browser@^1.1.1: - version "1.1.7" - resolved "https://registry.yarnpkg.com/domain-browser/-/domain-browser-1.1.7.tgz#867aa4b093faa05f1de08c06f4d7b21fdf8698bc" + version "1.2.0" + resolved "https://registry.yarnpkg.com/domain-browser/-/domain-browser-1.2.0.tgz#3d31f50191a6749dd1375a7f522e823d42e54eda" draftlog@^1.0.12: version "1.0.12" @@ -1870,7 +1870,7 @@ eslint-plugin-flowtype@2.42.0: dependencies: lodash "^4.15.0" -eslint-plugin-import@2.8.0: +eslint-plugin-import@^2.8.0: version "2.8.0" resolved "https://registry.yarnpkg.com/eslint-plugin-import/-/eslint-plugin-import-2.8.0.tgz#fa1b6ef31fcb3c501c09859c1b86f1fc5b986894" dependencies: @@ -2258,9 +2258,9 @@ fusion-core@0.3.0-4: node-mocks-http "^1.6.6" toposort "^1.0.6" -fusion-test-utils@0.4.1: - version "0.4.1" - resolved "https://registry.yarnpkg.com/fusion-test-utils/-/fusion-test-utils-0.4.1.tgz#c834945eb0bdecc6f56993c4daaf4f4e13416029" +fusion-test-utils@0.4.2: + version "0.4.2" + resolved "https://registry.yarnpkg.com/fusion-test-utils/-/fusion-test-utils-0.4.2.tgz#a8cc45eea3c6943cb41ba4b61f699adb126ef531" dependencies: assert "^1.4.1" koa "^2.4.1" @@ -3343,8 +3343,8 @@ node-pre-gyp@^0.6.39: tar-pack "^3.4.0" nodent-compiler@>=3.1.0: - version "3.1.6" - resolved "https://registry.yarnpkg.com/nodent-compiler/-/nodent-compiler-3.1.6.tgz#1fd2b37625f127405d90ddc9cf7a059affef82f5" + version "3.1.7" + resolved "https://registry.yarnpkg.com/nodent-compiler/-/nodent-compiler-3.1.7.tgz#78198d9baacc491b0065b8c65bc0ca83d3fcd2a2" dependencies: acorn ">=2.5.2" acorn-es7-plugin ">=1.1.6"