Skip to content

Commit 4359fd1

Browse files
authored
feat: improve plugin options (#8)
* fix: improve plugin options * fix: allow controlling wether coop headers are added or not * docs: changeset minor
1 parent e8bf535 commit 4359fd1

5 files changed

Lines changed: 57 additions & 24 deletions

File tree

.changeset/itchy-moose-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@intility/vite-plugin-msal": minor
3+
---
4+
5+
allow `redirectBridgePath` to work with and without leading `/` and trailing `.html`

.changeset/loud-coats-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@intility/vite-plugin-msal": minor
3+
---
4+
5+
allow controlling wether the dev and preview server emits COOP headers

.changeset/tender-falcons-stop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@intility/vite-plugin-msal": minor
3+
---
4+
5+
only fetch authority metadata if it is defined

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ export default defineConfig({
2222
msal({
2323
// optional - defaults to `/redirect`
2424
redirectBridgePath: "/redirect",
25-
// optional - defaults to the common authority
26-
// used to fetch authority metadata for the client during build
25+
// optional - defaults to true
26+
addCoopHeader: true,
27+
// optional - defaults to undefined
28+
// if defined: fetches authority metadata for the client during build
2729
authority: "https://login.microsoftonline.com/common"
2830
}),
2931
]
3032
});
3133
```
3234

33-
Use the `withMetadata` function to enable the metadata resolution bypassing feature:
35+
Optional: Use the `withMetadata` function to enable the metadata resolution bypassing feature:
3436

3537
```ts
3638
import { withMetadata } from "@intility/vite-plugin-msal/client";

packages/vite-plugin-msal/src/index.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { addBuildInput } from "./utils.js";
44

55
type VitePluginMsalConfig = {
66
redirectBridgePath: string;
7-
authority: string;
7+
authority?: string;
8+
addCoopHeader: boolean;
89
};
910

1011
const defaultConfig: VitePluginMsalConfig = {
11-
redirectBridgePath: "/redirect",
12-
authority: "https://login.microsoftonline.com/common",
12+
redirectBridgePath: "redirect",
13+
addCoopHeader: true,
1314
};
1415

1516
async function fetchMsalMetadata(authority: string) {
@@ -71,9 +72,14 @@ function useCoopHeader(
7172
server: ViteDevServer | PreviewServer,
7273
config: VitePluginMsalConfig,
7374
) {
75+
if (!config.addCoopHeader) return;
76+
7477
server.middlewares.use((req, res, next) => {
7578
const pathname = req.originalUrl?.split("?")[0];
76-
if (pathname !== config.redirectBridgePath) {
79+
if (
80+
pathname !== config.redirectBridgePath &&
81+
pathname !== `${config.redirectBridgePath}.html`
82+
) {
7783
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
7884
}
7985
next();
@@ -82,14 +88,18 @@ function useCoopHeader(
8288

8389
export default function msal(config?: Partial<VitePluginMsalConfig>): Plugin {
8490
const mergedConfig = { ...defaultConfig, ...config };
91+
// Normalize: ensure leading / and strip trailing .html
92+
mergedConfig.redirectBridgePath = mergedConfig.redirectBridgePath
93+
.replace(/\.html$/, "")
94+
.replace(/^\/?/, "/");
8595
let resolvedId: string;
8696

8797
return {
8898
name: "vite-plugin-msal",
8999

90100
async config(userConfig) {
91101
const root = userConfig.root ?? process.cwd();
92-
const htmlFileName = `${mergedConfig.redirectBridgePath.replace(/^\//, "")}.html`;
102+
const htmlFileName = `${mergedConfig.redirectBridgePath.slice(1)}.html`;
93103
resolvedId = resolve(root, htmlFileName);
94104

95105
addBuildInput(
@@ -99,24 +109,26 @@ export default function msal(config?: Partial<VitePluginMsalConfig>): Plugin {
99109
resolve(root, "index.html"),
100110
);
101111

102-
const { cloudDiscoveryMetadata, authorityMetadata } =
103-
await fetchMsalMetadata(mergedConfig.authority);
112+
if (mergedConfig.authority) {
113+
const { cloudDiscoveryMetadata, authorityMetadata } =
114+
await fetchMsalMetadata(mergedConfig.authority);
104115

105-
const define: Record<string, string> = {};
106-
define.__VITE_PLUGIN_MSAL_METADATA_AUTHORITY__ = JSON.stringify(
107-
mergedConfig.authority,
108-
);
109-
if (cloudDiscoveryMetadata) {
110-
define.__VITE_PLUGIN_MSAL_CLOUD_DISCOVERY_METADATA__ = JSON.stringify(
111-
cloudDiscoveryMetadata,
116+
const define: Record<string, string> = {};
117+
define.__VITE_PLUGIN_MSAL_METADATA_AUTHORITY__ = JSON.stringify(
118+
mergedConfig.authority,
112119
);
113-
}
114-
if (authorityMetadata) {
115-
define.__VITE_PLUGIN_MSAL_AUTHORITY_METADATA__ =
116-
JSON.stringify(authorityMetadata);
117-
}
120+
if (cloudDiscoveryMetadata) {
121+
define.__VITE_PLUGIN_MSAL_CLOUD_DISCOVERY_METADATA__ = JSON.stringify(
122+
cloudDiscoveryMetadata,
123+
);
124+
}
125+
if (authorityMetadata) {
126+
define.__VITE_PLUGIN_MSAL_AUTHORITY_METADATA__ =
127+
JSON.stringify(authorityMetadata);
128+
}
118129

119-
return { define };
130+
return { define };
131+
}
120132
},
121133

122134
resolveId(id) {
@@ -154,7 +166,11 @@ export default function msal(config?: Partial<VitePluginMsalConfig>): Plugin {
154166

155167
server.middlewares.use((req, res, next) => {
156168
const pathname = req.originalUrl?.split("?")[0];
157-
if (!req.originalUrl || pathname !== mergedConfig.redirectBridgePath) {
169+
if (
170+
!req.originalUrl ||
171+
(pathname !== mergedConfig.redirectBridgePath &&
172+
pathname !== `${mergedConfig.redirectBridgePath}.html`)
173+
) {
158174
return next();
159175
}
160176

0 commit comments

Comments
 (0)