component-kit-builder
Advanced tools
Comparing version 2.0.0 to 3.0.0
62
index.js
@@ -6,37 +6,43 @@ 'use strict'; | ||
const hoist = require('hoist-non-react-statics'); | ||
module.exports = function (context) { | ||
const uniquePaths = context.keys().reduce((obj, key) => { | ||
const existingKey = obj[context.resolve(key)]; | ||
if (existingKey && existingKey.length <= key.length) { | ||
return obj; | ||
} | ||
return Object.assign({}, obj, { | ||
[context.resolve(key)]: key | ||
}) | ||
}, {}); | ||
const moduleMap = values(uniquePaths).reduce((obj, key) => { | ||
let contextForKey = context(key); | ||
if (contextForKey.default) { | ||
contextForKey = contextForKey.default; | ||
} | ||
return Object.assign({}, obj, { | ||
[path.relative('.', key)]: contextForKey | ||
}) | ||
}, {}); | ||
const buildKit = module.exports = function (parts) { | ||
let wrappersList = ['redux', 'relay']; | ||
if (moduleMap.index && moduleMap.index.wrappers) { | ||
wrappersList = moduleMap.index.wrappers | ||
if (parts.index && parts.index.wrappers) { | ||
wrappersList = parts.index.wrappers | ||
} | ||
wrappersList = wrappersList.filter(moduleName => moduleMap[moduleName]); | ||
return Object.assign({}, moduleMap, | ||
wrappersList = wrappersList.filter(moduleName => parts[moduleName]); | ||
const complete = wrappersList.reduce( | ||
(complete, moduleName) => hoist(parts[moduleName](complete), complete), | ||
parts.component | ||
); | ||
return Object.assign(complete, parts, | ||
{ | ||
default: wrappersList.reduce( | ||
(complete, moduleName) => hoist(moduleMap[moduleName](complete), complete), | ||
moduleMap.component | ||
) | ||
default: complete | ||
}, | ||
wrappersList.reduce((obj, moduleName) => Object.assign({}, obj, { | ||
[`${moduleName}_wrapped`]: hoist(moduleMap[moduleName](moduleMap.component), moduleMap.component) | ||
[`${moduleName}_wrapped`]: hoist(parts[moduleName](parts.component), parts.component) | ||
}), {}) | ||
); | ||
}; | ||
Object.assign(module.exports, { | ||
fromContext: function (context) { | ||
const uniquePaths = context.keys().reduce((obj, key) => { | ||
const existingKey = obj[context.resolve(key)]; | ||
if (existingKey && existingKey.length <= key.length) { | ||
return obj; | ||
} | ||
return Object.assign({}, obj, { | ||
[context.resolve(key)]: key | ||
}) | ||
}, {}); | ||
const parts = values(uniquePaths).reduce((obj, key) => { | ||
let contextForKey = context(key); | ||
if (contextForKey.default) { | ||
contextForKey = contextForKey.default; | ||
} | ||
return Object.assign({}, obj, { | ||
[path.relative('.', key)]: contextForKey | ||
}) | ||
}, {}); | ||
return buildKit(parts); | ||
} | ||
}); |
{ | ||
"name": "component-kit-builder", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "builds component kits using webpack require.context", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
173
test/spec.js
@@ -5,3 +5,3 @@ const assert = require('assert'); | ||
const dedent = require('dedent'); | ||
const kitBuilder = require('..'); | ||
const buildKit = require('..'); | ||
const path = require('path'); | ||
@@ -19,83 +19,96 @@ const rimraf = require('rimraf').sync; | ||
describe('component kit builder', function () { | ||
beforeEach(function () { | ||
rimraf(testTmp); | ||
mkdirp(testTmp); | ||
mkdirp(myComponentDir); | ||
fs.writeFileSync(path.join(myComponentDir, 'component.js'), `module.exports = x => \`component \${x}\`;`); | ||
fs.writeFileSync(path.join(myComponentDir, 'redux.js'), `module.exports = x => y => x(\`redux \${y}\`);`); | ||
fs.writeFileSync(path.join(myComponentDir, 'relay.js'), `module.exports = x => y => x(\`relay \${y}\`);`); | ||
fs.writeFileSync(path.join(testTmp, 'build-manager.js'), dedent` | ||
const kitBuilder = require('${path.relative(testTmp, require.resolve('..'))}') | ||
module.exports = kitBuilder(require.context('./${path.relative(testTmp, myComponentDir)}/')); | ||
`); | ||
it('should compose the components', function () { | ||
const kit = buildKit({ | ||
component: x => `component ${x}`, | ||
redux: x => y => x(`redux ${y}`), | ||
relay: x => y => x(`relay ${y}`), | ||
}); | ||
assert.strictEqual(kit.default, kit); | ||
assert.equal(kit.redux_wrapped('bar'), 'component redux bar'); | ||
assert.equal(kit.relay_wrapped('bar'), 'component relay bar'); | ||
assert.equal(kit('bar'), 'component redux relay bar'); | ||
}); | ||
afterEach(function () { | ||
rimraf(testTmp); | ||
rimraf(myComponentDir); | ||
describe('fromContext', function () { | ||
beforeEach(function () { | ||
rimraf(testTmp); | ||
mkdirp(testTmp); | ||
mkdirp(myComponentDir); | ||
fs.writeFileSync(path.join(myComponentDir, 'component.js'), `module.exports = x => \`component \${x}\`;`); | ||
fs.writeFileSync(path.join(myComponentDir, 'redux.js'), `module.exports = x => y => x(\`redux \${y}\`);`); | ||
fs.writeFileSync(path.join(myComponentDir, 'relay.js'), `module.exports = x => y => x(\`relay \${y}\`);`); | ||
fs.writeFileSync(path.join(testTmp, 'build-manager.js'), dedent` | ||
const buildKit = require('${path.relative(testTmp, require.resolve('..'))}'); | ||
module.exports = buildKit.fromContext(require.context('./${path.relative(testTmp, myComponentDir)}/')); | ||
`); | ||
}); | ||
afterEach(function () { | ||
rimraf(testTmp); | ||
rimraf(myComponentDir); | ||
}); | ||
it('should cope with missing redux', function () { | ||
rimraf(path.join(myComponentDir, 'redux.js')); | ||
buildBundle(); | ||
assert.deepEqual( | ||
[ | ||
'relay', | ||
'relay_wrapped', | ||
'component', | ||
'default' | ||
].sort(), | ||
Object.keys(require(bundlePath)).sort() | ||
); | ||
}); | ||
it('should cope with missing relay', function () { | ||
rimraf(path.join(myComponentDir, 'relay.js')); | ||
buildBundle(); | ||
assert.deepEqual( | ||
[ | ||
'redux', | ||
'redux_wrapped', | ||
'component', | ||
'default' | ||
].sort(), | ||
Object.keys(require(bundlePath)).sort() | ||
); | ||
}); | ||
it('should export properties based on the files present in the component directory', function () { | ||
buildBundle(); | ||
assert.deepEqual( | ||
[ | ||
'relay', | ||
'redux', | ||
'relay_wrapped', | ||
'redux_wrapped', | ||
'component', | ||
'default' | ||
].sort(), | ||
Object.keys(require(bundlePath)).sort() | ||
); | ||
}); | ||
it('should compose all the composable wrappers together', function () { | ||
buildBundle(); | ||
assert(require(bundlePath).default(), 'redux relay component'); | ||
}); | ||
it('should work with es6-style default exports', function () { | ||
fs.writeFileSync(path.join(myComponentDir, 'redux.js'), `module.exports = {default: x => y => y(\`redux \${x}\`)};`); | ||
buildBundle(); | ||
require(bundlePath); | ||
}); | ||
it('should hoist static properties through composition', function () { | ||
fs.writeFileSync(path.join(myComponentDir, 'component.js'), ` | ||
module.exports = Object.assign(x => \`component \${x}\`, {componentStaticProp: true}); | ||
`); | ||
fs.writeFileSync(path.join(myComponentDir, 'redux.js'), ` | ||
module.exports = x => Object.assign(y => y(\`redux \${x}\`), {reduxStaticProp: true}); | ||
`); | ||
fs.writeFileSync(path.join(myComponentDir, 'relay.js'), ` | ||
module.exports = x => Object.assign(y => y(\`relay \${x}\`), {relayStaticProp: true}); | ||
`); | ||
buildBundle(); | ||
const bundle = require(bundlePath); | ||
assert(bundle.default.reduxStaticProp); | ||
assert(bundle.default.relayStaticProp); | ||
assert(bundle.default.componentStaticProp); | ||
}) | ||
}); | ||
it('should cope with missing redux', function () { | ||
rimraf(path.join(myComponentDir, 'redux.js')); | ||
buildBundle(); | ||
assert.deepEqual( | ||
[ | ||
'relay', | ||
'relay_wrapped', | ||
'component', | ||
'default' | ||
].sort(), | ||
Object.keys(require(bundlePath)).sort() | ||
); | ||
}); | ||
it('should cope with missing relay', function () { | ||
rimraf(path.join(myComponentDir, 'relay.js')); | ||
buildBundle(); | ||
assert.deepEqual( | ||
[ | ||
'redux', | ||
'redux_wrapped', | ||
'component', | ||
'default' | ||
].sort(), | ||
Object.keys(require(bundlePath)).sort() | ||
); | ||
}); | ||
it('should export properties based on the files present in the component directory', function () { | ||
buildBundle(); | ||
assert.deepEqual( | ||
[ | ||
'relay', | ||
'redux', | ||
'relay_wrapped', | ||
'redux_wrapped', | ||
'component', | ||
'default' | ||
].sort(), | ||
Object.keys(require(bundlePath)).sort() | ||
); | ||
}); | ||
it('should compose all the composable wrappers together', function () { | ||
buildBundle(); | ||
assert(require(bundlePath).default(), 'redux relay component'); | ||
}); | ||
it('should work with es6-style default exports', function () { | ||
fs.writeFileSync(path.join(myComponentDir, 'redux.js'), `module.exports = {default: x => y => y(\`redux \${x}\`)};`); | ||
buildBundle(); | ||
require(bundlePath); | ||
}); | ||
it('should hoist static properties through composition', function () { | ||
fs.writeFileSync(path.join(myComponentDir, 'component.js'), ` | ||
module.exports = Object.assign(x => \`component \${x}\`, {componentStaticProp: true}); | ||
`); | ||
fs.writeFileSync(path.join(myComponentDir, 'redux.js'), ` | ||
module.exports = x => Object.assign(y => y(\`redux \${x}\`), {reduxStaticProp: true}); | ||
`); | ||
fs.writeFileSync(path.join(myComponentDir, 'relay.js'), ` | ||
module.exports = x => Object.assign(y => y(\`relay \${x}\`), {relayStaticProp: true}); | ||
`); | ||
buildBundle(); | ||
const bundle = require(bundlePath); | ||
assert(bundle.default.reduxStaticProp); | ||
assert(bundle.default.relayStaticProp); | ||
assert(bundle.default.componentStaticProp); | ||
}) | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8962
159