Skip to content

Modularize javascript - #590

Closed
HelgeKrueger wants to merge 5 commits into
graphhopper:masterfrom
HelgeKrueger:modularize_javascript
Closed

HelgeKrueger wants to merge 5 commits into
graphhopper:masterfrom
HelgeKrueger:modularize_javascript

Conversation

@HelgeKrueger

Copy link
Copy Markdown
Contributor

This pull request splits the main.js file into several node modules and uses browserify to recreate main.js. With node.js installed main.js can be created using npm run bundle inside the web folder.

See discussion at https://discuss.graphhopper.com/t/modularize-javascript-code/306/16

Installation:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash
# close and reopen terminal now
nvm install 4.2.2
nvm use 4.2.2

# git clone https://github.com/graphhopper/graphhopper.git
cd graphhopper/web
# download required packages:
npm install
npm test
# create main.js
npm run bundle

cd ..
./graphhopper.sh web africa_canary-islands.pbf
firefox localhost:8989

If you wanted to add the underscore library to the project, then you would use npm install --save underscore command. This command automatically modifies package.json to include underscore and a version info. This means that the next person running nvm install would also download underscore.

# For development just use watchify:
npm run watch
# see the package.json where several other scripts like bundleDebug are defined
# bundle creates the main file
npm run bundle
# create main.js for debugging
npm run bundleDebug
npm run bundleUgly

# Forcing consistent code style with jshint. JsHint can be run with:
npm run jshint

To use npm to build main.js use:
- npm install to download required packages
- npm run bundle to create main.js
@HelgeKrueger
HelgeKrueger force-pushed the modularize_javascript branch from 4de2fc0 to 5edebca Compare December 3, 2015 08:58
@karussell

Copy link
Copy Markdown
Member

Thanks again :) !

Do we still provide an out of the box compiled main.js? Or do you think that this is a good solution?

@HelgeKrueger

Copy link
Copy Markdown
Contributor Author

My last commit looks odd. It was meant to include the precompiled minified main.js. This should be fixed now.

npm run bundleUgly

it is 600kB compared to 1.2 MB from npm run bundle
@HelgeKrueger
HelgeKrueger force-pushed the modularize_javascript branch from 5edebca to fe678a5 Compare December 3, 2015 17:09
JsHint can be run with
npm run jshint

Including automatic running of jshint and jasmine test in .travis.yml
@HelgeKrueger
HelgeKrueger force-pushed the modularize_javascript branch from 3ca208d to af70db7 Compare December 4, 2015 21:18
@HelgeKrueger

Copy link
Copy Markdown
Contributor Author

I added the jasmine test to travis-ci and added jshint, which is style checker for JavaScript. I also added various missing semicolons, that jshint found. I also pressed the close and comment button by accident.

Comment thread .travis.yml Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmh, as the web jar is also built in the core/files we should either avoid a separate web/files script or put the web jar building into the web/files I fear :)

@karussell

Copy link
Copy Markdown
Member

Thanks again for this work and also for making the tests working even on travis :) ! Is npm installed per default there?

Is leaflet.loading.js removed? See the functionality: https://github.com/ebrelsford/Leaflet.loading

@HelgeKrueger

Copy link
Copy Markdown
Contributor Author

npm is installed by default on travis-ci.

Comment thread web/package.json

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaflet loading inclusion part 1.

@karussell

Copy link
Copy Markdown
Member

Thanks!

I'm still struggeling to understand how I would enable 'debug mode'. It is still possible to change something in the source code and hit F5 without a build command in-between?

And also I've added a bit installation instructions in your initial comment of this PR ... would you mind to explain the different bundle commands (see TODO)?

@karussell

Copy link
Copy Markdown
Member

It looks like watchify is the solution (?)

npm install -g watchify
watchify src/main/webapp/js/main-template.js -o src/main/webapp/js/main.js

Will add a new script - do we need to include watchify in package.json too?

"watch": "watchify src/main/webapp/js/main-template.js -o src/main/webapp/js/main.js --debug --verbose",

BTW: There are million of other ways but watchify is good IMO.

Also should we rename main.js to bundle.js and main-template.js to index.js or main.js to be more align with the normal namings for npm projects?

@karussell karussell added this to the 0.6 milestone Dec 8, 2015
karussell pushed a commit that referenced this pull request Dec 8, 2015
@karussell

Copy link
Copy Markdown
Member

Merged it here 38e1c32

One open question for me: "Also should we rename main.js to bundle.js and main-template.js to index.js or main.js to be more align with the normal namings for npm projects?"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants