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

fix-next: cache package.json, starter.js and tns-java-classes.js between compilations #444

Merged
merged 1 commit into from
Mar 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions plugins/GenerateBundleStarterPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const { getPackageJson } = require("../projectHelpers");
exports.GenerateBundleStarterPlugin = (function() {
function GenerateBundleStarterPlugin(bundles) {
this.bundles = bundles;
this.files = {};
};

GenerateBundleStarterPlugin.prototype.apply = function(compiler) {
const plugin = this;
plugin.webpackContext = compiler.options.context;
this.webpackContext = compiler.options.context;

compiler.plugin("emit", function (compilation, cb) {
compilation.assets["package.json"] = plugin.generatePackageJson();
compilation.assets["starter.js"] = plugin.generateStarterModule();
plugin.generateTnsJavaClasses(compilation);
compiler.plugin("emit", (compilation, cb) => {
this.addAsset(compilation, "package.json", this.generatePackageJson());
this.addAsset(compilation, "starter.js", this.generateStarterModule());
this.generateTnsJavaClasses(compilation);

cb();
});
Expand All @@ -24,23 +24,30 @@ exports.GenerateBundleStarterPlugin = (function() {
const isAndroid = path.indexOf("android") > -1;

if (isAndroid && !compilation.assets["tns-java-classes.js"]) {
compilation.assets["tns-java-classes.js"] = new RawSource("");
this.addAsset(compilation, "tns-java-classes.js", "");
}
}

GenerateBundleStarterPlugin.prototype.generatePackageJson = function () {
const packageJson = getPackageJson(this.webpackContext);
packageJson.main = "starter";

return new RawSource(JSON.stringify(packageJson, null, 4));
return JSON.stringify(packageJson, null, 4);
}

GenerateBundleStarterPlugin.prototype.generateStarterModule = function () {
const moduleSource = this.bundles
.map(bundle => `require("${bundle}")`)
.join("\n");

return new RawSource(moduleSource);
return moduleSource;
}

GenerateBundleStarterPlugin.prototype.addAsset = function(compilation, name, content) {
if (this.files[name] !== content) {
this.files[name] = content;
compilation.assets[name] = new RawSource(content);
}
}

return GenerateBundleStarterPlugin;
Expand Down