Skip to content

Commit b3c3f20

Browse files
fix: remove rollup-plugin-node-polyfills dependency and use custom no-op polyfills (#212)
Fixes #210
1 parent 8cbdf4f commit b3c3f20

File tree

5 files changed

+47
-65
lines changed

5 files changed

+47
-65
lines changed

packages/plugin-check/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"@sdeverywhere/runtime-async": "^0.1.0",
4242
"assert-never": "^1.2.1",
4343
"picocolors": "^1.0.0",
44-
"rollup-plugin-node-polyfills": "^0.2.1",
4544
"vite": "^2.9.12",
4645
"vite-plugin-glob": "^0.3.2"
4746
},

packages/plugin-check/src/vite-config-for-report.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { dirname, relative, join as joinPath, resolve as resolvePath } from 'pat
44
import { fileURLToPath } from 'url'
55

66
import type { Alias, InlineConfig } from 'vite'
7-
// import globPlugin from 'vite-plugin-glob'
8-
import { nodeResolve } from '@rollup/plugin-node-resolve'
97

108
import type { SuiteSummary } from '@sdeverywhere/check-core'
119

@@ -48,24 +46,10 @@ export function createViteConfigForReport(
4846
// referenced by the check bundle (specifically in the Node implementation of
4947
// threads.js). These are not actually used in the browser, so we just need
5048
// to provide no-op polyfills for these.
51-
const polyfillAlias = (find: string) => {
49+
const noopPolyfillAlias = (find: string) => {
5250
return {
5351
find,
54-
replacement: find,
55-
customResolver: async function (_source, _importer, options) {
56-
const customResolver = nodeResolve()
57-
// Replace uses of Node built-ins (e.g. 'events') with the appropriate polyfill
58-
const customSource = `rollup-plugin-node-polyfills/polyfills/${find}`
59-
// Use this file as the "importer" so that we resolve `rollup-plugin-node-polyfills`
60-
// relative to `plugin-check/node_modules`. Without this workaround, the consuming
61-
// project would need `rollup-plugin-node-polyfills` as an explicit dependency, and
62-
// we want to avoid that since it's more of an implementation detail.
63-
const customImporter = __filename
64-
// Note that we need to use `resolveId.call` here in order to provide the
65-
// right `this` context, which provides Rollup plugin functionality
66-
const resolved = await customResolver.resolveId.call(this, customSource, customImporter, options)
67-
return resolved.id
68-
}
52+
replacement: '/polyfills/noop-polyfills.ts'
6953
} as Alias
7054
}
7155

@@ -116,15 +100,13 @@ export function createViteConfigForReport(
116100
// Make the overlay use the `messages.html` file that is written to the prep directory
117101
alias('@_prep_', prepDir),
118102

119-
// XXX: Include polyfills for these modules that are used in the Node-specific
120-
// implementation of threads.js; this allows us to use one bundle that works
121-
// in both Node and browser environments
122-
polyfillAlias('events'),
123-
polyfillAlias('os'),
124-
polyfillAlias('path'),
125-
// XXX: The following is only needed due to threads.js 1.7.0 importing `fileURLToPath`.
126-
// We use a no-op polyfill of our own for the time being.
127-
alias('url', '/src/url-polyfill.ts')
103+
// XXX: Include no-op polyfills for these modules that are used in the Node-specific
104+
// implementation of threads.js; this allows us to use one bundle that works in both
105+
// Node and browser environments
106+
noopPolyfillAlias('events'),
107+
noopPolyfillAlias('os'),
108+
noopPolyfillAlias('path'),
109+
noopPolyfillAlias('url')
128110
]
129111
},
130112

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2022 Climate Interactive / New Venture Fund
2+
3+
/*
4+
* This file contains no-op polyfills for the small set of Node APIs that are
5+
* referenced by the Node implementation of the threads.js package (which is
6+
* used by the default generated bundle any custom bundle that uses the
7+
* `@sdeverywhere/runtime-async` package). These polyfills are not actually
8+
* used at runtime in the browser because they are only referenced in the
9+
* (unused) Node code path.
10+
*/
11+
12+
// from 'events'
13+
export class EventEmitter {}
14+
15+
// from 'os'
16+
export function cpus(): number {
17+
return 1
18+
}
19+
20+
// from 'path'
21+
export function dirname(): string {
22+
return ''
23+
}
24+
25+
// from 'path'
26+
export function isAbsolute(): boolean {
27+
return false
28+
}
29+
30+
// from 'path'
31+
export function join(): string {
32+
return ''
33+
}
34+
35+
// from 'util'
36+
export function fileURLToPath(): string {
37+
return ''
38+
}

packages/plugin-check/template-report/src/url-polyfill.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

pnpm-lock.yaml

Lines changed: 0 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)