Skip to content
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

Merged
merged 14 commits into from
May 30, 2017
Merged

Rework export #13

merged 14 commits into from
May 30, 2017

Conversation

boguslavsky
Copy link
Contributor

Implemented a flow described at issue #5.

@boguslavsky boguslavsky mentioned this pull request May 24, 2017
9 tasks
Copy link
Contributor

@slowli slowli left a 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?); use import from individual files instead. I'd prefer for this to be solved here


/**
* 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) {
Copy link
Contributor

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) {
 // ...
}

Exonum.verifyBlock = function(data, validators) {
if (Exonum.isObject(data) === false) {
exports.verifyBlock = function(data, validators) {
var Block = this.newType({
Copy link
Contributor

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.

@@ -0,0 +1,5 @@
'use strict';

export * from './merkle';
Copy link
Contributor

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']
Copy link
Contributor

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).

Copy link
Contributor Author

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).

@@ -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) {
Copy link
Contributor

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.

@boguslavsky
Copy link
Contributor Author

boguslavsky commented May 25, 2017

@slowli ready for review since c6ce6b1

P.S.: please do not merge #13 before #12 will be merged and all conflicts will be resolved

Copy link
Contributor

@slowli slowli left a 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,
Copy link
Contributor

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'],
Copy link
Contributor

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.

Copy link
Contributor Author

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';
Copy link
Contributor

@slowli slowli May 25, 2017

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) {
Copy link
Contributor

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 @@
{
Copy link
Contributor

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.

Copy link
Contributor

@slowli slowli left a 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.

Ivan Bohuslavskyi added 2 commits May 30, 2017 17:54
# 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
@boguslavsky boguslavsky merged commit 826821f into exonum:master May 30, 2017
@boguslavsky boguslavsky deleted the rework-export branch May 30, 2017 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants