Skip to content

Commit 704bf34

Browse files
authored
fix: enhance deno polyfill (#1061)
* refactor: rm dynamic load for repl module * fix(deno): add process polyfill fix(deno): fix internal esm > cjs reexport * chore: tweak up polyfill * chore: tweak up deno process polyfill * chore(deno): `process.version` polyfill * chore(deno): tweak up process polyfill * refactor: explicit process module ref * chore: deno process tweak ups * chore: byte shrinking * chore: build-js tweak up * test: tweak up flaky test
1 parent 3f36969 commit 704bf34

11 files changed

+38
-17
lines changed

.size-limit.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{
1010
"name": "zx/index",
1111
"path": "build/*.{js,cjs}",
12-
"limit": "807 kB",
12+
"limit": "808 kB",
1313
"brotli": false,
1414
"gzip": false
1515
},
@@ -30,7 +30,7 @@
3030
{
3131
"name": "all",
3232
"path": "build/*",
33-
"limit": "844 kB",
33+
"limit": "846 kB",
3434
"brotli": false,
3535
"gzip": false
3636
}

scripts/build-js.mjs

+9-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,15 @@ plugins.push(
9999
on: 'end',
100100
if: !hybrid,
101101
pattern: /\.js$/,
102-
transform(contents) {
103-
return injectCode(contents, `import './deno.js'`)
102+
transform(contents, file) {
103+
const { name } = path.parse(file)
104+
const _contents = contents
105+
.toString()
106+
.replace(
107+
'} = __module__',
108+
`} = globalThis.Deno ? globalThis.require("./${name}.cjs") : __module__`
109+
)
110+
return injectCode(_contents, `import "./deno.js"`)
104111
},
105112
},
106113
{

scripts/deno.polyfill.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import { createRequire } from 'node:module'
2+
import * as process from 'node:process'
23

34
const require = createRequire(import.meta.url)
45
const __filename = new URL(import.meta.url).pathname
56
const __dirname = new URL('.', import.meta.url).pathname
67

8+
// prettier-ignore
79
if (globalThis.Deno) {
810
globalThis.require = require
911
globalThis.__filename = __filename
1012
globalThis.__dirname = __dirname
13+
globalThis.module = new Proxy({}, { set() { return true } })
14+
15+
const p = globalThis.process = globalThis.process || process
16+
p.version || (p.version = 'v18.0.0')
17+
p.version || (p.version = { node: '18.0.0' })
18+
p.env || (p.env = globalThis.Deno.env.toObject())
19+
p.argv || (p.argv = [globalThis.Deno.execPath(), globalThis.Deno.mainModule.replace('file://', ''), ...globalThis.Deno.args])
1120
}
1221

1322
export { require, __dirname, __filename }

src/cli.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
VERSION,
3030
} from './index.js'
3131
import { installDeps, parseDeps } from './deps.js'
32+
import { startRepl } from './repl.js'
3233
import { randomId } from './util.js'
3334
import { createRequire } from './vendor.js'
3435

@@ -109,7 +110,7 @@ export async function main() {
109110
return
110111
}
111112
if (argv.repl) {
112-
await (await import('./repl.js')).startRepl()
113+
await startRepl()
113114
return
114115
}
115116
if (argv.eval) {

src/goods.ts

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

1515
import assert from 'node:assert'
1616
import { createInterface } from 'node:readline'
17-
import { default as path } from 'node:path'
17+
import process from 'node:process'
1818
import { $, within, ProcessOutput } from './core.js'
1919
import {
2020
type Duration,
@@ -25,7 +25,6 @@ import {
2525
toCamelCase,
2626
} from './util.js'
2727
import {
28-
fs,
2928
minimist,
3029
nodeFetch,
3130
type RequestInfo,

src/repl.ts

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

1515
import os from 'node:os'
1616
import path from 'node:path'
17+
import repl from 'node:repl'
1718
import { inspect } from 'node:util'
1819
import { ProcessOutput, defaults } from './core.js'
1920
import { chalk } from './vendor-core.js'
2021

2122
export async function startRepl() {
2223
defaults.verbose = false
23-
const r = (await import('node:repl')).start({
24+
const r = repl.start({
2425
prompt: chalk.greenBright.bold('❯ '),
2526
useGlobal: true,
2627
preview: false,

src/vendor-core.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
// limitations under the License.
1414

1515
export {
16+
type TSpawnStore,
1617
exec,
1718
buildCmd,
1819
isStringLiteral,
19-
type TSpawnStore,
2020
VoidStream,
2121
} from 'zurk/spawn'
2222

test/cli.test.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -268,23 +268,26 @@ describe('cli', () => {
268268
const oldPath = process.env.PATH
269269

270270
const envPathSeparator = isWindows ? ';' : ':'
271-
process.env.PATH += envPathSeparator + path.resolve('/tmp/')
271+
const dir = tmpdir()
272+
process.env.PATH += envPathSeparator + dir
272273

273274
const toPOSIXPath = (_path) => _path.split(path.sep).join(path.posix.sep)
274275

275276
const zxPath = path.resolve('./build/cli.js')
276277
const zxLocation = isWindows ? toPOSIXPath(zxPath) : zxPath
278+
const scriptName = 'script-from-path'
277279
const scriptCode = `#!/usr/bin/env ${zxLocation}\nconsole.log('The script from path runs.')`
280+
const scriptPath = path.join(dir, scriptName)
278281

279282
try {
280283
await $`chmod +x ${zxLocation}`
281-
await $({ stdio: ['inherit', 'pipe', 'pipe'] })`echo ${scriptCode}`.pipe(
282-
fs.createWriteStream('/tmp/script-from-path', { mode: 0o744 })
284+
await $`echo ${scriptCode}`.pipe(
285+
fs.createWriteStream(scriptPath, { mode: 0o744 })
283286
)
284-
await $`script-from-path`
287+
await $`${scriptName}`
285288
} finally {
286289
process.env.PATH = oldPath
287-
fs.rmSync('/tmp/script-from-path')
290+
fs.rmSync(scriptPath)
288291
}
289292
})
290293

test/export.test.js

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ describe('cli', () => {
8484
assert.equal(typeof cli.runScript, 'function', 'cli.runScript')
8585
assert.equal(typeof cli.scriptFromHttp, 'function', 'cli.scriptFromHttp')
8686
assert.equal(typeof cli.scriptFromStdin, 'function', 'cli.scriptFromStdin')
87-
assert.equal(typeof cli.startRepl, 'undefined', 'cli.startRepl')
8887
assert.equal(typeof cli.transformMarkdown, 'function', 'cli.transformMarkdown')
8988
assert.equal(typeof cli.writeAndImport, 'function', 'cli.writeAndImport')
9089
})

test/smoke/bun.test.js

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

1515
import assert from 'node:assert'
1616
import { test, describe } from 'bun:test'
17-
import '../../build/globals.js'
17+
import { $, within, tmpdir } from '../../build/index.js'
18+
import '../../build/cli.js'
1819

1920
describe('bun', () => {
2021
test('smoke test', async () => {

test/smoke/deno.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
// limitations under the License.
1414

1515
import { assert } from 'https://deno.land/std@0.224.0/assert/assert.ts'
16-
import '../../build/globals.js'
16+
import { $ } from '../../build/index.js'
17+
import '../../build/cli.js'
1718

1819
Deno.test('deno smoke test', async () => {
1920
// smoke test

0 commit comments

Comments
 (0)