-
Notifications
You must be signed in to change notification settings - Fork 72
Update VirtualModulesPlugin from upstream #136
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8974856
Update VirtualModulesPlugin from upstream
syvb 8b5ac4f
Use single quotes
syvb 6c50e32
Use double quotes when needed to avoid escapes
syvb d1dc851
Integrate Webpack 5 watch mode from upstream
syvb 0ef2ded
Get rid of all usage of var
syvb 327bb22
Code improvements
syvb 1794d51
extract function for creating VirtualStats
syvb 0794b95
fix: optimize the implementation
deleonio 1993448
Merge pull request #1 from martinoppitz/help/update-virtual-modules
syvb ac1304c
fix: 'backendOrStorage' is not defined no-undef
deleonio 1d50ad9
Merge pull request #3 from martinoppitz/help/update-virtual-modules
syvb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,30 @@ | ||
// 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 }); | ||
} | ||
|
||
function getModulePath(filePath, compiler) { | ||
return path.isAbsolute(filePath) ? filePath : path.join(compiler.context, filePath); | ||
} | ||
|
||
/** | ||
* @param {Compiler} compiler - the webpack compiler | ||
|
@@ -16,24 +37,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) { | ||
syvb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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, | ||
syvb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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--; | ||
} | ||
}; | ||
} | ||
|
||
|
@@ -74,15 +140,104 @@ VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) { | |
ctime: time, | ||
birthtime: time | ||
}); | ||
var modulePath = getModulePath(filePath, this.compiler); | ||
|
||
// 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); | ||
if (finalWatchFileSystem && | ||
(finalWatchFileSystem.watcher.fileWatchers.size || | ||
finalWatchFileSystem.watcher.fileWatchers.length) | ||
) { | ||
var fileWatchers = finalWatchFileSystem.watcher.fileWatchers instanceof Map ? | ||
Array.from(finalWatchFileSystem.watcher.fileWatchers.values()) : | ||
finalWatchFileSystem.watcher.fileWatchers; | ||
fileWatchers.forEach(function(fileWatcher) { | ||
if (fileWatcher.path === modulePath) { | ||
delete fileWatcher.directoryWatcher._cachedTimeInfoEntries; | ||
fileWatcher.directoryWatcher.setFileTime( | ||
filePath, | ||
time, | ||
false, | ||
false, | ||
null | ||
); | ||
fileWatcher.emit("change", time, null); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
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) { | ||
syvb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
} else { | ||
return storage.data[key]; | ||
} | ||
} | ||
|
||
function setData(backendOrStorage, key, valueFactory) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation is not nice. Better looks like getData, a webpack switch and a generic code |
||
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"); | ||
syvb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.