Skip to content

Commit 0eb81c9

Browse files
authored
fix: provide compat with deno (google#800)
* 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
1 parent 8690f00 commit 0eb81c9

File tree

7 files changed

+77
-4
lines changed

7 files changed

+77
-4
lines changed

.github/workflows/test.yml

+17
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ jobs:
7979
env:
8080
FORCE_COLOR: 3
8181

82+
smoke-deno:
83+
runs-on: ubuntu-latest
84+
needs: build
85+
steps:
86+
- uses: actions/checkout@v4
87+
- name: Setup Deno
88+
uses: denoland/setup-deno@v1
89+
with:
90+
deno-version: v1.x
91+
- uses: actions/download-artifact@v4
92+
with:
93+
name: build
94+
- run: deno test ./test/smoke/deno.test.js --allow-read --allow-sys --allow-env --allow-run
95+
timeout-minutes: 1
96+
env:
97+
FORCE_COLOR: 3
98+
8299
smoke-node:
83100
runs-on: ubuntu-latest
84101
needs: build

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"test:smoke:win32": "node ./test/smoke/win32.test.js",
7373
"test:smoke:cjs": "node ./test/smoke/node.test.cjs",
7474
"test:smoke:mjs": "node ./test/smoke/node.test.mjs",
75+
"test:smoke:deno": "deno test ./test/smoke/deno.test.js --allow-read --allow-sys --allow-env --allow-run",
7576
"coverage": "c8 -x build/vendor.cjs -x build/esblib.cjs -x 'test/**' -x scripts --check-coverage npm test",
7677
"version": "cat package.json | fx .version"
7778
},

scripts/build-js.mjs

+18
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,26 @@ if (hybrid) {
8585
}
8686

8787
plugins.push(
88+
{
89+
name: 'deno',
90+
setup(build) {
91+
build.onEnd(() =>
92+
fs.copyFileSync('./scripts/deno.polyfill.js', './build/deno.js')
93+
)
94+
},
95+
},
8896
transformHookPlugin({
8997
hooks: [
98+
{
99+
on: 'end',
100+
pattern: /\.js$/,
101+
transform(contents, p) {
102+
if (!hybrid) return contents
103+
104+
const { header, body } = parseContentsLayout(contents)
105+
return [header, `import './deno.js'`, body].join('\n')
106+
},
107+
},
90108
{
91109
on: 'end',
92110
pattern: entryPointsToRegexp(entryPoints),

scripts/deno.polyfill.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createRequire } from 'node:module'
2+
3+
if (globalThis.Deno) {
4+
globalThis.require = createRequire(import.meta.url)
5+
globalThis.__filename = new URL(import.meta.url).pathname
6+
globalThis.__dirname = new URL('.', import.meta.url).pathname
7+
}

src/globals.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

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

17-
Object.assign(global, _)
17+
Object.assign(globalThis, _)
1818

1919
declare global {
2020
type ProcessPromise = _.ProcessPromise

test/fixtures/js-project/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/smoke/deno.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { assert } from 'https://deno.land/std@0.224.0/assert/assert.ts'
16+
import '../../build/globals.js'
17+
18+
Deno.test('deno smoke test', async () => {
19+
// smoke test
20+
{
21+
const p = await $`echo foo`
22+
assert(p.valueOf() === 'foo')
23+
}
24+
25+
// captures err stack
26+
{
27+
const p = await $({ nothrow: true })`echo foo; exit 3`
28+
assert(p.message.match(/exit code: 3/))
29+
}
30+
})

0 commit comments

Comments
 (0)