1717 * - The path to the module itself: /home/usr/pkg/node_modules
1818 * - The module name: mod
1919 * - The relative path: dir/file.js
20- * - Map the parsed path data to _nodeModuleResourcesMap by the parsed module name and its resource location
20+ * - Map the parsed path data to nodeModuleResourcesMap by the parsed module name and its resource location
2121 * - Example Map:
2222 * {
2323 * 'aurelia-templating-router': {
5252 * - If there are multiple entry points:
5353 * - Pick the most shallow resource
5454 * - If they are both as shallow as possible choose index over module name match
55- * - Map the normalized module id to _nodeModuleResourceIdMap by the resource file
55+ * - Map the normalized module id to nodeModuleResourceIdMap by the resource file
5656 * - Example Map:
5757 * {
5858 * '/home/usr/pkg/node_modules/aurelia-templating-router/dist/native-modules/router-view.js': 'aurelia-templating-router/router-view.js',
@@ -92,8 +92,8 @@ import path = require("path");
9292export const preserveModuleName = Symbol ( ) ;
9393
9494// node_module maps
95- const _nodeModuleResourcesMap : NodeModule . ResourcesMap = { } ;
96- const _nodeModuleResourceIdMap : NodeModule . ResourceIdMap = { } ;
95+ const nodeModuleResourcesMap : NodeModule . ResourcesMap = { } ;
96+ const nodeModuleResourceIdMap : NodeModule . ResourceIdMap = { } ;
9797
9898const TAP_NAME = "Aurelia:PreserveModuleName" ;
9999
@@ -147,7 +147,7 @@ export class PreserveModuleNamePlugin {
147147 if ( / ^ a s y n c [ ? ! ] / . test ( realModule . rawRequest ) )
148148 id = "async!" + id ;
149149
150- id = id . replace ( / \\ / g, '/' ) ;
150+ id = id . replace ( / \\ / g, "/" ) ;
151151 if ( module . buildMeta ) // meta can be null if the module contains errors
152152 module . buildMeta [ "aurelia-id" ] = id ;
153153 if ( ! this . isDll ) {
@@ -170,20 +170,13 @@ function getPreservedModules(modules: Webpack.Module[]) {
170170
171171 // Preserve the module if its dependencies are also preserved
172172 const reasons = ( m . reasons && Array . isArray ( m . reasons ) ) ? m . reasons : [ ] ;
173- return reasons . some ( ( reason ) => Boolean ( reason . dependency && reason . dependency [ preserveModuleName ] ) ) ;
173+ return reasons . some ( reason => ( reason . dependency && reason . dependency [ preserveModuleName ] ) ) ;
174174 } )
175175 ) ;
176176}
177177
178- /**
179- * Check if a module exists in node_modules/
180- *
181- * @param {Webpack.Module } module The module to check
182- *
183- * @return {Boolean } True if it exists in node_modules/, false otherwise
184- */
185- function isNodeModule ( module : Webpack . Module ) : boolean {
186- return ! ( ! module || ! module . resource || ! ( / \b n o d e _ m o d u l e s \b / i. test ( module . resource ) ) ) ;
178+ function isNodeModule ( module : Webpack . Module ) {
179+ return ( module . resource && / \b n o d e _ m o d u l e s \b / i. test ( module . resource ) ) ;
187180}
188181
189182/**
@@ -235,10 +228,10 @@ function mapNodeModule(module: Webpack.Module) {
235228 }
236229
237230 // Map it
238- if ( ! _nodeModuleResourcesMap [ moduleData . name ] ) {
239- _nodeModuleResourcesMap [ moduleData . name ] = { } ;
231+ if ( ! nodeModuleResourcesMap [ moduleData . name ] ) {
232+ nodeModuleResourcesMap [ moduleData . name ] = { } ;
240233 }
241- _nodeModuleResourcesMap [ moduleData . name ] [ module . resource ] = moduleData ;
234+ nodeModuleResourcesMap [ moduleData . name ] [ module . resource ] = moduleData ;
242235}
243236
244237/**
@@ -267,13 +260,13 @@ function mapNodeModule(module: Webpack.Module) {
267260 * @return {undefined }
268261 */
269262function parseNodeModules ( ) {
270- if ( ! _nodeModuleResourcesMap || ! Object . keys ( _nodeModuleResourcesMap ) . length ) {
263+ if ( ! nodeModuleResourcesMap || ! Object . keys ( nodeModuleResourcesMap ) . length ) {
271264 return ;
272265 }
273266
274267 // Parse each module
275- for ( const moduleKey in _nodeModuleResourcesMap ) {
276- const moduleResources : NodeModule . ModuleResource = _nodeModuleResourcesMap [ moduleKey ] ;
268+ for ( const moduleKey in nodeModuleResourcesMap ) {
269+ const moduleResources : NodeModule . ModuleResource = nodeModuleResourcesMap [ moduleKey ] ;
277270
278271 // Keep track of the common resource path and possible module entry points
279272 let commonPathParts : string [ ] = [ ] ;
@@ -282,15 +275,15 @@ function parseNodeModules() {
282275 // Parse each resource in the module
283276 for ( const resource in moduleResources ) {
284277 const data : NodeModule . Data = moduleResources [ resource ] ;
285- const pathParts : string [ ] = data . relative . split ( '/' ) ;
278+ const pathParts = data . relative . split ( "/" ) ;
286279 const resourceFile : string | null = pathParts . splice ( - 1 ) [ 0 ] ;
287280 if ( ! resourceFile ) {
288281 continue ;
289282 }
290283
291284 // Entry?
292- const resourceName : string = resourceFile . replace ( / \. .* / , '' ) ;
293- if ( resourceName === moduleKey || resourceName === ' index' ) {
285+ const resourceName = resourceFile . replace ( / \. .* / , "" ) ;
286+ if ( resourceName === moduleKey || resourceName === " index" ) {
294287 possibleEntryPoints . push ( resource ) ;
295288 }
296289
@@ -315,12 +308,12 @@ function parseNodeModules() {
315308 }
316309
317310 // Convert common path to string
318- let commonPath : string = commonPathParts . join ( '/' ) ;
319- commonPath = ( commonPath . startsWith ( '/' ) ) ? commonPath : `/${ commonPath } ` ;
311+ let commonPath = commonPathParts . join ( "/" ) ;
312+ commonPath = ( commonPath . startsWith ( "/" ) ) ? commonPath : `/${ commonPath } ` ;
320313
321314 // If there is more than one possible entry point, use the most shallow resource
322315 let moduleEntry : string | null = null ;
323- possibleEntryPoints . forEach ( ( resource : string ) => {
316+ possibleEntryPoints . forEach ( ( resource ) => {
324317 const data : NodeModule . Data = moduleResources [ resource ] ;
325318
326319 // No entry yet?
@@ -329,7 +322,7 @@ function parseNodeModules() {
329322 }
330323
331324 // Shallow?
332- else if ( moduleEntry . split ( '/' ) . length > data . relative . split ( '/' ) . length ) {
325+ else if ( moduleEntry . split ( "/" ) . length > data . relative . split ( "/" ) . length ) {
333326 moduleEntry = data . relative ;
334327 }
335328
@@ -341,25 +334,25 @@ function parseNodeModules() {
341334 } ) ;
342335
343336 // If an entry point still hasnt been found and there is only one resource, use that
344- const resourceKeys : string [ ] = Object . keys ( moduleResources ) ;
337+ const resourceKeys = Object . keys ( moduleResources ) ;
345338 if ( ! moduleEntry && resourceKeys . length === 1 ) {
346339 moduleEntry = moduleResources [ resourceKeys [ 0 ] ] . relative ;
347340 }
348341
349342 // Map the resources to the module id
350- resourceKeys . forEach ( ( resource : string ) => {
343+ resourceKeys . forEach ( ( resource ) => {
351344 const data : NodeModule . Data = moduleResources [ resource ] ;
352345
353346 // Entry?
354347 if ( moduleEntry === data . relative ) {
355- _nodeModuleResourceIdMap [ resource ] = moduleKey ;
348+ nodeModuleResourceIdMap [ resource ] = moduleKey ;
356349 return ;
357350 }
358351
359352 // Build the id from the resources common path
360- let key : string = data . relative . replace ( new RegExp ( `^${ escapeString ( commonPath ) } ` ) , '' ) ;
361- key = ( key . startsWith ( '/' ) ) ? key : `/${ key } ` ;
362- _nodeModuleResourceIdMap [ resource ] = `${ moduleKey } ${ key } ` ;
353+ let key = data . relative . replace ( new RegExp ( `^${ escapeString ( commonPath ) } ` ) , "" ) ;
354+ key = ( key . startsWith ( "/" ) ) ? key : `/${ key } ` ;
355+ nodeModuleResourceIdMap [ resource ] = `${ moduleKey } ${ key } ` ;
363356 } ) ;
364357 }
365358}
@@ -374,14 +367,14 @@ function parseNodeModules() {
374367 * @return {string|null } The relative path if available, null otherwise
375368 */
376369function getRelativeModule ( module : Webpack . Module , paths : string [ ] ) : string | null {
377- if ( ! module || ! module . resource || ! paths || ! paths . length ) {
370+ if ( ! module . resource || ! paths || ! paths . length ) {
378371 return null ;
379372 }
380373
381374 // Try to find the module in the resolver paths
382375 for ( let i = 0 , len = paths . length ; i < len ; i ++ ) {
383- const relative : string = path . relative ( paths [ i ] , module . resource ) ;
384- if ( ! relative . startsWith ( '..' ) ) {
376+ const relative = path . relative ( paths [ i ] , module . resource ) ;
377+ if ( ! relative . startsWith ( ".." ) ) {
385378 return relative ;
386379 }
387380 }
@@ -399,19 +392,19 @@ function getRelativeModule(module: Webpack.Module, paths: string[]): string | nu
399392 * @return {string|null } The alias path if available, null otherwise
400393 */
401394function getAliasModule ( module : Webpack . Module , aliases : { [ key : string ] : string } | null ) : string | null {
402- if ( ! module || ! module . resource || ! aliases || ! Object . keys ( aliases ) . length ) {
395+ if ( ! module . resource || ! aliases || ! Object . keys ( aliases ) . length ) {
403396 return null ;
404397 }
405398
406399 // Look for the module in each alias
407400 for ( let alias in aliases ) {
408- const relative : string = path . relative ( path . resolve ( aliases [ alias ] ) , module . resource ) ;
409- if ( relative . startsWith ( '..' ) ) {
401+ const relative = path . relative ( path . resolve ( aliases [ alias ] ) , module . resource ) ;
402+ if ( relative . startsWith ( ".." ) ) {
410403 continue ;
411404 }
412405
413406 // Absolute alias?
414- alias = alias . replace ( / \$ $ / , '' ) ;
407+ alias = alias . replace ( / \$ $ / , "" ) ;
415408 return ( relative && relative . length ) ? `${ alias } /${ relative } ` : alias ;
416409 }
417410
@@ -429,10 +422,6 @@ function getAliasModule(module: Webpack.Module, aliases: { [key: string]: string
429422 * @return {string|null } The module id if available, null otherwise
430423 */
431424function getModuleId ( module : Webpack . Module , paths : string [ ] , aliases : { [ key : string ] : string } | null ) : string | null {
432- if ( ! module ) {
433- return null ;
434- }
435-
436425 // Handling module ids can be a bit tricky
437426 // Modules can be included in any of the following ways:
438427 // import { Module } from 'module'
@@ -457,7 +446,7 @@ function getModuleId(module: Webpack.Module, paths: string[], aliases: { [key: s
457446 // In order to have the aurelia-loader work correctly, we need to coerce everything to absolute ids
458447 // Is it a node_module?
459448 if ( isNodeModule ( module ) ) {
460- return _nodeModuleResourceIdMap [ module . resource ] ;
449+ return nodeModuleResourceIdMap [ module . resource ] ;
461450 }
462451
463452 // Get the module relative to the webpack resolver paths
@@ -475,9 +464,5 @@ function getModuleId(module: Webpack.Module, paths: string[], aliases: { [key: s
475464 * @return {string|null } The escaped string
476465 */
477466function escapeString ( str : string ) : string | null {
478- if ( typeof str !== 'string' ) {
479- return null ;
480- }
481-
482- return str . replace ( / [ - \/ \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' ) ;
467+ return str . replace ( / [ - \/ \\ ^ $ * + ? . ( ) | [ \] { } ] / g, "\\$&" ) ;
483468}
0 commit comments