-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] javascript RollupJS

, 22 2017 . 12:11 +
rollupjs
javascript rollupJS. , . .

.

, Rollup webpack Browserify JavaScript.

Rollup :

  • ,
  • ,
  • ,
  • Node ,


  • JavaScript.
  • ES2015 .
  • npm. ( ? Node.js .)

Rollup?


:
Rollup JavaScript-. ES2015, Node.js. Browserify webpack. Rollup , Grunt Gulp. , , , Grunt Gulp JavaScript, Rollup, Browserify webpack.

Rollup?


Rollup , . , : JavaScript, Rollup , .

, Rollup ES2015, , CommonJS, Browserify webpack. , Rollup tree-shaking, , , , .

Tree-shaking , , . , lodash jQuery .

Browserify webpack . Rollup , .

UPDATE (2016-08-22)
: Rollup tree-shaking ES-. CommonJS, , , lodash, jQuery, tree-shaking. , tree-shaking Rollup /. .

- Rollup, webpack 2 tree-shaking.

Rollup JavaScript-?


, Rollup, , Rollup JavaScript.

0: JAVASCRIPT CSS.


, . , GitHub.

:

learn-rollup/
+-- build/
| +-- index.html
+-- src/
| +-- scripts/
| | +-- modules/
| | | +-- mod1.js
| | | +-- mod2.js
| | +-- main.js
| +-- styles/
| +-- main.css
+-- package.json


, , .

# Move to the folder where you keep your dev projects.
cd /path/to/your/projects

# Clone the starter branch of the app from GitHub.
git clone -b step-0 --single-branch https://github.com/jlengstorf/learn-rollup.git

# The files are downloaded to /path/to/your/projects/learn-rollup/

repo, build/index.html . HTML .

1: ROLLUP .


, Rollup :

npm install --save-dev rollup

rollup.config.js learn-rollup. .

export default {
  entry: 'src/scripts/main.js',
  dest: 'build/js/main.min.js',
  format: 'iife',
  sourceMap: 'inline',
};

:

  • Entry , , Rollup . JavaScript-, .
  • Dest , .
  • Format Rollup . , (IIFE)

    , , , , . IIFE , .

  • SourceMap , . , .

format, . Rollup's Wiki.

ROLLUP

, , , , :

./node_modules/.bin/rollup -c

build js, main.min.js.

, , build/index.html :

image

. , ES2015 / ES6, .



tree-shaking Rollup , , . , src/scripts/modules/mod1.js sayGoodbyeTo (), , Rollup :


(function () {
'use strict';

/**
 * Says hello.
 * @param  {String} name a name
 * @return {String}      a greeting for `name`
 */
function sayHelloTo( name ) {
  const toSay = `Hello, ${name}!`;

  return toSay;
}

/**
 * Adds all the values in an array.
 * @param  {Array} arr an array of numbers
 * @return {Number}    the sum of all the array values
 */
const addArray = arr => {
  const result = arr.reduce((a, b) => a + b, 0);

  return result;
};

// Import a couple modules for testing.
// Run some functions from our imported modules.
const result1 = sayHelloTo('Jason');
const result2 = addArray([1, 2, 3, 4]);

// Print the results on the page.
const printTarget = document.getElementsByClassName('debug__output')[0];

printTarget.innerText = `sayHelloTo('Jason') => ${result1}\n\n`
printTarget.innerText += `addArray([1, 2, 3, 4]) => ${result2}`;

}());
//# sourceMappingURL=data:application/json;charset=utf-8;base64,...


, , , lodash, .

, webpack sayGoodbyeTo (), , Rollup.

, , , . ~ 3KB ~ 8KB

2: BABEL, JAVASCRIPT


, , , .

, Babel . JavaScript (ES6/ES2015 ..) ES5, , .

Babel, . JavaScript , .

, .



, Babel Rollup Babel-.

# Install Rollups Babel plugin.
npm install --save-dev rollup-plugin-babel

# Install the Babel preset for transpiling ES2015.
npm install --save-dev babel-preset-es2015

# Install Babels external helpers for module support.
npm install --save-dev babel-plugin-external-helpers

Babel- Babel, Babel,

.babelrc.

.babelrc (learn-rollup/). JSON:

{
  "presets": [
    [
      "es2015",
      {
        "modules": false
      }
    ]
  ],
  "plugins": [
    "external-helpers"
  ]
}

Babel, .

