Skip to content

Commit

Permalink
Add cjs build
Browse files Browse the repository at this point in the history
  • Loading branch information
willeastcott committed Jun 17, 2024
1 parent e598772 commit e0aef42
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,6 +35,7 @@
}
},
"files": [
"dist/index.cjs",
"dist/index.js",
"dist/index.mjs",
"types",
Expand Down
29 changes: 21 additions & 8 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { babel } from '@rollup/plugin-babel';

const umdOptions = {
const esmOptions = {
babelHelpers: 'bundled',
babelrc: false,
comments: false,
Expand All @@ -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,
Expand All @@ -28,11 +29,10 @@ const esmOptions = {
presets: [
[
'@babel/preset-env', {
bugfixes: true,
loose: true,
modules: false,
targets: {
esmodules: true
ie: "11"
}
}
]
Expand All @@ -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)
]
};

Expand All @@ -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;
}
}

Expand Down

0 comments on commit e0aef42

Please sign in to comment.