From 2e7d959f4ced37c40de82a1c7a4fbb2f299f04cd Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 2 Dec 2022 21:00:14 -0500 Subject: [PATCH] feat: add ESM support Closes #9, closes #10 --- .github/workflows/build.yml | 3 +++ index.js | 1 + index.mjs | 3 +++ package.json | 14 +++++++++++--- test/index.mjs | 5 +++++ 5 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 index.mjs create mode 100644 test/index.mjs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3633305..39f61e3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,6 +26,9 @@ jobs: - name: Run unit tests run: npm run test:coverage + - name: Run ES modules tests + run: npm run test:module + - name: Build bundle run: npm run build diff --git a/index.js b/index.js index 5cb85bb..5358e1a 100644 --- a/index.js +++ b/index.js @@ -40,3 +40,4 @@ function StyleToObject(style, iterator) { } module.exports = StyleToObject; +module.exports.default = StyleToObject; // ESM support diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..f87b7b7 --- /dev/null +++ b/index.mjs @@ -0,0 +1,3 @@ +import StyleToObject from './index.js'; + +export default StyleToObject; diff --git a/package.json b/package.json index 001c021..fdb9d5e 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,11 @@ "author": "Mark ", "main": "index.js", "types": "index.d.ts", + "module": "index.mjs", + "exports": { + "import": "./index.mjs", + "require": "./index.js" + }, "scripts": { "build": "run-s build:*", "build:min": "NODE_ENV=production rollup --config --file dist/style-to-object.min.js --sourcemap", @@ -16,9 +21,10 @@ "postinstall": "husky install", "postpublish": "pinst --enable", "prepublishOnly": "pinst --disable && run-s lint lint:dts test clean build", - "test": "mocha", + "test": "mocha --exclude **/*.mjs", "test:coverage": "nyc npm test", - "test:watch": "mocha --watch" + "test:module": "node --experimental-modules test/index.mjs", + "test:watch": "npm run test -- --watch" }, "repository": { "type": "git", @@ -60,7 +66,9 @@ }, "files": [ "/dist", - "index.d.ts" + "index.d.ts", + "index.js", + "index.mjs" ], "license": "MIT" } diff --git a/test/index.mjs b/test/index.mjs new file mode 100644 index 0000000..8adad7c --- /dev/null +++ b/test/index.mjs @@ -0,0 +1,5 @@ +import assert from 'assert'; +import StyleToObject from '../index.mjs'; + +assert.strictEqual(typeof StyleToObject, 'function'); +assert.deepEqual(StyleToObject('foo: bar'), { foo: 'bar' });