Skip to content

Commit

Permalink
feat(esm): adds ESM support for latest Node.js through exports prop…
Browse files Browse the repository at this point in the history
… in package.json

Fixes #331
  • Loading branch information
stalniy committed Aug 20, 2020
1 parent 55fd6ee commit cac2506
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 30 deletions.
14 changes: 12 additions & 2 deletions packages/casl-ability/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
"funding": "https://github.com/stalniy/casl/blob/master/BACKERS.md",
"main": "dist/es6c/index.js",
"module": "dist/es5m/index.js",
"es2015": "dist/es6m/index.js",
"es2015": "dist/es6m/index.mjs",
"legacy": "dist/umd/index.js",
"typings": "./index.d.ts",
"exports": {
".": {
"import": "./dist/es6m/index.mjs",
"require": "./dist/es6c/index.js"
},
"./extra": {
"import": "./dist/es6m/extra.mjs",
"require": "./dist/es6c/extra.js"
}
},
"sideEffects": false,
"repository": {
"type": "git",
Expand Down Expand Up @@ -76,6 +86,6 @@
"extra"
],
"dependencies": {
"@ucast/mongo2js": "^1.2.0"
"@ucast/mongo2js": "^1.3.0"
}
}
8 changes: 7 additions & 1 deletion packages/casl-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"description": "Angular module for CASL which makes it easy to add permissions in any Angular app",
"main": "dist/umd/index.js",
"module": "dist/es5m/index.js",
"es2015": "dist/es6m/index.js",
"es2015": "dist/es6m/index.mjs",
"typings": "dist/types/public_api.d.ts",
"exports": {
".": {
"import": "./dist/es6m/index.mjs",
"require": "./dist/umd/index.js"
}
},
"repository": {
"type": "git",
"url": "https://github.com/stalniy/casl.git"
Expand Down
8 changes: 7 additions & 1 deletion packages/casl-aurelia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"description": "Aurelia plugin for CASL which makes it easy to add permissions in any Aurelia apps",
"main": "dist/umd/index.js",
"module": "dist/es5m/index.js",
"es2015": "dist/es6m/index.js",
"es2015": "dist/es6m/index.mjs",
"typings": "dist/types/index.d.ts",
"exports": {
".": {
"import": "./dist/es6m/index.mjs",
"require": "./dist/umd/index.js"
}
},
"repository": {
"type": "git",
"url": "https://github.com/stalniy/casl.git"
Expand Down
8 changes: 7 additions & 1 deletion packages/casl-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
"version": "3.0.3",
"description": "Allows to query accessible records from MongoDB based on CASL rules",
"main": "dist/es6c/index.js",
"es2015": "dist/es6m/index.js",
"es2015": "dist/es6m/index.mjs",
"typings": "index.d.ts",
"exports": {
".": {
"import": "./dist/es6m/index.mjs",
"require": "./dist/es6c/index.js"
}
},
"repository": {
"type": "git",
"url": "https://github.com/stalniy/casl.git"
Expand Down
8 changes: 7 additions & 1 deletion packages/casl-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"description": "React component for CASL which makes it easy to add permissions in any React application",
"main": "dist/umd/index.js",
"module": "dist/es5m/index.js",
"es2015": "dist/es6m/index.js",
"es2015": "dist/es6m/index.mjs",
"typings": "./index.d.ts",
"exports": {
".": {
"import": "./dist/es6m/index.mjs",
"require": "./dist/umd/index.js"
}
},
"repository": {
"type": "git",
"url": "https://github.com/stalniy/casl.git"
Expand Down
8 changes: 7 additions & 1 deletion packages/casl-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"description": "Vue plugin for CASL which makes it easy to add permissions in any Vue application",
"main": "dist/umd/index.js",
"module": "dist/es5m/index.js",
"es2015": "dist/es6m/index.js",
"es2015": "dist/es6m/index.mjs",
"typings": "./index.d.ts",
"exports": {
".": {
"import": "./dist/es6m/index.mjs",
"require": "./dist/umd/index.js"
}
},
"repository": {
"type": "git",
"url": "https://github.com/stalniy/casl.git"
Expand Down
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 38 additions & 8 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';
import { dirname, basename, extname } from 'path';

const output = (config) => {
let prop = 'dir';
let path = `dist/${config.id}${config.subpath}`;

if (config.ext) {
prop = 'file';
path += `/${basename(config.input, extname(config.input))}${config.ext}`;
}

return {
[prop]: path,
format: config.format,
name: config.name,
globals: config.globals,
};
};

const build = config => ({
input: config.input || 'src/index.ts',
input: config.input,
external: config.external,
output: {
sourcemap: true,
format: config.format,
dir: `dist/${config.id}`,
name: config.name,
...output(config),
plugins: [
config.minify
? terser({
Expand Down Expand Up @@ -38,16 +55,29 @@ const build = config => ({
function parseOptions(overrideOptions) {
const options = {
external: [],
input: 'src/index.ts',
subpath: '',
useInputSourceMaps: !!process.env.USE_SRC_MAPS,
minify: process.env.NODE_ENV === 'production' && process.env.LIB_MINIFY !== 'false'
};

if (overrideOptions.external) {
if (overrideOptions.input) {
options.input = overrideOptions.input[0];
options.subpath = dirname(options.input).replace(/^src/, '');
}

if (typeof overrideOptions.external === 'string') {
options.external = overrideOptions.external.split(',');
} else if (overrideOptions.external) {
options.external = options.external.concat(overrideOptions.external);
}

if (typeof overrideOptions.globals === 'string') {
options.globals = overrideOptions.globals.split(',');
}

if (overrideOptions.globals) {
options.globals = overrideOptions.globals.split(',').reduce((map, chunk) => {
if (options.globals) {
options.globals = options.globals.reduce((map, chunk) => {
const [moduleId, globalName] = chunk.split(':');
map[moduleId] = globalName;
return map;
Expand All @@ -60,7 +90,7 @@ function parseOptions(overrideOptions) {

export default (overrideOptions) => {
const builds = [
{ id: 'es6m', type: 'es6', format: 'es' },
{ id: 'es6m', type: 'es6', format: 'es', ext: '.mjs' },
{ id: 'es6c', type: 'es6', format: 'cjs' },
{ id: 'es5m', type: 'es5', format: 'es' },
{ id: 'umd', type: 'es5', format: 'umd', name: overrideOptions.name },
Expand Down

0 comments on commit cac2506

Please sign in to comment.