Skip to content

Commit

Permalink
CoffeeScript 1.11 now supports ES modules!
Browse files Browse the repository at this point in the history
- Fixed all source code to use ES import/export
- Added Babel build toolchain after CS compilation
- npm run build will now build a CommonJS module (lib/) and an ES6 module (es/)
- package.json includes a jsnext:main directive for ES6 imports
- main now points at lib/ (no more git submodule free rides)
  • Loading branch information
wcjohnson committed Oct 26, 2016
1 parent 5f8ce53 commit ef8d176
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 88 deletions.
36 changes: 35 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
{ "presets": ["es2015", "stage-1"] }
{
"plugins": [
["transform-es2015-template-literals", { "loose": true }],
"transform-es2015-literals",
"transform-es2015-function-name",
"transform-es2015-arrow-functions",
"transform-es2015-block-scoped-functions",
["transform-es2015-classes", { "loose": true }],
"transform-es2015-object-super",
"transform-es2015-shorthand-properties",
["transform-es2015-computed-properties", { "loose": true }],
["transform-es2015-for-of", { "loose": true }],
"transform-es2015-sticky-regex",
"transform-es2015-unicode-regex",
"check-es2015-constants",
["transform-es2015-spread", { "loose": true }],
"transform-es2015-parameters",
["transform-es2015-destructuring", { "loose": true }],
"transform-es2015-block-scoping",
"transform-object-rest-spread",
"transform-es3-member-expression-literals",
"transform-es3-property-literals"
],
"env": {
"commonjs": {
"plugins": [
["transform-es2015-modules-commonjs", { "loose": true }]
]
},
"es": {
"plugins": [
]
}
}
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ node_modules

# Build products
dist
lib
lib/
build/
es/
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ test/
*.swp
.DS_Store
src/
coverage/
scripts/
build/
book.json
coffeelint.json
.eslintrc
Expand Down
2 changes: 1 addition & 1 deletion ReduxComponent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////
// ES5 EXPORT STUB
///////////////////////////////////////////////////////////////////////
module.exports = require('./lib/ReduxComponent');
module.exports = require('./lib').ReduxComponent;
2 changes: 1 addition & 1 deletion SubtreeMixin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////
// ES5 EXPORT STUB
///////////////////////////////////////////////////////////////////////
module.exports = require('./lib/subtree').SubtreeMixin;
module.exports = require('./lib').SubtreeMixin;
2 changes: 1 addition & 1 deletion createClass.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////
// ES5 EXPORT STUB
///////////////////////////////////////////////////////////////////////
module.exports = require('./lib/createClass');
module.exports = require('./lib').createClass;
2 changes: 1 addition & 1 deletion createComponent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////
// ES5 EXPORT STUB
///////////////////////////////////////////////////////////////////////
module.exports = require('./lib/subtree').createComponent;
module.exports = require('./lib').createComponent;
12 changes: 4 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// Default exports for redux-components
module.exports = {
createClass: require('./createClass.js'),
createComponent: require('./createComponent.js'),
mountComponent: require('./mountComponent.js'),
ReduxComponent: require('./ReduxComponent.js'),
SubtreeMixin: require('./SubtreeMixin.js')
};
///////////////////////////////////////////////////////////////////////
// ES5 EXPORT STUB
///////////////////////////////////////////////////////////////////////
module.exports = require('./lib');
2 changes: 1 addition & 1 deletion mountComponent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////
// ES5 EXPORT STUB
///////////////////////////////////////////////////////////////////////
module.exports = require('./lib/mountComponent');
module.exports = require('./lib').mountComponent;
53 changes: 43 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
{
"name": "redux-components",
"version": "0.1.0",
"version": "0.1.1",
"description": "A component model for Redux state trees based on the React.js component model.",
"main": "./index.js",
"main": "lib/index.js",
"jsnext:main": "es/index.js",
"scripts": {
"clean": "rm -rf lib",
"build": "mkdir -p lib && coffee -o lib src/*.coffee",
"prepublish": "npm run clean && npm run build"
"clean": "rimraf lib build es coverage",
"build:babel:commonjs": "cross-env BABEL_ENV=commonjs babel build --out-dir lib",
"build:babel:es": "cross-env BABEL_ENV=es babel build --out-dir es",
"build:coffee": "mkdir -p build && coffee -o build src/*.coffee",
"build": "npm run build:coffee && npm run build:babel:commonjs && npm run build:babel:es",
"prepublish": "npm run clean && npm run build",
"test": "node_modules/.bin/mocha --recursive --compilers coffee:coffee-script/register",
"coverage": "node_modules/.bin/mocha --recursive --compilers coffee:coffee-script/register --require coffee-coverage/register-istanbul",
"coverageReport": "node_modules/.bin/istanbul report"
},
"repository": {
"type": "git",
Expand All @@ -24,17 +31,43 @@
},
"homepage": "https://github.com/",
"devDependencies": {
"babel-preset-es2015": "^6.6.0",
"babel-preset-stage-1": "^6.5.0",
"babel-register": "^6.7.2",
"babel-cli": "^6.3.15",
"babel-core": "^6.3.15",
"babel-eslint": "^4.1.6",
"babel-plugin-check-es2015-constants": "^6.3.13",
"babel-plugin-transform-es2015-arrow-functions": "^6.3.13",
"babel-plugin-transform-es2015-block-scoped-functions": "^6.3.13",
"babel-plugin-transform-es2015-block-scoping": "^6.3.13",
"babel-plugin-transform-es2015-classes": "^6.3.13",
"babel-plugin-transform-es2015-computed-properties": "^6.3.13",
"babel-plugin-transform-es2015-destructuring": "^6.3.13",
"babel-plugin-transform-es2015-for-of": "^6.3.13",
"babel-plugin-transform-es2015-function-name": "^6.3.13",
"babel-plugin-transform-es2015-literals": "^6.3.13",
"babel-plugin-transform-es2015-modules-commonjs": "^6.3.13",
"babel-plugin-transform-es2015-object-super": "^6.3.13",
"babel-plugin-transform-es2015-parameters": "^6.3.13",
"babel-plugin-transform-es2015-shorthand-properties": "^6.3.13",
"babel-plugin-transform-es2015-spread": "^6.3.13",
"babel-plugin-transform-es2015-sticky-regex": "^6.3.13",
"babel-plugin-transform-es2015-template-literals": "^6.3.13",
"babel-plugin-transform-es2015-unicode-regex": "^6.3.13",
"babel-plugin-transform-es3-member-expression-literals": "^6.5.0",
"babel-plugin-transform-es3-property-literals": "^6.5.0",
"babel-plugin-transform-object-rest-spread": "^6.3.13",
"babel-register": "^6.3.13",
"chai": "^3.5.0",
"coffee-script": "^1.10.0",
"coffee-coverage": "^1.0.1",
"coffee-script": "^1.11.1",
"coffeelint": "^1.16.0",
"cross-env": "^1.0.7",
"istanbul": "^0.4.5",
"mocha": "^2.4.5"
},
"dependencies": {
"inv": "0.0.1"
},
"peerDependencies": {
"redux": "^3.3.1"
"redux": "^3.6.0"
}
}
10 changes: 2 additions & 8 deletions src/DefaultMixin.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"use strict"
_export = null

{ get } = require './util'
import { get } from './util'
slice = [].slice

# Scope a selector to a component.
Expand All @@ -11,7 +8,7 @@ scopeSelector = (sel, self) -> ->
sel.apply(self, fwdArgs)

# DefaultMixin is mixed into all component specs automatically by createClass.
DefaultMixin = {
export default DefaultMixin = {
componentWillMount: ->
store = @store; myPath = @path

Expand All @@ -32,6 +29,3 @@ DefaultMixin = {
# Make sure coffeescript doesn't generate an extra array here.
undefined
}

_export = DefaultMixin
module.exports = _export
10 changes: 2 additions & 8 deletions src/ReduxComponent.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"use strict"
_export = null

invariant = require 'inv'
import invariant from 'inv'

################################
# Component prototype
ReduxComponent = ( -> )
export default ReduxComponent = ( -> )

ReduxComponent.prototype.__willMount = (@store, @path = [], @parentComponent = null) ->
invariant(not @__mounted, "redux-component of type #{@constructor.displayName} was multiply initialized. This can indicate a cycle in your component graph, which is illegal. Make sure each instance is only used once in your tree. If you wish to use a component in multiple places, construct additional instances.")
Expand All @@ -14,6 +11,3 @@ ReduxComponent.prototype.__willMount = (@store, @path = [], @parentComponent = n
@componentWillMount?()
# XXX: should we invariant() that the reducer is an actual reducer?
@reducer = @getReducer().bind(@)

_export = ReduxComponent
module.exports = _export
13 changes: 3 additions & 10 deletions src/applyMixin.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"use strict"
_export = null

{ assign, chain } = require './util'
invariant = require 'inv'
import { assign, chain } from './util'
import invariant from 'inv'

# Handle various special mixin keys
chainedKeyHandler = (spec, mixin, key, val) ->
Expand Down Expand Up @@ -57,7 +54,7 @@ mixinKeyHandlers = {
reducer: bannedKeyHandler
}

baseApplyMixin = (spec, mixin) ->
export default baseApplyMixin = (spec, mixin) ->
# Force mixin of submixins to happen before everything else.
if mixin.mixins then mixinKeyHandlers.mixins(spec, mixin, 'mixins', mixin.mixins)
# Apply this mixin
Expand All @@ -69,7 +66,3 @@ baseApplyMixin = (spec, mixin) ->
spec[key] = val
# Don't let coffeescript make a comprehension here
undefined

_export = baseApplyMixin

module.exports = _export
14 changes: 4 additions & 10 deletions src/createClass.coffee
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
"use strict"
_export = null

applyMixin = require './applyMixin'
DefaultMixin = require './DefaultMixin'
ReduxComponent = require './ReduxComponent'
import applyMixin from './applyMixin'
import DefaultMixin from './DefaultMixin'
import ReduxComponent from './ReduxComponent'

dontBindThese = {
applyMixin: true
}

createClass = (spec) ->
export default createClass = (spec) ->
# Apply default mixin, then setup the spec
newSpec = { applyMixin }
newSpec.applyMixin(newSpec, DefaultMixin)
Expand All @@ -33,6 +30,3 @@ createClass = (spec) ->
Constructor[k] = v

Constructor

_export = createClass
module.exports = _export
16 changes: 16 additions & 0 deletions src/index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import applyMixin from './applyMixin'
import createClass from './createClass'
import DefaultMixin from './DefaultMixin'
import mountComponent from './mountComponent'
import ReduxComponent from './ReduxComponent'
import { createComponent, SubtreeMixin } from './subtree'

export {
applyMixin
createClass
DefaultMixin
mountComponent
ReduxComponent
createComponent
SubtreeMixin
}
8 changes: 1 addition & 7 deletions src/mountComponent.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
"use strict"
_export = null

defaultMounter = (store, componentInstance) ->
store.replaceReducer(componentInstance.reducer)

mountComponent = (store, componentInstance, path = [], mounter = defaultMounter) ->
export default mountComponent = (store, componentInstance, path = [], mounter = defaultMounter) ->
componentInstance.__mounter = mounter
componentInstance.__willMount(store, path, null)
mounter?(store, componentInstance)
componentInstance.componentDidMount?()

_export = mountComponent
module.exports = _export
17 changes: 6 additions & 11 deletions src/subtree.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict"
_export = null
import { combineReducers } from 'redux'
import invariant from 'inv'
import ReduxComponent from './ReduxComponent'
import createClass from './createClass'

{ combineReducers } = require 'redux'
invariant = require 'inv'
ReduxComponent = require './ReduxComponent'
createClass = require './createClass'

##### SubtreeMixin
attachComponent = (parentComponent, key, component) ->
Expand All @@ -17,7 +15,7 @@ attachComponent = (parentComponent, key, component) ->
applyDescriptor = (parentComponent, key, descriptor) ->
attachComponent(parentComponent, key, createComponent(descriptor))

SubtreeMixin = {
export SubtreeMixin = {
componentWillMount: ->
subtree = @getSubtree(); @__reducerMap = {}
# Conjure child components
Expand Down Expand Up @@ -50,7 +48,7 @@ ReducerNonce = createClass {
displayName: '(reducer)'
}

createComponent = (descriptor) ->
export createComponent = (descriptor) ->
if descriptor instanceof ReduxComponent
descriptor
else if descriptor.prototype and (descriptor.prototype instanceof ReduxComponent)
Expand All @@ -68,6 +66,3 @@ createComponent = (descriptor) ->
throw new Error("pure reducer: descriptor function should be a reducer (must have at least one argument)")
else
throw new Error("invalid component descriptor")

_export = { createComponent, SubtreeMixin }
module.exports = _export
13 changes: 4 additions & 9 deletions src/util.coffee
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
"use strict"
_export = null
export assign = (dst, src) ->
(if src.hasOwnProperty(k) then dst[k] = src[k]) for k of src; dst

assign = (dst, src) -> (if src.hasOwnProperty(k) then dst[k] = src[k]) for k of src; dst
export chain = (one, two) -> -> one.apply(@, arguments); two.apply(@, arguments)

chain = (one, two) -> -> one.apply(@, arguments); two.apply(@, arguments)

get = (object, path) ->
export get = (object, path) ->
index = 0; length = path.length
while object? and index < length
object = object[path[index++]]
if (index is length) then object else undefined

_export = { assign, chain, get }
module.exports = _export

0 comments on commit ef8d176

Please sign in to comment.