1 | const { RawSource } = require("webpack-sources");
|
2 | const { getPackageJson } = require("../projectHelpers");
|
3 |
|
4 | exports.GenerateBundleStarterPlugin = (function() {
|
5 | function GenerateBundleStarterPlugin(bundles) {
|
6 | this.bundles = bundles;
|
7 | this.files = {};
|
8 | };
|
9 |
|
10 | GenerateBundleStarterPlugin.prototype.apply = function(compiler) {
|
11 | this.webpackContext = compiler.options.context;
|
12 |
|
13 | compiler.hooks.emit.tapAsync("GenerateBundleStarterPlugin", (compilation, cb) => {
|
14 | this.addAsset(compilation, "package.json", this.generatePackageJson());
|
15 | this.addAsset(compilation, "starter.js", this.generateStarterModule());
|
16 | this.generateTnsJavaClasses(compilation);
|
17 |
|
18 | cb();
|
19 | });
|
20 | }
|
21 |
|
22 | GenerateBundleStarterPlugin.prototype.generateTnsJavaClasses = function (compilation) {
|
23 | const path = compilation.compiler.outputPath;
|
24 | const isAndroid = path.indexOf("android") > -1;
|
25 |
|
26 | if (isAndroid && !compilation.assets["tns-java-classes.js"]) {
|
27 | this.addAsset(compilation, "tns-java-classes.js", "");
|
28 | }
|
29 | }
|
30 |
|
31 | GenerateBundleStarterPlugin.prototype.generatePackageJson = function () {
|
32 | const packageJson = getPackageJson(this.webpackContext);
|
33 | packageJson.main = "starter";
|
34 |
|
35 | return JSON.stringify(packageJson, null, 4);
|
36 | }
|
37 |
|
38 | GenerateBundleStarterPlugin.prototype.generateStarterModule = function () {
|
39 | const moduleSource = this.bundles
|
40 | .map(bundle => `require("${bundle}")`)
|
41 | .join("\n");
|
42 |
|
43 | return moduleSource;
|
44 | }
|
45 |
|
46 | GenerateBundleStarterPlugin.prototype.addAsset = function(compilation, name, content) {
|
47 | if (this.files[name] !== content) {
|
48 | this.files[name] = content;
|
49 | compilation.assets[name] = new RawSource(content);
|
50 | }
|
51 | }
|
52 |
|
53 | return GenerateBundleStarterPlugin;
|
54 | })();
|