Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] improve rollup section of the bundling guide #32440

Merged
merged 5 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions documentation/Bundling.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,46 @@ Once this is done, you can use rollup by configuring your project in the way tha

In order to use Azure SDK libraries inside JS, you need to import code from the package you installed earlier. Create `src/index.js` with the following content:

```js
// src/index.js
import { SomeClient } from "@azure/some-sdk-package";
// Now do something interesting with the client
```

Next we need to configure Rollup to take the above code and turn it into a bundle. Save the following `rollup.config.mjs` file next to your `package.json` file you created earlier:

```js
// rollup.config.mjs
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
import { nodeResolve } from "@rollup/plugin-node-resolve";
export default {
input: "src/main.js",
output: {
file: "dist/bundle.js",
format: "esm",
name: "main",
},
plugins: [nodeResolve({ browser: true })],
};
```

We also need to install the plugins we referenced in the above file:

```sh
npm install --save-dev @rollup/plugin-node-resolve
```

This configuration should work for most of our SDK packages. However, if the package that you are using have runtime dependencies that are not available on browsers, you may need a more complex configuration. For example, bundling `@azure/storage-blob` usage

```js
// src/index.js
const { BlobServiceClient } = require("@azure/storage-blob");
// Now do something interesting with BlobServiceClient :)
```

Next we need to configure Rollup to take the above code and turn it into a bundle. Save the following `rollup.config.js` file next to your `package.json` file you created earlier:
requires the follow

```js
// rollup.config.js
// rollup.config.mjs
import resolve from "@rollup/plugin-node-resolve";
import cjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
Expand All @@ -198,7 +228,7 @@ export default {
input: "src/index.js",
output: {
file: "dist/bundle.js",
format: "iife",
format: "esm",
name: "main"
},
plugins: [
Expand Down Expand Up @@ -283,10 +313,10 @@ import { BlobServiceClient } from "@azure/storage-blob";
// Now do something interesting with BlobServiceClient :)
```

Next we need to configure Rollup to take the above code and turn it into a bundle. Save the following `rollup.config.js` file next to your `package.json` file you created earlier:
Next we need to configure Rollup to take the above code and turn it into a bundle. Save the following `rollup.config.mjs` file next to your `package.json` file you created earlier:

