From 9c76898fb11d21c3feabb23fa4ca4b8ad86bfe3c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 26 Jun 2020 11:58:43 +0200 Subject: [PATCH] [Backport 7.x] ESM support (#1236) Co-authored-by: Tomas Della Vedova --- index.mjs | 10 ++++++++++ package.json | 10 +++++++++- test/unit/esm/index.mjs | 8 ++++++++ test/unit/esm/index.test.js | 19 +++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 index.mjs create mode 100644 test/unit/esm/index.mjs create mode 100644 test/unit/esm/index.test.js diff --git a/index.mjs b/index.mjs new file mode 100644 index 000000000..2a7fbb6a9 --- /dev/null +++ b/index.mjs @@ -0,0 +1,10 @@ +import mod from './index.js' + +export default mod +export const Client = mod.Client +export const Transport = mod.Transport +export const ConnectionPool = mod.ConnectionPool +export const Connection = mod.Connection +export const Serializer = mod.Serializer +export const events = mod.events +export const errors = mod.errors diff --git a/package.json b/package.json index 7358c17b4..df8685644 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,13 @@ "description": "The official Elasticsearch client for Node.js", "main": "index.js", "types": "index.d.ts", + "exports": { + ".": { + "require": "./index.js", + "import": "./index.mjs" + }, + "./": "./" + }, "homepage": "http://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html", "version": "7.7.1", "keywords": [ @@ -28,7 +35,8 @@ "test:coverage-ui": "tap test/{unit,acceptance}/{*,**/*}.test.js --coverage --coverage-report=html --nyc-arg=\"--exclude=api\"", "lint": "standard", "lint:fix": "standard --fix", - "license-checker": "license-checker --production --onlyAllow='MIT;Apache-2.0;Apache1.1;ISC;BSD-3-Clause;BSD-2-Clause'" + "license-checker": "license-checker --production --onlyAllow='MIT;Apache-2.0;Apache1.1;ISC;BSD-3-Clause;BSD-2-Clause'", + "build-esm": "npx gen-esm-wrapper . index.mjs && standard --fix index.mjs" }, "author": { "name": "Tomas Della Vedova", diff --git a/test/unit/esm/index.mjs b/test/unit/esm/index.mjs new file mode 100644 index 000000000..d78462891 --- /dev/null +++ b/test/unit/esm/index.mjs @@ -0,0 +1,8 @@ +import t from 'tap' +import { Client } from '../../../index.mjs' + +t.test('esm support', t => { + t.plan(1) + const client = new Client({ node: 'http://localhost:9200' }) + t.strictEqual(client.name, 'elasticsearch-js') +}) diff --git a/test/unit/esm/index.test.js b/test/unit/esm/index.test.js new file mode 100644 index 000000000..48772ae2f --- /dev/null +++ b/test/unit/esm/index.test.js @@ -0,0 +1,19 @@ +'use strict' + +const t = require('tap') +const semver = require('semver') + +if (semver.lt(process.versions.node, '12.17.0')) { + t.skip('Skip because Node version < 12.17.0') + t.end() +} else { + // Node v8 throw a `SyntaxError: Unexpected token import` + // even if this branch is never touch in the code, + // by using `eval` we can avoid this issue. + // eslint-disable-next-line + new Function('module', 'return import(module)')('./index.mjs').catch((err) => { + process.nextTick(() => { + throw err + }) + }) +}