From 937075b8d58ba40d7447de735205dab0858dc95d Mon Sep 17 00:00:00 2001 From: Eirik Sletteberg Date: Wed, 31 Mar 2021 23:30:28 +0200 Subject: [PATCH] Add support for ESM modules This generates ESM module syntax ("export default") in a separate esm/ directory. For simplicity, it assumes that if you want to use ESM modules, you also have ES6 features in your environment, so the build script turns that on as well. The esm index.js is exposed through the "module" field in package.json. --- build.js | 3 +++ package.json | 4 +++- src/index.jst | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/build.js b/build.js index e236a9a..e946671 100644 --- a/build.js +++ b/build.js @@ -10,3 +10,6 @@ fs.writeFileSync('./react.js', jst({es6: false, react: true})); try { fs.mkdirSync('./es6'); } catch(e) {} fs.writeFileSync('./es6/index.js', jst({es6: true})); fs.writeFileSync('./es6/react.js', jst({es6: true, react: true})); +try { fs.mkdirSync('./esm'); } catch(e) {} +fs.writeFileSync('./esm/index.js', jst({es6: true, esm: true})); +fs.writeFileSync('./esm/react.js', jst({es6: true, esm: true, react: true})); diff --git a/package.json b/package.json index 3cfe66c..767c3e4 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "3.1.3", "description": "Fast deep equal", "main": "index.js", + "module": "esm/index.js", "scripts": { "eslint": "eslint *.js benchmark/*.js spec/*.js", "build": "node build", @@ -55,7 +56,8 @@ "index.d.ts", "react.js", "react.d.ts", - "es6/" + "es6/", + "esm/" ], "types": "index.d.ts" } diff --git a/src/index.jst b/src/index.jst index 5d4ee2f..754280d 100644 --- a/src/index.jst +++ b/src/index.jst @@ -6,7 +6,12 @@ var envHasBigInt64Array = typeof BigInt64Array !== 'undefined'; {{?}} +{{? it.esm }} +export default function equal(a, b) { +{{?}} +{{? !it.esm }} module.exports = function equal(a, b) { +{{?}} if (a === b) return true; if (a && b && typeof a == 'object' && typeof b == 'object') {