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

ADD: Webpack module bundler to compile code from /src/ to /dist/ #512

Merged
merged 16 commits into from
Feb 6, 2020
Merged

ADD: Webpack module bundler to compile code from /src/ to /dist/ #512

merged 16 commits into from
Feb 6, 2020

Conversation

VladimirMikulic
Copy link
Contributor

@VladimirMikulic VladimirMikulic commented Jan 7, 2020

Up to this point, we have been writing old ES5.
This change introduces Webpack, Webpack development server, Babel...
From now on, we will be able to write modern JavaScript.

I know this is a game-changer for the project, so I'll do my best to explain what I did.

Webpack 🎉 🎉 🎉

Let's start from NPM scripts:

  • build
  • dev

build NPM script

The script uses cross-env package to set NODE_ENV environment variable.
It's cross-platform environment variables setter.

The NODE_ENV variable is used by Webpack to determine how it should bundle files.
Webpack has 2 modes:

  1. production
  2. development

In the production mode, it will minify JS, do various optimizations...
In the development mode, it won't do that many optimizations because it would slow down our Webpack development server. That's why it's called development mode.

By running this script, your JS is ready to be deployed.
I removed leaflet.distortableimage.js from the git tracker. Should I add it back?

dev NPM script
The script runs the build script and starts a Webpack development server, which opens examples/index.html file in the browser.

We run build script first because it produces vendor.js & vendor.css files.
The files contain 3rd party JS and CSS.

Previously, we included a bunch of script and link tags, now it's all bundled in these 2 files.
(webpack.config.js:72 MergeIntoSingleFilePlugin)

Grunt

In the Gruntfile.js Webpack is loaded. It is used by build script(grunt build).
The watch task has been removed in favour of Webpack development server, which is much more flexible.

Karma

In karma.conf.js, I've introduced karma-babel-preprocessor. As the name suggests it will transpile our ESNext to ES5, so Karma can run our tests.

webpack.config.js

  • entry -> Grunt is fired :)
  • output -> outputs the bundle
  • module -> contains babel-loader(it transpiles our JS to old ES5)
  • devServer -> configuration object for Webpack development server
  • devtool -> genereates leaflet.distortableimage.map.js file
  • stats -> minimizes Webpack's terminal output

The if statement executes if we are running Webpack in the production mode.
Including these plugins from the if statement directly in plugins key on the config object doesn't make sense. It drastically slows down our server(2s+ reload time!) and these are the files from node_modules, we don't modify them :)

If something is unclear, feel free to ask.
It took me quite a bit of time and research and hopefully it'll benefit the project in the long run 👍

We can also copy this file to other PublicLab projects and properly adjust it. 📝
Everyone should enjoy the beauty of ESNext 😄

Resolves #372

@cesswairimu @jywarren @sashadev-sky could you review this? Thanks.

@jywarren
Copy link
Member

jywarren commented Jan 7, 2020

OMGGGGGGG this is so awesome!!!! Ok, this is a big one. We'll want to be careful, and it may take some time to be thorough. AMAZING WORK @VladimirMikulic !!!!! 🎉

@jywarren
Copy link
Member

jywarren commented Jan 7, 2020

And noting Sasha is offline and not available right away!

@jywarren
Copy link
Member

jywarren commented Jan 7, 2020

So now, we can do includes via require(...)? I haven't used webpack as much as browserify but assume it's pretty similar.

@rexagod this may make #312 more possible now, if I remember correctly?

@jywarren jywarren changed the title ADD: Webpack module bundler ADD: Webpack module bundler to compile code from /src/ to /dist/ Jan 7, 2020
@jywarren
Copy link
Member

jywarren commented Jan 7, 2020

I changed the title to be more detailed, i hope it's correct: to compile code from /src/ to /dist/

Also, let's start working on getting the above description in as readable a form as possible into the README file?

@VladimirMikulic
Copy link
Contributor Author

So now, we can do includes via require(...)? I haven't used webpack as much as browserify but assume it's pretty similar.

Correct, you can use either require('module') or import 'module'.

@cesswairimu
Copy link
Collaborator

Wow awesome 🎉 🎉

@VladimirMikulic
Copy link
Contributor Author

VladimirMikulic commented Jan 7, 2020

@jywarren Alright, I updated the README to show project set up with Webpack.
This is for average contributors. They don't need to know the inner working of webpack.config.js.

Maybe I can write a separate file like WEBPACK.md where I explain every single line of the configuration file? It would be for core project maintainers.

What do you think?

@jywarren
Copy link
Member

