项目作者: ssrwpo

项目描述 :
Meteor package that exposes options for UglifyJS2 JS minifier
高级语言: JavaScript
项目地址: git://github.com/ssrwpo/uglifyjs2.git
创建时间: 2017-01-08T05:45:53Z
项目社区:https://github.com/ssrwpo/uglifyjs2

开源协议:MIT License

下载


Description

A JS minifier with more aggressive set of default options. Options are configurable
in your project’s package.json.

Interesting side effect This package increases security by removing part of
your server side code which is normally included by the Meteor’s standard minifier.

Usage

  1. meteor remove standard-minifier-js
  2. meteor add ssrwpo:uglifyjs2

Configuration

Default options

The following default options are activated:

  1. uglifyjs2: {
  2. deadCodes: [
  3. '_meteor.Meteor.isServer',
  4. 'Meteor.isServer'
  5. ],
  6. fileRemoval: [
  7. 'packages/ddp-server.js',
  8. 'packages/shell-server.js',
  9. 'packages/ssrwpo_uglifyjs2.js'
  10. ],
  11. packageDebug: false,
  12. options: {
  13. fromString: true,
  14. compress: {
  15. properties: true,
  16. dead_code: true,
  17. drop_debugger: true,
  18. conditionals: true,
  19. comparisons: true,
  20. evaluate: true,
  21. booleans: true,
  22. loops: true,
  23. unused: true,
  24. hoist_funs: true,
  25. if_return: true,
  26. join_vars: true,
  27. cascade: true,
  28. collapse_vars: true,
  29. negate_iife: true,
  30. pure_getters: true,
  31. drop_console: true,
  32. keep_fargs: false,
  33. keep_fnames: false,
  34. passes: 2,
  35. global_defs: {
  36. UGLYFYJS_DEAD: false
  37. }
  38. }
  39. }
  40. }

Dead code elimination

deadCodes is a list of Strings is used for text replacement as a global definition
transformed into UGLYFYJS_DEAD allowing minifications with dead code removal.
This acts as macro for code removal in your project.

In your package.json, add the deadCodes option to additional dead code removals:

  1. "uglifyjs2": {
  2. ...
  3. "deadCodes": ["Meteor.isServer", ...],
  4. ...
  5. }

By default, the list only contains: ['Meteor.isServer'].

UglifyJS2 options

Overwrite the default options to add or remove UglifyJS2’s options.

In your package.json, add the options for tweaking UglifyJS2’s options:

  1. "uglifyjs2": {
  2. ...
  3. "options": {
  4. "fromString": true,
  5. "compress": {
  6. ...
  7. "properties": false,
  8. ...
  9. }
  10. }
  11. ...
  12. }

:warning: Meteor relies on text analysis when using UglifyJS2. Therefore,
removing the fromString option leads to unexpected results.

Development minification

Meteor’s standard minifier skip minification while in development. As dead code
removal could affect your project’s behavior, this package allows you to minify
your code while developping.

In your package.json, add the development option to activate minification
while in development mode:

  1. "uglifyjs2": {
  2. ...
  3. "development": true,
  4. ...
  5. }

Package debug options

This adds some debug informations while processing production build. It helps
tracking the files that will get included into the web bundle JS file.

For activating it, simply add the packageDebug option in your package.json.

  1. "uglifyjs2": {
  2. ...
  3. "packageDebug": true,
  4. ...
  5. }

File removals

Meteor injects files in the client that doesn’t belong to the client. This is
used by Meteor to display informations on used packages in the server. Removing
these informations increase security and removes some unecessary lost bytes.

Some defaults files are removed automatically with this package and you can
tweak these removals using fileRemoval options in your package.json.

  1. "uglifyjs2": {
  2. ...
  3. "fileRemoval": [
  4. "packages/ddp-server.js",
  5. "packages/shell-server.js",
  6. "packages/ssrwpo_uglifyjs2.js"
  7. ],
  8. ...
  9. }

Aggressive minification

By default, minification is done file by file. Aggressive minification aggregates
all files and then apply the minification allowing UglifyJS2 to go a little bit
further in its optimization.

Turned off by default, this option can be added using the aggressive flag
in your package.json.

  1. "uglifyjs2": {
  2. ...
  3. "aggressive": true,
  4. ...
  5. }

Package development

Exported symbols

This package exports a function loadPackageJson that parses the content
of then NPM’s package.json file of a Meteor project.

Dependencies & demo project

At the root of the repository:

  1. yarn install
  2. cd demo
  3. yarn install
  4. # Launch the demo in development mode
  5. yarn dev
  6. # Launch the demo in production mode
  7. yarn prod

Tips

  • My build process is stucked at “Minifying app code”: meteor reset.
  • Start with a small project when choosing your options.
  • Don’t add reduce_vars as option as it prevents Meteor from starting.
  • Use the default options as a canevas for experimenting additional UglifyJS2 options.