```js
// rollup.config.js
// rollup.config.mjs
import resolve from "@rollup/plugin-node-resolve";
import cjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
Expand All @@ -297,7 +327,7 @@ export default {
input: "src/index.ts",
output: {
file: "dist/bundle.js",
format: "iife",
format: "esm",
name: "main"
},
plugins: [
Expand Down
41 changes: 20 additions & 21 deletions samples/Bundling/parcel/js/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
const { BlobServiceClient } = require("@azure/storage-blob");
import { BlobServiceClient } from "@azure/storage-blob";

function getServiceSasUrl() {
return document.getElementById("serviceSasUrl").value;
return document.getElementById("serviceSasUrl").value;
}

function showContainerList(containers) {
const outputEl = document.getElementById("output");
// empty previous output
outputEl.textContent = "";
for (const container of containers) {
const containerDiv = document.createElement("div");
containerDiv.textContent = container.name;
outputEl.appendChild(containerDiv);
}
const outputEl = document.getElementById("output");
// empty previous output
outputEl.textContent = "";
for (const container of containers) {
const containerDiv = document.createElement("div");
containerDiv.textContent = container.name;
outputEl.appendChild(containerDiv);
}
}

async function listContainers() {
const blobServiceClient = new BlobServiceClient(getServiceSasUrl());
let iter = await blobServiceClient.listContainers();
const containers = [];
for await (const container of iter) {
containers.push(container);
}
showContainerList(containers);

const blobServiceClient = new BlobServiceClient(getServiceSasUrl());
let iter = await blobServiceClient.listContainers();
const containers = [];
for await (const container of iter) {
containers.push(container);
}
showContainerList(containers);
}

window.addEventListener("DOMContentLoaded", () => {
const listContainersButton = document.getElementById("listContainers");
listContainersButton.addEventListener("click", () => listContainers());
});
const listContainersButton = document.getElementById("listContainers");
listContainersButton.addEventListener("click", () => listContainers());
});
10 changes: 5 additions & 5 deletions samples/Bundling/parcel/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "storage-example",
"version": "1.0.0",
"description": "A small example of using the Azure Storage SDK",
"main": "index.js",
"source": "storage.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -13,8 +13,8 @@
"last 1 Edge version"
],
"devDependencies": {
"@azure/core-lro": "^1.0.0",
"@azure/storage-blob": "^12.0.0"
},
"dependencies": {}
"@azure/storage-blob": "^12.26.0",
"buffer": "^6.0.3",
"process": "^0.11.10"
}
}
2 changes: 1 addition & 1 deletion samples/Bundling/parcel/js/storage.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>Azure SDK Storage Example</title>
<script src="./index.js"></script>
<script type="module" src="index.js"></script>
</head>
<body>
<h1>Azure SDK Storage Example</h1>
Expand Down
12 changes: 5 additions & 7 deletions samples/Bundling/parcel/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"name": "storage-example",
"version": "1.0.0",
"description": "A small example of using the Azure Storage SDK",
"main": "index.js",
"source": "storage.html",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "MIT",
Expand All @@ -14,9 +13,8 @@
"last 1 Edge version"
],
"devDependencies": {
"@azure/core-lro": "^1.0.0",
"@azure/storage-blob": "^12.0.0",
"typescript": "^3.6.4"
},
"dependencies": {}
"@azure/storage-blob": "^12.26.0",
"buffer": "^6.0.3",
"process": "^0.11.10"
}
}
2 changes: 1 addition & 1 deletion samples/Bundling/parcel/ts/storage.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Azure SDK Storage Example</title>
<script src="./index.ts"></script>
<script type="module" src="./index.ts"></script>
v-jiaodi marked this conversation as resolved.
Show resolved Hide resolved
</head>
<body>
<h1>Azure SDK Storage Example</h1>
Expand Down
10 changes: 0 additions & 10 deletions samples/Bundling/parcel/ts/tsconfig.json

This file was deleted.

9 changes: 4 additions & 5 deletions samples/Bundling/rollup/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
},
"license": "MIT",
"devDependencies": {
"@azure/core-lro": "^2.0.0",
"@azure/storage-blob": "^12.7.0",
"@rollup/plugin-json": "^4.0.0",
"@rollup/plugin-commonjs": "^11.0.1",
"@rollup/plugin-node-resolve": "^7.0.0",
"@azure/storage-blob": "^12.26.0",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-commonjs": "^28.0.2",
"@rollup/plugin-node-resolve": "^16.0.0",
"rollup-plugin-shim": "^1.0.0"
},
"dependencies": {}
Expand Down
14 changes: 7 additions & 7 deletions samples/Bundling/rollup/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
},
"license": "MIT",
"devDependencies": {
"@azure/core-lro": "^2.0.0",
"@azure/storage-blob": "^12.7.0",
"@rollup/plugin-json": "^4.0.0",
"@rollup/plugin-commonjs": "^11.0.1",
"@rollup/plugin-node-resolve": "^7.0.0",
"@azure/async-iterator-polyfill": "^1.0.0",
v-jiaodi marked this conversation as resolved.
Show resolved Hide resolved
"@azure/storage-blob": "^12.26.0",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-commonjs": "^28.0.2",
"@rollup/plugin-node-resolve": "^16.0.0",
"rollup-plugin-shim": "^1.0.0",
"rollup-plugin-typescript2": "^0.24.3",
"typescript": "^3.6.4"
"rollup-plugin-typescript2": "^0.36.0",
"typescript": "^5.7.2"
},
"dependencies": {}
}
19 changes: 10 additions & 9 deletions samples/Bundling/rollup/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"strict": true,
"module": "es6",
"moduleResolution": "node",
"target": "es6"
}
}
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"strict": true,
"lib": ["ES2022", "DOM"],
"module": "ES2022",
"moduleResolution": "node",
"target": "ES2022"
}
}
3 changes: 1 addition & 2 deletions samples/Bundling/webpack/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
},
"license": "MIT",
"devDependencies": {
"@azure/core-lro": "^1.0.0",
"@azure/storage-blob": "^12.0.0"
"@azure/storage-blob": "^12.26.0"
},
"dependencies": {}
}
41 changes: 20 additions & 21 deletions samples/Bundling/webpack/js/src/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
const { BlobServiceClient } = require("@azure/storage-blob");
import { BlobServiceClient } from "@azure/storage-blob";

function getServiceSasUrl() {
return document.getElementById("serviceSasUrl").value;
return document.getElementById("serviceSasUrl").value;
}

function showContainerList(containers) {
const outputEl = document.getElementById("output");
// empty previous output
outputEl.textContent = "";
for (const container of containers) {
const containerDiv = document.createElement("div");
containerDiv.textContent = container.name;
outputEl.appendChild(containerDiv);
}
const outputEl = document.getElementById("output");
// empty previous output
outputEl.textContent = "";
for (const container of containers) {
const containerDiv = document.createElement("div");
containerDiv.textContent = container.name;
outputEl.appendChild(containerDiv);
}
}

async function listContainers() {
const blobServiceClient = new BlobServiceClient(getServiceSasUrl());
let iter = await blobServiceClient.listContainers();
const containers = [];
for await (const container of iter) {
containers.push(container);
}
showContainerList(containers);

const blobServiceClient = new BlobServiceClient(getServiceSasUrl());
let iter = await blobServiceClient.listContainers();
const containers = [];
for await (const container of iter) {
containers.push(container);
}
showContainerList(containers);
}

window.addEventListener("DOMContentLoaded", () => {
const listContainersButton = document.getElementById("listContainers");
listContainersButton.addEventListener("click", () => listContainers());
});
const listContainersButton = document.getElementById("listContainers");
listContainersButton.addEventListener("click", () => listContainers());
});
7 changes: 3 additions & 4 deletions samples/Bundling/webpack/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
},
"license": "MIT",
"devDependencies": {
"@azure/core-lro": "^1.0.0",
"@azure/storage-blob": "^12.0.0",
"ts-loader": "^6.2.1",
"typescript": "^3.6.4"
"@azure/storage-blob": "^12.26.0",
"ts-loader": "^9.5.1",
"typescript": "^5.7.2"
},
"dependencies": {}
}
18 changes: 9 additions & 9 deletions samples/Bundling/webpack/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"strict": true,
"module": "es6",
"moduleResolution": "node",
"target": "es6"
}
}
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"strict": true,
"module": "ES2022",
"moduleResolution": "node",
"target": "ES2022"
}
}
Loading