Skip to content

Commit

Permalink
ESM exports [feat]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Apr 4, 2020
1 parent 4d02648 commit 06f8540
Show file tree
Hide file tree
Showing 21 changed files with 1,402 additions and 48 deletions.
22 changes: 22 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* --------------------
* is-it-type module
* Babel config
* ------------------*/

'use strict';

// Exports

module.exports = api => ({
presets: [
[
'@babel/preset-env',
{
// Loose mode to reduce ponyfills
loose: true,
// If running tests, compile for current Node version
...(api.env('test') && {targets: {node: 'current'}})
}
]
]
});
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
!.*
dist
coverage
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,15 @@ module.exports = {
extends: [
'@overlookmotel/eslint-config',
'@overlookmotel/eslint-config-node'
],
overrides: [
{
// Entry point references files in dist folder which only exist after build
files: ['./index.js'],
rules: {
'node/no-missing-require': 'off',
'import/no-unresolved': 'off'
}
}
]
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
coverage
dist
node_modules
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
src
test
coverage

.DS_Store
.babelrc.js
.editorconfig
.eslintignore
.eslintrc.js
.gitattributes
.travis.yml
jest.config.js
rollup.config.js
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# Determine type of a variable

All the functions from [core-util-is](https://www.npmjs.com/package/core-util-is) plus a few extras.
All the functions from [core-util-is](https://www.npmjs.com/package/core-util-is) plus a few extras, in both CJS and ES forms for use in Node.js or browser.

## Usage

Expand All @@ -17,6 +17,12 @@ All the functions from [core-util-is](https://www.npmjs.com/package/core-util-is
const { isString } = require('is-it-type');
```

or:

```js
import { isString } from 'is-it-type/es';
```

### Use a method

```js
Expand Down
23 changes: 23 additions & 0 deletions es/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* --------------------
* is-it-type module
* ESLint src config
* ------------------*/

'use strict';

// Exports

module.exports = {
overrides: [{
files: ['!.*'],
parserOptions: {
sourceType: 'module'
},
rules: {
'node/no-unsupported-features/es-syntax': ['error', {ignores: ['modules']}],
// Entry point references files in dist folder which only exist after build
'node/no-missing-import': 'off',
'import/no-unresolved': 'off'
}
}]
};
8 changes: 8 additions & 0 deletions es/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* --------------------
* is-it-type module
* ESM Entry point
* ------------------*/

// Exports

export * from '../dist/esm/is-it-type.js';
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* --------------------
* is-it-type module
* CJS Entry point
* ------------------*/

'use strict';

// Exports

module.exports = require('./lib/index.js');
module.exports = require('./dist/cjs/is-it-type.js');
29 changes: 20 additions & 9 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@
module.exports = {
testEnvironment: 'node',
coverageDirectory: 'coverage',
collectCoverageFrom: [
'**/*.js',
'!.**',
'!**/.**',
'!**/node_modules/**',
'!test/**',
'!jest.config.js'
],
setupFilesAfterEnv: ['jest-extended']
collectCoverageFrom: ['src/**/*.js'],
setupFilesAfterEnv: ['jest-extended'],
// Resolve `import from 'is-it-type'` to src or build, depending on env variable
moduleNameMapper: {
'^is-it-type$': resolvePath()
}
};

function resolvePath() {
const testEnv = (process.env.TEST_ENV || '').toLowerCase(),
isProd = process.env.NODE_ENV === 'production';

if (!testEnv) return '<rootDir>/src/index.js';
if (testEnv === 'cjs') return '<rootDir>/index.js';
if (testEnv === 'esm') return `<rootDir>/dist/esm/is-it-type${isProd ? '.min' : ''}.js`;
if (testEnv === 'umd') return `<rootDir>/dist/umd/is-it-type${isProd ? '.min' : ''}.js`;

throw new Error(
`Invalid TEST_ENV '${testEnv}' - valid options are 'cjs', 'esm', 'umd' or undefined`
);
}
Loading

0 comments on commit 06f8540

Please sign in to comment.