Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion node-modules-polyfill/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
"author": "Tommaso De Rossi, morse <beats.by.morse@gmail.com>",
"license": "ISC",
"devDependencies": {
"rollup": "^2.75.7",
"safe-buffer": "^5.2.1",
"test-support": "*",
"@esbuild-plugins/node-globals-polyfill": "*"
},
"dependencies": {
"escape-string-regexp": "^4.0.0",
"rollup-plugin-node-polyfills": "^0.2.1"
"rollup-plugin-polyfill-node": "^0.12.0"
},
"peerDependencies": {
"esbuild": "*"
Expand Down
18 changes: 15 additions & 3 deletions node-modules-polyfill/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ test('works', async () => {
paths: [ENTRY],
} = await writeFiles({
'entry.ts': `import {x} from './utils'; console.log(x);`,
'utils.ts': `import path from 'path'; import { Buffer } from 'buffer'; export const x = path.resolve(Buffer.from('x').toString());`,
'utils.ts': `
import path from 'path';
import { Buffer } from 'buffer';
import { Writable } from 'stream';
import { request } from 'http';

export const x = new Writable({
write() {
const path = path.resolve(Buffer.from('x').toString());
const call = request(path)
}
});
`,
})
// const outfile = randomOutputFile()
const res = await build({
Expand All @@ -23,8 +35,8 @@ test('works', async () => {
bundle: true,
plugins: [NodeModulesPolyfillsPlugin()],
})
eval(res.outputFiles[0].text)
// console.log(res.outputFiles[0].text)
//eval(res.outputFiles[0].text) -- need jsdom or equiv for globalThis.XMLHttpRequest
console.log(res.outputFiles[0].text)
unlink()
})

Expand Down
47 changes: 35 additions & 12 deletions node-modules-polyfill/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { OnResolveArgs, Plugin } from 'esbuild'
import type { OnResolveArgs, Plugin } from 'esbuild'
import esbuild from 'esbuild'
import escapeStringRegexp from 'escape-string-regexp'
import fs from 'fs'
import path from 'path'
import esbuild from 'esbuild'
import { builtinsPolyfills } from './polyfills'
import { getModules as builtinsPolyfills } from 'rollup-plugin-polyfill-node/dist/modules'
import polyfills from 'rollup-plugin-polyfill-node/dist/polyfills'

// import { NodeResolvePlugin } from '@esbuild-plugins/node-resolve'
const NAME = 'node-modules-polyfills'
const NAMESPACE = NAME

Expand Down Expand Up @@ -51,13 +50,10 @@ export function NodeModulesPolyfillPlugin(
const argsPath = args.path.replace(/^node:/, '')
const isCommonjs = args.namespace.endsWith('commonjs')

const resolved = polyfilledBuiltins.get(
removeEndingSlash(argsPath),
)
const contents = await (
await fs.promises.readFile(resolved)
).toString()
let resolveDir = path.dirname(resolved)
const key = removeEndingSlash(argsPath)
const contents =
polyfilledBuiltins.get(key) || polyfills[`${key}.js`]
const resolveDir = path.dirname(key)

if (isCommonjs) {
return {
Expand Down Expand Up @@ -91,6 +87,33 @@ export function NodeModulesPolyfillPlugin(
.map(escapeStringRegexp)
.join('|'), // TODO builtins could end with slash, keep in mind in regex
)

const prefixFilter = new RegExp(`^.*\0polyfill-node\.`)
onResolve({ filter: prefixFilter }, (args) => {
const path = args.path.replace(prefixFilter, '')
const isCommonjs =
args.namespace !== commonjsNamespace &&
args.kind === 'require-call'
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this from the other resolver but didn't really take the time to understand fully what it is doing so may need some consideration.


return {
namespace: isCommonjs ? commonjsNamespace : namespace,
path,
}
})
const localResolver = (args: OnResolveArgs) => {
return {
namespace: args.namespace,
path: `${path.basename(
args.resolveDir,
)}/${args.path.replace('./', '')}`,
}
}
onResolve({ filter: /\.\//, namespace }, localResolver)
onResolve(
{ filter: /\.\//, namespace: commonjsNamespace },
localResolver,
)

async function resolver(args: OnResolveArgs) {
const argsPath = args.path.replace(/^node:/, '')
const ignoreRequire = args.namespace === commonjsNamespace
Expand Down
151 changes: 0 additions & 151 deletions node-modules-polyfill/src/polyfills.ts

This file was deleted.

Loading