Skip to content
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
30 changes: 14 additions & 16 deletions lib/virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,29 +158,27 @@ VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) {
}
};

function getStorageData(storage) {
return storage._data /* webpack 5 */ || storage.data /* webpack 4 */;
}

function getData(storage, key) {
const data = storage._data /* webpack 5 */ || storage.data /* webpack 4 */;
if (data instanceof Map) {
return storage.data.get(key);
const storageData = getStorageData(storage);
if (storageData instanceof Map) {
return storageData.get(key);
} else {
return storageData.data[key];
}
return data.data[key];
}

function setData(backendOrStorage, key, valueFactory) {
function setData(storage, key, valueFactory) {
const storageData = getStorageData(storage);
const value = valueFactory(backendOrStorage);

// Webpack v5
if (backendOrStorage._data instanceof Map) {
backendOrStorage._data.set(key, value);
} else if (backendOrStorage._data) {
backendOrStorage.data[key] = value;
} else if (backendOrStorage.data instanceof Map) {
// Webpack 4
backendOrStorage.data.set(key, value);
backendOrStorage.data.set(key, value);
if (storageData instanceof Map) {
storageData.set(key, value);
} else {
backendOrStorage.data[key] = value;
backendOrStorage.data[key] = value;
storageData.data[key] = value;
}
}

Expand Down