From 274ff53256a6f6678521de4fde6f70c473da95fc Mon Sep 17 00:00:00 2001 From: DimitarTachev Date: Mon, 3 Jun 2019 15:20:13 +0300 Subject: [PATCH 1/3] fix: handle entry points with custom output filename like output.filename: "[name].custom.js" --- .../GenerateNativeScriptEntryPointsPlugin.js | 25 ++++++++++++------- plugins/NativeScriptSnapshotPlugin/index.js | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/plugins/GenerateNativeScriptEntryPointsPlugin.js b/plugins/GenerateNativeScriptEntryPointsPlugin.js index d8c689be..0ae0758c 100644 --- a/plugins/GenerateNativeScriptEntryPointsPlugin.js +++ b/plugins/GenerateNativeScriptEntryPointsPlugin.js @@ -1,6 +1,6 @@ const { RawSource } = require("webpack-sources"); const { getPackageJson } = require("../projectHelpers"); -const { SNAPSHOT_ENTRY_MODULE } = require("./NativeScriptSnapshotPlugin"); +const { SNAPSHOT_ENTRY_NAME } = require("./NativeScriptSnapshotPlugin"); exports.GenerateNativeScriptEntryPointsPlugin = (function () { function GenerateNativeScriptEntryPointsPlugin(appEntryName) { @@ -39,8 +39,9 @@ exports.GenerateNativeScriptEntryPointsPlugin = (function () { } GenerateNativeScriptEntryPointsPlugin.prototype.generateEntryFile = function (compilation, entryPoint) { - const entryPointFileName = `${entryPoint.options.name}.js`; - if (entryPointFileName === SNAPSHOT_ENTRY_MODULE) { + 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; } @@ -48,17 +49,23 @@ exports.GenerateNativeScriptEntryPointsPlugin = (function () { const requireDeps = entryPoint.chunks.map(chunk => { let requireChunkFiles = ""; - chunk.files.forEach(fileName => { - if (fileName !== entryPointFileName) { + if (chunk.name === entryPointName) { + entryChunk = chunk; + } else { + chunk.files.forEach(fileName => { requireChunkFiles += `require("./${fileName}");`; - } - }); + }); + } return requireChunkFiles; }).join("\n"); - const currentEntryPointContent = compilation.assets[entryPointFileName].source(); - compilation.assets[entryPointFileName] = new RawSource(`${requireDeps}${currentEntryPointContent}`); + if (entryChunk) { + entryChunk.files.forEach(fileName => { + const currentEntryFileContent = compilation.assets[fileName].source(); + compilation.assets[fileName] = new RawSource(`${requireDeps}${currentEntryFileContent}`); + }); + } } GenerateNativeScriptEntryPointsPlugin.prototype.addAsset = function (compilation, name, content) { diff --git a/plugins/NativeScriptSnapshotPlugin/index.js b/plugins/NativeScriptSnapshotPlugin/index.js index a285bb21..97cebd7e 100644 --- a/plugins/NativeScriptSnapshotPlugin/index.js +++ b/plugins/NativeScriptSnapshotPlugin/index.js @@ -12,7 +12,7 @@ const schema = require("./options.json"); const SNAPSHOT_ENTRY_NAME = "snapshot-entry"; const SNAPSHOT_ENTRY_MODULE = `${SNAPSHOT_ENTRY_NAME}.js`; -exports.SNAPSHOT_ENTRY_MODULE = SNAPSHOT_ENTRY_MODULE; +exports.SNAPSHOT_ENTRY_NAME = SNAPSHOT_ENTRY_NAME; exports.NativeScriptSnapshotPlugin = (function () { function NativeScriptSnapshotPlugin(options) { From 23675a4812ee41271f2b5ffe5e4c4517fa4b5d77 Mon Sep 17 00:00:00 2001 From: DimitarTachev Date: Mon, 3 Jun 2019 16:33:23 +0300 Subject: [PATCH 2/3] fix: avoid affecting the source maps by inserting new lines --- plugins/GenerateNativeScriptEntryPointsPlugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/GenerateNativeScriptEntryPointsPlugin.js b/plugins/GenerateNativeScriptEntryPointsPlugin.js index 0ae0758c..27401d53 100644 --- a/plugins/GenerateNativeScriptEntryPointsPlugin.js +++ b/plugins/GenerateNativeScriptEntryPointsPlugin.js @@ -58,7 +58,7 @@ exports.GenerateNativeScriptEntryPointsPlugin = (function () { } return requireChunkFiles; - }).join("\n"); + }).join(""); if (entryChunk) { entryChunk.files.forEach(fileName => { From 3aef461ec385292600440df070cc960c7d566fdf Mon Sep 17 00:00:00 2001 From: DimitarTachev Date: Mon, 3 Jun 2019 16:34:43 +0300 Subject: [PATCH 3/3] fix: throw errors when the compilation state is not valid in order to make the investigation easier --- .../GenerateNativeScriptEntryPointsPlugin.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/plugins/GenerateNativeScriptEntryPointsPlugin.js b/plugins/GenerateNativeScriptEntryPointsPlugin.js index 27401d53..3932f3ee 100644 --- a/plugins/GenerateNativeScriptEntryPointsPlugin.js +++ b/plugins/GenerateNativeScriptEntryPointsPlugin.js @@ -2,7 +2,10 @@ const { RawSource } = require("webpack-sources"); const { getPackageJson } = require("../projectHelpers"); const { SNAPSHOT_ENTRY_NAME } = require("./NativeScriptSnapshotPlugin"); + exports.GenerateNativeScriptEntryPointsPlugin = (function () { + const GenerationFailedError = "Unable to generate entry files."; + function GenerateNativeScriptEntryPointsPlugin(appEntryName) { this.appEntryName = appEntryName; this.files = {}; @@ -60,12 +63,18 @@ exports.GenerateNativeScriptEntryPointsPlugin = (function () { return requireChunkFiles; }).join(""); - if (entryChunk) { - entryChunk.files.forEach(fileName => { - const currentEntryFileContent = compilation.assets[fileName].source(); - compilation.assets[fileName] = new RawSource(`${requireDeps}${currentEntryFileContent}`); - }); + if (!entryChunk) { + throw new Error(`${GenerationFailedError} Entry chunk not found for entry "${entryPointName}".`); } + + entryChunk.files.forEach(fileName => { + if (!compilation.assets[fileName]) { + throw new Error(`${GenerationFailedError} File "${fileName}" not found for entry "${entryPointName}".`); + } + + const currentEntryFileContent = compilation.assets[fileName].source(); + compilation.assets[fileName] = new RawSource(`${requireDeps}${currentEntryFileContent}`); + }); } GenerateNativeScriptEntryPointsPlugin.prototype.addAsset = function (compilation, name, content) {