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

fix: handle file dependencies in non root entry modules #929

Merged
merged 1 commit into from
Jun 7, 2019
Merged
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
50 changes: 27 additions & 23 deletions plugins/GenerateNativeScriptEntryPointsPlugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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.";
Expand Down Expand Up @@ -49,38 +49,42 @@ exports.GenerateNativeScriptEntryPointsPlugin = (function () {
return;
}

const requireDeps =
entryPoint.chunks.map(chunk => {
let requireChunkFiles = "";
if (chunk.name === entryPointName) {
entryChunk = chunk;
} else {
chunk.files.forEach(fileName => {
if (!this.isHMRFile(fileName)) {
requireChunkFiles += `require("./${fileName}");`;
}
});
}

return requireChunkFiles;
}).join("");
const requiredFiles = [];
entryPoint.chunks.forEach(chunk => {
if (chunk.name === entryPointName) {
entryChunk = chunk;
} else {
chunk.files.forEach(fileName => {
if (!this.isHMRFile(fileName)) {
requiredFiles.push(fileName);
}
});
}
});

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}".`);
entryChunk.files.forEach(filePath => {
if (!compilation.assets[filePath]) {
throw new Error(`${GenerationFailedError} File "${filePath}" not found for entry "${entryPointName}".`);
}

if (!this.isHMRFile(fileName)) {
const currentEntryFileContent = compilation.assets[fileName].source();
compilation.assets[fileName] = new RawSource(`${requireDeps}${currentEntryFileContent}`);
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;
Expand Down