@@ -5,6 +5,7 @@ import { EditorElement } from './editor';
55import { CLIENTID , SETPROVIDER_EVENT , AUTH_PAGE } from '../../env' ;
66
77const mgtScriptName = './mgt.storybook.js' ;
8+ const mgtPreviewScriptName = './mgt.preview.storybook.js' ;
89
910// function is used for dragging and moving
1011const setupEditorResize = ( first , separator , last , dragComplete ) => {
@@ -185,21 +186,27 @@ export const withCodeEditor = makeDecorator({
185186 </header>
186187` ;
187188
189+ const resolveScript = ( ) => {
190+ const usingPreview =
191+ editor . files . js . indexOf ( `import '@microsoft/mgt-components/dist/es6/components/preview'` ) > - 1 ;
192+ return usingPreview ? mgtPreviewScriptName : mgtScriptName ;
193+ } ;
194+
188195 let providerInitCode = `
189- import {Providers, MockProvider} from "${ mgtScriptName } ";
196+ import {Providers, MockProvider} from "${ resolveScript ( ) } ";
190197 Providers.globalProvider = new MockProvider(true);
191198 ` ;
192199
193200 const channel = addons . getChannel ( ) ;
194201 channel . on ( SETPROVIDER_EVENT , params => {
195202 if ( params . state === ProviderState . SignedIn && params . name === 'MgtMockProvider' ) {
196203 providerInitCode = `
197- import { Providers, MockProvider } from "${ mgtScriptName } ";
204+ import { Providers, MockProvider } from "${ resolveScript ( ) } ";
198205 Providers.globalProvider = new MockProvider(true);
199206 ` ;
200207 } else if ( params . state === ProviderState . SignedIn && params . name === 'MgtMsal2Provider' ) {
201208 providerInitCode = `
202- import { Providers, Msal2Provider, LoginType } from "${ mgtScriptName } ";
209+ import { Providers, Msal2Provider, LoginType } from "${ resolveScript ( ) } ";
203210 Providers.globalProvider = new Msal2Provider({
204211 clientId: "${ CLIENTID } ",
205212 loginType: LoginType.Popup,
@@ -219,15 +226,17 @@ export const withCodeEditor = makeDecorator({
219226 let doc = storyElement . contentDocument ;
220227
221228 let { html, css, js } = editor . files ;
229+ // strip the preview import, we include it in the resolved script
230+ js = js . replace ( / i m p o r t ' @ m i c r o s o f t \/ m g t - c o m p o n e n t s \/ d i s t \/ e s 6 \/ c o m p o n e n t s \/ p r e v i e w ' / gm, `` ) ;
222231 js = js . replace (
223232 / i m p o r t \{ ( [ ^ \} ] + ) \} \s + f r o m \s + [ ' " ] @ m i c r o s o f t \/ m g t [ ' " ] ; / gm,
224- `import {$1} from '${ mgtScriptName } ';`
233+ `import {$1} from '${ resolveScript ( ) } ';`
225234 ) ;
226235
227236 const docContent = `
228237 <html>
229238 <head>
230- <script type="module" src="${ mgtScriptName } "></script>
239+ <script type="module" src="${ resolveScript ( ) } "></script>
231240 <script type="module">
232241 ${ providerInitCode }
233242 </script>
0 commit comments