1- import { reactive , watch , computed } from ' vue'
1+ import { reactive , watch , computed } from " vue" ;
22
3- const globalKeys = Object . getOwnPropertyNames ( globalThis )
4- . filter ( key => key [ 0 ] . toLocaleLowerCase ( ) === key [ 0 ] && key !== 'console' && key !== 'module' )
3+ const globalKeys = Object . getOwnPropertyNames ( globalThis ) . filter (
4+ ( key ) =>
5+ key [ 0 ] . toLocaleLowerCase ( ) === key [ 0 ] &&
6+ key !== "console" &&
7+ key !== "module"
8+ ) ;
59
6- export function evaluateSource ( src ) {
10+ export function evaluateSource ( src ) {
711 // Basic commonjs transform
812 src = src
9- . replace ( ' export default' , ' _module._exports = ' )
10- . replace ( / e x p o r t ( c o n s t | l e t | v a r ) ( \w + ) ? = ? / g, ' _exports.$2 = ' )
11- . replace ( / e x p o r t ( f u n c t i o n | c l a s s ) ( \w + ) / g, ' _exports.$2 = $1 $2 ' )
13+ . replace ( " export default" , " _module._exports = " )
14+ . replace ( / e x p o r t ( c o n s t | l e t | v a r ) ( \w + ) ? = ? / g, " _exports.$2 = " )
15+ . replace ( / e x p o r t ( f u n c t i o n | c l a s s ) ( \w + ) / g, " _exports.$2 = $1 $2 " ) ;
1216
1317 // eslint-disable-next-line no-new-func
1418 const fn = Function ( `
@@ -17,69 +21,71 @@ export function evaluateSource (src) {
1721 const sandbox = {
1822 _module,
1923 _exports,
20- ${ globalKeys . map ( key => `"${ key } ": {}` ) . join ( ',' ) }
24+ ${ globalKeys . map ( ( key ) => `"${ key } ": {}` ) . join ( "," ) }
2125 }
2226 with (sandbox) {
2327 ${ src } ;
2428 };
2529 return sandbox._module._exports;
26- ` )
30+ ` ) ;
2731
28- return fn . call ( { } )
32+ return fn . call ( { } ) ;
2933}
3034
31- export function tryFn ( fn ) {
35+ export function tryFn ( fn ) {
3236 try {
33- return fn ( )
37+ return fn ( ) ;
3438 } catch ( err ) {
3539 // eslint-disable-next-line no-console
36- console . error ( err )
37- return null
40+ console . error ( err ) ;
41+ return null ;
3842 }
3943}
4044
41- export function persistedState ( initialState = { } ) {
45+ export function persistedState ( initialState = { } ) {
4246 const state = reactive ( {
4347 ...initialState ,
44- ...tryFn ( ( ) => JSON . parse ( atob ( window . location . hash . substr ( 1 ) ) || '{}' ) )
45- } )
48+ ...tryFn ( ( ) => JSON . parse ( atob ( window . location . hash . substr ( 1 ) ) || "{}" ) ) ,
49+ } ) ;
4650 watch ( state , ( ) => {
47- window . location . hash = '#' + btoa ( JSON . stringify ( state ) )
48- } )
49- return state
51+ window . location . hash = "#" + btoa ( JSON . stringify ( state ) ) ;
52+ } ) ;
53+ return state ;
5054}
5155
52- export function safeComputed ( fn ) {
53- let lastState = null
56+ export function safeComputed ( fn ) {
57+ let lastState = null ;
5458 return computed ( ( ) => {
5559 try {
56- lastState = fn ( )
57- return lastState
60+ lastState = fn ( ) ;
61+ return lastState ;
5862 } catch ( err ) {
5963 // eslint-disable-next-line no-console
60- console . error ( err )
61- return lastState
64+ console . error ( err ) ;
65+ return lastState ;
6266 }
63- } )
67+ } ) ;
6468}
6569
66- export function asyncComputed ( fn ) {
67- const state = ref ( undefined )
68- const computedPromise = safeComputed ( fn )
69- watch ( computedPromise , val => {
70- val . then ( r => state . value = r )
71- } )
72- return state
70+ export function asyncComputed ( fn ) {
71+ const state = ref ( undefined ) ;
72+ const computedPromise = safeComputed ( fn ) ;
73+ watch ( computedPromise , ( val ) => {
74+ val . then ( ( r ) => ( state . value = r ) ) ;
75+ } ) ;
76+ return state ;
7377}
7478
75- export function asyncImport ( { loader, loading, error } ) {
76- const m = reactive ( loading || { } )
77- loader ( ) . then ( res => Object . assign ( m , res ) ) . catch ( ( err ) => {
78- if ( error ) {
79- Object . assign ( m , error ( err ) )
80- }
81- // eslint-disable-next-line no-console
82- console . error ( err )
83- } )
84- return m
79+ export function asyncImport ( { loader, loading, error } ) {
80+ const m = reactive ( loading || { } ) ;
81+ loader ( )
82+ . then ( ( res ) => Object . assign ( m , res ) )
83+ . catch ( ( err ) => {
84+ if ( error ) {
85+ Object . assign ( m , error ( err ) ) ;
86+ }
87+ // eslint-disable-next-line no-console
88+ console . error ( err ) ;
89+ } ) ;
90+ return m ;
8591}
0 commit comments