22import fs from 'node:fs/promises'
33
44/**
5+ * Plugin to upload the built blueprint to a server after bundling
6+ * This also writes out a json file of the assets, which is needed for the bundling process
57 * @returns {import('rollup').OutputPlugin }
68 */
79export const uploadPlugin = ( server , bundleId , development ) => {
@@ -20,23 +22,25 @@ export const uploadPlugin = (server, bundleId, development) => {
2022 } else if ( item . type === 'chunk' ) {
2123 if ( ! item . isEntry ) throw new Error ( `Expected a single code chunk: ${ id } ` )
2224
23- try {
24- const res = await fetch (
25- server + '/api/private/blueprints/restore/' + bundleId + ( development ? '?developmentMode=1' : '' ) ,
26- {
27- method : 'POST' ,
28- body : item . code ,
29- headers : {
30- 'Content-Type' : 'text/javascript' ,
31- } ,
25+ if ( server ) {
26+ try {
27+ const res = await fetch (
28+ server + '/api/private/blueprints/restore/' + bundleId + ( development ? '?developmentMode=1' : '' ) ,
29+ {
30+ method : 'POST' ,
31+ body : item . code ,
32+ headers : {
33+ 'Content-Type' : 'text/javascript' ,
34+ } ,
35+ }
36+ )
37+ if ( ! res . ok ) {
38+ throw new Error ( `HTTP ${ res . status } : ${ await res . text ( ) } ` )
3239 }
33- )
34- if ( ! res . ok ) {
35- throw new Error ( `HTTP ${ res . status } : ${ await res . text ( ) } ` )
40+ console . log ( `Blueprints ' ${ id } ' uploaded` )
41+ } catch ( e ) {
42+ console . error ( `Blueprints ' ${ id } ' upload failed:` , e . toString ( ) , e . stack )
3643 }
37- console . log ( `Blueprints '${ id } ' uploaded` )
38- } catch ( e ) {
39- console . error ( `Blueprints '${ id } ' upload failed:` , e . toString ( ) , e . stack )
4044 }
4145 } else {
4246 // @ts -expect-error
@@ -48,20 +52,22 @@ export const uploadPlugin = (server, bundleId, development) => {
4852 fs . writeFile ( `dist/${ bundleId } -assets.json` , JSON . stringify ( assetsBundle ) ) . catch ( ( e ) => {
4953 console . error ( `Failed to write assets bundle to disk:` , e . toString ( ) , e . stack )
5054 } )
51- try {
52- const res = await fetch ( server + '/api/private/blueprints/assets' , {
53- method : 'POST' ,
54- body : JSON . stringify ( assetsBundle ) ,
55- headers : {
56- 'Content-Type' : 'application/json' ,
57- } ,
58- } )
59- if ( ! res . ok ) {
60- throw new Error ( `HTTP ${ res . status } : ${ await res . text ( ) } ` )
55+ if ( server ) {
56+ try {
57+ const res = await fetch ( server + '/api/private/blueprints/assets' , {
58+ method : 'POST' ,
59+ body : JSON . stringify ( assetsBundle ) ,
60+ headers : {
61+ 'Content-Type' : 'application/json' ,
62+ } ,
63+ } )
64+ if ( ! res . ok ) {
65+ throw new Error ( `HTTP ${ res . status } : ${ await res . text ( ) } ` )
66+ }
67+ console . log ( `Blueprints assets uploaded` )
68+ } catch ( e ) {
69+ console . error ( `Blueprints assets upload failed:` , e . toString ( ) , e . stack )
6170 }
62- console . log ( `Blueprints assets uploaded` )
63- } catch ( e ) {
64- console . error ( `Blueprints assets upload failed:` , e . toString ( ) , e . stack )
6571 }
6672 } else {
6773 fs . rm ( `dist/${ bundleId } -assets.json` ) . catch ( ( ) => {
0 commit comments