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
122 lines (100 loc) · 4.85 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
const { resolve, join } = require("path");
const { closeSync, openSync } = require("fs");
const validateOptions = require("schema-utils");
const ProjectSnapshotGenerator = require("../../snapshot/android/project-snapshot-generator");
const { resolveAndroidAppPath } = require("../../projectHelpers");
const schema = require("./options.json");
exports.NativeScriptSnapshotPlugin = (function() {
function NativeScriptSnapshotPlugin(options) {
NativeScriptSnapshotPlugin.validateSchema(options);
ProjectSnapshotGenerator.call(this, options); // Call the parent constructor
if (this.options.webpackConfig) {
if (this.options.webpackConfig.output && this.options.webpackConfig.output.libraryTarget) {
this.options.webpackConfig.output.libraryTarget = undefined;
}
if (this.options.webpackConfig.entry) {
if (typeof this.options.webpackConfig.entry === "string" ||
this.options.webpackConfig.entry instanceof Array)
this.options.webpackConfig.entry = { bundle: this.options.webpackConfig.entry };
}
this.options.webpackConfig.entry["tns-java-classes"] = this.getTnsJavaClassesBuildPath();
}
}
NativeScriptSnapshotPlugin.validateSchema = function(options) {
if (!options.chunk) {
const error = NativeScriptSnapshotPlugin.extendError({ message: `No chunk specified!` });
throw error;
}
try {
validateOptions(schema, options, "NativeScriptSnapshotPlugin");
} catch (error) {
throw new Error(error.message);
}
}
// inherit ProjectSnapshotGenerator
NativeScriptSnapshotPlugin.prototype = Object.create(ProjectSnapshotGenerator.prototype);
NativeScriptSnapshotPlugin.prototype.constructor = NativeScriptSnapshotPlugin;
NativeScriptSnapshotPlugin.prototype.getTnsJavaClassesBuildPath = function () {
return resolve(this.getBuildPath(), "../tns-java-classes.js");
}
NativeScriptSnapshotPlugin.prototype.generate = function (webpackChunk) {
const options = this.options;
const inputFile = join(options.webpackConfig.output.path, webpackChunk.files[0]);
console.log(`\n Snapshotting bundle at ${inputFile}`);
const preparedAppRootPath = resolveAndroidAppPath(this.options.projectRoot);
const preprocessedInputFile = join(preparedAppRootPath, "_embedded_script_.js");
return ProjectSnapshotGenerator.prototype.generate.call(this, {
inputFile,
preprocessedInputFile,
targetArchs: options.targetArchs,
useLibs: options.useLibs,
androidNdkPath: options.androidNdkPath,
v8Version: options.v8Version,
tnsJavaClassesPath: join(preparedAppRootPath, "tns-java-classes.js")
}).then(() => {
// Make the original file empty
if (inputFile !== preprocessedInputFile) {
closeSync(openSync(inputFile, "w")); // truncates the input file content
}
});
}
NativeScriptSnapshotPlugin.prototype.apply = function (compiler) {
const options = this.options;
// Generate tns-java-classes.js file
debugger;
ProjectSnapshotGenerator.prototype.generateTnsJavaClassesFile.call(this, {
output: this.getTnsJavaClassesBuildPath(),
options: options.tnsJavaClassesOptions
});
// Generate snapshots
compiler.plugin("after-emit", function (compilation, callback) {
debugger;
const chunkToSnapshot = compilation.chunks.find(chunk => chunk.name == options.chunk);
if (!chunkToSnapshot) {
const error = NativeScriptSnapshotPlugin.extendError({ message: `No chunk named '${options.chunk}' found.` });
compilation.errors.push(error);
return callback();
}
this.generate(chunkToSnapshot)
.then(() => {
console.log("Successfully generated snapshots!");
return callback();
})
.catch((error) => {
const extendedError = NativeScriptSnapshotPlugin.extendError({ originalError: error });
compilation.errors.push(extendedError);
return callback();
});
}.bind(this));
}
NativeScriptSnapshotPlugin.extendError = function ({ originalError, message } = {}) {
const header = `NativeScriptSnapshot. Snapshot generation failed!\n`;
if (originalError) {
originalError.message = `${header}${originalError.message}`;
return originalError;
}
const newMessage = message ? `${header}${message}` : header;
return new Error(newMessage);
};
return NativeScriptSnapshotPlugin;
})();