Skip to content

Commit

Permalink
Update VirtualModulesPlugin from upstream
Browse files Browse the repository at this point in the history
This updates the internal VirtualModulesPlugin, which is copied from
https://github.com/sysgears/webpack-virtual-modules. I went through all
commits since this virtual.js was added, and applied them manually to
the svelte-loader version where applicable. These upstream changes
allow for using Webpack 5 with svelte-loader.

This also adds a LICENSE file to comply with the requirements of the MIT
license for the plugin files. The LICENSE file also adds in the text of
MIT license that svelte-loader is licensed under (with a different
copyright line, since svelte-loader wasn't written by SysGears)
  • Loading branch information
syvb committed Sep 9, 2020
1 parent 929eebe commit c32ecf0
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 17 deletions.
42 changes: 42 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
svelte-loader is licensed under the MIT license:
Copyright (c) 2020 svelte-loader contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

lib/virtual.js and lib/virtual-stats.js also contain code licensed under the
MIT license:
Copyright (c) 2017 SysGears

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,6 @@ module.exports = function(source, map) {
}, err => callback(err)).catch(err => {
// wrap error to provide correct
// context when logging to console
callback(new Error(`${err.name}: ${err.toString()}`));
callback(new Error(`${err.name || "Error"}: ${err.toString()}`));
});
};
2 changes: 2 additions & 0 deletions lib/virtual-stats.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Adapted from https://github.com/sysgears/webpack-virtual-modules

/**
* Used to cache a stats object for the virtual file.
* Extracted from the `mock-fs` package.
Expand Down
161 changes: 145 additions & 16 deletions lib/virtual.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
// Adapted from https://github.com/sysgears/webpack-virtual-modules

var VirtualStats = require('./virtual-stats');
var path = require("path");

var inode = 45000000;

// Adapted from https://github.com/sysgears/webpack-virtual-modules
// MIT Licensed https://github.com/sysgears/webpack-virtual-modules/blob/master/LICENSE
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) {
const curLevelIdx = backendOrStorage._currentLevel;
const curLevel = backendOrStorage._levels[curLevelIdx];
return {
result: this.result,
level: curLevel
};
}
// Webpack 4
return [null, result];
}).bind({ result: result });
}

/**
* @param {Compiler} compiler - the webpack compiler
Expand All @@ -16,24 +33,69 @@ function VirtualModulesPlugin(compiler) {

compiler.inputFileSystem.purge = function() {
if (originalPurge) {
originalPurge.call(this, arguments);
originalPurge.apply(this, arguments);
}
if (this._virtualFiles) {
Object.keys(this._virtualFiles).forEach(
function(file) {
var data = this._virtualFiles[file];
setData(this._statStorage, file, [null, data.stats]);
setData(this._readFileStorage, file, [null, data.contents]);
}.bind(this)
);
Object.keys(this._virtualFiles).forEach(function(file) {
var data = this._virtualFiles[file];
this._writeVirtualFile(file, data.stats, data.contents);
}.bind(this));
}
};

compiler.inputFileSystem._writeVirtualFile = function(file, stats, contents) {
const statStorage = getStatStorage(this);
const fileStorage = getFileStorage(this);
const 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(/[\\/]/);
var count = segments.length - 1;
var minCount = segments[0] ? 1 : 0;
while (count > minCount) {
var dir = segments.slice(0, count).join(path.sep) || path.sep;
try {
compiler.inputFileSystem.readdirSync(dir);
} catch (e) {
var time = new Date();
var timeMs = time.getTime();
var dirStats = new VirtualStats({
dev: 8675309,
nlink: 0,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: inode++,
mode: 16877,
size: stats.size,
blocks: Math.floor(stats.size / 4096),
atimeMs: timeMs,
mtimeMs: timeMs,
ctimeMs: timeMs,
birthtimeMs: timeMs,
atime: time,
mtime: time,
ctime: time,
birthtime: time
});
setData(readDirStorage, dir, createWebpackData([]));
setData(statStorage, dir, createWebpackData(dirStats));
}
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.indexOf(filename) < 0) {
var files = dirData.concat([filename]).sort();
setData(getReadDirBackend(this), dir, createWebpackData(files));
} else {
break;
}
count--;
}
};
}

Expand Down Expand Up @@ -75,14 +137,81 @@ VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) {
birthtime: time
});

// When using the WatchIgnorePlugin (https://github.com/webpack/webpack/blob/52184b897f40c75560b3630e43ca642fcac7e2cf/lib/WatchIgnorePlugin.js),
// the original watchFileSystem is stored in `wfs`. The following "unwraps" the ignoring
// wrappers, giving us access to the "real" watchFileSystem.
let finalWatchFileSystem = this._watcher && this._watcher.watchFileSystem;

while (finalWatchFileSystem && finalWatchFileSystem.wfs) {
finalWatchFileSystem = finalWatchFileSystem.wfs;
}
this.compiler.inputFileSystem._writeVirtualFile(filePath, stats, contents);
};

function setData(storage, key, value) {
if (storage.data instanceof Map) {
storage.data.set(key, value);
function getData(storage, key) {
// 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);
} else {
return storage.data[key];
}
}

function setData(backendOrStorage, key, valueFactory) {
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);
} else {
backendOrStorage.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 {
storage.data[key] = value;
throw new Error("Couldn't find a readDirStorage from Webpack Internals");
}
}

Expand Down

0 comments on commit c32ecf0

Please sign in to comment.