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 combineModules:min enabling minified output with uglify #3956

Merged
merged 5 commits into from
Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1526,13 +1526,24 @@
]
},
{
"login":"singhvisha",
"name":"Vishal Singh",
"avatar_url":"https://avatars0.githubusercontent.com/u/38842688?s=460&v=4",
"profile":"https://github.com/singhvisha",
"contributions":[
"login":"singhvisha",
"name":"Vishal Singh",
"avatar_url":"https://avatars0.githubusercontent.com/u/38842688?s=460&v=4",
"profile":"https://github.com/singhvisha",
"contributions": [
"doc"
]
]
},
{
"login": "coreygo",
"name": "Corey Gouker",
"avatar_url": "https://avatars1.githubusercontent.com/u/649879?v=4",
"profile": "https://www.coreygo.com",
"contributions": [
"code",
"doc",
"bug"
]
}
],
"repoType": "github"
Expand Down
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ module.exports = grunt => {
},
dist: {
files: {
'lib/p5.min.js': 'lib/p5.pre-min.js'
'lib/p5.min.js': 'lib/p5.pre-min.js',
'lib/modules/p5Custom.min.js': 'lib/modules/p5Custom.pre-min.js'
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ We recognize all types of contributions. This project follows the [all-contribut
| [<img src="https://avatars3.githubusercontent.com/u/7628664?v=4" width="100px;" alt="Mislav Milicevic"/><br /><sub><b>Mislav Milicevic</b></sub>](https://github.com/dekmm)<br />[💻](https://github.com/processing/p5.js/commits?author=dekmm "Code") [🐛](https://github.com/processing/p5.js/issues?q=author%3Adekmm "Bug reports") | [<img src="https://avatars1.githubusercontent.com/u/25344311?v=4" width="100px;" alt="Yuting Lu"/><br /><sub><b>Yuting Lu</b></sub>](https://github.com/yutinglu413)<br />[📖](https://github.com/processing/p5.js/commits?author=yutinglu413 "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/15272015?v=4" width="100px;" alt="Adil Rabbani"/><br /><sub><b>Adil Rabbani</b></sub>](https://github.com/adilrabbani)<br />[💻](https://github.com/processing/p5.js/commits?author=adilrabbani "Code") [🐛](https://github.com/processing/p5.js/issues?q=author%3Aadilrabbani "Bug reports") [💡](#example-adilrabbani "Examples") | [<img src="https://avatars3.githubusercontent.com/u/908496?v=4" width="100px;" alt="Pierre Krafft"/><br /><sub><b>Pierre Krafft</b></sub>](http://zalastax.github.io/)<br />[🐛](https://github.com/processing/p5.js/issues?q=author%3AZalastax "Bug reports") [💻](https://github.com/processing/p5.js/commits?author=Zalastax "Code") [📖](https://github.com/processing/p5.js/commits?author=Zalastax "Documentation") [💡](#example-Zalastax "Examples") [👀](#review-Zalastax "Reviewed Pull Requests") [⚠️](https://github.com/processing/p5.js/commits?author=Zalastax "Tests") [🔧](#tool-Zalastax "Tools") | [<img src="https://avatars2.githubusercontent.com/u/12074409?v=4" width="100px;" alt="Zoë Ingram"/><br /><sub><b>Zoë Ingram</b></sub>](https://github.com/zoeingram)<br />[📖](https://github.com/processing/p5.js/commits?author=zoeingram "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/6486359?s=460&v=4" width="100px;" alt="Aidan Nelson"/><br /><sub><b>Aidan Nelson</b></sub>](https://github.com/AidanNelson)<br />[🐛](https://github.com/processing/p5.js/issues?q=author%3Aaidannelson "Bug reports") [💻](https://github.com/processing/p5.js/commits?author=aidannelson "Code") [📖](https://github.com/processing/p5.js/commits?author=aidannelson "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/9020979?s=460&v=4" width="100px;" alt="Cameron Yick"/><br /><sub><b>Cameron Yick</b></sub>](https://github.com/hydrosquall)<br />[📖](https://github.com/processing/p5.js/commits?author=hydrosquall "Documentation") |
| [<img src="https://avatars3.githubusercontent.com/u/18724229?v=4" width="100px;" alt="Tanvi Kumar"/><br /><sub><b>Tanvi Kumar</b></sub>](https://github.com/TanviKumar)<br />[🐛](https://github.com/processing/p5.js/issues?q=author%3ATanviKumar "Bug reports") [💻](https://github.com/processing/p5.js/commits?author=TanviKumar "Code") [📖](https://github.com/processing/p5.js/commits?author=TanviKumar "Documentation") [💡](#example-TanviKumar "Examples") | [<img src="https://avatars0.githubusercontent.com/u/7820411?v=4" width="100px;" alt="Katsuya Endoh"/><br /><sub><b>Katsuya Endoh</b></sub>](https://enkatsu.org)<br /> | [<img src="https://avatars1.githubusercontent.com/u/7752014?v=4" width="100px;" alt="Kevin Bradley"/><br /><sub><b>Kevin Bradley</b></sub>](https://github.com/OsakaStarbux)<br />[📖](https://github.com/processing/p5.js/commits?author=OsakaStarbux "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/31749430?v=4" width="100px;" alt="Justin Kim"/><br /><sub><b>Justin Kim</b></sub>](https://justinsunho.com/)<br />[📖](https://github.com/processing/p5.js/commits?author=justinsunho "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/26386270?s=460&v=4" width="100px;" alt="Federico Grandi"/><br /><sub><b>Federico Grandi</b></sub>](https://github.com/EndBug)<br />[💻](https://github.com/processing/p5.js/commits?author=EndBug "Code") [📖](https://github.com/processing/p5.js/commits?author=EndBug "Documentation") | [<img src="https://discourse-cdn-sjc2.com/standard10/user_avatar/discourse.processing.org/freddiera/120/4078_2.png" width="100px;" alt="Freddie Rawlins"/><br /><sub><b>Freddie Rawlins</b></sub>](https://freddierawlins.wixsite.com/site)<br />[💻](https://github.com/processing/p5.js/commits?author=FreddieRa "Code") [📖](https://github.com/processing/p5.js/commits?author=FreddieRa "Documentation") | [<img src="https://media.discordapp.net/attachments/499488127245615135/499488260435869696/normal_luke.png" width="100px;" alt="Luc de wit"/><br /><sub><b>Luc de wit</b></sub>](https://github.com/justlucdewit)<br />[💻](https://github.com/processing/p5.js/commits?author=Luke_ "Code") [🐛](https://github.com/processing/p5.js/issues?q=author%3ALuke_ "Bug reports") |
| [<img src="https://avatars3.githubusercontent.com/u/44824130?s=40&v=4" width="100px;" alt="Mark Nikora"/><br /><sub><b>Mark Nikora</b></sub>](https://github.com/mcuz)<br />[💻](https://github.com/processing/p5.js/commits?author=mcuz "Code") | [<img src="https://avatars3.githubusercontent.com/u/48560751?s=400&u=652ea1a1720b1986c3ea5b96028bdcb5f4f18f96&v=4" width="100px;" alt="Louis Demange"/><br /><sub><b>Louis Demange</b></sub>](https://github.com/Nekzuris)<br />[🐛](https://github.com/processing/p5.js/issues?q=author%3ANekzuris "Bug reports") | [<img src="https://avatars3.githubusercontent.com/u/24548786?v=4" width="100px;" alt="Sanket Singh"/><br /><sub><b>Sanket Singh</b></sub>](https://twitter.com/sanket24singh)<br />[💻](https://github.com/processing/p5.js/commits?author=sanketsingh24 "Code") [🐛](https://github.com/processing/p5.js/issues?q=author%3Asanketsingh24 "Bug reports") [📖](https://github.com/processing/p5.js/commits?author=sanketsingh24 "Documentation") [💡](#example-sanketsingh24 "Examples") | [<img src="https://avatars0.githubusercontent.com/u/2325893?s=460&v=4" width="100px;" alt="Oren Shoham"/><br /><sub><b>Oren Shoham</b></sub>](https://orenshoham.com)<br />[💻](https://github.com/processing/p5.js/commits?author=oshoham "Code") | [<img src="https://avatars0.githubusercontent.com/u/40603139?v=4" width="100px;" alt="Abhinav Sagar"/><br /><sub><b>Abhinav Sagar</b></sub>](https://github.com/abhinavsagar)<br />[💻](https://github.com/processing/p5.js/commits?author=abhinavsagar "Code") | [<img src="https://avatars2.githubusercontent.com/u/13507796?s=40&v=4" width="100px;" alt="Jonathan Heindl"/><br /><sub><b>Jonathan Heindl</b></sub>](https://github.com/jonnytest1)<br />[💻](https://github.com/processing/p5.js/commits?author=jonnytest1 "Code") [💡](#example-jonnytest1 "Examples") [🤔](#ideas-jonnytest1 "Ideas, Planning, & Feedback") [📖](https://github.com/processing/p5.js/commits?author=jonnytest1 "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/11205091" width="100px;" alt="Hirad Sab"/><br /><sub><b>Hirad Sab</b></sub>](https://hiradsab.com)<br />[💻](https://github.com/processing/p5.js/commits?author=hsab "Code") [🐛](https://github.com/processing/p5.js/issues?q=author%3Ahsab "Bug reports") [📖](https://github.com/processing/p5.js/commits?author=hsab "Documentation") [💡](#example-hsab "Examples") |
| [<img src="https://avatars0.githubusercontent.com/u/38842688?s=460&v=4" width="100px;" alt="Vishal Singh"/><br /><sub><b>Vishal Singh</b></sub>](https://github.com/singhvisha)<br />[📖](https://github.com/processing/p5.js/commits?author=singhvisha "Documentation") |
| [<img src="https://avatars0.githubusercontent.com/u/38842688?s=460&v=4" width="100px;" alt="Vishal Singh"/><br /><sub><b>Vishal Singh</b></sub>](https://github.com/singhvisha)<br />[📖](https://github.com/processing/p5.js/commits?author=singhvisha "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/649879?v=4" width="100px;" alt="Corey Gouker"/><br /><sub><b>Corey Gouker</b></sub>](https://www.coreygo.com)<br />[💻](https://github.com/processing/p5.js/commits?author=coreygo "Code") [📖](https://github.com/processing/p5.js/commits?author=coreygo "Documentation") [🐛](https://github.com/processing/p5.js/issues?q=author%3Acoreygo "Bug reports") |
<!-- ALL-CONTRIBUTORS-LIST:END -->
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key))!
33 changes: 25 additions & 8 deletions developer_docs/custom_p5_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,43 @@

## Overview

An excellent new [feature](https://github.com/processing/p5.js/pull/2051) of p5.js allows user to build p5.js as a custom combination of its modules. This greatly helps in reducing the production version size of the library, as well as improves overall performance.
An excellent new [feature](https://github.com/processing/p5.js/pull/2051) of p5.js allows user to build p5.js as a custom combination of its modules. This greatly helps in reducing the production version size of the library, as well as improves overall performance.

This feature was suggested as a part of a proposal for Google Summer of Code 2017.

## Usage
## Usage

Currently, the usage is through invoking a Grunt task manually from the command line.
Currently, the usage is through invoking a Grunt task manually from the command line:

```
```sh
git clone https://github.com/processing/p5.js.git
cd p5.js
npm install
npm run grunt
npm run grunt combineModules:module_x:module_y
```

Here, `module_n` refers to the name of the module which you want to select. Multiple modules must be passed as shown above. Also, these modules must have the same name as their folders in `/src` directory to work correctly. While `core` is included by default, `core/shape` needs to be included for shapes like line() and other core features to work.
Here, `module_n` refers to the name of the modules which you want to select. Multiple modules must be passed as shown above. Also, these modules must have the same name as their folders in `/src` directory to work correctly. While `core` is included by default, `core/shape` needs to be included for shapes like line() and other core features to work.

The above usage example will likely output a `p5Custom.js` larger than the complete `p5.min.js` as the output is not minified using the `uglify` task.

The recommended steps to reduce bundle size as much as possible are:

```sh
git clone https://github.com/processing/p5.js.git
cd p5.js
npm install
npm run grunt
npm run grunt combineModules:min:module_x:module_y uglify
```

## Examples

## Example
- `npm run grunt combineModules:min:core/shape:color:math:image uglify`
Generates a `p5Custom.min.js` bundle in the `lib/modules` directory with the combineModules and uglify tasks. Note that modules should be listed after `combineModules:min` and the `uglify` task should have a space after the modules list.

`npm run grunt combineModules:core/shape:color:math:image`
- `npm run grunt combineModules:core/shape:color:math:image`
Generates a non-minified `p5Custom.js` bundle in the `lib/modules` directory.

This will generate your `p5Custom.js` bundle in the `lib/modules` directory.
- `npm run grunt combineModules:min:core/shape:color:math:image`
Generates a `p5Custom.pre-min.js` in the `lib/modules` directory with the `combineModules:min` task. Note that in this example `npm run grunt uglify` can be run separately after running the `combineModules:min` task.
45 changes: 37 additions & 8 deletions tasks/build/combineModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const path = require('path');
const browserify = require('browserify');
const derequire = require('derequire');
const { format } = require('prettier');

module.exports = function(grunt) {
grunt.registerTask(
Expand All @@ -16,12 +17,14 @@ module.exports = function(grunt) {
// Module sources are space separated names in a single string (enter within quotes)
const temp = [];
for (const arg of arguments) {
temp.push(arg);
if (arg !== 'min') {
temp.push(arg);
}
}
const module_src = temp.join(', ');

// Render the banner for the top of the file. Includes the Module name.
const bannerTemplate = `/*! Custom p5.js v<%= pkg.version %> <%= grunt.template.today("mmmm dd, yyyy") %>
const bannerTemplate = `/*! Custom p5.js v<%= pkg.version %> <%= grunt.template.today("mmmm dd, yyyy") %>
Contains the following modules : ${module_src}*/`;
const banner = grunt.template.process(bannerTemplate);

Expand Down Expand Up @@ -54,15 +57,25 @@ module.exports = function(grunt) {
}

console.log(srcFilePath);
// Check whether combineModules is minified
const isMin = args === 'min';
const filename = isMin ? 'p5Custom.pre-min.js' : 'p5Custom.js';

// Target file path
const libFilePath = path.resolve('lib/modules/p5Custom.js');
const libFilePath = path.resolve('lib/modules/' + filename);

// Invoke Browserify programatically to bundle the code
const bundle = browserify(srcFilePath, {
let browseified = browserify(srcFilePath, {
standalone: 'p5'
})
.transform('babelify', { plugins: ['static-fs'] })
.bundle();
});

if (isMin) {
browseified = browseified.exclude('../../docs/reference/data.json');
}

const babelifyOpts = { plugins: ['static-fs'] };

const bundle = browseified.transform('babelify', babelifyOpts).bundle();

// Start the generated output with the banner comment,
let code = banner + '\n';
Expand All @@ -73,7 +86,23 @@ module.exports = function(grunt) {
code += data;
})
.on('end', function() {
grunt.file.write(libFilePath, derequire(code));
// "code" is complete: create the distributable UMD build by running
// the bundle through derequire
// (Derequire changes the bundle's internal "require" function to
// something that will not interfere with this module being used
// within a separate browserify bundle.)
code = derequire(code);

// and prettify the code
if (!isMin) {
code = format(code, {
singleQuote: true,
printWidth: 80 + 12
});
}

// finally, write it to disk
grunt.file.write(libFilePath, code);

// Print a success message
grunt.log.writeln(
Expand Down