mirror of
https://github.com/lancedikson/bowser
synced 2025-12-05 06:02:14 +00:00
Fix ESM import issue by adding proper exports field and module type
Co-authored-by: naorpeled <6171622+naorpeled@users.noreply.github.com>
This commit is contained in:
parent
c3886be821
commit
dfdffcdc52
13
package.json
13
package.json
@ -19,10 +19,17 @@
|
|||||||
"url": "http://twitter.com/lancedikson"
|
"url": "http://twitter.com/lancedikson"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"main": "es5.js",
|
"main": "src/bowser.js",
|
||||||
"browser": "es5.js",
|
"module": "src/bowser.js",
|
||||||
"module": "src/bowser.js",
|
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
|
"type": "module",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"import": "./src/bowser.js",
|
||||||
|
"require": "./src/bowser.js",
|
||||||
|
"types": "./index.d.ts"
|
||||||
|
}
|
||||||
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/lancedikson/bowser.git"
|
"url": "git+https://github.com/lancedikson/bowser.git"
|
||||||
|
|||||||
39
test/unit/esm-imports.cjs
Normal file
39
test/unit/esm-imports.cjs
Normal file
@ -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');
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user