GianlucaGuarini/parallax

View on GitHub
tasks/build.js

Summary

Maintainability
A
1 hr
Test Coverage
var utils = require('./_utils'),
  rollup = require( 'rollup' ),
  mkdirp = require('mkdirp'),
  fs = require('fs'),
  npm = require('rollup-plugin-node-resolve'),
  commonjs = require('rollup-plugin-commonjs'),
  babel = require('babel-core')

module.exports = function(options) {

  // delete the old ./dist folder
  utils.clean('./dist')

  /**
   * Create a promise based on the result of the webpack compiling script
   */

  return new Promise(function(resolve, reject) {

    rollup.rollup({
      // The bundle's starting point. This file will be
      // included, along with the minimum necessary code
      // from its dependencies
      input: './src/index.js',
      plugins: [
        npm({
          jsnext: true,
          main: true
        }),
        commonjs({
          include: 'node_modules/**',
          // search for files other than .js files (must already
          // be transpiled by a previous plugin!)
          extensions: [ '.js', '.coffee' ] // defaults to [ '.js' ]
        })
      ]
    }).then( function ( bundle ) {
      // create a single bundle file
      return bundle.generate({
        format: 'cjs'
      }).then(({code}) => {
        // convert to valid es5 code with babel
        return babel.transform(code, {
          moduleId: global.library,
          moduleIds: true,
          comments: false,
          presets: ['es2015'],
          plugins: ['transform-es2015-modules-umd']
        })
      })
    })
      .then(({code}) => {
        mkdirp('./dist', function() {
          try {
            fs.writeFileSync(`./dist/${ global.library }.js`, code, 'utf8')
            resolve()
          } catch (e) {
            reject(e)
          }
        })
      })
      .catch(e =>{ utils.print(e, 'error') })
  })

}