generated from imranbarbhuiya/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: port modern polyfill code to add caching in internal call (#122)
- Loading branch information
1 parent
c345cf6
commit ed22079
Showing
9 changed files
with
110 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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,5 +1,8 @@ | ||
{ | ||
"root": true, | ||
"extends": ["mahir/common", "mahir/node", "mahir/typescript", "mahir/prettier"], | ||
"ignorePatterns": ["**/dist/*"] | ||
"ignorePatterns": ["**/dist/*"], | ||
"rules": { | ||
"unicorn/prefer-string-replace-all": "off" | ||
} | ||
} |
This file contains 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 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 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,84 @@ | ||
/** | ||
* `polyfillPath` and `getCachedPolyfillContent` are taken from below source with some modifications for my use case. | ||
* https://github.com/Aslemammad/modern-node-polyfills | ||
* @author Aslemammad | ||
* @license MIT | ||
*/ | ||
|
||
import { builtinModules } from 'node:module'; | ||
import { resolve, join } from 'node:path'; | ||
|
||
import { build } from 'esbuild'; | ||
import { loadPackageJSON, resolveModule } from 'local-pkg'; | ||
import { resolve as resolveExports } from 'resolve.exports'; | ||
|
||
import { normalizeNodeBuiltinPath } from './utils/util.js'; | ||
|
||
async function polyfillPath(importPath: string) { | ||
if (!builtinModules.includes(importPath)) | ||
throw new Error(`Node.js does not have ${importPath} in its builtin modules`); | ||
|
||
const jspmPath = resolve( | ||
require.resolve(`@jspm/core/nodelibs/${importPath}`), | ||
// ensure "fs/promises" is resolved properly | ||
'../../..' + (importPath.includes('/') ? '/..' : ''), | ||
); | ||
|
||
const jspmPackageJson = await loadPackageJSON(jspmPath); | ||
const exportPath = resolveExports(jspmPackageJson, `./nodelibs/${importPath}`, { | ||
browser: true, | ||
}); | ||
|
||
const exportFullPath = resolveModule(join(jspmPath, exportPath?.[0] ?? '')); | ||
|
||
if (!exportPath || !exportFullPath) { | ||
throw new Error( | ||
'resolving failed, please try creating an issue in https://github.com/imranbarbhuiya/esbuild-plugins-node-modules-polyfill', | ||
); | ||
} | ||
|
||
return exportFullPath; | ||
} | ||
|
||
const polyfillPathCache: Map<string, Promise<string>> = new Map(); | ||
export const getCachedPolyfillPath = (importPath: string): Promise<string> => { | ||
const normalizedImportPath = normalizeNodeBuiltinPath(importPath); | ||
|
||
const cachedPromise = polyfillPathCache.get(normalizedImportPath); | ||
if (cachedPromise) { | ||
return cachedPromise; | ||
} | ||
|
||
const promise = polyfillPath(normalizedImportPath); | ||
polyfillPathCache.set(normalizedImportPath, promise); | ||
return promise; | ||
}; | ||
|
||
export const polyfillContentAndTransform = async (importPath: string) => { | ||
const exportFullPath = await getCachedPolyfillPath(importPath); | ||
|
||
const content = ( | ||
await build({ | ||
write: false, | ||
format: 'esm', | ||
bundle: true, | ||
entryPoints: [exportFullPath], | ||
}) | ||
).outputFiles[0]!.text; | ||
|
||
return content.replace(/eval\(/g, '(0,eval)('); | ||
}; | ||
|
||
const polyfillContentCache: Map<string, Promise<string>> = new Map(); | ||
export const getCachedPolyfillContent = (_importPath: string): Promise<string> => { | ||
const normalizedImportPath = normalizeNodeBuiltinPath(_importPath); | ||
|
||
const cachedPromise = polyfillContentCache.get(normalizedImportPath); | ||
if (cachedPromise) { | ||
return cachedPromise; | ||
} | ||
|
||
const promise = polyfillContentAndTransform(normalizedImportPath); | ||
polyfillContentCache.set(normalizedImportPath, promise); | ||
return promise; | ||
}; |
This file contains 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 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,6 +1,5 @@ | ||
// eslint-disable-next-line no-restricted-globals, n/prefer-global/process | ||
/* eslint-disable no-restricted-globals, n/prefer-global/process */ | ||
console.log(process.version); | ||
|
||
// Ensure that environment variables can still be injected via `define` | ||
// eslint-disable-next-line no-restricted-globals, n/prefer-global/process | ||
console.log(process.env.NODE_ENV); |
This file contains 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 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 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