Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

fix: handle entry points with custom output filename #922

Merged
merged 3 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions plugins/GenerateNativeScriptEntryPointsPlugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const { RawSource } = require("webpack-sources");
const { getPackageJson } = require("../projectHelpers");
const { SNAPSHOT_ENTRY_MODULE } = require("./NativeScriptSnapshotPlugin");
const { SNAPSHOT_ENTRY_NAME } = require("./NativeScriptSnapshotPlugin");


exports.GenerateNativeScriptEntryPointsPlugin = (function () {
const GenerationFailedError = "Unable to generate entry files.";

function GenerateNativeScriptEntryPointsPlugin(appEntryName) {
this.appEntryName = appEntryName;
this.files = {};
Expand Down Expand Up @@ -39,26 +42,39 @@ 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;
}

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");
}).join("");

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 currentEntryPointContent = compilation.assets[entryPointFileName].source();
compilation.assets[entryPointFileName] = new RawSource(`${requireDeps}${currentEntryPointContent}`);
const currentEntryFileContent = compilation.assets[fileName].source();
compilation.assets[fileName] = new RawSource(`${requireDeps}${currentEntryFileContent}`);
});
}

GenerateNativeScriptEntryPointsPlugin.prototype.addAsset = function (compilation, name, content) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/NativeScriptSnapshotPlugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down