Giter Site home page Giter Site logo

simple-builder2-demo's Introduction

simple-builder2-demo

This is a simple component builder demonstrating the use of builder2.js.

To build the sample.

  $ ./bin/build

Or to run the minimal koa server that builds component for every request.

  $ node server

index.js

/**
 * Module dependencies
 */

var path = require('path');
var mkdir = require('mkdirp');
var Builder = require('component-builder2');
var Resolver = require('component-resolver');
var myth = require('builder-myth');
var debug = require('debug')('simple-builder2:builder');

/**
 * Retuns Generator Function that handles building component
 *
 * `params` can accept following options
 *
 * - `out`: output directory
 * - `bundled`: if you want to build bundled component
 */

module.exports = function(params){

  params = params || {};
  params.out = params.out || 'build';

  var copy = params.copy;

  return function*(){
    
    var resolver = new Resolver(process.cwd(), { install: true });
    var tree = yield* resolver.tree();
    var out = params.out;

    if(!params.bundled){
      debug('Building component to %s', out);
      yield buildBundle(resolver, tree, out);
    } else {
      for(var bundle in tree.locals){
        debug('Building a bundle: %s', bundle);
        out = path.resolve(params.out, bundle);
        yield buildBundle(resolver, tree.locals[bundle], out);
      }
    }
  };

  function* buildBundle(resolver, tree, out){

    // mkdir -p
    mkdir.sync(out);

    var nodes = resolver.flatten(tree); 

    /**
     * Builders
     */

    var script = new Builder.scripts(nodes);
    var style = new Builder.styles(nodes);
    var file = new Builder.files(nodes, {dest: out});

    /**
     * Script Plugin(s)
     */

    script.use('scripts', Builder.plugins.js());

    /**
     * Style Plugins
     *
     * - `myth`: Enables `myth`
     * - `urlRewriter`: Rewrite `url()` rules in css
     */

    style.use('styles', Builder.plugins.css());
    style.use('styles', myth({whitespace: false}));
    style.use('styles', Builder.plugins.urlRewriter());
    
    /**
     * File Plugins
     */

    file.use('images', Builder.plugins[copy ? 'copy' : 'symlink']());

    /**
     * Yield all :)
     */

    yield [
      script.toFile(path.resolve(out, 'build.js')),
      style.toFile(path.resolve(out, 'build.css')),
      file.end()
    ];
  }
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.