@@ -3,6 +3,11 @@ import { promisify } from "util";
3
3
import { loader } from "webpack" ;
4
4
import { convertSlashesInPath } from "./projectHelpers" ;
5
5
6
+ interface NamespaceEntry {
7
+ name : string ;
8
+ path : string
9
+ }
10
+
6
11
const loader : loader . Loader = function ( source , map ) {
7
12
this . value = source ;
8
13
const { ignore } = this . query ;
@@ -11,9 +16,9 @@ const loader: loader.Loader = function (source, map) {
11
16
const { XmlParser } = require ( "tns-core-modules/xml" ) ;
12
17
13
18
const resolvePromise = promisify ( this . resolve ) ;
14
- const promises = [ ] ;
19
+ const promises : Promise < any > [ ] = [ ] ;
15
20
16
- const namespaces = [ ] ;
21
+ const namespaces : NamespaceEntry [ ] = [ ] ;
17
22
const parser = new XmlParser ( ( event ) => {
18
23
const { namespace, elementName } = event ;
19
24
const moduleName = `${ namespace } /${ elementName } ` ;
@@ -87,20 +92,21 @@ const loader: loader.Loader = function (source, map) {
87
92
parser . parse ( source ) ;
88
93
89
94
Promise . all ( promises ) . then ( ( ) => {
90
- const moduleRegisters = namespaces
91
- . map ( convertPath )
92
- . map ( n =>
93
- `global.registerModule("${ n . name } ", function() { return require("${ n . path } "); });\n`
94
- )
95
- . join ( "" ) ;
95
+ const distinctNamespaces = new Map < string , string > ( ) ;
96
+ namespaces . forEach ( ( { name, path } ) => distinctNamespaces . set ( name , convertSlashesInPath ( path ) ) ) ;
97
+
98
+ const moduleRegisters : string [ ] = [ ] ;
99
+ distinctNamespaces . forEach ( ( path , name ) => {
100
+ moduleRegisters . push ( `global.registerModule("${ name } ", function() { return require("${ path } "); });\n` ) ;
101
+ } ) ;
96
102
97
103
// escape special whitespace characters
98
104
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Issue_with_plain_JSON.stringify_for_use_as_JavaScript
99
105
const json = JSON . stringify ( source )
100
106
. replace ( / \u2028 / g, '\\u2028' )
101
107
. replace ( / \u2029 / g, '\\u2029' ) ;
102
108
103
- const wrapped = `${ moduleRegisters } \nmodule.exports = ${ json } ` ;
109
+ const wrapped = `${ moduleRegisters . join ( "" ) } \nmodule.exports = ${ json } ` ;
104
110
105
111
callback ( null , wrapped , map ) ;
106
112
} ) . catch ( ( err ) => {
@@ -109,9 +115,4 @@ const loader: loader.Loader = function (source, map) {
109
115
110
116
}
111
117
112
- function convertPath ( obj ) {
113
- obj . path = convertSlashesInPath ( obj . path ) ;
114
- return obj ;
115
- }
116
-
117
118
export default loader ;
0 commit comments