npm (2.15.11) es2015-rollup. npm, . .babelrc.

UPDATE (2016-11-13)
.babelrc . . pull request , package.json.

rollup.config.js.

Babel Rollup, rollup.config.js. Babel, , plugins, .


// Rollup plugins
import babel from 'rollup-plugin-babel';

export default {
  entry: 'src/scripts/main.js',
  dest: 'build/js/main.min.js',
  format: 'iife',
  sourceMap: 'inline',
  plugins: [
    babel({
      exclude: 'node_modules/**',
    }),
  ],
};

, exclude node_modules.



, :

./node_modules/.bin/rollup -c

, . : , addArray ():


var addArray = function addArray(arr) {
  var result = arr.reduce(function (a, b) {
    return a + b;
  }, 0);

  return result;
};

, Babel (arr.reduce ((a, b) => a + b, 0)) .

: , IE9.

Babel babel-polyfill, Array.prototype.reduce () IE8 .

3: ESLINT JAVASCRIPT


linter , , .

ESLint.



ESLint, ESLint Rollup:

npm install --save-dev rollup-plugin-eslint

.eslintrc.json.

, , , ESLint. , , :


$ ./node_modules/.bin/eslint --init
? How would you like to configure ESLint? Answer questions about your style
? Are you using ECMAScript 6 features? Yes
? Are you using ES6 modules? Yes
? Where will your code run? Browser
? Do you use CommonJS? No
? Do you use JSX? No
? What style of indentation do you use? Spaces
? What quotes do you use for strings? Single
? What line endings do you use? Unix
? Do you require semicolons? Yes
? What format do you want your config file to be in? JSON
Successfully created .eslintrc.json file in /Users/jlengstorf/dev/code.lengstorf.com/projects/learn-rollup


, , .eslintrc.json:

.eslintrc.json
{
  "env": {
    "browser": true,
    "es6": true
  },
  "extends": "eslint:recommended",
  "parserOptions": {
    "sourceType": "module"
  },
  "rules": {
    "indent": [
      "error",
      4
    ],
    "linebreak-style": [
      "error",
      "unix"
    ],
    "quotes": [
      "error",
      "single"
    ],
    "semi": [
      "error",
      "always"
    ]
  }
}


TWEAK .eslintrc.json

, :

  1. 2 4.
  2. , ENV, .

.eslintrc.json globals indent:

.eslintrc.json
{
  "env": {
    "browser": true,
    "es6": true
  },
  "globals": {
    "ENV": true
  },
  "extends": "eslint:recommended",
  "parserOptions": {
    "sourceType": "module"
  },
  "rules": {
    "indent": [
      "error",
      2
    ],
    "linebreak-style": [
      "error",
      "unix"
    ],
    "quotes": [
      "error",
      "single"
    ],
    "semi": [
      "error",
      "always"
    ]
  }
}


rollup.config.js

ESLint Rollup:


// Rollup plugins
import babel from 'rollup-plugin-babel';
import eslint from 'rollup-plugin-eslint';

export default {
  entry: 'src/scripts/main.js',
  dest: 'build/js/main.min.js',
  format: 'iife',
  sourceMap: 'inline',
  plugins: [
    eslint({
      exclude: [
        'src/styles/**',
      ]
    }),
    babel({
      exclude: 'node_modules/**',
    }),
  ],
};



./node_modules/.bin/rollup -c, , . , linter .

, , , ESLint :


$ ./node_modules/.bin/rollup -c

/Users/jlengstorf/dev/code.lengstorf.com/projects/learn-rollup/src/scripts/main.js
  12:64  error  Missing semicolon  semi

 1 problem (1 error, 0 warnings)

, , , , .

, , , .

, , - , , .

4: ES-


, Node-style . require.

NODE-

, . , Rollup , , .

, debug. :


npm install --save debug

, --save, , devDependencies .

, src/scripts/main.js, :

main.js

// Import a couple modules for testing.
import { sayHelloTo } from './modules/mod1';
import addArray from './modules/mod2';

// Import a logger for easier debugging.
import debug from 'debug';
const log = debug('app:log');

// Enable the logger.
debug.enable('*');
log('Logging is enabled!');

// Run some functions from our imported modules.
const result1 = sayHelloTo('Jason');
const result2 = addArray([1, 2, 3, 4]);

// Print the results on the page.
const printTarget = document.getElementsByClassName('debug__output')[0];

