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
172 lines (140 loc) · 6.76 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const { relative, resolve, join } = require("path");
const { closeSync, openSync, writeFileSync } = require("fs");
const validateOptions = require("schema-utils");
const ProjectSnapshotGenerator = require("../../snapshot/android/project-snapshot-generator");
const { getPackageJson } = require("../../projectHelpers");
const {
ANDROID_PROJECT_DIR,
ANDROID_APP_PATH,
} = require("../../androidProjectHelpers");
const schema = require("./options.json");
const SNAPSHOT_ENTRY_NAME = "snapshot-entry";
const SNAPSHOT_ENTRY_MODULE = `${SNAPSHOT_ENTRY_NAME}.js`;
exports.SNAPSHOT_ENTRY_NAME = SNAPSHOT_ENTRY_NAME;
exports.NativeScriptSnapshotPlugin = (function () {
function NativeScriptSnapshotPlugin(options) {
NativeScriptSnapshotPlugin.validateSchema(options);
ProjectSnapshotGenerator.call(this, options);
const { webpackConfig } = this.options;
NativeScriptSnapshotPlugin.removeLibraryTarget(webpackConfig);
const { entry } = webpackConfig;
if (typeof entry === "string" || Array.isArray(entry)) {
webpackConfig.entry = { bundle: entry };
}
NativeScriptSnapshotPlugin.ensureSnapshotModuleEntry(this.options);
}
NativeScriptSnapshotPlugin.removeLibraryTarget = function (webpackConfig) {
const { output } = webpackConfig;
if (output) {
output.libraryTarget = undefined;
}
}
NativeScriptSnapshotPlugin.ensureSnapshotModuleEntry = function (options) {
const { webpackConfig, requireModules, chunks, includeApplicationCss } = options;
const internalRequireModules = this.getInternalRequireModules(webpackConfig.context);
const snapshotEntryPath = join(ANDROID_PROJECT_DIR, SNAPSHOT_ENTRY_MODULE);
let snapshotEntryContent = "";
if (includeApplicationCss) {
snapshotEntryContent += `
require("${
options.angular ?
'nativescript-dev-webpack/load-application-css-angular' :
'nativescript-dev-webpack/load-application-css-regular'
}")();
`;
}
snapshotEntryContent += [...requireModules, ...internalRequireModules]
.map(mod => `require('${mod}')`).join(";");
writeFileSync(snapshotEntryPath, snapshotEntryContent, { encoding: "utf8" });
// add the module to the entry points to make sure it's content is evaluated
webpackConfig.entry[SNAPSHOT_ENTRY_NAME] = relative(webpackConfig.context, snapshotEntryPath);
// prepend the module to the script that will be snapshotted
chunks.unshift(SNAPSHOT_ENTRY_NAME);
// ensure that the runtime is installed only in the snapshotted chunk
webpackConfig.optimization.runtimeChunk = { name: SNAPSHOT_ENTRY_NAME };
}
NativeScriptSnapshotPlugin.getInternalRequireModules = function (webpackContext) {
const packageJson = getPackageJson(webpackContext);
return (packageJson && packageJson["android"] && packageJson["android"]["requireModules"]) || [];
}
NativeScriptSnapshotPlugin.validateSchema = function (options) {
if (!options.chunk && !options.chunks) {
const error = NativeScriptSnapshotPlugin.extendError({ message: `No chunks specified!` });
throw error;
}
try {
validateOptions(schema, options, "NativeScriptSnapshotPlugin");
if (options.chunk) {
options.chunks = options.chunks || [];
options.chunks.push(options.chunk);
}
} catch (error) {
throw new Error(error.message);
}
}
NativeScriptSnapshotPlugin.prototype = Object.create(ProjectSnapshotGenerator.prototype);
NativeScriptSnapshotPlugin.prototype.constructor = NativeScriptSnapshotPlugin;
NativeScriptSnapshotPlugin.prototype.generate = function (webpackChunks) {
const options = this.options;
if (options.skipSnapshotTools) {
console.log(`Skipping snapshot tools.`);
return Promise.resolve();
}
const inputFiles = webpackChunks.map(chunk => join(options.webpackConfig.output.path, chunk.files[0]));
const preprocessedInputFile = join(
this.options.projectRoot,
ANDROID_APP_PATH,
"_embedded_script_.js"
);
console.log(`\n Snapshotting bundle from ${inputFiles}`);
return ProjectSnapshotGenerator.prototype.generate.call(this, {
inputFiles,
preprocessedInputFile,
targetArchs: options.targetArchs,
useLibs: options.useLibs,
androidNdkPath: options.androidNdkPath,
v8Version: options.v8Version,
snapshotInDocker: options.snapshotInDocker,
skipSnapshotTools: options.skipSnapshotTools
}).then(() => {
// Make the original files empty
inputFiles.forEach(inputFile =>
closeSync(openSync(inputFile, "w")) // truncates the input file content
);
});
}
NativeScriptSnapshotPlugin.prototype.apply = function (compiler) {
const options = this.options;
compiler.hooks.afterEmit.tapAsync("NativeScriptSnapshotPlugin", function (compilation, callback) {
const chunksToSnapshot = options.chunks
.map(name => ({ name, chunk: compilation.chunks.find(chunk => chunk.name === name) }));
const unexistingChunks = chunksToSnapshot.filter(pair => !pair.chunk);
if (unexistingChunks.length) {
const message = `The following chunks does not exist: ` + unexistingChunks.map(pair => pair.name).join(", ");
const error = NativeScriptSnapshotPlugin.extendError({ message });
compilation.errors.push(error);
return callback();
}
this.generate(chunksToSnapshot.map(pair => pair.chunk))
.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;
})();