feat(webpack): migrate to memfs (#27652)

This commit is contained in:
Daniel Roe 2024-06-17 12:33:50 +01:00 committed by GitHub
parent e89ec33163
commit 7cec79c437
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 94 additions and 19 deletions

View File

@ -30,7 +30,7 @@
"hash-sum": "^2.0.0",
"html-webpack-plugin": "^4.5.1",
"lodash": "^4.17.21",
"memory-fs": "^0.5.0",
"memfs": "^4.9.3",
"optimize-css-assets-webpack-plugin": "^6.0.1",
"pify": "^5.0.0",
"pnp-webpack-plugin": "^1.7.0",

View File

@ -7,7 +7,7 @@ import webpackHotMiddleware from 'webpack-hot-middleware'
import consola from 'consola'
import { TARGETS, parallel, sequence, tryResolve, wrapArray, isModernRequest } from '@nuxt/utils'
import AsyncMFS from './utils/async-mfs'
import createFS from './utils/async-mfs'
import * as WebpackConfigs from './config'
import PerfLoader from './utils/perf-loader'
@ -29,7 +29,7 @@ export class WebpackBundler {
// Initialize shared MFS for dev
if (this.buildContext.options.dev) {
this.mfs = new AsyncMFS()
this.mfs = createFS()
}
}

View File

@ -1,24 +1,54 @@
import MFS from 'memory-fs'
export default class AsyncMFS extends MFS {}
import path from 'path'
import mkdirp from 'mkdirp'
import { createFsFromVolume, Volume } from 'memfs'
import { dirname } from 'upath'
const syncRegex = /Sync$/
const propsToPromisify = Object.getOwnPropertyNames(MFS.prototype).filter(n => syncRegex.test(n))
export default function createFS () {
const volume = new Volume()
const fs = createFsFromVolume(volume)
for (const prop of propsToPromisify) {
const asyncProp = prop.replace(syncRegex, '')
const origAsync = AsyncMFS.prototype[asyncProp]
fs.join = path.join.bind(path)
fs.mkdirp = mkdirp.bind(mkdirp)
AsyncMFS.prototype[asyncProp] = function (...args) {
// Callback support for webpack
if (origAsync && args.length && typeof args[args.length - 1] === 'function') {
return origAsync.call(this, ...args)
}
const propsToPromisify = Object.getOwnPropertyNames(fs).filter(n => syncRegex.test(n))
try {
return Promise.resolve(MFS.prototype[prop].call(this, ...args))
} catch (error) {
return Promise.reject(error)
const writeFileSync = fs.writeFileSync
function ensureDirSync (...args) {
if (typeof args[0] === 'string') {
const dir = dirname(args[0])
fs.mkdirSync(dir, { recursive: true })
}
}
fs.writeFileSync = function (...args) {
ensureDirSync(...args)
return writeFileSync.call(fs, ...args)
}
for (const prop of propsToPromisify) {
const asyncProp = prop.replace(syncRegex, '')
const origAsync = fs[asyncProp]
fs[asyncProp] = function (...args) {
if (asyncProp === 'writeFile') {
ensureDirSync(...args)
}
// Callback support for webpack
if (origAsync && args.length && typeof args[args.length - 1] === 'function') {
return origAsync.call(fs, ...args)
}
try {
return Promise.resolve(fs[prop](...args))
} catch (error) {
return Promise.reject(error)
}
}
}
return fs
}

View File

@ -1790,6 +1790,26 @@
"@jridgewell/resolve-uri" "^3.1.0"
"@jridgewell/sourcemap-codec" "^1.4.14"
"@jsonjoy.com/base64@^1.1.1":
version "1.1.2"
resolved "https://registry.npmjs.org/@jsonjoy.com/base64/-/base64-1.1.2.tgz#cf8ea9dcb849b81c95f14fc0aaa151c6b54d2578"
integrity sha512-q6XAnWQDIMA3+FTiOYajoYqySkO+JSat0ytXGSuRdq9uXE7o92gzuQwQM14xaCRlBLGq3v5miDGC4vkVTn54xA==
"@jsonjoy.com/json-pack@^1.0.3":
version "1.0.4"
resolved "https://registry.npmjs.org/@jsonjoy.com/json-pack/-/json-pack-1.0.4.tgz#ab59c642a2e5368e8bcfd815d817143d4f3035d0"
integrity sha512-aOcSN4MeAtFROysrbqG137b7gaDDSmVrl5mpo6sT/w+kcXpWnzhMjmY/Fh/sDx26NBxyIE7MB1seqLeCAzy9Sg==
dependencies:
"@jsonjoy.com/base64" "^1.1.1"
"@jsonjoy.com/util" "^1.1.2"
hyperdyperid "^1.2.0"
thingies "^1.20.0"
"@jsonjoy.com/util@^1.1.2":
version "1.1.3"
resolved "https://registry.npmjs.org/@jsonjoy.com/util/-/util-1.1.3.tgz#75b1c3cf21b70e665789d1ad3eabeff8b7fd1429"
integrity sha512-g//kkF4kOwUjemValCtOc/xiYzmwMRmWq3Bn+YnzOzuZLHq2PpMOxxIayN3cKbo7Ko2Np65t6D9H81IvXbXhqg==
"@lerna/create@8.1.3":
version "8.1.3"
resolved "https://registry.npmjs.org/@lerna/create/-/create-8.1.3.tgz#6cbdcc762fe5eb5dde24b34761c7bb61940fcb2a"
@ -8180,6 +8200,11 @@ humanize-ms@^1.2.1:
dependencies:
ms "^2.0.0"
hyperdyperid@^1.2.0:
version "1.2.0"
resolved "https://registry.npmjs.org/hyperdyperid/-/hyperdyperid-1.2.0.tgz#59668d323ada92228d2a869d3e474d5a33b69e6b"
integrity sha512-Y93lCzHYgGWdrJ66yIktxiaGULYc6oGiABxhcO5AufBeOyoIdZF7bIfLaOrbM0iGIOXQQgxxRrFEnb+Y6w1n4A==
iconv-lite@0.4.24, iconv-lite@^0.4.24:
version "0.4.24"
resolved "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
@ -9988,6 +10013,16 @@ memfs@^3.4.3:
dependencies:
fs-monkey "^1.0.4"
memfs@^4.9.3:
version "4.9.3"
resolved "https://registry.npmjs.org/memfs/-/memfs-4.9.3.tgz#41a3218065fe3911d9eba836250c8f4e43f816bc"
integrity sha512-bsYSSnirtYTWi1+OPMFb0M048evMKyUYe0EbtuGQgq6BVQM1g1W8/KIUJCCvjgI/El0j6Q4WsmMiBwLUBSw8LA==
dependencies:
"@jsonjoy.com/json-pack" "^1.0.3"
"@jsonjoy.com/util" "^1.1.2"
tree-dump "^1.0.1"
tslib "^2.0.0"
memory-fs@^0.4.1:
version "0.4.1"
resolved "https://registry.npmjs.org/memory-fs/-/memory-fs-0.4.1.tgz#3a9a20b8462523e447cfbc7e8bb80ed667bfc552"
@ -14266,6 +14301,11 @@ text-table@^0.2.0:
resolved "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
integrity sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==
thingies@^1.20.0:
version "1.21.0"
resolved "https://registry.npmjs.org/thingies/-/thingies-1.21.0.tgz#e80fbe58fd6fdaaab8fad9b67bd0a5c943c445c1"
integrity sha512-hsqsJsFMsV+aD4s3CWKk85ep/3I9XzYV/IXaSouJMYIoDlgyi11cBhsqYe9/geRfB0YIikBQg6raRaM+nIMP9g==
thread-loader@^3.0.4:
version "3.0.4"
resolved "https://registry.npmjs.org/thread-loader/-/thread-loader-3.0.4.tgz#c392e4c0241fbc80430eb680e4886819b504a31b"
@ -14413,6 +14453,11 @@ tr46@~0.0.3:
resolved "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==
tree-dump@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/tree-dump/-/tree-dump-1.0.1.tgz#b448758da7495580e6b7830d6b7834fca4c45b96"
integrity sha512-WCkcRBVPSlHHq1dc/px9iOfqklvzCbdRwvlNfxGZsrHqf6aZttfPrd7DJTt6oR10dwUfpFFQeVTkPbBIZxX/YA==
trim-newlines@^3.0.0:
version "3.0.1"
resolved "https://registry.npmjs.org/trim-newlines/-/trim-newlines-3.0.1.tgz#260a5d962d8b752425b32f3a7db0dcacd176c144"
@ -14462,7 +14507,7 @@ tslib@^1.9.0:
resolved "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==
tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.3.1, tslib@^2.4.0:
tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.3.1, tslib@^2.4.0:
version "2.6.3"
resolved "https://registry.npmjs.org/tslib/-/tslib-2.6.3.tgz#0438f810ad7a9edcde7a241c3d80db693c8cbfe0"
integrity sha512-xNvxJEOUiWPGhUuUdQgAJPKOOJfGnIyKySOc09XkKsgdUV/3E2zvwZYdejjmRgPCgcym1juLH3226yA7sEFJKQ==