jywarren commented Jan 7, 2020 via email

@VladimirMikulic
Copy link
Contributor Author

@jywarren I've documented Webpack configuration file. It should be understandable now :)

@sashadev-sky
Copy link
Member

will go through this asap!

@jywarren
Copy link
Member

@VladimirMikulic maybe rebuild that dist file and add it? Then if @sashadev-sky approves we can merge 🎉

@VladimirMikulic
Copy link
Contributor Author

@jywarren 👍

@jywarren
Copy link
Member

Hi @sashadev-sky - no rush, but if you happen to have a moment to read this through it's really big and would be great to merge before too many other PRs!

@VladimirMikulic
Copy link
Contributor Author

Let's do this 😄

Copy link
Member

@sashadev-sky sashadev-sky left a comment

Choose a reason for hiding this comment

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

Thanks this is great - really appreciate you working through all the webpack configs here.

I made some change requests and besides those:

  1. Is it possible to enable running the tests during development as well? I think they are super helpful!
  2. Is it possible to make it so that the user doesn't have to run $ npm run build after changes? I think a lot of people will forget and commit their dist file unminified. Maybe a pre-commit hook?
  3. I think webpack is meant to be integrated with either grunt or npm scripts, not both? Is it possible to shift to one of those directions? (Don't worry about the $ grunt icons task just referring to the build, server, tests, linting)

@VladimirMikulic
Copy link
Contributor Author

VladimirMikulic commented Jan 18, 2020

@sashadev-sky thank you very much for taking time to review this.

Is it possible to enable running the tests during development as well? I think they are super helpful!

Yes, it is, but then we would sacrifice our Webpack development server.
I already thought about this and I've chosen Webpack server to be more important in development.

In my opinion, running tests on every minor change is just adding overhead. After the developer finishes the work, he runs npm build and then tests will warn him if he has done something wrong.

One possible workaround to keep both would be to return the grunt watch task and make it run tests on change. A developer would need 2 terminal windows (tabs if you are using modern terminal), but that shouldn't be a problem, right?

Is it possible to make it so that the user doesn't have to run $ npm run build after changes? I think a lot of people will forget and commit their dist file unminified. Maybe a pre-commit hook?

Pre-commit hook would be awesome 👍 I'll add it.

I think webpack is meant to be integrated with either grunt or npm scripts, not both? Is it possible to shift to one of those directions? (Don't worry about the $ grunt icons task just referring to the build, server, tests, linting)

Not really, it depends on the project. NPM scripts serves as an interface for easier management of our project. If you take a look at my npm build command you'll notice that it does quite a few things.

If we were to rewrite it to stay in Gruntfile.js, we would have to use 3rd party plugin and it wouldn't be as clean as it is now.

I've integrated Webpack with Grunt because it's not possible to go with Webpack-only approach in our project. They are different things built for different purposes.
Grunt is a task runner and Webpack is a module bundler.

For example, JSHint is not supported in Webpack. We would need to adjust the coverage as well (if it's even possible).

I hope this clears your doubts. Thanks a lot!

@jywarren
Copy link
Member

One possible workaround to keep both would be to return the grunt watch task and make it run tests on change. A developer would need 2 terminal windows (tabs if you are using modern terminal), but that shouldn't be a problem, right?

I wonder, would it be possible to have a variant command which allows running both at one if you want? I know everyone has a slightly different workflow.

Thanks, both of you!!

@VladimirMikulic
Copy link
Contributor Author

@jywarren, unfortunately, it's not possible. grunt watch starts a process and Webpack development server starts its own process. These are 2 different processes and they can not run in the same shell instance. That's why I mentioned double windows.

I can get rid of WDS if you'd prefer that approach.

@sashadev-sky
Copy link
Member

One possible workaround to keep both would be to return the grunt watch task and make it run tests on change. A developer would need 2 terminal windows (tabs if you are using modern terminal), but that shouldn't be a problem, right?

Its no problem let's do it! @VladimirMikulic

VladimirMikulic and others added 16 commits January 29, 2020 19:54
Up to this point, we have been writing old ES5.
This change introduces Webpack, Webpack development server, Babel...
From now on, we will be able to write modern JavaScript.

Resolves #372
"Contributing" section updated to show project setup with Webpack.
The documentation for the Webpack configuration file can be found in
WEBPACK.md
Added to resolve merge conflict.
Fixed redundant spaces and a grammar issue.
Changes:
 - Add npm build script to pre-commit task
 - Remove redundant Grunt's "default" task
 - Empty line to .gitignore
 - Move vendor.css to load before our css
 - Change WDS port from 3000 to 8080
 - Update README.md
Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>
Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>
Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>
Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>
Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>
Karma configuration modified to generate the coverage folder inside of
the working directory and without PhantomJS subfolder.

Minor change: browserlist update to include browsers with the usage >
0.2%

Resolves #537
@VladimirMikulic
Copy link
Contributor Author

@sashadev-sky no worries. The configuration has been updated properly :)

@sashadev-sky
Copy link
Member

ok lets do this!!!

@sashadev-sky sashadev-sky merged commit be33b46 into publiclab:main Feb 6, 2020
@VladimirMikulic
Copy link
Contributor Author

🚀 🚀 🎉 🎉

@sashadev-sky
Copy link
Member

Congrats on GCI!!!! Great work

@VladimirMikulic
Copy link
Contributor Author

Thanks a lot @sashadev-sky. It's been a pleasure to work with you :)

@sashadev-sky
Copy link
Member

you too! Published this on npm

@jywarren
Copy link
Member

jywarren commented Feb 8, 2020

Wow this is thrilling! Amazing work folks! cc @rexagod as this is now ready for some portions of es6 upgrades due to webpack usage.

@jywarren
Copy link
Member

jywarren commented Feb 8, 2020

@jywarren
Copy link
Member

jywarren commented Feb 8, 2020

Oops actually #312

@VladimirMikulic
Copy link
Contributor Author

Thanks, @jywarren. We did it :)

sashadev-sky added a commit that referenced this pull request Apr 13, 2020
* ADD: Webpack module bundler

Up to this point, we have been writing old ES5.
This change introduces Webpack, Webpack development server, Babel...
From now on, we will be able to write modern JavaScript.

Resolves #372

* UPDATE: README "Contributing" section

"Contributing" section updated to show project setup with Webpack.

* ADD: Webpack config file documentation

The documentation for the Webpack configuration file can be found in
WEBPACK.md

* ADD: "npm run build" to travis.yml

* ADD: leaflet.distortableimage.js to git tracker

Added to resolve merge conflict.

* UPDATE: Webpack.md

Fixed redundant spaces and a grammar issue.

* UPDATE: Project configuration files

Changes:
 - Add npm build script to pre-commit task
 - Remove redundant Grunt's "default" task
 - Empty line to .gitignore
 - Move vendor.css to load before our css
 - Change WDS port from 3000 to 8080
 - Update README.md

* ADD: "watch" Grunt task to run tests on change

* Update README.md

Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>

* Update README.md

Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>

* Update README.md

Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>

* Update WEBPACK.md

Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>

* Update README.md

Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>

* v0.12.6 🚀🚀🚀

* FIX: Karma tests coverage

Karma configuration modified to generate the coverage folder inside of
the working directory and without PhantomJS subfolder.

Minor change: browserlist update to include browsers with the usage >
0.2%

Resolves #537

* Rebase the branch

Co-authored-by: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>
sashadev-sky added a commit to carlo-ev/Leaflet.DistortableImage that referenced this pull request Apr 14, 2020
…liclab#512)

* ADD: Webpack module bundler

Up to this point, we have been writing old ES5.
This change introduces Webpack, Webpack development server, Babel...
From now on, we will be able to write modern JavaScript.

Resolves publiclab#372

* UPDATE: README "Contributing" section

"Contributing" section updated to show project setup with Webpack.

* ADD: Webpack config file documentation

The documentation for the Webpack configuration file can be found in
WEBPACK.md

* ADD: "npm run build" to travis.yml

* ADD: leaflet.distortableimage.js to git tracker

Added to resolve merge conflict.

* UPDATE: Webpack.md

Fixed redundant spaces and a grammar issue.

* UPDATE: Project configuration files

Changes:
 - Add npm build script to pre-commit task
 - Remove redundant Grunt's "default" task
 - Empty line to .gitignore
 - Move vendor.css to load before our css
 - Change WDS port from 3000 to 8080
 - Update README.md

* ADD: "watch" Grunt task to run tests on change

* Update README.md

Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>

* Update README.md

Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>

* Update README.md

Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>

* Update WEBPACK.md

Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>

* Update README.md

Co-Authored-By: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>

* v0.12.6 🚀🚀🚀

* FIX: Karma tests coverage

Karma configuration modified to generate the coverage folder inside of
the working directory and without PhantomJS subfolder.

Minor change: browserlist update to include browsers with the usage >
0.2%

Resolves publiclab#537

* Rebase the branch

Co-authored-by: Sasha Boginsky <41092741+sashadev-sky@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert to ES6
5 participants