-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup is not capable of exposing a default export AND a named export as a UMD module in a useful way for the global context. So this adds some custom UMD logic to the bottom of the main file. This should hopefully never need to be touched, but there are tests that validate the CJS and ES6 module exports. Things will be challenging when people want to use native ES6 modules with this module (because now we're not exposing this as a native ES6 module). But we weren't really doing that in the first place (we were transpiling) so that's fine for now. Fixes #14
- Loading branch information
Kent C. Dodds
committed
Aug 31, 2016
1 parent
774ca9c
commit 8c221e3
Showing
6 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* eslint import/default:0, import/named:0, import/no-unresolved:0, import/extensions:0, no-console:0 */ | ||
/* | ||
* This file is here to validate that the built version of the library exposes the module in the way that we | ||
* want it to. Specifically that the ES6 module import can get the matchSorter function via default import and the | ||
* rankings via named import. Also that the CommonJS require returns the matchSorter function (rather than an object | ||
* that has the matchSorter as a `default` property). | ||
* | ||
* This file is unable to validate the AMD or global exports. | ||
*/ | ||
import assert from 'assert' | ||
|
||
import cjsImport, {rankings as cjsRankings} from '../dist/cjs' | ||
import umdImport, {rankings as umdRankings} from '../dist/umd/match-sorter' | ||
|
||
const cjsRequire = require('../dist/cjs') | ||
const umdRequire = require('../dist/umd/match-sorter') | ||
|
||
|
||
assert( | ||
isMatchSorterFunction(cjsImport) && isRankingsObject(cjsRankings), | ||
'CJS build has a problem with ES6 modules' | ||
) | ||
|
||
assert( | ||
isMatchSorterFunction(cjsRequire), | ||
'CJS build has a problem with CJS' | ||
) | ||
|
||
assert( | ||
isMatchSorterFunction(umdImport) && isRankingsObject(umdRankings), | ||
'UMD build has a problem with ES6 modules' | ||
) | ||
|
||
assert( | ||
isMatchSorterFunction(umdRequire), | ||
'UMD build has a problem with CJS' | ||
) | ||
|
||
// TODO: how could we validate the AMD/global modules? | ||
|
||
console.log('Built modules look good 👍') | ||
|
||
function isMatchSorterFunction(thing) { | ||
if (typeof thing !== 'function') { | ||
console.error(`matchSorter thing should be a function. It's a ${typeof thing}`) | ||
return false | ||
} | ||
if (thing.name !== 'matchSorter') { | ||
console.error(`the function is not called "matchSorter". It's called ${thing.name}`) | ||
return false | ||
} | ||
return isRankingsObject(thing.rankings) | ||
} | ||
|
||
function isRankingsObject(thing) { | ||
if (typeof thing !== 'object') { | ||
console.error(`rankings object thing should be an object. It's a ${typeof thing}`) | ||
return false | ||
} | ||
if (!Object.keys(thing).includes('NO_MATCH')) { | ||
console.error(`rankings object should include a NO_MATCH key. It only has ${Object.keys(thing)}`) | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,12 @@ | |
"author": "Kent C. Dodds <[email protected]> (http://kentcdodds.com/)", | ||
"license": "MIT", | ||
"bundledDependencies": [ | ||
"diacritic" | ||
"diacritic", | ||
"global-object" | ||
], | ||
"dependencies": { | ||
"diacritic": "0.0.2" | ||
"diacritic": "0.0.2", | ||
"global-object": "1.0.0" | ||
}, | ||
"devDependencies": { | ||
"all-contributors-cli": "^3.0.0", | ||
|
@@ -34,6 +36,7 @@ | |
"eslint": "^3.1.1", | ||
"eslint-config-kentcdodds": "^10.0.1", | ||
"ghooks": "^1.3.2", | ||
"nodemon": "1.10.2", | ||
"nyc": "8.1.0", | ||
"opt-cli": "^1.4.2", | ||
"p-s": "^2.3.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
* @author Kent C. Dodds <[email protected]> | ||
*/ | ||
import diacritics from 'diacritic' | ||
import globalObject from 'global-object' | ||
|
||
const rankings = { | ||
EQUAL: 6, | ||
|
@@ -17,7 +18,6 @@ const rankings = { | |
} | ||
|
||
matchSorter.rankings = rankings | ||
export {matchSorter as default, rankings} | ||
|
||
/** | ||
* Takes an array of items and a value and returns a new array with the items that match the given value | ||
|
@@ -227,4 +227,15 @@ function getAllValuesToRank(item, keys) { | |
return keys.reduce((allVals, key) => allVals.concat(getItemValue(item, key)), []) | ||
} | ||
|
||
module.exports = exports.default // CommonJS compat | ||
// some manual ✨ magic umd ✨ here because Rollup isn't capable of exposing our module the way we want | ||
// see dist-test/index.js | ||
/* istanbul ignore next */ | ||
if (typeof exports === 'object' && typeof module !== 'undefined') { | ||
matchSorter.default = matchSorter | ||
Object.defineProperty(exports, '__esModule', {value: true}) | ||
module.exports = matchSorter | ||
} else if (typeof define === 'function' && define.amd) { // eslint-disable-line | ||
define(() => matchSorter) // eslint-disable-line | ||
} else { | ||
globalObject.matchSorter = matchSorter // eslint-disable-line | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters