@@ -4,12 +4,13 @@ import { addBuildInput } from "./utils.js";
44
55type VitePluginMsalConfig = {
66 redirectBridgePath : string ;
7- authority : string ;
7+ authority ?: string ;
8+ addCoopHeader : boolean ;
89} ;
910
1011const defaultConfig : VitePluginMsalConfig = {
11- redirectBridgePath : "/ redirect" ,
12- authority : "https://login.microsoftonline.com/common" ,
12+ redirectBridgePath : "redirect" ,
13+ addCoopHeader : true ,
1314} ;
1415
1516async 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
8389export 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 ( / \. h t m l $ / , "" )
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