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 pathGenerateNativeScriptEntryPointsPlugin.js
104 lines (85 loc) · 4.05 KB
/
GenerateNativeScriptEntryPointsPlugin.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
const { RawSource } = require("webpack-sources");
const { getPackageJson } = require("../projectHelpers");
const { SNAPSHOT_ENTRY_NAME } = require("./NativeScriptSnapshotPlugin");
const path = require("path");
exports.GenerateNativeScriptEntryPointsPlugin = (function () {
const GenerationFailedError = "Unable to generate entry files.";
function GenerateNativeScriptEntryPointsPlugin(appEntryName) {
this.appEntryName = appEntryName;
this.files = {};
};
GenerateNativeScriptEntryPointsPlugin.prototype.apply = function (compiler) {
this.webpackContext = compiler.options.context;
compiler.hooks.emit.tapAsync("GenerateNativeScriptEntryPointsPlugin", (compilation, cb) => {
compilation.entrypoints.forEach(entryPoint => {
this.generateEntryFile(compilation, entryPoint);
});
this.addAsset(compilation, "package.json", this.generatePackageJson());
this.generateTnsJavaClasses(compilation);
cb();
});
}
GenerateNativeScriptEntryPointsPlugin.prototype.generateTnsJavaClasses = function (compilation) {
const path = compilation.compiler.outputPath;
const isAndroid = path.indexOf("android") > -1;
if (isAndroid && !compilation.assets["tns-java-classes.js"]) {
this.addAsset(compilation, "tns-java-classes.js", ""); 0
}
}
GenerateNativeScriptEntryPointsPlugin.prototype.generatePackageJson = function () {
const packageJson = getPackageJson(this.webpackContext);
packageJson.main = this.appEntryName;
return JSON.stringify(packageJson, null, 4);
}
GenerateNativeScriptEntryPointsPlugin.prototype.generateEntryFile = function (compilation, entryPoint) {
const entryPointName = entryPoint.options.name;
let entryChunk;
if (entryPointName === SNAPSHOT_ENTRY_NAME) {
// Do not require the snapshot entry dependencies as the snapshot will fail.
return;
}
const requiredFiles = [];
entryPoint.chunks.forEach(chunk => {
if (chunk.name === entryPointName) {
entryChunk = chunk;
} else {
chunk.files.forEach(fileName => {
if (!this.isHMRFile(fileName) && this.isSourceFile(fileName)) {
requiredFiles.push(fileName);
}
});
}
});
if (!entryChunk) {
throw new Error(`${GenerationFailedError} Entry chunk not found for entry "${entryPointName}".`);
}
entryChunk.files.forEach(filePath => {
if (!compilation.assets[filePath]) {
throw new Error(`${GenerationFailedError} File "${filePath}" not found for entry "${entryPointName}".`);
}
if (!this.isHMRFile(filePath)) {
const currFileDirRelativePath = path.dirname(filePath);
const pathToRootFromCurrFile = path.relative(currFileDirRelativePath, ".");
const requireDeps = requiredFiles.map(depPath => {
const depRelativePath = path.join(pathToRootFromCurrFile, depPath);
return `require("./${depRelativePath}");`;
}).join("");
const currentEntryFileContent = compilation.assets[filePath].source();
compilation.assets[filePath] = new RawSource(`${requireDeps}${currentEntryFileContent}`);
}
});
}
GenerateNativeScriptEntryPointsPlugin.prototype.addAsset = function (compilation, name, content) {
if (this.files[name] !== content) {
this.files[name] = content;
compilation.assets[name] = new RawSource(content);
}
}
GenerateNativeScriptEntryPointsPlugin.prototype.isHMRFile = function (fileName) {
return fileName.indexOf("hot-update") > -1;
}
GenerateNativeScriptEntryPointsPlugin.prototype.isSourceFile = function (fileName) {
return fileName.endsWith(".js");
}
return GenerateNativeScriptEntryPointsPlugin;
})();