1
1
import { BuildContext } from '../util/interfaces' ;
2
2
import { getSystemData , toUnixPath } from '../util/helpers' ;
3
- import { Logger } from '../logger/logger' ;
4
- import * as MagicString from 'magic-string' ;
5
-
6
-
7
- export function prependIonicGlobal ( context : BuildContext , fileName : string , code : string ) {
8
- const rtn : { code : string , map : any } = {
9
- code : code ,
10
- map : undefined
11
- } ;
12
-
13
- try {
14
- const ionicGlobal = buildIonicGlobal ( context ) ;
15
-
16
- const s = new MagicString ( code ) ;
17
-
18
- s . prepend ( ionicGlobal ) ;
19
-
20
- rtn . code = s . toString ( ) ;
21
-
22
- rtn . map = s . generateMap ( {
23
- source : fileName ,
24
- file : fileName ,
25
- includeContent : true
26
- } ) ;
27
-
28
- } catch ( e ) {
29
- Logger . error ( `prependIonicGlobal: ${ e } ` ) ;
30
- }
31
-
32
- return rtn ;
33
- }
34
3
35
4
36
5
export function buildIonicGlobal ( context : BuildContext ) {
37
- if ( ( < any > context ) . windowIonic ) {
38
- // just a quick way to cache this to avoid unnecessary readFiles
39
- return ( < any > context ) . windowIonic ;
40
- }
6
+ context . ionicGlobal = context . ionicGlobal || { } ;
41
7
8
+ // gather data to add to window.Ionic
42
9
const systemData = getSystemData ( context . rootDir ) ;
10
+ if ( systemData . ionicFramework ) {
11
+ context . ionicGlobal [ 'version' ] = `'${ systemData . ionicFramework } '` ;
12
+ }
13
+ if ( systemData . angularCore ) {
14
+ context . ionicGlobal [ 'angular' ] = `'${ systemData . angularCore } '` ;
15
+ }
16
+ if ( systemData . ionicNative ) {
17
+ context . ionicGlobal [ 'ionicNative' ] = `'${ systemData . ionicNative } '` ;
18
+ }
43
19
44
20
let staticDir = toUnixPath ( context . buildDir . replace ( context . wwwDir , '' ) ) ;
45
21
staticDir += '/' ;
@@ -48,19 +24,19 @@ export function buildIonicGlobal(context: BuildContext) {
48
24
staticDir = staticDir . substring ( 1 ) ;
49
25
}
50
26
51
- let output = `
52
- (function(w){
53
- var i = w.Ionic = w.Ionic || {};
54
- ${ systemData . ionicFramework ? `i.version = '${ systemData . ionicFramework } ';` : '' }
55
- ${ systemData . angularCore ? `i.angular = '${ systemData . angularCore } ';` : '' }
56
- ${ systemData . ionicNative ? `i.ionicNative = '${ systemData . ionicNative } ';` : '' }
57
- i.staticDir = '${ staticDir } ';
58
- })(window);` ;
27
+ context . ionicGlobal [ 'staticDir' ] = `'${ staticDir } '` ;
28
+
29
+ // output the JS
30
+ let o = [
31
+ '(function(w){' ,
32
+ 'var i=w.Ionic=w.Ionic||{};'
33
+ ] ;
34
+
35
+ Object . keys ( context . ionicGlobal ) . forEach ( key => {
36
+ o . push ( `i.${ key } =${ context . ionicGlobal [ key ] } ;` ) ;
37
+ } ) ;
59
38
60
- // real quick minification hacks
61
- output = output . replace ( 'var i' , 'var_i' ) ;
62
- output = output . replace ( / \s / g, '' ) ;
63
- output = output . replace ( 'var_i' , 'var i' ) ;
39
+ o . push ( '})(window);' ) ;
64
40
65
- return ( < any > context ) . windowIonic = output ;
41
+ return o . join ( '' ) ;
66
42
}
0 commit comments