From b285d180b39b1e323b603497b6baf003f4f9f069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguye=CC=82=CC=83n=20Tha=CC=80nh=20Long?= Date: Tue, 12 May 2026 11:15:32 +0700 Subject: [PATCH] fix: guard optional Babel dependency tracking API --- __tests__/index.test.js | 12 ++++++++++++ index.js | 10 ++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/__tests__/index.test.js b/__tests__/index.test.js index 17b5b3a..4c9c561 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -162,6 +162,18 @@ describe('react-native-dotenv', () => { expect(code).toBe('console.log(undefined);') }) + it('should support Babel API versions without addExternalDependency', () => { + const plugin = require('../index.js') + const api = { + types: {}, + cache: { + using: jest.fn(), + }, + } + + expect(() => plugin(api, {path: FIXTURES + 'default/.env'})).not.toThrow() + }) + it('should not throw if .env exists in safe mode', () => { const {code} = transformFileSync(FIXTURES + 'safe-no-dotenv/source.js') expect(code).toBe('console.log(undefined);') diff --git a/index.js b/index.js index e2a360d..642429c 100644 --- a/index.js +++ b/index.js @@ -96,10 +96,12 @@ module.exports = (api, options) => { env = (options.safe) ? safeObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary, ['NODE_ENV', 'BABEL_ENV', options.envName]) : undefObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary) - api.addExternalDependency(path.resolve(options.path)) - api.addExternalDependency(path.resolve(modeFilePath)) - api.addExternalDependency(path.resolve(localFilePath)) - api.addExternalDependency(path.resolve(modeLocalFilePath)) + if (typeof api.addExternalDependency === 'function') { + api.addExternalDependency(path.resolve(options.path)) + api.addExternalDependency(path.resolve(modeFilePath)) + api.addExternalDependency(path.resolve(localFilePath)) + api.addExternalDependency(path.resolve(modeLocalFilePath)) + } return ({ name: 'dotenv-import',