Commit 0eb81c9

Anton Golub <antongolub@antongolub.com>
2024-05-10 18:20:35
fix: provide compat with deno (#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
Changed files (7)
.github/workflows/test.yml
@@ -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
scripts/build-js.mjs
@@ -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),
scripts/deno.polyfill.js
@@ -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
+}
src/globals.ts
@@ -14,7 +14,7 @@
 
 import * as _ from './index.js'
 
-Object.assign(global, _)
+Object.assign(globalThis, _)
 
 declare global {
   type ProcessPromise = _.ProcessPromise
test/fixtures/js-project/package-lock.json
@@ -26,7 +26,7 @@
         "create-require": "^1.1.1",
         "depseek": "^0.4.1",
         "dts-bundle-generator": "^9.5.1",
-        "esbuild": "^0.21.0",
+        "esbuild": "^0.21.1",
         "esbuild-node-externals": "^1.13.1",
         "esbuild-plugin-entry-chunks": "^0.1.14",
         "esbuild-plugin-extract-helpers": "^0.0.5",
@@ -48,11 +48,11 @@
         "zurk": "^0.1.4"
       },
       "engines": {
-        "node": ">= 12.0.0"
+        "node": ">= 12.17.0"
       },
       "optionalDependencies": {
         "@types/fs-extra": "^11.0.4",
-        "@types/node": ">=20.12.10"
+        "@types/node": ">=20.12.11"
       }
     },
     "node_modules/zx": {
test/smoke/deno.test.js
@@ -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/std@0.224.0/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/))
+  }
+})
package.json
@@ -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"
   },