Skip to content

fix: optimize the implementation #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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