node.js - NodeJs: external javascript with dependencies -


we trying use nodejs minimal dependencies other packages, challenge encounter handelbarsjs. found package, assemble can generate html us. only, very slow, 3 seconds each time, of these 3 seconds, there 2,5 / 2,7 seconds of next line:

var assemble = require('assemble'); 

our package.json script section:

"scripts": {   "build:handlebars": "node scripts/handlebars.js",   "watch:handlebars": "nodemon --watch assets --exec \"npm run build:handlebars\"" } 

the script/handlebars.js file

#! /usr/bin/env node  var assemble = require('assemble'); var extname = require('gulp-extname');  console.log(date.now() - start);  assemble.data('assets/templates/data/*.json'); assemble.layouts('assets/templates/layouts/*.hbs'); assemble.partials('assets/templates/partials/*.hbs');  assemble.src('assets/templates/*.hbs', { layout: 'default' })     .pipe(extname())     .pipe(assemble.dest('build/')); 

each time, when save .hbs file, nodemon restart , external javascript file called.

how can ensure 'require' called once, or whether remain in memory?

thank you!

since want accomplish using assemble, without gulp, recommend chokidar.

npm install chokidar --save 

now can require chokidar this:

var chokidar = require('chokidar'); 

then define little helper runs handler whenever in pattern changes:

function watch(patterns, handler) {   chokidar.watch(patterns, {     ignoreinitial: false   }).on('add', handler).on('change', handler).on('unlink', handler); } 

now can alter script this:

#! /usr/bin/env node  var assemble = require('assemble'); var extname = require('gulp-extname'); var chokidar = require('chokidar');  console.log(date.now() - start);  assemble.data('assets/templates/data/*.json'); assemble.layouts('assets/templates/layouts/*.hbs'); assemble.partials('assets/templates/partials/*.hbs');  // enable --watch command line chokidar, otherwise, run! if (process.argv.pop() === '--watch') {   watch('assets', runonce); } else {   runonce(); }  function watch(patterns, handler) {   chokidar.watch(patterns, {     ignoreinitial: false   }).on('add', handler).on('change', handler).on('unlink', handler); }  function runonce() {   assemble.src('assets/templates/*.hbs', { layout: 'default' })       .pipe(extname())       .pipe(assemble.dest('build/')); } 

and instead of nodemon, keep script alive , running. so, in npm, want this:

"scripts": {   "build:handlebars": "node scripts/handlebars.js",   "watch:handlebars": "node scripts/handlebars.js --watch" } 

whenever file changes, script run, without re-invoking scratch.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -