Skip to content

Commit

Permalink
fix: provide compat with deno (#800)
Browse files Browse the repository at this point in the history
* chore: fix node engine requirement

https://nodejs.org/api/async_context.html#class-asynclocalstorage

* chore: update esbuild to v0.21.1

* fix provide compat with deno
  • Loading branch information
antongolub authored May 10, 2024
1 parent 8690f00 commit 0eb81c9
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 4 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ jobs:
env:
FORCE_COLOR: 3

smoke-deno:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- uses: actions/download-artifact@v4
with:
name: build
- run: deno test ./test/smoke/deno.test.js --allow-read --allow-sys --allow-env --allow-run
timeout-minutes: 1
env:
FORCE_COLOR: 3

smoke-node:
runs-on: ubuntu-latest
needs: build
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"test:smoke:win32": "node ./test/smoke/win32.test.js",
"test:smoke:cjs": "node ./test/smoke/node.test.cjs",
"test:smoke:mjs": "node ./test/smoke/node.test.mjs",
"test:smoke:deno": "deno test ./test/smoke/deno.test.js --allow-read --allow-sys --allow-env --allow-run",
"coverage": "c8 -x build/vendor.cjs -x build/esblib.cjs -x 'test/**' -x scripts --check-coverage npm test",
"version": "cat package.json | fx .version"
},
Expand Down
18 changes: 18 additions & 0 deletions scripts/build-js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,26 @@ if (hybrid) {
}

plugins.push(
{
name: 'deno',
setup(build) {
build.onEnd(() =>
fs.copyFileSync('./scripts/deno.polyfill.js', './build/deno.js')
)
},
},
transformHookPlugin({
hooks: [
{
on: 'end',
pattern: /\.js$/,
transform(contents, p) {
if (!hybrid) return contents

const { header, body } = parseContentsLayout(contents)
return [header, `import './deno.js'`, body].join('\n')
},
},
{
on: 'end',
pattern: entryPointsToRegexp(entryPoints),
Expand Down
7 changes: 7 additions & 0 deletions scripts/deno.polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createRequire } from 'node:module'

if (globalThis.Deno) {
globalThis.require = createRequire(import.meta.url)
globalThis.__filename = new URL(import.meta.url).pathname
globalThis.__dirname = new URL('.', import.meta.url).pathname
}
2 changes: 1 addition & 1 deletion src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as _ from './index.js'

Object.assign(global, _)
Object.assign(globalThis, _)

declare global {
type ProcessPromise = _.ProcessPromise
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/js-project/package-lock.json

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

30 changes: 30 additions & 0 deletions test/smoke/deno.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { assert } from 'https://deno.land/[email protected]/assert/assert.ts'
import '../../build/globals.js'

Deno.test('deno smoke test', async () => {
// smoke test
{
const p = await $`echo foo`
assert(p.valueOf() === 'foo')
}

// captures err stack
{
const p = await $({ nothrow: true })`echo foo; exit 3`
assert(p.message.match(/exit code: 3/))
}
})

0 comments on commit 0eb81c9

Please sign in to comment.