@@ -59,18 +59,49 @@ class AureliaWebpackPlugin {
59
59
}
60
60
61
61
apply ( compiler ) {
62
- compiler . plugin ( 'context-module-factory' , cmf => {
62
+ const options = this . options ;
63
+ const self = this ;
64
+
65
+ compiler . plugin ( 'run' , function ( compiler , callback ) {
66
+ debug ( 'run' ) ;
67
+ resolveTemplates . processAll ( options ) . then ( contextElements => {
68
+ compiler . __aureliaContextElements = contextElements ;
69
+ debug ( 'finished run: got contextElements' ) ;
70
+ callback ( ) ;
71
+ } , ( e ) => {
72
+ handleError ( e ) ;
73
+ return callback ( error ) ;
74
+ } ) ;
75
+ } ) ;
76
+
77
+ compiler . plugin ( 'watch-run' , function ( watching , callback ) {
78
+ resolveTemplates . processAll ( options ) . then ( contextElements => {
79
+ watching . compiler . __aureliaContextElements = contextElements ;
80
+ debug ( 'finished watch-run: got contextElements' ) ;
81
+ callback ( ) ;
82
+ } , ( e ) => {
83
+ handleError ( e ) ;
84
+ return callback ( error ) ;
85
+ } ) ;
86
+ } ) ;
87
+
88
+ compiler . plugin ( 'context-module-factory' , function ( cmf ) {
89
+ var contextElements = compiler . __aureliaContextElements ;
90
+ debug ( 'context-module-factory' ) ;
91
+
63
92
cmf . plugin ( 'before-resolve' , ( result , callback ) => {
64
93
if ( ! result ) return callback ( ) ;
65
- if ( this . options . resourceRegExp . test ( result . request ) ) {
66
- result . request = this . options . src ;
94
+ if ( self . options . resourceRegExp . test ( result . request ) ) {
95
+ result . request = self . options . src ;
67
96
}
68
97
return callback ( null , result ) ;
69
98
} ) ;
99
+
70
100
cmf . plugin ( 'after-resolve' , ( result , callback ) => {
71
101
if ( ! result ) return callback ( ) ;
102
+
72
103
const resourcePath = path . normalizeSafe ( result . resource ) ;
73
- if ( this . options . src . indexOf ( resourcePath , this . options . src . length - resourcePath . length ) !== - 1 ) {
104
+ if ( self . options . src . indexOf ( resourcePath , self . options . src . length - resourcePath . length ) !== - 1 ) {
74
105
const resolveDependencies = result . resolveDependencies ;
75
106
76
107
// substitute resolveDependencies method with an enhanced version:
@@ -88,39 +119,76 @@ class AureliaWebpackPlugin {
88
119
dependencies . push ( dependency ) ;
89
120
}
90
121
91
- resolveTemplates . processAll ( this . options ) . then ( contextElements => {
92
- for ( let requireRequestPath of Object . keys ( contextElements ) . reverse ( ) ) {
93
- try {
94
- const resource = contextElements [ requireRequestPath ] ;
95
- // ensure we have './' at the beginning of the request path
96
- requireRequestPath = path . joinSafe ( './' , requireRequestPath ) ;
97
- let newDependency = new ContextElementDependency ( getPath ( resource ) , requireRequestPath ) ;
98
- if ( resource . hasOwnProperty ( 'optional' ) )
99
- newDependency . optional = ! ! resource . optional ;
100
- else
101
- newDependency . optional = true ;
102
- let previouslyAdded = dependencies . findIndex ( dependency => dependency . userRequest === requireRequestPath ) ;
103
- if ( previouslyAdded > - 1 ) {
104
- dependencies [ previouslyAdded ] = newDependency ;
105
- } else {
106
- dependencies . push ( newDependency ) ;
107
- }
108
- } catch ( e ) {
109
- handleError ( e ) ;
122
+ for ( let requireRequestPath of Object . keys ( contextElements ) . reverse ( ) ) {
123
+ try {
124
+ const resource = contextElements [ requireRequestPath ] ;
125
+ // ensure we have './' at the beginning of the request path
126
+ requireRequestPath = path . joinSafe ( './' , requireRequestPath ) ;
127
+ let newDependency = new ContextElementDependency ( self . getPath ( resource ) , requireRequestPath ) ;
128
+ if ( resource . hasOwnProperty ( 'optional' ) )
129
+ newDependency . optional = ! ! resource . optional ;
130
+ else
131
+ newDependency . optional = true ;
132
+ let previouslyAdded = dependencies . findIndex ( dependency => dependency . userRequest === requireRequestPath ) ;
133
+ if ( previouslyAdded > - 1 ) {
134
+ dependencies [ previouslyAdded ] = newDependency ;
135
+ } else {
136
+ dependencies . push ( newDependency ) ;
110
137
}
111
- // TODO: optional filtering of context (things we don't want to require)
138
+ } catch ( e ) {
139
+ handleError ( e ) ;
112
140
}
113
-
114
- return callback ( null , dependencies ) ;
115
- } , ( e ) => {
116
- handleError ( e ) ;
117
- return callback ( error ) ;
118
- } ) ;
141
+ // TODO: optional filtering of context (things we don't want to require)
142
+ }
143
+
144
+ return callback ( null , dependencies ) ;
119
145
} ) ;
120
146
}
121
147
return callback ( null , result ) ;
122
148
} ) ;
123
149
} ) ;
150
+
151
+ /**
152
+ * used to inject Aurelia's Origin to all build resources
153
+ */
154
+ compiler . plugin ( 'compilation' , function ( compilation ) {
155
+ debug ( 'compilation' ) ;
156
+ const contextElements = compiler . __aureliaContextElements ;
157
+ let paths = [ ] ;
158
+ try {
159
+ paths = Object . getOwnPropertyNames ( contextElements ) ;
160
+ } catch ( e ) {
161
+ console . error ( 'No context elements' ) ;
162
+ }
163
+
164
+ compilation . plugin ( 'normal-module-loader' , function ( loaderContext , module ) {
165
+ // this is where all the modules are loaded
166
+ // one by one, no dependencies are created yet
167
+ if ( typeof module . resource == 'string' && / \. ( j s | t s ) x ? $ / i. test ( module . resource ) ) {
168
+ let moduleId ;
169
+ if ( module . resource . startsWith ( options . src ) ) {
170
+ moduleId = path . relative ( options . src , module . resource ) ;
171
+ }
172
+ if ( ! moduleId && typeof module . userRequest == 'string' ) {
173
+ moduleId = paths . find ( originPath => contextElements [ originPath ] . source === module . userRequest ) ;
174
+ if ( moduleId ) {
175
+ moduleId = path . normalize ( moduleId ) ;
176
+ }
177
+ }
178
+ if ( ! moduleId && typeof module . rawRequest == 'string' && ! module . rawRequest . startsWith ( '.' ) ) {
179
+ // requested module:
180
+ let index = paths . indexOf ( module . rawRequest ) ;
181
+ if ( index >= 0 ) {
182
+ moduleId = module . rawRequest ;
183
+ }
184
+ }
185
+ if ( moduleId ) {
186
+ const originLoader = path . join ( __dirname , 'origin-loader.js' ) + '?' + JSON . stringify ( { moduleId } ) ;
187
+ module . loaders . unshift ( originLoader ) ;
188
+ }
189
+ }
190
+ } ) ;
191
+ } ) ;
124
192
}
125
193
}
126
194
0 commit comments