From 324b388b5088ea95ef0772380e717f4473101b06 Mon Sep 17 00:00:00 2001
From: ramya0820 <45977823+ramya0820@users.noreply.github.com>
Date: Tue, 28 May 2019 13:43:13 -0700
Subject: [PATCH] Enable rollup for browser with sample (#3206)
* Enable rollup for browser with sample
---
sdk/eventhub/event-hubs/package.json | 2 +
sdk/eventhub/event-hubs/rollup.base.config.js | 39 +++++++++++++++++--
sdk/eventhub/event-hubs/rollup.config.js | 7 ++--
.../event-hubs/samples/browserSample.html | 37 ++++++++++++++++++
.../event-hubs/src/connectionContext.ts | 2 +-
5 files changed, 78 insertions(+), 9 deletions(-)
create mode 100644 sdk/eventhub/event-hubs/samples/browserSample.html
diff --git a/sdk/eventhub/event-hubs/package.json b/sdk/eventhub/event-hubs/package.json
index 57621e5df602..a94f165fff88 100644
--- a/sdk/eventhub/event-hubs/package.json
+++ b/sdk/eventhub/event-hubs/package.json
@@ -97,10 +97,12 @@
"rimraf": "^2.6.2",
"rollup": "^1.0.0",
"rollup-plugin-commonjs": "^9.2.0",
+ "rollup-plugin-inject": "^2.2.0",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-multi-entry": "^2.1.0",
"rollup-plugin-node-resolve": "^4.2.0",
"rollup-plugin-replace": "^2.1.0",
+ "rollup-plugin-shim": "^1.0.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-uglify": "^6.0.0",
"ts-node": "^7.0.1",
diff --git a/sdk/eventhub/event-hubs/rollup.base.config.js b/sdk/eventhub/event-hubs/rollup.base.config.js
index 46c5daa7f272..286500870f33 100644
--- a/sdk/eventhub/event-hubs/rollup.base.config.js
+++ b/sdk/eventhub/event-hubs/rollup.base.config.js
@@ -8,6 +8,8 @@ import json from "rollup-plugin-json";
import replace from "rollup-plugin-replace";
import { uglify } from "rollup-plugin-uglify";
import sourcemaps from "rollup-plugin-sourcemaps";
+import shim from "rollup-plugin-shim";
+import inject from "rollup-plugin-inject";
import path from "path";
@@ -17,7 +19,7 @@ const input = "dist-esm/src/index.js";
const production = process.env.NODE_ENV === "production";
export function nodeConfig(test = false) {
- const externalNodeBuiltins = ["events", "util"];
+ const externalNodeBuiltins = ["events", "util", "os"];
const baseConfig = {
input: input,
external: depNames.concat(externalNodeBuiltins),
@@ -75,9 +77,9 @@ export function browserConfig(test = false) {
input: input,
external: ["ms-rest-js"],
output: {
- file: "browser/index.js",
+ file: "browser/event-hubs.js",
format: "umd",
- name: "ExampleClient",
+ name: "Azure.Messaging.EventHubs",
sourcemap: true,
globals: { "ms-rest-js": "msRest" }
},
@@ -96,13 +98,42 @@ export function browserConfig(test = false) {
}
}
),
+
+ // fs, net, and tls are used by rhea and need to be shimmed
+ // dotenv doesn't work in the browser, so replace it with a no-op function
+ shim({
+ fs: `export default {}`,
+ net: `export default {}`,
+ tls: `export default {}`,
+ dotenv: `export function config() { }`,
+ os: `
+ export function arch() { return "javascript" }
+ export function type() { return "Browser" }
+ export function release() { typeof navigator === 'undefined' ? '' : navigator.appVersion }
+ `,
+ path: `export default {}`,
+ dns: `export function resolve() { }`
+ }),
+
nodeResolve({
- mainFields: ['module', 'browser'],
+ mainFields: ["module", "browser"],
preferBuiltins: false
}),
+
cjs({
namedExports: { events: ["EventEmitter"] }
}),
+
+ // rhea and rhea-promise use the Buffer global which requires
+ // injection to shim properly
+ inject({
+ modules: {
+ Buffer: ["buffer", "Buffer"],
+ process: "process"
+ },
+ exclude: ["./**/package.json"]
+ }),
+
json()
]
};
diff --git a/sdk/eventhub/event-hubs/rollup.config.js b/sdk/eventhub/event-hubs/rollup.config.js
index a62dabd573b4..01f209a32fa8 100644
--- a/sdk/eventhub/event-hubs/rollup.config.js
+++ b/sdk/eventhub/event-hubs/rollup.config.js
@@ -9,9 +9,8 @@ if (!process.env.ONLY_BROWSER) {
inputs.push(base.nodeConfig());
}
-// Disable this until we are ready to run rollup for the browser.
-// if (!process.env.ONLY_NODE) {
-// inputs.push(base.browserConfig());
-// }
+if (!process.env.ONLY_NODE) {
+ inputs.push(base.browserConfig());
+}
export default inputs;
diff --git a/sdk/eventhub/event-hubs/samples/browserSample.html b/sdk/eventhub/event-hubs/samples/browserSample.html
new file mode 100644
index 000000000000..ddc360fa7faf
--- /dev/null
+++ b/sdk/eventhub/event-hubs/samples/browserSample.html
@@ -0,0 +1,37 @@
+
+
+
+
+
+
diff --git a/sdk/eventhub/event-hubs/src/connectionContext.ts b/sdk/eventhub/event-hubs/src/connectionContext.ts
index 3d3c82230cbc..141fd9893205 100644
--- a/sdk/eventhub/event-hubs/src/connectionContext.ts
+++ b/sdk/eventhub/event-hubs/src/connectionContext.ts
@@ -2,7 +2,7 @@
// Licensed under the MIT License.
import * as log from "./log";
-import os from "os";
+import * as os from "os";
import { packageJsonInfo } from "./util/constants";
import { EventHubReceiver } from "./eventHubReceiver";
import { EventHubSender } from "./eventHubSender";