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

Commit aa84f58

Browse files
committed
ExcludeUnusedElements plugin + loader added
1 parent 2564bfd commit aa84f58

File tree

4 files changed

+95
-10
lines changed

4 files changed

+95
-10
lines changed

Diff for: index.js

+43-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var path = require("path");
44

55
//HACK: changes the JSONP chunk eval function to `global["nativescriptJsonp"]`
66
// applied to tns-java-classes.js only
7-
exports.NativeScriptJsonpPlugin = function(options) {
7+
exports.NativeScriptJsonpPlugin = function (options) {
88
};
99

1010
exports.NativeScriptJsonpPlugin.prototype.apply = function (compiler) {
@@ -27,7 +27,42 @@ exports.NativeScriptJsonpPlugin.prototype.apply = function (compiler) {
2727
});
2828
};
2929

30-
exports.GenerateBundleStarterPlugin = function(bundles) {
30+
exports.ExcludeUnusedElementsPlugin = function () {
31+
};
32+
33+
exports.ExcludeUnusedElementsPlugin.prototype.apply = function (compiler) {
34+
compiler.plugin("normal-module-factory", function (nmf) {
35+
nmf.plugin("before-resolve", function (result, callback) {
36+
if (!result) {
37+
return callback();
38+
}
39+
40+
if (result.request === "globals" || result.request === "ui/core/view") {
41+
return callback(null, result);
42+
}
43+
44+
if (result.context.indexOf("tns-core-modules") === -1) {
45+
if (result.contextInfo.issuer &&
46+
result.contextInfo.issuer.indexOf("element-registry") !== -1 && global["ELEMENT_REGISTRY"] &&
47+
!global["ELEMENT_REGISTRY"][result.request]) {
48+
return callback();
49+
50+
} else {
51+
return callback(null, result);
52+
}
53+
}
54+
55+
if (result.contextInfo.issuer.indexOf("bundle-entry-points") !== -1 && global["ELEMENT_REGISTRY"] &&
56+
!global["ELEMENT_REGISTRY"][result.request]) {
57+
return callback();
58+
}
59+
60+
return callback(null, result);
61+
});
62+
});
63+
};
64+
65+
exports.GenerateBundleStarterPlugin = function (bundles) {
3166
this.bundles = bundles;
3267
};
3368

@@ -45,22 +80,22 @@ exports.GenerateBundleStarterPlugin.prototype = {
4580
cb();
4681
});
4782
},
48-
generatePackageJson: function() {
83+
generatePackageJson: function () {
4984
var packageJsonPath = path.join(this.webpackContext, "package.json");
5085
var packageData = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
5186
packageData.main = "starter";
5287

5388
return new sources.RawSource(JSON.stringify(packageData, null, 4));
5489
},
55-
generateStarterModule: function() {
56-
var moduleSource = this.bundles.map(function(bundle) {
90+
generateStarterModule: function () {
91+
var moduleSource = this.bundles.map(function (bundle) {
5792
return "require(\"" + bundle + "\");";
5893
}).join("\n");
5994
return new sources.RawSource(moduleSource);
6095
},
6196
};
6297

63-
exports.getEntryModule = function() {
98+
exports.getEntryModule = function () {
6499
var projectDir = path.dirname(path.dirname(__dirname));
65100
var appPackageJsonPath = path.join(projectDir, "app", "package.json");
66101
var appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath, "utf8"));
@@ -70,12 +105,12 @@ exports.getEntryModule = function() {
70105
return appPackageJson.main.replace(/\.js$/i, "");
71106
};
72107

73-
exports.getAppPath = function(platform) {
108+
exports.getAppPath = function (platform) {
74109
var projectDir = path.dirname(path.dirname(__dirname));
75110

76111
if (/ios/i.test(platform)) {
77112
var appName = path.basename(projectDir);
78-
var sanitizedName = appName.split("").filter(function(c) {
113+
var sanitizedName = appName.split("").filter(function (c) {
79114
return /[a-zA-Z0-9]/.test(c);
80115
}).join("");
81116
return "platforms/ios/" + sanitizedName + "/app";

Diff for: postinstall.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ configureDevDependencies(packageJson, function (add) {
5454
if (isAngular) {
5555
add("@angular/compiler-cli", "2.2.1");
5656
add("@ngtools/webpack", "1.1.6");
57+
add("htmlparser2", "^3.9.2");
5758
} else {
5859
add("awesome-typescript-loader", "~3.0.0-beta.9");
5960
}

Diff for: tns-xml-loader.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var htmlparser = require("htmlparser2");
2+
3+
var UI_PATH = "ui/";
4+
5+
var MODULES = {
6+
"TabViewItem": "ui/tab-view",
7+
"FormattedString": "text/formatted-string",
8+
"Span": "text/span",
9+
"ActionItem": "ui/action-bar",
10+
"NavigationButton": "ui/action-bar",
11+
"SegmentedBarItem": "ui/segmented-bar",
12+
};
13+
14+
var ELEMENT_REGISTRY = "ELEMENT_REGISTRY";
15+
16+
if (!global[ELEMENT_REGISTRY]) {
17+
global[ELEMENT_REGISTRY] = {};
18+
}
19+
20+
module.exports = function (source, map) {
21+
this.cacheable();
22+
23+
var loader = this;
24+
25+
var parser = new htmlparser.Parser({
26+
onopentag: function (name, attribs) {
27+
// kebab-case to CamelCase
28+
var elementName = name.split("-").map(function (s) { return s[0].toUpperCase() + s.substring(1); }).join("");
29+
// Module path from element name
30+
var modulePath = MODULES[elementName] || UI_PATH +
31+
(elementName.toLowerCase().indexOf("layout") !== -1 ? "layouts/" : "") +
32+
elementName.split(/(?=[A-Z])/).join("-").toLowerCase();
33+
// Update ELEMENT_REGISTRY
34+
global[ELEMENT_REGISTRY][modulePath] = elementName;
35+
}
36+
}, { decodeEntities: true, lowerCaseTags: false });
37+
parser.write(source);
38+
parser.end();
39+
40+
this.callback(null, source, map);
41+
};

Diff for: webpack.common.js.angular.template

+10-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ module.exports = function (platform, destinationApp) {
4444
"./vendor",
4545
"./bundle",
4646
]),
47+
48+
// Exclude explicitly required but never declared in XML elements.
49+
// Loader nativescript-dev-webpack/tns-xml-loader should be added for *.xml/html files.
50+
new nsWebpack.ExcludeUnusedElementsPlugin(),
51+
4752
//Angular AOT compiler
4853
new AotPlugin({
4954
tsConfigPath: "tsconfig.aot.json",
@@ -99,8 +104,11 @@ module.exports = function (platform, destinationApp) {
99104
module: {
100105
loaders: [
101106
{
102-
test: /\.html$/,
103-
loader: "raw-loader"
107+
test: /\.html$|\.xml$/,
108+
loaders: [
109+
"raw-loader",
110+
'nativescript-dev-webpack/tns-xml-loader'
111+
]
104112
},
105113
// Root app.css file gets extracted with bundled dependencies
106114
{

0 commit comments

Comments
 (0)