From ef8d1760f788f1ed453d20a998056f2e85dbd77f Mon Sep 17 00:00:00 2001 From: "William C. Johnson" Date: Wed, 26 Oct 2016 18:12:18 -0400 Subject: [PATCH] CoffeeScript 1.11 now supports ES modules! - 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) --- .babelrc | 36 +++++++++++++++++++++++++- .gitignore | 4 ++- .npmignore | 2 ++ ReduxComponent.js | 2 +- SubtreeMixin.js | 2 +- createClass.js | 2 +- createComponent.js | 2 +- index.js | 12 +++------ mountComponent.js | 2 +- package.json | 53 +++++++++++++++++++++++++++++++-------- src/DefaultMixin.coffee | 10 ++------ src/ReduxComponent.coffee | 10 ++------ src/applyMixin.coffee | 13 +++------- src/createClass.coffee | 14 +++-------- src/index.coffee | 16 ++++++++++++ src/mountComponent.coffee | 8 +----- src/subtree.coffee | 17 +++++-------- src/util.coffee | 13 +++------- 18 files changed, 130 insertions(+), 88 deletions(-) create mode 100644 src/index.coffee diff --git a/.babelrc b/.babelrc index 978b807..f3b41e9 100644 --- a/.babelrc +++ b/.babelrc @@ -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": [ + ] + } + } +} diff --git a/.gitignore b/.gitignore index af30734..641f8f2 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,6 @@ node_modules # Build products dist -lib +lib/ +build/ +es/ diff --git a/.npmignore b/.npmignore index caac28e..f1ff32b 100644 --- a/.npmignore +++ b/.npmignore @@ -4,7 +4,9 @@ test/ *.swp .DS_Store src/ +coverage/ scripts/ +build/ book.json coffeelint.json .eslintrc diff --git a/ReduxComponent.js b/ReduxComponent.js index 0c07ab9..9faebf9 100644 --- a/ReduxComponent.js +++ b/ReduxComponent.js @@ -1,4 +1,4 @@ /////////////////////////////////////////////////////////////////////// // ES5 EXPORT STUB /////////////////////////////////////////////////////////////////////// -module.exports = require('./lib/ReduxComponent'); +module.exports = require('./lib').ReduxComponent; diff --git a/SubtreeMixin.js b/SubtreeMixin.js index 921739c..680e86d 100644 --- a/SubtreeMixin.js +++ b/SubtreeMixin.js @@ -1,4 +1,4 @@ /////////////////////////////////////////////////////////////////////// // ES5 EXPORT STUB /////////////////////////////////////////////////////////////////////// -module.exports = require('./lib/subtree').SubtreeMixin; +module.exports = require('./lib').SubtreeMixin; diff --git a/createClass.js b/createClass.js index 886bef2..df671af 100644 --- a/createClass.js +++ b/createClass.js @@ -1,4 +1,4 @@ /////////////////////////////////////////////////////////////////////// // ES5 EXPORT STUB /////////////////////////////////////////////////////////////////////// -module.exports = require('./lib/createClass'); +module.exports = require('./lib').createClass; diff --git a/createComponent.js b/createComponent.js index b89d4cd..370e1bc 100644 --- a/createComponent.js +++ b/createComponent.js @@ -1,4 +1,4 @@ /////////////////////////////////////////////////////////////////////// // ES5 EXPORT STUB /////////////////////////////////////////////////////////////////////// -module.exports = require('./lib/subtree').createComponent; +module.exports = require('./lib').createComponent; diff --git a/index.js b/index.js index b69894c..0ea808c 100644 --- a/index.js +++ b/index.js @@ -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'); diff --git a/mountComponent.js b/mountComponent.js index 53e3266..b97fe18 100644 --- a/mountComponent.js +++ b/mountComponent.js @@ -1,4 +1,4 @@ /////////////////////////////////////////////////////////////////////// // ES5 EXPORT STUB /////////////////////////////////////////////////////////////////////// -module.exports = require('./lib/mountComponent'); +module.exports = require('./lib').mountComponent; diff --git a/package.json b/package.json index a5d7c55..a4430ac 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/src/DefaultMixin.coffee b/src/DefaultMixin.coffee index 099e5b8..b6d6065 100644 --- a/src/DefaultMixin.coffee +++ b/src/DefaultMixin.coffee @@ -1,7 +1,4 @@ -"use strict" -_export = null - -{ get } = require './util' +import { get } from './util' slice = [].slice # Scope a selector to a component. @@ -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 @@ -32,6 +29,3 @@ DefaultMixin = { # Make sure coffeescript doesn't generate an extra array here. undefined } - -_export = DefaultMixin -module.exports = _export diff --git a/src/ReduxComponent.coffee b/src/ReduxComponent.coffee index b9a13a2..eacb2c5 100644 --- a/src/ReduxComponent.coffee +++ b/src/ReduxComponent.coffee @@ -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.") @@ -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 diff --git a/src/applyMixin.coffee b/src/applyMixin.coffee index 11c8d23..3b2ccbf 100644 --- a/src/applyMixin.coffee +++ b/src/applyMixin.coffee @@ -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) -> @@ -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 @@ -69,7 +66,3 @@ baseApplyMixin = (spec, mixin) -> spec[key] = val # Don't let coffeescript make a comprehension here undefined - -_export = baseApplyMixin - -module.exports = _export diff --git a/src/createClass.coffee b/src/createClass.coffee index 7094109..a925b3d 100644 --- a/src/createClass.coffee +++ b/src/createClass.coffee @@ -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) @@ -33,6 +30,3 @@ createClass = (spec) -> Constructor[k] = v Constructor - -_export = createClass -module.exports = _export diff --git a/src/index.coffee b/src/index.coffee new file mode 100644 index 0000000..f56edf6 --- /dev/null +++ b/src/index.coffee @@ -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 +} diff --git a/src/mountComponent.coffee b/src/mountComponent.coffee index 6e45e83..96b4429 100644 --- a/src/mountComponent.coffee +++ b/src/mountComponent.coffee @@ -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 diff --git a/src/subtree.coffee b/src/subtree.coffee index 5431fa9..39d1363 100644 --- a/src/subtree.coffee +++ b/src/subtree.coffee @@ -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) -> @@ -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 @@ -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) @@ -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 diff --git a/src/util.coffee b/src/util.coffee index b24b496..f6b4ba6 100644 --- a/src/util.coffee +++ b/src/util.coffee @@ -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