From ea3adcfc93fed6d7060b514599d7ff3e4bd1ab6f Mon Sep 17 00:00:00 2001 From: Trevor Manz Date: Sun, 14 Mar 2021 06:04:30 -0400 Subject: [PATCH] Add package.json `exports` field (#773) These changes adds an `exports` field to package.json with the main goal of allowing the root/main export to be our ESM syntax version of the package, as opposed to the UMD/CommonJS version. This will make it easier for using projects written with ESM to use mustache.js out of the box, without having to figure out that they'd have to `import mustache/mustache.mjs` to get the ESM version. It is also assumed to be beneficial for tools like http://skypack.dev to choose the correct ESM version when appropriate. Refs https://nodejs.org/api/packages.html#packages_package_entry_points --- .github/workflows/usage.yml | 2 ++ package.json | 7 +++++++ test/module-systems/esm-test-exports.mjs | 12 ++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 test/module-systems/esm-test-exports.mjs diff --git a/.github/workflows/usage.yml b/.github/workflows/usage.yml index 75d68ff..f22ede8 100644 --- a/.github/workflows/usage.yml +++ b/.github/workflows/usage.yml @@ -90,9 +90,11 @@ jobs: export UNPACK_DESTINATION=$(mktemp -d) mv mustache.tgz $UNPACK_DESTINATION cp test/module-systems/esm-test.mjs $UNPACK_DESTINATION + cp test/module-systems/esm-test-exports.mjs $UNPACK_DESTINATION cd $UNPACK_DESTINATION npm install mustache.tgz node esm-test.mjs + node esm-test-exports.mjs browser-usage: runs-on: ubuntu-latest diff --git a/package.json b/package.json index f5dda06..bec0f76 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,13 @@ "mustache.min.js", "wrappers/" ], + "exports": { + ".": { + "import": "./mustache.mjs", + "require": "./mustache.js" + }, + "./*": "./*" + }, "volo": { "url": "https://raw.github.com/janl/mustache.js/{version}/mustache.js" }, diff --git a/test/module-systems/esm-test-exports.mjs b/test/module-systems/esm-test-exports.mjs new file mode 100644 index 0000000..d79feb6 --- /dev/null +++ b/test/module-systems/esm-test-exports.mjs @@ -0,0 +1,12 @@ +import assert from 'assert'; +import mustache from 'mustache'; + +const view = { + title: 'Joe', + calc: () => 2 + 4 +}; + +assert.strictEqual( + mustache.render('{{title}} spends {{calc}}', view), + 'Joe spends 6' +);