@@ -44,21 +44,31 @@ function createEnvify(env) {
44
44
return visitProcessEnv
45
45
}
46
46
47
- function createGlobal ( content ) {
48
- var ast = esprima . parse ( content , {
49
- range : true ,
50
- tokens : true
51
- } ) ;
52
- var scopes = escope . analyze ( ast ) . scopes ;
53
- var gs = scopes . filter ( function ( scope ) {
54
- return scope . type == 'global' ;
55
- } ) [ 0 ] ;
56
-
47
+ function createGlobal ( ) {
57
48
function visitGlobal ( traverse , node , path , state ) {
58
- var varGlobal = findVar ( gs , node . name ) ;
49
+ var idx = 0 ;
50
+ while ( path [ idx ] ) {
51
+ if ( path [ idx ] . type === Syntax . FunctionExpression || path [ idx ] . type === Syntax . Program ) {
52
+ var scopes = escope . analyze ( path [ idx ] ) . scopes . filter ( function ( item ) {
53
+ return item . block === path [ idx ] ;
54
+ } ) ;
55
+ var resolved = false ;
56
+
57
+ scopes . every ( function ( scope ) {
58
+ if ( findVar ( scope , node . name ) ) {
59
+ resolved = true ;
60
+ return false ;
61
+ }
62
+
63
+ return true ;
64
+ } ) ;
59
65
60
- if ( varGlobal ) {
61
- return false ;
66
+ if ( resolved ) {
67
+ return false ;
68
+ }
69
+ }
70
+
71
+ idx ++ ;
62
72
}
63
73
64
74
utils . catchup ( node . range [ 0 ] , state )
@@ -77,23 +87,14 @@ function createGlobal(content) {
77
87
function findVar ( scope , name ) {
78
88
var refs = scope . variables ;
79
89
var i = 0 ;
80
- var ref , childScope ;
90
+ var ref ;
81
91
82
92
while ( ( ref = refs [ i ++ ] ) ) {
83
93
84
94
if ( ref . name === name ) {
85
95
return ref ;
86
96
}
87
97
}
88
-
89
- i = 0 ;
90
-
91
- while ( ( childScope = scope . childScopes [ i ++ ] ) ) {
92
-
93
- if ( ( ref = findVar ( childScope , name ) ) ) {
94
- return ref ;
95
- }
96
- }
97
98
}
98
99
99
100
return visitGlobal ;
@@ -103,9 +104,9 @@ function createGlobal(content) {
103
104
module . exports = function ( content ) {
104
105
var visitors = [ createEnvify ( {
105
106
NODE_ENV : 'production'
106
- } ) , createGlobal ( content ) ] ;
107
+ } ) , createGlobal ( ) ] ;
107
108
return jstransform . transform ( visitors , content ) . code
108
109
}
109
110
110
- // console.log(module.exports('var global2 = global.xxx; global.xxx = xxx;if (process.env.NODE_ENV === "production") {xx.global.xxx =1;}'));
111
+ // console.log(module.exports('(function(){ var global2 = global.xxx; global.xxx = xxx;if (process.env.NODE_ENV === "production") {xx.global.xxx =1;} })();(function() {var global = 1; global.xxx = 1;})(); '));
111
112
0 commit comments