-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework export #13
Rework export #13
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review as of 9dd3615. Good job overall, but I have 2 main issues:
- It seems that the package won't work on Node without using
require('babel-register')
beforehand (which is what happens during testing). Transpiling code with Babel to a separate dir (say,lib
) may be a better choice. This can be solved in a separate PR, though this
binding to manage imports is awkward (e.g., how would it work with non-function exports?); useimport
from individual files instead. I'd prefer for this to be solved here
src/blockchain/block.js
Outdated
|
||
/** | ||
* Verifies block | ||
* @param {Object} data | ||
* @param {Array} validators | ||
* @return {Boolean} | ||
*/ | ||
Exonum.verifyBlock = function(data, validators) { | ||
if (Exonum.isObject(data) === false) { | ||
exports.verifyBlock = function(data, validators) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you use ES6's import
statement and export * from ...
, you may use
export function verifyBlock (data, validators) {
// ...
}
src/blockchain/block.js
Outdated
Exonum.verifyBlock = function(data, validators) { | ||
if (Exonum.isObject(data) === false) { | ||
exports.verifyBlock = function(data, validators) { | ||
var Block = this.newType({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I like binding to this
everywhere. IMO, exports like this could be cleaner:
import * as types from './types';
// later
const Block = types.newType({
// ...
});
That is, you import exactly what you need from files you need.
src/blockchain/index.js
Outdated
@@ -0,0 +1,5 @@ | |||
'use strict'; | |||
|
|||
export * from './merkle'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ES6 exports 👍
Gruntfile.js
Outdated
} | ||
}, | ||
mochaTest: { | ||
options: { | ||
reporter: 'spec' | ||
reporter: 'spec', | ||
require: ['babel-register'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure about this. I mean, if we want to publish to npm/yarn, we would want to transpile the source code using Babel to something that Node understands; afaik, Node does not understand import
/export
. Maybe, perform transpiling with the prepublish
hook; also see this random repo that seems to do this. As a random (?) coincidence, note that we have source files in the src/
directory, which matches the scenario in which they are transpiled to the lib/
directory before the publication.
Putting it all together: it could be beneficial to have a build
(or prepublish
) Grunt task, which would transpile source files from src/
to lib/
, and which could be plugged into scripts.prepublish
section of package.json
and other Grunt tasks (e.g., tests).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, added prepublish
scenario which prepare minified browser version (dist
folder) and node version (lib
folder).
src/blockchain/merkle-patricia.js
Outdated
@@ -51,23 +48,23 @@ Exonum.merklePatriciaProof = function(rootHash, proofNode, key, type) { | |||
var elementsHash; | |||
|
|||
if (typeof data === 'string') { | |||
if (Exonum.validateHexHash(data) === true) { | |||
if (self.validateHexHash.call(self, data) === true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per a comment above, .call(self, ...)
looks overly verbose. Wouldn't it be better to have something like
import { validateHexHash } from './validators';
// ...
if (validateHexHash(data)) // ...
Additionally, in this case the function could be moved to the top level, improving readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly good as of 39e8477, with a couple of nitpicks.
Gruntfile.js
Outdated
}, | ||
babel: { | ||
options: { | ||
sourceMap: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure source maps are needed here? The transpiled files are still human-readable, so they may be a little superfluous, imo. Maps add quite an overhead (~100% of the source size, on the first glance).
dist: { | ||
src: ['./dist'] | ||
} | ||
}, | ||
jshint: { | ||
files: ['./src/index.js'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: you should probably specify ./src/**/*.js
here; otherwise, only a single file is linted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to separate issue #15
src/index.js
Outdated
@@ -1,10 +1,13 @@ | |||
'use strict'; | |||
var Exonum = require('../src/core'); | |||
import * as types from './types/index'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file could be simplified to
export * from './types';
export * from './crypto';
export * from './blockchain';
(Note that mentioning /index
files is optional, as is the .js
extension.) I don't quite like that every export is collected in a single namespace, but changing it would be a breaking change.
* @param {Object} type | ||
* @returns {NewType} | ||
*/ | ||
export function newType(type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't quite like this code and the similar code for messages, but I suppose it's OK for the purpose of this PR.
.babelrc
Outdated
@@ -0,0 +1,5 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if .babelrc
is actually needed if the Babel config is specified in the Grunt task. grunt prepublish
seems to be working without this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM as of 763b23a.
# Conflicts: # .gitignore # Gruntfile.js # dist/exonum-client.js # dist/exonum-client.min.js # src/blockchain/block.js # src/blockchain/merkle-patricia.js # src/blockchain/merkle.js # src/crypto/index.js # src/data-management.js # src/helpers.js # src/types/convert.js # src/types/primitive.js # src/types/serialization.js # src/types/validate.js
# Conflicts: # .gitignore # Gruntfile.js # dist/exonum-client.js # dist/exonum-client.min.js # src/blockchain/block.js # src/blockchain/merkle-patricia.js # src/blockchain/merkle.js # src/crypto/index.js # src/data-management.js # src/helpers.js # src/types/convert.js # src/types/primitive.js # src/types/serialization.js # src/types/validate.js
Implemented a flow described at issue #5.