From 1348e4442492ac0338cd1742e9cf9503de6ae806 Mon Sep 17 00:00:00 2001 From: Blue Daydreaming <26271654+blueDaydreaming725@users.noreply.github.com> Date: Wed, 4 Nov 2026 09:28:18 +0800 Subject: [PATCH] fix: keep the library entry free of a shebang ref:0 --- index.js | 2 -- package-lock.json | 2 +- package.json | 2 +- scripts/build.js | 7 ++++--- tests/test-cli-signals.js | 6 ++++-- tests/test-cli.js | 8 +++++--- tests/test-config-import.js | 12 ++++++++++++ 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 0bbc4245..9311688b 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,3 @@ -#!/usr/bin/env node - const dotenv = require('./lib/main') const run = require('./cli') diff --git a/package-lock.json b/package-lock.json index ea032d8e..5cfa6ab0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "18.0.9", "license": "BSD-2-Clause", "bin": { - "dotenv": "dist/index.cjs" + "dotenv": "dist/cli.cjs" }, "devDependencies": { "@types/node": "^18.11.3", diff --git a/package.json b/package.json index 79bffa72..2fff28bd 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Loads environment variables from .env file", "main": "dist/index.cjs", "bin": { - "dotenv": "./dist/index.cjs" + "dotenv": "./dist/cli.cjs" }, "types": "dist/index.d.ts", "exports": { diff --git a/scripts/build.js b/scripts/build.js index bcfa14d2..c61a85fe 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -8,12 +8,13 @@ const dist = path.join(root, 'dist') fs.rmSync(dist, { recursive: true, force: true }) esbuild.buildSync({ - entryPoints: [path.join(root, 'index.js')], + entryPoints: [path.join(root, 'index.js'), path.join(root, 'cli.js')], bundle: true, platform: 'node', format: 'cjs', target: 'node12', - outfile: path.join(dist, 'index.cjs'), + outdir: dist, + outExtension: { '.js': '.cjs' }, legalComments: 'none', minify: true }) @@ -22,4 +23,4 @@ fs.copyFileSync(path.join(root, 'lib/main.d.ts'), path.join(dist, 'index.d.ts')) const config = fs.readFileSync(path.join(root, 'config.js'), 'utf8') fs.writeFileSync(path.join(dist, 'config.cjs'), config.replace("require('./lib/main')", "require('./index.cjs')")) fs.copyFileSync(path.join(root, 'config.d.ts'), path.join(dist, 'config.d.ts')) -fs.chmodSync(path.join(dist, 'index.cjs'), 0o755) +fs.chmodSync(path.join(dist, 'cli.cjs'), 0o755) diff --git a/tests/test-cli-signals.js b/tests/test-cli-signals.js index b253554c..2dc5e5d7 100644 --- a/tests/test-cli-signals.js +++ b/tests/test-cli-signals.js @@ -4,6 +4,8 @@ const os = require('os') const path = require('path') const t = require('tap') +const cli = path.resolve(__dirname, '..', require('../package.json').bin.dotenv) + function kill (pid, signal = 'SIGKILL') { try { process.kill(pid, signal) @@ -15,7 +17,7 @@ function kill (pid, signal = 'SIGKILL') { async function launch (ct, script, preload) { const args = preload ? ['--require', preload] : [] const child = cp.spawn(process.execPath, [ - ...args, 'dist/index.cjs', 'run', '-q', '--', process.execPath, '-e', script + ...args, cli, 'run', '-q', '--', process.execPath, '-e', script ], { cwd: path.resolve(__dirname, '..'), detached: true, @@ -77,7 +79,7 @@ t.test('POSIX signal forwarding', { skip: process.platform === 'win32' }, async await ct.test('exits when the command dies from a signal Node ignores', async st => { // Node ignores SIGPIPE, so re-raising it does not stop dotenv (`dotenv run -- cmd | head`). - const result = cp.spawnSync(process.execPath, ['dist/index.cjs', 'run', '-q', '--', 'sh', '-c', 'kill -PIPE $$'], { + const result = cp.spawnSync(process.execPath, [cli, 'run', '-q', '--', 'sh', '-c', 'kill -PIPE $$'], { cwd: path.resolve(__dirname, '..'), encoding: 'utf8', timeout: 5000, diff --git a/tests/test-cli.js b/tests/test-cli.js index 3c81d490..9d4bd6ed 100644 --- a/tests/test-cli.js +++ b/tests/test-cli.js @@ -2,8 +2,10 @@ const cp = require('child_process') const path = require('path') const t = require('tap') +const cli = path.resolve(__dirname, '..', require('../package.json').bin.dotenv) + function run (args, env = process.env) { - return cp.spawnSync(process.execPath, ['dist/index.cjs', 'run', ...args], { + return cp.spawnSync(process.execPath, [cli, 'run', ...args], { cwd: path.resolve(__dirname, '..'), encoding: 'utf8', env @@ -94,7 +96,7 @@ t.test('empty path environment variables load the default .env like config()', c delete env.BASIC for (const name of ['DOTENV_PATH', 'DOTENV_CONFIG_PATH']) { const result = cp.spawnSync(process.execPath, [ - path.resolve(__dirname, '../dist/index.cjs'), 'run', '-q', + cli, 'run', '-q', process.execPath, '-e', 'console.log(process.env.BASIC)' ], { cwd, encoding: 'utf8', env: { ...env, [name]: '' } }) @@ -167,7 +169,7 @@ for (const testCase of [ const cwd = ct.testdir({ '.env': testCase.file }) const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.toUpperCase().startsWith('DOTENV_'))) const result = cp.spawnSync(process.execPath, [ - path.resolve(__dirname, '../dist/index.cjs'), 'run', ...(testCase.flags || []), + cli, 'run', ...(testCase.flags || []), process.execPath, '-e', 'console.log("child ran")' ], { cwd, encoding: 'utf8', env: { ...env, ...testCase.env } }) diff --git a/tests/test-config-import.js b/tests/test-config-import.js index 802c0578..ff4db999 100644 --- a/tests/test-config-import.js +++ b/tests/test-config-import.js @@ -1,11 +1,23 @@ const cp = require('child_process') +const fs = require('fs') +const { createRequire, wrap } = require('module') const path = require('path') +const vm = require('vm') const t = require('tap') const cleanEnv = Object.fromEntries(Object.entries(process.env).filter(([key]) => { return !key.toUpperCase().startsWith('DOTENV_') && key !== 'BASIC' })) +t.test('built library can be wrapped by a bundler', ct => { + const filename = require.resolve('dotenv') + const load = vm.runInThisContext(wrap(fs.readFileSync(filename, 'utf8')), { filename }) + const entry = { exports: {} } + load(entry.exports, createRequire(filename), entry, filename, path.dirname(filename)) + ct.same(entry.exports.parse('HELLO=world'), { HELLO: 'world' }) + ct.end() +}) + t.test('built package supports named, default, and namespace ESM imports', ct => { const result = cp.spawnSync(process.execPath, [ '--input-type=module',