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',