|
| 1 | +##### This design document focuses on the following - |
| 2 | +1. Separate entry points for node and browser. |
| 3 | +2. Specifying the browser field in package.json. |
| 4 | +3. Changes in the bundling process. |
| 5 | + |
| 6 | +##### Terms - |
| 7 | + |
| 8 | +* bundler - Module bundlers are tools frontend developers used to bundle JavaScript modules into a single JavaScript files that can be executed in the browser. |
| 9 | + |
| 10 | +* rollup - rollup.js is the module bundler that the JS SDK uses. |
| 11 | + |
| 12 | +* package.json fields - |
| 13 | + * main - The main field is a module ID that is the primary entry point to your program. Points to the CJS modules. |
| 14 | + |
| 15 | + * module - The module field is not an official npm feature but a common convention among bundlers to designate how to import an ESM version of a library. Points to the ES modules. |
| 16 | + |
| 17 | + * browser - If the module is meant to be used client-side, the browser field should be used instead of the main field. |
| 18 | + |
| 19 | +##### Current set up - |
| 20 | + |
| 21 | +1. |
| 22 | + TypeScript Source Code |
| 23 | + / \ |
| 24 | + Transpiles into JavaScript |
| 25 | + 'lib' folder |
| 26 | + / \ |
| 27 | + CJS module ES modules |
| 28 | +2. main - `lib/src/index.js` |
| 29 | + module - `lib/es/src/index.js` |
| 30 | + |
| 31 | +3. Rollup bundling output |
| 32 | +* `graph-js-sdk.js` - Bundled and minified file in IIFE format. This file can be directly used in the browser with a `<script>` tag. |
| 33 | +* `graph-es-sdk.js` - Bundled file in ES format. |
| 34 | +4. Entry point for rollup - `lib/es/src/browser/index.js`. |
| 35 | + |
| 36 | +##### Difference between src/index.js and src/browser/index.js |
| 37 | +1. `src/browser/index.js` does not export `RedirectHandler` and `RedirectHandlerOptions`. Redirection is handled by the browser. |
| 38 | +2. `src/browser/index.js` exports `src/browser/ImplicitMsalProvider`. |
| 39 | +3. `src/browser/ImplicitMsalProvider` does not import or require 'msal' dependency. While, |
| 40 | +`src/ImplicitMsalProvider` imports or requires 'msal' in the implementation. |
| 41 | +4. My assumtion is that `src/browser/ImplicitMsalProvider` is implemented specifically for the rollup process and to skip the rollup external dependency while using `graph-js-sdk.js` in the browser. |
| 42 | + |
| 43 | +Note - Browser applications using the ES modules from the npm package of the JS SDK refer to the `module` entry point - `lib/es/src/index.js`(not the browser entry point). Example - Graph Explorer. |
| 44 | + |
| 45 | +##### Upcoming changes - |
| 46 | + |
| 47 | +1. Use the browser field for the following - |
| 48 | + |
| 49 | +* The Graph JS SDK currently has two entry files, `src/index` and `src/browser/index`. |
| 50 | +Use the browser field to indicate the browser entry point. |
| 51 | +Example - |
| 52 | +```json |
| 53 | +"browser": |
| 54 | +{ "lib/es/src/index.js": "lib/es/src/browser/index.js" } |
| 55 | +``` |
| 56 | +Currently, the main and "module field in the package.json. This will remain the same. |
| 57 | +* Better way to handle environment specific implementation. For example, using the `browser` field as follows - |
| 58 | +"browser": |
| 59 | +```json |
| 60 | +{ |
| 61 | + "stream": "stream-browserify", |
| 62 | + "Feature-Node.js": "Feature-Browser.js" |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +2. Remove export of `src/browser/ImplicitMsalProvider` from `src/browser/index`. |
| 67 | +* Till `ImplicitMsalProvider` is maintained in the repo, maintain a separate entry point say `rollup-index` for the rollup process which exports `src/browser/index` and `src/browser/ImplicitMsalProvider`. |
| 68 | +* Continue rolling up the `src/browser/ImplicitMsalProvider` as it is currently done and not introduce breaking changes here as it is going to be deprecated. |
| 69 | +* Remove the separate entry point once `ImplicitMsalProvider` is removed and use the browser entry point for roll up thereafter. The goal is to maintain a consistent entry point or usage for browser applications using the JS SDK and the rollup/bundling process. |
| 70 | + |
| 71 | +3. Bundle the authproviders separately as they are optional. |
| 72 | + |
| 73 | +4. Stop bundling in ES format, that is remove `graph-es-sdk.js` as the ES modules are being shipped. |
0 commit comments