From e0aef4294a00d95bb3ba2baca8a16eb413264d4f Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Mon, 17 Jun 2024 11:27:05 +0100 Subject: [PATCH] Add cjs build --- package.json | 13 +++++++++---- rollup.config.js | 29 +++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index f39a736..590b97d 100644 --- a/package.json +++ b/package.json @@ -9,12 +9,16 @@ "observer" ], "license": "MIT", - "main": "dist/index.js", + "main": "./dist/index.cjs", + "module": "./dist/index.mjs", + "types": "./types/index.d.ts", "exports": { - ".": "./dist/index.mjs" + ".": { + "types": "./types/index.d.ts", + "import": "./dist/index.mjs", + "require": "./dist/index.cjs" + } }, - "module": "dist/index.mjs", - "types": "types/index.d.ts", "type": "module", "bugs": { "url": "https://github.com/playcanvas/observer/issues" @@ -31,6 +35,7 @@ } }, "files": [ + "dist/index.cjs", "dist/index.js", "dist/index.mjs", "types", diff --git a/rollup.config.js b/rollup.config.js index 550c762..706192a 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,6 +1,6 @@ import { babel } from '@rollup/plugin-babel'; -const umdOptions = { +const esmOptions = { babelHelpers: 'bundled', babelrc: false, comments: false, @@ -9,17 +9,18 @@ const umdOptions = { presets: [ [ '@babel/preset-env', { + bugfixes: true, loose: true, modules: false, targets: { - ie: "11" + esmodules: true } } ] ] }; -const esmOptions = { +const nonEsmOptions = { babelHelpers: 'bundled', babelrc: false, comments: false, @@ -28,11 +29,10 @@ const esmOptions = { presets: [ [ '@babel/preset-env', { - bugfixes: true, loose: true, modules: false, targets: { - esmodules: true + ie: "11" } } ] @@ -47,7 +47,19 @@ const umd = { name: 'observer' }, plugins: [ - babel(umdOptions) + babel(nonEsmOptions) + ] +}; + +const cjs = { + input: 'src/index.js', + output: { + file: 'dist/index.cjs', + format: 'cjs', + name: 'observer' + }, + plugins: [ + babel(nonEsmOptions) ] }; @@ -62,11 +74,12 @@ const esm = { ] }; -let targets = [umd, esm]; +let targets = [cjs, esm, umd]; if (process.env.target) { switch (process.env.target.toLowerCase()) { - case "umd": targets = [umd]; break; + case "cjs": targets = [cjs]; break; case "esm": targets = [esm]; break; + case "umd": targets = [umd]; break; } }