Beispiel #1
0
  grunt.registerTask('componentbuild', 'Build components', function () {
    /* eslint no-invalid-this: 0 */
    grunt.task.requires('shell:componentinstall');
    grunt.task.requires('metalsmith');

    const Component = require('component-builder');
    const mkdir = require('mkdirp');
    const write = require('fs').writeFileSync;
    const myth = require('myth');

    let done = this.async();

    const c = new Component(__dirname);
    c.copyAssetsTo('build');
    c.development();
    c.addSourceURLs();
    c.copyFiles();

    c.build((err, res) => {
      if (err) {
        /* eslint no-console: 0 */
        console.log('Error: ' + err);
        return done(false);
      }

      mkdir('build');
      write('build/build.js', res.require + res.js);
      write('build/build.css', myth(res.css));

      return done();
    });
  });
Beispiel #2
0
function build (prefix, output, dev, fn) {
  var builder = new Builder('.')
    , start = new Date()
    , boot = '\nrequire(\'boot\')'

  if (dev) builder.addSourceURLs()
  builder.prefixUrls(prefix)
  builder.copyAssetsTo(output)
  builder
    .use(stylus)
    .use(jade)
    .build(function (err, res) {
      if (err) return fn(err)

      var js = res.require+res.js+boot

      write(join(output, '/app.js'), js)
      write(join(output, '/app.css'), res.css)

      debug('built in %dms', new Date() - start)
      debug('css: %s', bytes(res.css.length))
      debug('js: %s', bytes(js.length))

      fn()
    })
}
Beispiel #3
0
function createBuilder () {
  var builder = new Builder('.');
  builder.copyFiles();
  builder.addLookup('lib');
  builder.copyAssetsTo('public');
  builder.use(styles);
  builder.use(templates);

  return builder;
}
Beispiel #4
0
app.use(function(req, res, next){
  var builder = new Builder('.');
  builder.copyAssetsTo('build');
  builder.development();
  builder.build(function(err, res){
    if (err) return next(err);
    writeFile('build/build.js', res.require + res.js);
    writeFile('build/build.css', res.css);
    next();
  });
});
Beispiel #5
0
var build = exports.build = function(fn) {
  var builder = new Builder('.');
  builder.addLookup('lib'); // TODO: shouldn't be necessary
  builder.copyAssetsTo('build');
  builder.use(style);
  builder.use(jade);
  builder.build(function(err, res){
    if (err) return fn(err);
    write('build/build.js', res.require + res.js);
    write('build/build.css', res.css);
    fn && fn();
  });
};
        installer.install(function (err) {
          if (err) {
            grunt.fail.warn(err);
            return;
          }

          var builder = new Builder(manifestDir); 
          builder.copyAssetsTo(path.resolve(process.cwd(), path.dirname(f.dest)));

          // Load plugins
          options.plugins.forEach(function (plugin) {
            builder.use(require(plugin));
          });

          if (options.development) {
            builder.development();
            builder.addSourceURLs();
          }

          builder.build(function (err, obj) {
            if (err) {
              grunt.fail.warn(err);
              return;
            }

            var js = '';

            if (options.require) {
              js += obj.require;
            }

            js += obj.js;

            // Cleanup
            js = js.replace(/\.coffee(\'|\")/g, '.js$1');

            // Write the destination file.
            grunt.file.write(f.dest, js);

            // Print a success message.
            grunt.log.writeln('File "' + f.dest + '" built.');

            writeGraphFiles(grunt.file.readJSON(manifestPath), options.graph_scripts, manifestDir, path.resolve(process.cwd(), path.dirname(f.dest)), path.basename(f.dest));

            todo--;
            if (todo === 0) {
              done();
            }
          });
        });
Beispiel #7
0
module.exports = function(req, res, next){
  var start = new Date;
  var builder = new Builder('.');
  builder.copyAssetsTo('public');
  builder.use(rework);
  builder.use(templates);
  builder.build(function(err, res){
    if (err) return next(err);
    write('public/app.js', res.require + res.js);
    write('public/app.css', res.css);
    console.log('built in %sms', new Date - start);
    next();
  });
};
Beispiel #8
0
module.exports = function(req, res, next) {
  mkdir.sync('public');
  var start = new Date;
  var builder = new Builder('.');
  builder.copyAssetsTo('public');
  builder.use(rework);
  builder.addSourceURLs(); //dev mode
  builder.build(function(err, res){
    if (err) return next(err);
    var js = res.require + res.js;
    write('public/blog.js', js);
    write('public/blog.css', res.css);
    utils.log('js', (js.length / 1024 | 0) + 'kb');
    utils.log('css', (res.css.length / 1024 | 0) + 'kb');
    utils.log('duration', (new Date - start) + 'ms');
  });
  if(next) next();
};
Beispiel #9
0
  return function(dir, output, done) {
    if(!fs.existsSync(dir + '/component.json')) return done();

    if(!output) {
      output = join(dir, 'build');
    }

    mkdirp.sync(output);

    var builder = new Builder(dir);
    builder.copyAssetsTo(output);

    var config = grunt.file.readJSON(path.join(dir, 'component.json'));

    config.paths = (config.paths || []).map(function(p) {
      return path.resolve(dir, p);
    });

    var standalone = typeof options.standalone === 'string' ? options.standalone : config.name;

    if (options.prefix) {
      builder.prefixUrls(options.prefix);
    }

    if (options.dev) {
      builder.development();
    }

    if (options.sourceUrls) {
      builder.addSourceURLs();
    }

    if(options.copy) {
      builder.copyFiles();
    }

    if (options.ignore) {
      Object.keys(options.ignore).forEach(function(n) {
        var type = options.ignore[n];
        builder.ignore(n, type);
      });
    }

    // By default Builder takes the paths of the dependencies
    // from the current directory (here the Gruntfile path).
    // So in case the dependencies are not stored in the /components
    // but in the baseOption/components, we have to add it to the lookup.
    builder.addLookup(path.join(dir, 'components'));
    builder.addLookup(config.paths);

    // Set the config on the builder. We've modified
    // the original config from the file and this will
    // override settings during the build
    builder.config = config;

    if (options.configure) {
      options.configure.call(this, builder);
    }

    builder.build(function(err, obj) {
      if (err) return done(err);

      var cssFile = join(output, options.name) + '.css';
      var jsFile = join(output, options.name) + '.js';

      if (obj.css.trim() != "") {
        fs.writeFileSync(cssFile, obj.css.trim());
      }

      if (obj.js.trim() != "") {
        if (options.standalone) {
          obj.name = standalone;
          obj.config = config;
          var string = template(obj);
          fs.writeFileSync(jsFile, string);
        }
        else if(options.noRequire) {
          fs.writeFileSync(jsFile, obj.js);
        }
        else {
          fs.writeFileSync(jsFile, obj.require + obj.js);
        }
      }

      done();
    });
  };
  grunt.registerMultiTask('component', 'component-build for grunt.', function() {
    var opts = this.data;
    var name = this.target;
    var output = path.resolve(this.data.output);
    var done = this.async();
    var self = this;

    // The component builder
    var builder = new Builder( path.resolve(path.dirname(this.data.config)) );

    if( opts.sourceUrls === true ) {
      builder.addSourceURLs();
    }

    // Where to output the final file
    builder.copyAssetsTo(output);

    // Ignore component parts
    if( opts.ignore ) {
      Object.keys(opts.ignore).forEach(function(n){
        var type = opts.ignore[n];
        builder.ignore(n, type);
      });
    }

    // The component config
    var config = require( path.resolve(this.data.base, 'component.json') );

    // Add in extra scripts during the build since Component makes
    // us define each and every file in our component to build it.
    config.scripts = grunt.file.expand( config.scripts || [] );
    config.templates = grunt.file.expand( config.templates || [] );

    if( config.paths ) {
      builder.addLookup(config.paths);
    }    

    // Prefix urls
    if( this.data.prefix ) {
      builder.prefixUrls(this.data.prefix);
    }

    // Development mode
    if( this.data.dev ) {
      builder.development();
    }

    // Set the config on the builder. We've modified
    // the original config from the file and this will
    // override settings during the build
    builder.conf = config;

    if( opts.plugins ) {
      opts.plugins.forEach(function(name){
        var plugin = require('../plugins/' + name);
        builder.use(plugin);
      });
    }

    // Configure hook
    if( opts.configure ) {
      opts.configure.call(this, builder);
    }

    // Build the component
    builder.build(function(err, obj){

      if (err) {
        grunt.log.error( err.message );
        grunt.fatal( err.message );
      }

      // Write CSS file
      if( opts.styles !== false ) {
        var cssFile = path.join(output, name + '.css');
        grunt.file.write(cssFile, obj.css);
      }

      // Write JS file
      if( opts.scripts !== false ) {

        var js = '';

        if (opts.standalone) js += ';(function(){\n';
        js += obj.require;
        js += obj.js;

        if (opts.standalone) {
          var umd = [
            'if (typeof module == "object") {',
            '  module.exports = require("' + name + '");',
            '} else if (typeof define == "function" && define.amd) {',
            '  define(require("' + name + '"));',
            '} else {',
            '  window["' + name + '"] = require("' + name + '");',
            '}'
          ];

          js += umd.join('\n');
          js += '})();';
        }
        
        var jsFile = path.join(output, name + '.js');

        grunt.file.write(jsFile, js);
   
      }

      done();

    });
  });
  function buildComponent(grunt, opts, src, srcDir, destDir, cb) {
  
    // The component builder
    var builder = new Builder(srcDir);

    // copy files instead of linking
    if (opts.useLinks !== true) {
      builder.copyFiles();
    }

    // Where to output the final file
    builder.copyAssetsTo(destDir);

    // By default Builder takes the paths of the dependencies
    // from the current directory (here the Gruntfile path).
    // So in case the dependencies are not stored in the /components
    // but in the srcDir/components, we have to add it to the lookup.
    builder.addLookup(path.join(srcDir, 'components'));
  
    // The component config
    var absPath = path.resolve(src);
    debug("require %s", absPath);
    var config = require(absPath);

    if (config.paths) {
      config.paths = config.paths.map(function(p) {
        return path.resolve(srcDir, p);
      });

      builder.addLookup(config.paths);
    }

    // CSS URL prefix
    if (opts.CSSURLPrefix) {
      builder.prefixUrls(opts.CSSURLPrefix);
    }

    // Development mode
    if (opts.includeDevelopmentDependencies) {
      builder.development();
    }

    if (opts.addSourceUrls === true) {
      debug("adding source urls!");
      builder.addSourceURLs();
    } else {
      debug("not adding source urls!");
    }

    // Ignore component parts
    // if (opts.ignore) {
    //   Object.keys(opts.ignore).forEach(function(n) {
    //     var type = opts.ignore[n];
    //     builder.ignore(n, type);
    //   });
    // }

    // Set the config on the builder. We've modified
    // the original config from the file and this will
    // override settings during the build
    builder.conf = config;

    var name;
    if (typeof config.name === 'string') {
      name = config.name;
    } else {
      return cb(new Error("No name given in component.json"));
    }
    debug("processing component %s",name);

    if (opts.plugins) {
      opts.plugins.forEach(function(name) {
        
        var plugin;
        if (typeof name === 'string') {
            plugin = require('../plugins/' + name);
        } else {
          plugin = name;
        }
        if (plugin) {
          builder.use(plugin);
        } else {
          grunt.fatal("failed to load component plugin "+ name);
        }
      });
    }

    // Configure hook
    // if (opts.configure) {
    //   opts.configure.call(null, builder);
    // }

    debug("running builder ..");

    // Build the component
    builder.build(function(err, obj) {
      if (err) {
        debug("builder failed");
        return cb(err);
      } else {
        debug("builder ok");
      }

      // Write CSS file
      if (opts.processStyles !== false) {
        var cssFile = path.join(destDir, name + '.css');
        debug("writing %s", cssFile);
        grunt.file.write(cssFile, obj.css.trim());
      }

      // Write JS file
      if (opts.processScripts !== false) {
        var jsFile = path.join(destDir, name + '.js');
        debug("writing %s", jsFile);
        if (opts.standalone) {
          // Defines the name of the global variable (window[opts.name]).
          // By default we use the name defined in the component.json,
          // else we use the `standalone` option defined in the Gruntfile.
          obj.name = name;
          obj.config = config;

          var string = grunt.template.process(template, { data: obj });
          grunt.file.write(jsFile, string);
        } else {
          grunt.file.write(jsFile, obj.require + obj.js);
        }
      }

      cb(null);
    });
  }
  grunt.registerMultiTask('component', 'component-build for grunt.', function() {
    var self = this;
    var opts = this.data;
    var name = opts.name || this.target;
    var dir = path.resolve(opts.base || '');
    var output = path.resolve(this.data.output);
    var done = this.async();

    // The component builder
    var builder = new Builder(dir);

    // Where to output the final file
    builder.copyAssetsTo(output);

    // Prefix urls
    if (opts.prefix) {
      builder.prefixUrls(opts.prefix);
    }

    // Development mode
    if (opts.dev) {
      builder.development();
    }

    if (opts.sourceUrls === true) {
      builder.addSourceURLs();
    }

    // Ignore component parts
    if (opts.ignore) {
      Object.keys(opts.ignore).forEach(function(n) {
        var type = opts.ignore[n];
        builder.ignore(n, type);
      });
    }

    // By default Builder takes the paths of the dependencies
    // from the current directory (here the Gruntfile path).
    // So in case the dependencies are not stored in the /components
    // but in the baseOption/components, we have to add it to the lookup.
    builder.addLookup(path.join(dir, 'components'));

    // The component config
    var config = require(path.join(dir, 'component.json'));

    if (config.paths) {
      config.paths = config.paths.map(function(p) {
        return path.resolve(dir, p);
      });

      builder.addLookup(config.paths);
    }

    // Set the config on the builder. We've modified
    // the original config from the file and this will
    // override settings during the build
    builder.config = config;

    if (opts.plugins) {
      opts.plugins.forEach(function(name) {
        var plugin = require('../plugins/' + name);
        builder.use(plugin);
      });
    }

    // Configure hook
    if (opts.configure) {
      opts.configure.call(this, builder);
    }

    // Build the component
    builder.build(function(err, obj) {
      if (err) {
        grunt.log.error(err.message);
        grunt.fatal(err.message);
      }

      // Write CSS file
      if (opts.styles !== false) {
        var cssFile = path.join(output, name + '.css');
        grunt.file.write(cssFile, obj.css.trim());
      }

      // Write JS file
      if (opts.scripts !== false) {
        var jsFile = path.join(output, name + '.js');
        if (opts.standalone) {
          // Defines the name of the global variable (window[opts.name]).
          // By default we use the name defined in the component.json,
          // else we use the `standalone` option defined in the Gruntfile.
          obj.name = (typeof opts.standalone === 'string') ? opts.standalone : config.name;
          obj.config = config;

          var string = grunt.template.process(template, { data: obj });
          grunt.file.write(jsFile, string);
        } else {
          grunt.file.write(jsFile, obj.require + obj.js);
        }
      }

      done();
    });
  });
Beispiel #13
0
      )
      .use( branch( filetype('meta') )
              .use( markdown() )
              .use( permalinks({ pattern: 'meta/:title' }) )
      )
      .use( branch( filetype('diary') )
              .use( markdown() )
              .use( permalinks({ pattern: ':date/:title' }) )
      )

      .use( templates({ engine: 'swig', directory: 'templates', default: 'index.swig' })
      )
  )


var c = new Component(__dirname);
c.copyAssetsTo('build');
c.copyFiles();

m.build( function(err){ 
  if (err) throw(err); 
  c.build( function(err,res){
    if (err) throw(err);
    mkdir('build');
    write('build/build.js', res.require + res.js);
    write('build/build.css', res.css);
  });
});