**fix(cache): remove outdated ES module compatibility solution and enhance shims for 'os' and 'url' modules**
- Removed `SOLUTION.md` as it contained an outdated explanation of ES module compatibility fixes for cache detection.
- Updated `rollup.config.js`:
- Added detailed shims for 'url' and 'os' modules to ensure proper functionality in browser environments:
- **OS Shim**: Provided reasonable defaults for `totalmem()`, `freemem()`, `platform()`, and `arch()` to maintain compatibility and consistency.
- **URL Shim**: Added `fileURLToPath` and `pathToFileURL` to handle browser-specific file paths.
- Updated `nodeBuiltins` to include `'node:url'` and `'node:os'` for handling edge cases in module resolution.
**Purpose**: Streamline codebase by removing redundant documentation and enhancing compatibility shims for ES modules, ensuring seamless operation in browser environments.
This commit is contained in:
parent
42571c5883
commit
d8de4083f5
3 changed files with 88 additions and 81 deletions
|
|
@ -18,10 +18,14 @@ const nodeModuleShims = () => {
|
|||
'path',
|
||||
'util',
|
||||
'child_process',
|
||||
'url',
|
||||
'os',
|
||||
'node:fs',
|
||||
'node:path',
|
||||
'node:util',
|
||||
'node:child_process'
|
||||
'node:child_process',
|
||||
'node:url',
|
||||
'node:os'
|
||||
]
|
||||
|
||||
if (nodeBuiltins.includes(source)) {
|
||||
|
|
@ -67,6 +71,61 @@ export const promises = {};
|
|||
`
|
||||
}
|
||||
|
||||
// Special handling for url module
|
||||
if (moduleName === 'url' || moduleName === 'node:url') {
|
||||
return `
|
||||
// URL shim for browser environments
|
||||
const fileURLToPath = (url) => {
|
||||
if (typeof url === 'string') {
|
||||
// Simple conversion for file:// URLs
|
||||
if (url.startsWith('file://')) {
|
||||
return url.slice(7); // Remove 'file://' prefix
|
||||
}
|
||||
return url;
|
||||
}
|
||||
return url.pathname || url.toString();
|
||||
};
|
||||
|
||||
const pathToFileURL = (path) => {
|
||||
return new URL('file://' + path);
|
||||
};
|
||||
|
||||
export default { fileURLToPath, pathToFileURL };
|
||||
export { fileURLToPath, pathToFileURL };
|
||||
`
|
||||
}
|
||||
|
||||
// Special handling for os module
|
||||
if (moduleName === 'os' || moduleName === 'node:os') {
|
||||
return `
|
||||
// OS shim for browser environments
|
||||
const totalmem = () => {
|
||||
// Return a reasonable default for browser environments (8GB)
|
||||
return 8 * 1024 * 1024 * 1024;
|
||||
};
|
||||
|
||||
const freemem = () => {
|
||||
// Return a reasonable default for browser environments (4GB)
|
||||
return 4 * 1024 * 1024 * 1024;
|
||||
};
|
||||
|
||||
const platform = () => {
|
||||
if (typeof navigator !== 'undefined') {
|
||||
return navigator.platform.toLowerCase().includes('win') ? 'win32' :
|
||||
navigator.platform.toLowerCase().includes('mac') ? 'darwin' : 'linux';
|
||||
}
|
||||
return 'linux';
|
||||
};
|
||||
|
||||
const arch = () => {
|
||||
return 'x64'; // Default architecture for browser
|
||||
};
|
||||
|
||||
export default { totalmem, freemem, platform, arch };
|
||||
export { totalmem, freemem, platform, arch };
|
||||
`
|
||||
}
|
||||
|
||||
// Default empty shim for other modules
|
||||
return 'export default {}; export const promises = {};'
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue