This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathindex.js
126 lines (103 loc) · 4.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var sources = require("webpack-sources");
var fs = require("fs");
var path = require("path");
var projectDir = path.dirname(path.dirname(__dirname));
var packageJsonPath = path.join(projectDir, "package.json");
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
var isAngular = Object.keys(packageJson.dependencies).filter(function (dependency) {
return /^@angular\b/.test(dependency);
}).length > 0;
if (isAngular) {
exports.StyleUrlResolvePlugin = require("./resource-resolver-plugins/StyleUrlResolvePlugin");
}
//HACK: changes the JSONP chunk eval function to `global["nativescriptJsonp"]`
// applied to tns-java-classes.js only
exports.NativeScriptJsonpPlugin = function () {
};
exports.NativeScriptJsonpPlugin.prototype.apply = function (compiler) {
compiler.plugin("compilation", function (compilation) {
compilation.plugin("optimize-chunk-assets", function (chunks, callback) {
chunks.forEach(function (chunk) {
chunk.files.forEach(function (file) {
if (file === "vendor.js") {
var src = compilation.assets[file];
var code = src.source();
var match = code.match(/window\["nativescriptJsonp"\]/);
if (match) {
compilation.assets[file] = new sources.ConcatSource(code.replace(/window\["nativescriptJsonp"\]/g, "global[\"nativescriptJsonp\"]"));
}
}
});
});
callback();
});
});
};
exports.GenerateBundleStarterPlugin = function (bundles) {
this.bundles = bundles;
};
exports.GenerateBundleStarterPlugin.prototype = {
apply: function (compiler) {
var plugin = this;
plugin.webpackContext = compiler.options.context;
compiler.plugin("emit", function (compilation, cb) {
compilation.assets["package.json"] = plugin.generatePackageJson();
compilation.assets["starter.js"] = plugin.generateStarterModule();
cb();
});
},
generatePackageJson: function () {
var packageJsonPath = path.join(this.webpackContext, "package.json");
var packageData = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
packageData.main = "starter";
return new sources.RawSource(JSON.stringify(packageData, null, 4));
},
generateStarterModule: function () {
var moduleSource = this.bundles.map(function (bundle) {
return "require(\"" + bundle + "\");";
}).join("\n");
return new sources.RawSource(moduleSource);
},
};
exports.getEntryModule = function () {
const maybePackageJsonEntry = getPackageJsonEntry();
if (!maybePackageJsonEntry) {
throw new Error("app/package.json must contain a `main` attribute.");
}
const maybeAotEntry = getAotEntry(maybePackageJsonEntry);
return maybeAotEntry || maybePackageJsonEntry;
};
exports.getAppPath = function (platform) {
var projectDir = path.dirname(path.dirname(__dirname));
if (/ios/i.test(platform)) {
var appName = path.basename(projectDir);
var sanitizedName = appName.split("").filter(function (c) {
return /[a-zA-Z0-9]/.test(c);
}).join("");
return "platforms/ios/" + sanitizedName + "/app";
} else if (/android/i.test(platform)) {
return path.join(projectDir, "platforms/android/src/main/assets/app");
} else {
throw new Error("Invalid platform: " + platform);
}
};
exports.uglifyMangleExcludes = require("./mangle-excludes");
function getPackageJsonEntry() {
const packageJsonSource = getAppPackageJsonSource();
const entry = packageJsonSource.main;
return entry ? entry.replace(/\.js$/i, "") : null;
}
function getAppPackageJsonSource() {
const projectDir = getProjectDir();
const appPackageJsonPath = path.join(projectDir, "app", "package.json");
return JSON.parse(fs.readFileSync(appPackageJsonPath, "utf8"));
}
function getAotEntry(entry) {
const aotEntry = `${entry}.aot.ts`;
const projectDir = getProjectDir();
const aotEntryPath = path.join(projectDir, "app", aotEntry);
return fs.existsSync(aotEntryPath) ? aotEntry : null;
}
function getProjectDir() {
return path.dirname(path.dirname(__dirname));
}