webpack-virtual-modules
Advanced tools
Comparing version 0.2.2 to 0.3.0
103
index.js
@@ -75,4 +75,27 @@ var VirtualStats = require('./virtual-stats'); | ||
function createWebpackData(result) { | ||
return (function (backendOrStorage) { | ||
// In Webpack v5, this variable is a "Backend", and has the data stored in a field | ||
// _data. In V4, the `_` prefix isn't present. | ||
if (backendOrStorage._data) { | ||
var curLevelIdx = backendOrStorage._currentLevel; | ||
var curLevel = backendOrStorage._levels[curLevelIdx]; | ||
return { | ||
result: this.result, | ||
level: curLevel | ||
} | ||
} | ||
// Webpack 4 | ||
return [null, this.result] | ||
}).bind({result: result}); | ||
} | ||
function getData(storage, key) { | ||
if (storage.data instanceof Map) { | ||
// Webpack 5 | ||
if (storage._data instanceof Map) { | ||
return storage._data.get(key); | ||
} else if (storage._data) { | ||
return storage.data[key]; | ||
} else if (storage.data instanceof Map) { | ||
// Webpack v4 | ||
return storage.data.get(key); | ||
@@ -84,10 +107,54 @@ } else { | ||
function setData(storage, key, value) { | ||
if (storage.data instanceof Map) { | ||
storage.data.set(key, value); | ||
function setData(backendOrStorage, key, valueFactory) { | ||
var 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); | ||
} else { | ||
storage.data[key] = value; | ||
backendOrStorage.data[key] = value; | ||
} | ||
} | ||
function getStatStorage(fileSystem) { | ||
if (fileSystem._statStorage) { | ||
// Webpack v4 | ||
return fileSystem._statStorage; | ||
} else if (fileSystem._statBackend) { | ||
// webpack v5 | ||
return fileSystem._statBackend | ||
} else { | ||
// Unknown version? | ||
throw new Error("Couldn't find a stat storage"); | ||
} | ||
} | ||
function getFileStorage(fileSystem) { | ||
if (fileSystem._readFileStorage) { | ||
// Webpack v4 | ||
return fileSystem._readFileStorage; | ||
} else if (fileSystem._readFileBackend) { | ||
// Webpack v5 | ||
return fileSystem._readFileBackend; | ||
} else { | ||
throw new Error("Couldn't find a readFileStorage") | ||
} | ||
} | ||
function getReadDirBackend(fileSystem) { | ||
if (fileSystem._readdirBackend) { | ||
return fileSystem._readdirBackend; | ||
} else if (fileSystem._readdirStorage) { | ||
return fileSystem._readdirStorage; | ||
} else { | ||
throw new Error("Couldn't find a readDirStorage from Webpack Internals") | ||
} | ||
} | ||
VirtualModulesPlugin.prototype.apply = function(compiler) { | ||
@@ -99,2 +166,3 @@ var self = this; | ||
var afterEnvironmentHook = function() { | ||
var finalInputFileSystem = compiler.inputFileSystem; | ||
@@ -119,6 +187,9 @@ while (finalInputFileSystem && finalInputFileSystem._inputFileSystem) { | ||
finalInputFileSystem._writeVirtualFile = function(file, stats, contents) { | ||
var statStorage = getStatStorage(this); | ||
var fileStorage = getFileStorage(this); | ||
var readDirStorage = getReadDirBackend(this); | ||
this._virtualFiles = this._virtualFiles || {}; | ||
this._virtualFiles[file] = {stats: stats, contents: contents}; | ||
setData(this._statStorage, file, [null, stats]); | ||
setData(this._readFileStorage, file, [null, contents]); | ||
setData(statStorage, file, createWebpackData(stats)); | ||
setData(fileStorage, file, createWebpackData(contents)); | ||
var segments = file.split(/[\\/]/); | ||
@@ -149,11 +220,14 @@ var count = segments.length - 1; | ||
}); | ||
setData(this._readdirStorage, dir, [null, []]); | ||
setData(this._statStorage, dir, [null, dirStats]); | ||
setData(readDirStorage, dir, createWebpackData([])) | ||
setData(statStorage, dir, createWebpackData(dirStats)) | ||
} | ||
var dirData = getData(this._readdirStorage, dir); | ||
var dirData = getData(getReadDirBackend(this), dir); | ||
// Webpack v4 returns an array, webpack v5 returns an object | ||
dirData = dirData[1] || dirData.result; | ||
var filename = segments[count]; | ||
if (dirData[1].indexOf(filename) < 0) { | ||
var files = dirData[1].concat([filename]).sort(); | ||
setData(this._readdirStorage, dir, [null, files]); | ||
} else { | ||
if (dirData.indexOf(filename) < 0) { | ||
var files = dirData.concat([filename]).sort(); | ||
setData(getReadDirBackend(this), dir, createWebpackData(files)); | ||
} else { | ||
break; | ||
@@ -166,3 +240,2 @@ } | ||
} | ||
var afterResolversHook = function() { | ||
@@ -169,0 +242,0 @@ if (self._staticModules) { |
{ | ||
"name": "webpack-virtual-modules", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "Webpack Virtual Modules", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"posttest": "npm run lint", | ||
"test": "NODE_ENV=coverage nyc --check-coverage --lines 90 --branches 85 npm run tests", | ||
"test": "NODE_ENV=coverage nyc --check-coverage --lines 85 --branches 60 npm run tests", | ||
"tests": "mocha", | ||
@@ -36,3 +36,3 @@ "tests:watch": "mocha -w" | ||
"nyc": "^14.1.1", | ||
"webpack": "^4.0.0" | ||
"webpack": "^5.0.0-beta.29" | ||
}, | ||
@@ -39,0 +39,0 @@ "dependencies": { |
16463
304