printTarget.innerText = `sayHelloTo('Jason') => ${result1}\n\n`;
printTarget.innerText += `addArray([1, 2, 3, 4]) => ${result2}`;


rollup, :


$ ./node_modules/.bin/rollup -c
Treating 'debug' as external dependency
No name was provided for external module 'debug' in options.globals  guessing 'debug'

index.html, , ReferenceError:

image

, . .

- , Node- CommonJS, Rollup. , Node CommonJS.



, :

  1. rollup-plugin-node-resolve, node_modules.
  2. rollup-plugin-commonjs, CommonJS-.

:

npm install --save-dev rollup-plugin-node-resolve rollup-plugin-commonjs

rollup.config.js

Rollup:

rollup.config.js

// Rollup plugins
import babel from 'rollup-plugin-babel';
import eslint from 'rollup-plugin-eslint';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default {
  entry: 'src/scripts/main.js',
  dest: 'build/js/main.min.js',
  format: 'iife',
  sourceMap: 'inline',
  plugins: [
    resolve({
      jsnext: true,
      main: true,
      browser: true,
    }),
    commonjs(),
    eslint({
      exclude: [
        'src/styles/**',
      ]
    }),
    babel({
      exclude: 'node_modules/**',
    }),
  ],
};


jsnext ES2015- Node-. main browser , .



./node_modules/.bin/rollup -c, , :

image

5: ,


, / , dev- .

, Rollup .

ENV main.js

, . src/scripts/main.js, log():


// Import a logger for easier debugging.
import debug from 'debug';
const log = debug('app:log');

// The logger should only be disabled if were not in production.
if (ENV !== 'production') {

  // Enable the logger.
  debug.enable('*');
  log('Logging is enabled!');
} else {
  debug.disable();
}

, (./node_modules/.bin/rollup -c) , , ReferenceError ENV.

, . - ENV = production ./node_modules/.bin/rollup -c, . , Rollup, , Rollup.

.



rollup-plugin-replace, find-and-replace. , (, ENV production ).

npm install --save-dev rollup-plugin-replace

rollup.config.js


rollup.config.js .

: -, , , .

rollup.config.js

// Rollup plugins
import babel from 'rollup-plugin-babel';
import eslint from 'rollup-plugin-eslint';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';

export default {
  entry: 'src/scripts/main.js',
  dest: 'build/js/main.min.js',
  format: 'iife',
  sourceMap: 'inline',
  plugins: [
    resolve({
      jsnext: true,
      main: true,
      browser: true,
    }),
    commonjs(),
    eslint({
      exclude: [
        'src/styles/**',
      ]
    }),
    babel({
      exclude: 'node_modules/**',
    }),
    replace({
      exclude: 'node_modules/**',
      ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
    }),
  ],
};


ENV process.env.NODE_ENV Node development. JSON.stringify (), , .

, exclude node_modules , . ( @wesleycoder ).



. , . , .
, :

NODE_ENV=production ./node_modules/.bin/rollup -c

Windows SET NODE_ENV = production ./node_modules/.bin/rollup -c, . , . .

, :

image

6: UGLIFYJS,


, , UglifyJS . , , , , .



UglifyJS , rollup-plugin-uglify.
:

npm install --save-dev rollup-plugin-uglify

rollup.config.js

Uglify Rollup. , uglification :

rollup.config.js

// Rollup plugins
import babel from 'rollup-plugin-babel';
import eslint from 'rollup-plugin-eslint';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
import uglify from 'rollup-plugin-uglify';

export default {
  entry: 'src/scripts/main.js',
  dest: 'build/js/main.min.js',
  format: 'iife',
  sourceMap: 'inline',
  plugins: [
    resolve({
      jsnext: true,
      main: true,
      browser: true,
    }),
    commonjs(),
    eslint({
      exclude: [
        'src/styles/**',
      ]
    }),
    babel({
      exclude: 'node_modules/**',
    }),
    replace({
      ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
    }),
    (process.env.NODE_ENV === 'production' && uglify()),
  ],
};


uglify (), NODE_ENV production.



, Rollup :

NODE_ENV=production ./node_modules/.bin/rollup -c

, . , build/js/main.min.js:

image

~ 42 . UglifyJS, ~ 29 30% .

->

:

rollup , webpack( ), , , . , , , (tree-shaking) . , , , .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/331412/

:  

: [1] []
 

:
: 

: ( )

:

  URL