Here you have it: https://github.com/IngwiePhoenix/connect-oj/blob/master/pp.js <https://github.com/IngwiePhoenix/connect-oj/blob/master/pp.js> You can copy it, put it in your project and use it. Its simple and pretty cool. :) It does not just work with JS, but really everything. I am using it to preprocess .oj files, mainly to use #include() to import base classes. > Am 28.10.2014 um 20:13 schrieb Axel Kittenberger <[email protected]>: > > Why not use the javascript VM as preprocessor? Node is about putting more > javascript into javascript anyway. > > Every line starting with '#' is coded in the preprocessor context, every line > that isn't, is for the result. > > I wrote it in a way, so line numbers are ought to be preserved unless an > include( ) happens, in that case line numbers go a little amiss. > > example input.js: > > For example: > > preprocessor.js: > ----------------------------- > var fs = require( 'fs' ); > var vm = require( 'vm' ); > var file = fs.readFileSync( 'input.js' ).toString( ); > var lines = file.split('\n'); > var skipped = ''; > var code = [ ]; > var result; > > for( var ln in lines ) > { > var line = lines[ ln ]; > > if( line[ 0 ] === '#' ) > { > lines[ ln ] = line.substr( 1 ); > skipped += '\\n'; > } > else > { > lines[ ln ] = > 'code.push("' > + skipped > + line.replace( /\"/g, '\\"' ).replace( /"/g, '\\"' ) > + '");'; > > skipped = ''; > } > } > > lines.push( 'code; '); > > result = > vm.runInNewContext( > lines.join( '\n' ), > { > code : code, > require : require, > include : > function( filename ) > { > code.push( fs.readFileSync( filename ) > ); > } > } > ) > .join( '\n' ); > > console.log( result ); > ------------------- > Do with result what you want, like using it in the http server module, cache > it, etc. you might wrap some async code to replace readFileSync() with async > alternatives in the flow control of your choice. > ------------------------ > #var test = require( './test' ); > > #if( test ) { > console.log( '"hello"' ); > #} > console.log( 'world' ); > > #include( 'include-me.js' ); > ----------------------- > > example test.js : > ------------------------ > // just contains a yes/no switch > module.exports = true || false; > ---------------------------- > > Handing the preprocessors own require function to the to be preprocessed file > might be a little naive and have quirky effects if they use the same files, I > just suppose its trusted code anyway, but for a starter it works. Might be > replaced with a custom require if so desired. > > One can also directly alter the 'code' array in preprocessor context to add > custom stuff. > > -- > Job board: http://jobs.nodejs.org/ <http://jobs.nodejs.org/> > New group rules: > https://gist.github.com/othiym23/9886289#file-moderation-policy-md > <https://gist.github.com/othiym23/9886289#file-moderation-policy-md> > Old group rules: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > <https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines> > --- > You received this message because you are subscribed to the Google Groups > "nodejs" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected] > <mailto:[email protected]>. > To post to this group, send email to [email protected] > <mailto:[email protected]>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/nodejs/CABg07fthq%2BerYUggubCSBJoKE%2BiK9eBc67A690on3R85cNmQwQ%40mail.gmail.com > > <https://groups.google.com/d/msgid/nodejs/CABg07fthq%2BerYUggubCSBJoKE%2BiK9eBc67A690on3R85cNmQwQ%40mail.gmail.com?utm_medium=email&utm_source=footer>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>.
-- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/870BA99D-8DFD-42E3-9EA1-2663E7546B59%40googlemail.com. For more options, visit https://groups.google.com/d/optout.
