@@ -15,6 +15,32 @@ const quotemeta = str => {
15
15
return str . replace ( / [ - [ \] \\ / { } ( ) * + ? . ^ $ | ] / g, "\\$&" ) ;
16
16
} ;
17
17
18
+ const splitContextFromPrefix = prefix => {
19
+ const idx = prefix . lastIndexOf ( "/" ) ;
20
+ let context = "." ;
21
+ if ( idx >= 0 ) {
22
+ context = prefix . substr ( 0 , idx ) ;
23
+ prefix = `.${ prefix . substr ( idx ) } ` ;
24
+ }
25
+ return {
26
+ context,
27
+ prefix
28
+ } ;
29
+ } ;
30
+
31
+ const splitQueryFromPostfix = postfix => {
32
+ const idx = postfix . indexOf ( "?" ) ;
33
+ let query = "" ;
34
+ if ( idx >= 0 ) {
35
+ query = postfix . substr ( idx ) ;
36
+ postfix = postfix . substr ( 0 , idx ) ;
37
+ }
38
+ return {
39
+ postfix,
40
+ query
41
+ } ;
42
+ } ;
43
+
18
44
ContextDependencyHelpers . create = (
19
45
Dep ,
20
46
range ,
@@ -23,38 +49,30 @@ ContextDependencyHelpers.create = (
23
49
options ,
24
50
contextOptions
25
51
) => {
26
- let dep ;
27
- let prefix ;
28
- let postfix ;
29
- let prefixRange ;
30
- let valueRange ;
31
- let idx ;
32
- let context ;
33
- let regExp ;
34
52
if ( param . isTemplateString ( ) ) {
35
- prefix = param . quasis [ 0 ] . string ;
36
- postfix =
53
+ let prefixRaw = param . quasis [ 0 ] . string ;
54
+ let postfixRaw =
37
55
param . quasis . length > 1
38
56
? param . quasis [ param . quasis . length - 1 ] . string
39
57
: "" ;
40
- prefixRange = [ param . quasis [ 0 ] . range [ 0 ] , param . quasis [ 0 ] . range [ 1 ] ] ;
41
- valueRange = param . range ;
42
- idx = prefix . lastIndexOf ( "/" ) ;
43
- context = "." ;
44
- if ( idx >= 0 ) {
45
- context = prefix . substr ( 0 , idx ) ;
46
- prefix = `. ${ prefix . substr ( idx ) } ` ;
47
- }
58
+ const prefixRange = [ param . quasis [ 0 ] . range [ 0 ] , param . quasis [ 0 ] . range [ 1 ] ] ;
59
+ const postfixRange =
60
+ param . quasis . length > 1
61
+ ? param . quasis [ param . quasis . length - 1 ] . range
62
+ : "" ;
63
+ const valueRange = param . range ;
64
+ const { context , prefix } = splitContextFromPrefix ( prefixRaw ) ;
65
+ const { postfix , query } = splitQueryFromPostfix ( postfixRaw ) ;
48
66
// If there are more than two quasis, maybe the generated RegExp can be more precise?
49
- regExp = new RegExp (
67
+ const regExp = new RegExp (
50
68
`^${ quotemeta ( prefix ) } ${ options . wrappedContextRegExp . source } ${ quotemeta (
51
69
postfix
52
70
) } $`
53
71
) ;
54
- dep = new Dep (
72
+ const dep = new Dep (
55
73
Object . assign (
56
74
{
57
- request : context ,
75
+ request : context + query ,
58
76
recursive : options . wrappedContextRecursive ,
59
77
regExp,
60
78
mode : "sync"
@@ -65,12 +83,20 @@ ContextDependencyHelpers.create = (
65
83
valueRange
66
84
) ;
67
85
dep . loc = expr . loc ;
68
- dep . replaces = [
69
- {
86
+ const replaces = [ ] ;
87
+ if ( prefixRange && prefix !== prefixRaw ) {
88
+ replaces . push ( {
70
89
range : prefixRange ,
71
90
value : prefix
72
- }
73
- ] ;
91
+ } ) ;
92
+ }
93
+ if ( postfixRange && postfix !== postfixRaw ) {
94
+ replaces . push ( {
95
+ range : postfixRange ,
96
+ value : postfix
97
+ } ) ;
98
+ }
99
+ dep . replaces = replaces ;
74
100
dep . critical =
75
101
options . wrappedContextCritical &&
76
102
"a part of the request of a dependency is an expression" ;
@@ -80,30 +106,26 @@ ContextDependencyHelpers.create = (
80
106
( ( param . prefix && param . prefix . isString ( ) ) ||
81
107
( param . postfix && param . postfix . isString ( ) ) )
82
108
) {
83
- prefix = param . prefix && param . prefix . isString ( ) ? param . prefix . string : "" ;
84
- postfix =
109
+ let prefixRaw =
110
+ param . prefix && param . prefix . isString ( ) ? param . prefix . string : "" ;
111
+ let postfixRaw =
85
112
param . postfix && param . postfix . isString ( ) ? param . postfix . string : "" ;
86
- prefixRange =
113
+ const prefixRange =
87
114
param . prefix && param . prefix . isString ( ) ? param . prefix . range : null ;
88
- valueRange = [
89
- prefixRange ? prefixRange [ 1 ] : param . range [ 0 ] ,
90
- param . range [ 1 ]
91
- ] ;
92
- idx = prefix . lastIndexOf ( "/" ) ;
93
- context = "." ;
94
- if ( idx >= 0 ) {
95
- context = prefix . substr ( 0 , idx ) ;
96
- prefix = `.${ prefix . substr ( idx ) } ` ;
97
- }
98
- regExp = new RegExp (
115
+ const postfixRange =
116
+ param . postfix && param . postfix . isString ( ) ? param . postfix . range : null ;
117
+ const valueRange = param . range ;
118
+ const { context, prefix } = splitContextFromPrefix ( prefixRaw ) ;
119
+ const { postfix, query } = splitQueryFromPostfix ( postfixRaw ) ;
120
+ const regExp = new RegExp (
99
121
`^${ quotemeta ( prefix ) } ${ options . wrappedContextRegExp . source } ${ quotemeta (
100
122
postfix
101
123
) } $`
102
124
) ;
103
- dep = new Dep (
125
+ const dep = new Dep (
104
126
Object . assign (
105
127
{
106
- request : context ,
128
+ request : context + query ,
107
129
recursive : options . wrappedContextRecursive ,
108
130
regExp,
109
131
mode : "sync"
@@ -114,13 +136,26 @@ ContextDependencyHelpers.create = (
114
136
valueRange
115
137
) ;
116
138
dep . loc = expr . loc ;
117
- dep . prepend = param . prefix && param . prefix . isString ( ) ? prefix : null ;
139
+ const replaces = [ ] ;
140
+ if ( prefixRange && prefix !== prefixRaw ) {
141
+ replaces . push ( {
142
+ range : prefixRange ,
143
+ value : JSON . stringify ( prefix )
144
+ } ) ;
145
+ }
146
+ if ( postfixRange && postfix !== postfixRaw ) {
147
+ replaces . push ( {
148
+ range : postfixRange ,
149
+ value : JSON . stringify ( postfix )
150
+ } ) ;
151
+ }
152
+ dep . replaces = replaces ;
118
153
dep . critical =
119
154
options . wrappedContextCritical &&
120
155
"a part of the request of a dependency is an expression" ;
121
156
return dep ;
122
157
} else {
123
- dep = new Dep (
158
+ const dep = new Dep (
124
159
Object . assign (
125
160
{
126
161
request : options . exprContextRequest ,
0 commit comments