@@ -151,7 +151,7 @@ class AureliaWebpackPlugin {
151
151
/**
152
152
* used to inject Aurelia's Origin to all build resources
153
153
*/
154
- compiler . plugin ( 'compilation' , function ( compilation ) {
154
+ compiler . plugin ( 'compilation' , function ( compilation ) {
155
155
debug ( 'compilation' ) ;
156
156
const contextElements = compiler . __aureliaContextElements ;
157
157
let paths = [ ] ;
@@ -161,32 +161,82 @@ class AureliaWebpackPlugin {
161
161
console . error ( 'No context elements' ) ;
162
162
}
163
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 ) ;
164
+ function customWebpackRequire ( moduleId ) {
165
+ // Check if module is in cache
166
+ if ( installedModules [ moduleId ] )
167
+ return installedModules [ moduleId ] . exports ;
168
+
169
+ // Create a new module (and put it into the cache)
170
+ var module = installedModules [ moduleId ] = {
171
+ i : moduleId ,
172
+ l : false ,
173
+ exports : { }
174
+ } ;
175
+
176
+ // Try adding .js / .ts
177
+ if ( ! modules [ moduleId ] && typeof moduleId === 'string' ) {
178
+ var newModuleId ;
179
+ if ( modules [ newModuleId = moduleId + '.js' ] || modules [ newModuleId = moduleId + '.ts' ] ) {
180
+ moduleId = newModuleId ;
181
+ // alias also installedModules:
182
+ installedModules [ moduleId ] = module ;
171
183
}
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
- }
184
+ }
185
+
186
+ // Execute the module function
187
+ modules [ moduleId ] . call ( module . exports , module , module . exports , __webpack_require__ ) ;
188
+
189
+ // Flag the module as loaded
190
+ module . l = true ;
191
+
192
+ // Return the exports of the module
193
+ return module . exports ;
194
+ }
195
+
196
+ compilation . mainTemplate . plugin ( 'require' , function ( source , chunk , hash ) {
197
+ let newSourceArray = customWebpackRequire . toString ( ) . split ( '\n' ) ;
198
+ newSourceArray . pop ( ) ; // remove header 'function... {'
199
+ newSourceArray . shift ( ) ; // remove footer ' }'
200
+ return newSourceArray . join ( '\n' ) ;
201
+ } ) ;
202
+
203
+ compilation . plugin ( 'before-module-ids' , function ( modules ) {
204
+ modules . forEach ( ( module ) => {
205
+ if ( module . id !== null ) {
206
+ return ;
177
207
}
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 ;
208
+
209
+ if ( typeof module . resource == 'string' ) {
210
+ let moduleId ;
211
+
212
+ if ( module . resource . startsWith ( options . src ) ) {
213
+ // paths inside SRC
214
+ let relativeToSrc = path . relative ( options . src , module . resource ) ;
215
+ moduleId = relativeToSrc ;
216
+ }
217
+ if ( ! moduleId && typeof module . userRequest == 'string' ) {
218
+ // paths resolved as build resources
219
+ let matchingModuleIds = paths
220
+ . filter ( originPath => contextElements [ originPath ] . source === module . userRequest )
221
+ . map ( originPath => path . normalize ( originPath ) ) ;
222
+
223
+ if ( matchingModuleIds . length ) {
224
+ matchingModuleIds . sort ( ( a , b ) => b . length - a . length ) ;
225
+ moduleId = matchingModuleIds [ 0 ] ;
226
+ }
227
+ }
228
+ if ( ! moduleId && typeof module . rawRequest == 'string' && module . rawRequest . indexOf ( '.' ) !== 0 ) {
229
+ // requested modules from node_modules:
230
+ let index = paths . indexOf ( module . rawRequest ) ;
231
+ if ( index >= 0 ) {
232
+ moduleId = module . rawRequest ;
233
+ }
234
+ }
235
+ if ( moduleId ) {
236
+ module . id = moduleId ;
183
237
}
184
238
}
185
- if ( moduleId ) {
186
- const originLoader = path . join ( __dirname , 'origin-loader.js' ) + '?' + JSON . stringify ( { moduleId } ) ;
187
- module . loaders . unshift ( originLoader ) ;
188
- }
189
- }
239
+ } ) ;
190
240
} ) ;
191
241
} ) ;
192
242
}
0 commit comments