diff --git a/node-modules-polyfill/package.json b/node-modules-polyfill/package.json index d9a6e41..60a60a6 100644 --- a/node-modules-polyfill/package.json +++ b/node-modules-polyfill/package.json @@ -19,13 +19,14 @@ "author": "Tommaso De Rossi, morse ", "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": "*" diff --git a/node-modules-polyfill/src/index.test.ts b/node-modules-polyfill/src/index.test.ts index 92792e6..e6ca7b3 100644 --- a/node-modules-polyfill/src/index.test.ts +++ b/node-modules-polyfill/src/index.test.ts @@ -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({ @@ -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() }) diff --git a/node-modules-polyfill/src/index.ts b/node-modules-polyfill/src/index.ts index f7397e4..4b1e719 100644 --- a/node-modules-polyfill/src/index.ts +++ b/node-modules-polyfill/src/index.ts @@ -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 @@ -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 { @@ -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' + + 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 diff --git a/node-modules-polyfill/src/polyfills.ts b/node-modules-polyfill/src/polyfills.ts deleted file mode 100644 index cea5f8d..0000000 --- a/node-modules-polyfill/src/polyfills.ts +++ /dev/null @@ -1,151 +0,0 @@ -// Taken from https://github.com/ionic-team/rollup-plugin-node-polyfills/blob/master/src/modules.ts - -import { NodePolyfillsOptions } from '.' - -const EMPTY_PATH = require.resolve( - 'rollup-plugin-node-polyfills/polyfills/empty.js', -) - -export function builtinsPolyfills() { - const libs = new Map() - - libs.set( - 'process', - require.resolve('rollup-plugin-node-polyfills/polyfills/process-es6'), - ) - libs.set( - 'buffer', - require.resolve('rollup-plugin-node-polyfills/polyfills/buffer-es6'), - ) - libs.set( - 'util', - require.resolve('rollup-plugin-node-polyfills/polyfills/util'), - ) - libs.set('sys', libs.get('util')) - libs.set( - 'events', - require.resolve('rollup-plugin-node-polyfills/polyfills/events'), - ) - libs.set( - 'stream', - require.resolve('rollup-plugin-node-polyfills/polyfills/stream'), - ) - libs.set( - 'path', - require.resolve('rollup-plugin-node-polyfills/polyfills/path'), - ) - libs.set( - 'querystring', - require.resolve('rollup-plugin-node-polyfills/polyfills/qs'), - ) - libs.set( - 'punycode', - require.resolve('rollup-plugin-node-polyfills/polyfills/punycode'), - ) - libs.set( - 'url', - require.resolve('rollup-plugin-node-polyfills/polyfills/url'), - ) - libs.set( - 'string_decoder', - require.resolve( - 'rollup-plugin-node-polyfills/polyfills/string-decoder', - ), - ) - libs.set( - 'http', - require.resolve('rollup-plugin-node-polyfills/polyfills/http'), - ) - libs.set( - 'https', - require.resolve('rollup-plugin-node-polyfills/polyfills/http'), - ) - libs.set('os', require.resolve('rollup-plugin-node-polyfills/polyfills/os')) - libs.set( - 'assert', - require.resolve('rollup-plugin-node-polyfills/polyfills/assert'), - ) - libs.set( - 'constants', - require.resolve('rollup-plugin-node-polyfills/polyfills/constants'), - ) - libs.set( - '_stream_duplex', - require.resolve( - 'rollup-plugin-node-polyfills/polyfills/readable-stream/duplex', - ), - ) - libs.set( - '_stream_passthrough', - require.resolve( - 'rollup-plugin-node-polyfills/polyfills/readable-stream/passthrough', - ), - ) - libs.set( - '_stream_readable', - require.resolve( - 'rollup-plugin-node-polyfills/polyfills/readable-stream/readable', - ), - ) - libs.set( - '_stream_writable', - require.resolve( - 'rollup-plugin-node-polyfills/polyfills/readable-stream/writable', - ), - ) - libs.set( - '_stream_transform', - require.resolve( - 'rollup-plugin-node-polyfills/polyfills/readable-stream/transform', - ), - ) - libs.set( - 'timers', - require.resolve('rollup-plugin-node-polyfills/polyfills/timers'), - ) - libs.set( - 'console', - require.resolve('rollup-plugin-node-polyfills/polyfills/console'), - ) - libs.set('vm', require.resolve('rollup-plugin-node-polyfills/polyfills/vm')) - libs.set( - 'zlib', - require.resolve('rollup-plugin-node-polyfills/polyfills/zlib'), - ) - libs.set( - 'tty', - require.resolve('rollup-plugin-node-polyfills/polyfills/tty'), - ) - libs.set( - 'domain', - require.resolve('rollup-plugin-node-polyfills/polyfills/domain'), - ) - - // not shimmed - libs.set('dns', EMPTY_PATH) - libs.set('dgram', EMPTY_PATH) - libs.set('child_process', EMPTY_PATH) - libs.set('cluster', EMPTY_PATH) - libs.set('module', EMPTY_PATH) - libs.set('net', EMPTY_PATH) - libs.set('readline', EMPTY_PATH) - libs.set('repl', EMPTY_PATH) - libs.set('tls', EMPTY_PATH) - libs.set('fs', EMPTY_PATH) - libs.set('crypto', EMPTY_PATH) - - // libs.set( - // 'fs', - // require.resolve('rollup-plugin-node-polyfills/polyfills/browserify-fs'), - // ) - - // TODO enable crypto and fs https://github.com/ionic-team/rollup-plugin-node-polyfills/issues/20 - // libs.set( - // 'crypto', - // require.resolve( - // 'rollup-plugin-node-polyfills/polyfills/crypto-browserify', - // ), - // ) - - return libs -} diff --git a/yarn.lock b/yarn.lock index 6953fbc..79162d2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -767,6 +767,11 @@ "@types/yargs" "^15.0.0" chalk "^4.0.0" +"@jridgewell/sourcemap-codec@^1.4.13": + version "1.4.14" + resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.14.tgz#add4c98d341472a289190b424efbdb096991bb24" + integrity sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw== + "@manypkg/find-root@^1.1.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@manypkg/find-root/-/find-root-1.1.0.tgz#a62d8ed1cd7e7d4c11d9d52a8397460b5d4ad29f" @@ -809,6 +814,24 @@ "@nodelib/fs.scandir" "2.1.3" fastq "^1.6.0" +"@rollup/plugin-inject@^5.0.1": + version "5.0.3" + resolved "https://registry.yarnpkg.com/@rollup/plugin-inject/-/plugin-inject-5.0.3.tgz#0783711efd93a9547d52971db73b2fb6140a67b1" + integrity sha512-411QlbL+z2yXpRWFXSmw/teQRMkXcAAC8aYTemc15gwJRpvEVDQwoe+N/HTFD8RFG8+88Bme9DK2V9CVm7hJdA== + dependencies: + "@rollup/pluginutils" "^5.0.1" + estree-walker "^2.0.2" + magic-string "^0.27.0" + +"@rollup/pluginutils@^5.0.1": + version "5.0.2" + resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-5.0.2.tgz#012b8f53c71e4f6f9cb317e311df1404f56e7a33" + integrity sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA== + dependencies: + "@types/estree" "^1.0.0" + estree-walker "^2.0.2" + picomatch "^2.3.1" + "@sinonjs/commons@^1.7.0": version "1.8.1" resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.1.tgz#e7df00f98a203324f6dc7cc606cad9d4a8ab2217" @@ -863,6 +886,11 @@ dependencies: "@babel/types" "^7.3.0" +"@types/estree@^1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.0.tgz#5fb2e536c1ae9bf35366eed879e827fa59ca41c2" + integrity sha512-WulqXMDUTYAXCjZnk6JtIHPigp55cVtDgDrO2gHRwhyJto21+1zbVCtOYB2L1F9w4qCQ0rOGWBnBe0FNTiEJIQ== + "@types/fs-extra@^9.0.4": version "9.0.4" resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.4.tgz#12553138cf0438db9a31cdc8b0a3aa9332eb67aa" @@ -1885,10 +1913,10 @@ estraverse@^4.2.0: resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.3.0.tgz#398ad3f3c5a24948be7725e83d11a7de28cdbd1d" integrity sha512-39nnKffWz8xN1BU/2c79n9nB9HDzo0niYUqx6xyqUnyoAnQyyWpOTdZEeiCch8BBu515t4wp9ZmgVfVhn9EBpw== -estree-walker@^0.6.1: - version "0.6.1" - resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362" - integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w== +estree-walker@^2.0.2: + version "2.0.2" + resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac" + integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w== esutils@^2.0.2: version "2.0.3" @@ -2175,6 +2203,11 @@ fsevents@^2.1.2: resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.2.1.tgz#1fb02ded2036a8ac288d507a65962bd87b97628d" integrity sha512-bTLYHSeC0UH/EFXS9KqWnXuOl/wHK5Z/d+ghd5AsFMYN7wIGkUCOJyzy88+wJKkZPGON8u4Z9f6U4FdgURE9qA== +fsevents@~2.3.2: + version "2.3.2" + resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a" + integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA== + function-bind@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d" @@ -3274,12 +3307,12 @@ lru-cache@^6.0.0: dependencies: yallist "^4.0.0" -magic-string@^0.25.3: - version "0.25.7" - resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051" - integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA== +magic-string@^0.27.0: + version "0.27.0" + resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.27.0.tgz#e4a3413b4bab6d98d2becffd48b4a257effdbbf3" + integrity sha512-8UnnX2PeRAPZuN12svgR9j7M1uWMovg/CEnIwIG0LFkXSJJe4PdfUGiTGl8V9bsBHFUtfVINcSyYxd7q+kx9fA== dependencies: - sourcemap-codec "^1.4.4" + "@jridgewell/sourcemap-codec" "^1.4.13" make-dir@^3.0.0: version "3.1.0" @@ -3720,6 +3753,11 @@ picomatch@^2.0.4, picomatch@^2.0.5, picomatch@^2.2.1: resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad" integrity sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg== +picomatch@^2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42" + integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA== + pid-cwd@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/pid-cwd/-/pid-cwd-1.2.0.tgz#c14c03d812b1d23f97aee27767957fc16272c979" @@ -4009,28 +4047,19 @@ rimraf@^3.0.0: dependencies: glob "^7.1.3" -rollup-plugin-inject@^3.0.0: - version "3.0.2" - resolved "https://registry.yarnpkg.com/rollup-plugin-inject/-/rollup-plugin-inject-3.0.2.tgz#e4233855bfba6c0c12a312fd6649dff9a13ee9f4" - integrity sha512-ptg9PQwzs3orn4jkgXJ74bfs5vYz1NCZlSQMBUA0wKcGp5i5pA1AO3fOUEte8enhGUC+iapTCzEWw2jEFFUO/w== - dependencies: - estree-walker "^0.6.1" - magic-string "^0.25.3" - rollup-pluginutils "^2.8.1" - -rollup-plugin-node-polyfills@^0.2.1: - version "0.2.1" - resolved "https://registry.yarnpkg.com/rollup-plugin-node-polyfills/-/rollup-plugin-node-polyfills-0.2.1.tgz#53092a2744837164d5b8a28812ba5f3ff61109fd" - integrity sha512-4kCrKPTJ6sK4/gLL/U5QzVT8cxJcofO0OU74tnB19F40cmuAKSzH5/siithxlofFEjwvw1YAhPmbvGNA6jEroA== +rollup-plugin-polyfill-node@^0.12.0: + version "0.12.0" + resolved "https://registry.yarnpkg.com/rollup-plugin-polyfill-node/-/rollup-plugin-polyfill-node-0.12.0.tgz#33d421ddb7fcb69c234461e508ca6d2db6193f1d" + integrity sha512-PWEVfDxLEKt8JX1nZ0NkUAgXpkZMTb85rO/Ru9AQ69wYW8VUCfDgP4CGRXXWYni5wDF0vIeR1UoF3Jmw/Lt3Ug== dependencies: - rollup-plugin-inject "^3.0.0" + "@rollup/plugin-inject" "^5.0.1" -rollup-pluginutils@^2.8.1: - version "2.8.2" - resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e" - integrity sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ== - dependencies: - estree-walker "^0.6.1" +rollup@^2.75.7: + version "2.79.1" + resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.79.1.tgz#bedee8faef7c9f93a2647ac0108748f497f081c7" + integrity sha512-uKxbd0IhMZOhjAiD5oAFp7BqvkA4Dv47qpOCtaNvng4HBwdbWtdOh8f5nZNuk2rp51PMGk3bzfWu5oayNEuYnw== + optionalDependencies: + fsevents "~2.3.2" rsvp@^4.8.4: version "4.8.5" @@ -4247,11 +4276,6 @@ source-map@^0.7.3: resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383" integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ== -sourcemap-codec@^1.4.4: - version "1.4.8" - resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4" - integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA== - spawndamnit@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/spawndamnit/-/spawndamnit-2.0.0.tgz#9f762ac5c3476abb994b42ad592b5ad22bb4b0ad"