diff --git a/package.json b/package.json index 8411f81..f24a1ba 100644 --- a/package.json +++ b/package.json @@ -19,10 +19,17 @@ "url": "http://twitter.com/lancedikson" } ], - "main": "es5.js", - "browser": "es5.js", - "module": "src/bowser.js", + "main": "src/bowser.js", + "module": "src/bowser.js", "types": "index.d.ts", + "type": "module", + "exports": { + ".": { + "import": "./src/bowser.js", + "require": "./src/bowser.js", + "types": "./index.d.ts" + } + }, "repository": { "type": "git", "url": "git+https://github.com/lancedikson/bowser.git" diff --git a/test/unit/esm-imports.cjs b/test/unit/esm-imports.cjs new file mode 100644 index 0000000..5c9259a --- /dev/null +++ b/test/unit/esm-imports.cjs @@ -0,0 +1,39 @@ +// Test for ESM import compatibility (fixes issue #568) +// This test ensures that `import Bowser from 'bowser'` works in ESM/nodenext projects + +const test = require('ava'); + +test('package.json should have proper exports for ESM support', (t) => { + const packageJson = require('../../package.json'); + + // Check that package has type: module for proper ESM support + t.is(packageJson.type, 'module'); + + // Check that exports field is present and configured correctly + t.truthy(packageJson.exports); + t.truthy(packageJson.exports['.']); + t.is(packageJson.exports['.'].import, './src/bowser.js'); + t.is(packageJson.exports['.'].require, './src/bowser.js'); + t.is(packageJson.exports['.'].types, './index.d.ts'); +}); + +test('main and module fields should point to source', (t) => { + const packageJson = require('../../package.json'); + + // Both main and module should point to the source file + t.is(packageJson.main, 'src/bowser.js'); + t.is(packageJson.module, 'src/bowser.js'); +}); + +test('can require the source file (CommonJS interop)', (t) => { + // This tests that CommonJS can load the source file when using exports + const bowserModule = require('../../src/bowser.js'); + t.truthy(bowserModule); + t.truthy(bowserModule.default); + t.is(typeof bowserModule.default, 'function'); + + // Test that it provides the expected API + const Bowser = bowserModule.default; + t.is(typeof Bowser.getParser, 'function'); + t.is(typeof Bowser.parse, 'function'); +}); \ No newline at end of file