Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit d786a04

Browse files
author
vakrilov
committed
refactor: remove duplicate registers
1 parent a090f75 commit d786a04

File tree

2 files changed

+15
-31
lines changed

2 files changed

+15
-31
lines changed

xml-namespace-loader.spec.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -173,23 +173,6 @@ fdescribe("XmlNamespaceLoader", () => {
173173
xmlNsLoader.call(loaderContext, CODE_FILE);
174174
})
175175

176-
it("with plugin path", (done) => {
177-
const resolveMap = {
178-
"nativescript-ui-chart": "node_module/nativescript-ui-chart/ui-chart.js",
179-
}
180-
181-
const expectedDeps = [
182-
];
183-
184-
const expectedRegs = [
185-
{ name: "nativescript-ui-chart", path: "nativescript-ui-chart" },
186-
{ name: "nativescript-ui-chart/RadCartesianChart", path: "nativescript-ui-chart" },
187-
];
188-
189-
const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs });
190-
xmlNsLoader.call(loaderContext, CODE_FILE);
191-
})
192-
193176
it("with ignored namespace should not add deps or register calls", (done) => {
194177
const resolveMap = {
195178
"app/nativescript-ui-chart": "app/nativescript-ui-chart.js",

xml-namespace-loader.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { promisify } from "util";
33
import { loader } from "webpack";
44
import { convertSlashesInPath } from "./projectHelpers";
55

6+
interface NamespaceEntry {
7+
name: string;
8+
path: string
9+
}
10+
611
const loader: loader.Loader = function (source, map) {
712
this.value = source;
813
const { ignore } = this.query;
@@ -11,9 +16,9 @@ const loader: loader.Loader = function (source, map) {
1116
const { XmlParser } = require("tns-core-modules/xml");
1217

1318
const resolvePromise = promisify(this.resolve);
14-
const promises = [];
19+
const promises: Promise<any>[] = [];
1520

16-
const namespaces = [];
21+
const namespaces: NamespaceEntry[] = [];
1722
const parser = new XmlParser((event) => {
1823
const { namespace, elementName } = event;
1924
const moduleName = `${namespace}/${elementName}`;
@@ -87,20 +92,21 @@ const loader: loader.Loader = function (source, map) {
8792
parser.parse(source);
8893

8994
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+
});
96102

97103
// escape special whitespace characters
98104
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Issue_with_plain_JSON.stringify_for_use_as_JavaScript
99105
const json = JSON.stringify(source)
100106
.replace(/\u2028/g, '\\u2028')
101107
.replace(/\u2029/g, '\\u2029');
102108

103-
const wrapped = `${moduleRegisters}\nmodule.exports = ${json}`;
109+
const wrapped = `${moduleRegisters.join("")}\nmodule.exports = ${json}`;
104110

105111
callback(null, wrapped, map);
106112
}).catch((err) => {
@@ -109,9 +115,4 @@ const loader: loader.Loader = function (source, map) {
109115

110116
}
111117

112-
function convertPath(obj) {
113-
obj.path = convertSlashesInPath(obj.path);
114-
return obj;
115-
}
116-
117118
export default loader;

0 commit comments

Comments
 (0)