|
| 1 | +const { RawSource } = require("webpack-sources"); |
| 2 | +const { getPackageJson } = require("../projectHelpers"); |
| 3 | +const { SNAPSHOT_ENTRY_NAME } = require("./NativeScriptSnapshotPlugin"); |
| 4 | + |
| 5 | + |
| 6 | +exports.GenerateNativeScriptEntryPointsPlugin = (function () { |
| 7 | + const GenerationFailedError = "Unable to generate entry files."; |
| 8 | + |
| 9 | + function GenerateNativeScriptEntryPointsPlugin(appEntryName) { |
| 10 | + this.appEntryName = appEntryName; |
| 11 | + this.files = {}; |
| 12 | + }; |
| 13 | + |
| 14 | + GenerateNativeScriptEntryPointsPlugin.prototype.apply = function (compiler) { |
| 15 | + this.webpackContext = compiler.options.context; |
| 16 | + |
| 17 | + compiler.hooks.emit.tapAsync("GenerateNativeScriptEntryPointsPlugin", (compilation, cb) => { |
| 18 | + compilation.entrypoints.forEach(entryPoint => { |
| 19 | + this.generateEntryFile(compilation, entryPoint); |
| 20 | + }); |
| 21 | + this.addAsset(compilation, "package.json", this.generatePackageJson()); |
| 22 | + this.generateTnsJavaClasses(compilation); |
| 23 | + |
| 24 | + cb(); |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + GenerateNativeScriptEntryPointsPlugin.prototype.generateTnsJavaClasses = function (compilation) { |
| 29 | + const path = compilation.compiler.outputPath; |
| 30 | + const isAndroid = path.indexOf("android") > -1; |
| 31 | + |
| 32 | + if (isAndroid && !compilation.assets["tns-java-classes.js"]) { |
| 33 | + this.addAsset(compilation, "tns-java-classes.js", ""); 0 |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + GenerateNativeScriptEntryPointsPlugin.prototype.generatePackageJson = function () { |
| 38 | + const packageJson = getPackageJson(this.webpackContext); |
| 39 | + packageJson.main = this.appEntryName; |
| 40 | + |
| 41 | + return JSON.stringify(packageJson, null, 4); |
| 42 | + } |
| 43 | + |
| 44 | + GenerateNativeScriptEntryPointsPlugin.prototype.generateEntryFile = function (compilation, entryPoint) { |
| 45 | + const entryPointName = entryPoint.options.name; |
| 46 | + let entryChunk; |
| 47 | + if (entryPointName === SNAPSHOT_ENTRY_NAME) { |
| 48 | + // Do not require the snapshot entry dependencies as the snapshot will fail. |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const requireDeps = |
| 53 | + entryPoint.chunks.map(chunk => { |
| 54 | + let requireChunkFiles = ""; |
| 55 | + if (chunk.name === entryPointName) { |
| 56 | + entryChunk = chunk; |
| 57 | + } else { |
| 58 | + chunk.files.forEach(fileName => { |
| 59 | + if (!this.isHMRFile(fileName)) { |
| 60 | + requireChunkFiles += `require("./${fileName}");`; |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + return requireChunkFiles; |
| 66 | + }).join(""); |
| 67 | + |
| 68 | + if (!entryChunk) { |
| 69 | + throw new Error(`${GenerationFailedError} Entry chunk not found for entry "${entryPointName}".`); |
| 70 | + } |
| 71 | + |
| 72 | + entryChunk.files.forEach(fileName => { |
| 73 | + if (!compilation.assets[fileName]) { |
| 74 | + throw new Error(`${GenerationFailedError} File "${fileName}" not found for entry "${entryPointName}".`); |
| 75 | + } |
| 76 | + |
| 77 | + if (!this.isHMRFile(fileName)) { |
| 78 | + const currentEntryFileContent = compilation.assets[fileName].source(); |
| 79 | + compilation.assets[fileName] = new RawSource(`${requireDeps}${currentEntryFileContent}`); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + GenerateNativeScriptEntryPointsPlugin.prototype.addAsset = function (compilation, name, content) { |
| 85 | + if (this.files[name] !== content) { |
| 86 | + this.files[name] = content; |
| 87 | + compilation.assets[name] = new RawSource(content); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + GenerateNativeScriptEntryPointsPlugin.prototype.isHMRFile = function (fileName) { |
| 92 | + return fileName.indexOf("hot-update") > -1; |
| 93 | + } |
| 94 | + |
| 95 | + return GenerateNativeScriptEntryPointsPlugin; |
| 96 | +})(); |
0 commit comments