Skip to content

Commit

Permalink
fix: remove rollup-plugin-node-polyfills dependency and use custom no…
Browse files Browse the repository at this point in the history
…-op polyfills (#212)

Fixes #210
  • Loading branch information
chrispcampbell authored Jul 8, 2022
1 parent 8cbdf4f commit b3c3f20
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 65 deletions.
1 change: 0 additions & 1 deletion packages/plugin-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"@sdeverywhere/runtime-async": "^0.1.0",
"assert-never": "^1.2.1",
"picocolors": "^1.0.0",
"rollup-plugin-node-polyfills": "^0.2.1",
"vite": "^2.9.12",
"vite-plugin-glob": "^0.3.2"
},
Expand Down
36 changes: 9 additions & 27 deletions packages/plugin-check/src/vite-config-for-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { dirname, relative, join as joinPath, resolve as resolvePath } from 'pat
import { fileURLToPath } from 'url'

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

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

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

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

// XXX: Include polyfills for these modules that are used in the Node-specific
// implementation of threads.js; this allows us to use one bundle that works
// in both Node and browser environments
polyfillAlias('events'),
polyfillAlias('os'),
polyfillAlias('path'),
// XXX: The following is only needed due to threads.js 1.7.0 importing `fileURLToPath`.
// We use a no-op polyfill of our own for the time being.
alias('url', '/src/url-polyfill.ts')
// XXX: Include no-op polyfills for these modules that are used in the Node-specific
// implementation of threads.js; this allows us to use one bundle that works in both
// Node and browser environments
noopPolyfillAlias('events'),
noopPolyfillAlias('os'),
noopPolyfillAlias('path'),
noopPolyfillAlias('url')
]
},

Expand Down
38 changes: 38 additions & 0 deletions packages/plugin-check/template-report/polyfills/noop-polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2022 Climate Interactive / New Venture Fund

/*
* This file contains no-op polyfills for the small set of Node APIs that are
* referenced by the Node implementation of the threads.js package (which is
* used by the default generated bundle any custom bundle that uses the
* `@sdeverywhere/runtime-async` package). These polyfills are not actually
* used at runtime in the browser because they are only referenced in the
* (unused) Node code path.
*/

// from 'events'
export class EventEmitter {}

// from 'os'
export function cpus(): number {
return 1
}

// from 'path'
export function dirname(): string {
return ''
}

// from 'path'
export function isAbsolute(): boolean {
return false
}

// from 'path'
export function join(): string {
return ''
}

// from 'util'
export function fileURLToPath(): string {
return ''
}
10 changes: 0 additions & 10 deletions packages/plugin-check/template-report/src/url-polyfill.ts

This file was deleted.

27 changes: 0 additions & 27 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b3c3f20

Please sign in to comment.