JAVASCRIPT   53

varmod js

Guest on 25th July 2022 04:18:00 PM

  1. var test = require('tape');
  2. var concat = require('concat-stream');
  3. var quote = require('quote-stream');
  4. var staticModule = require('../');
  5. var fs = require('fs');
  6. var path = require('path');
  7.  
  8. test('variable modules', function (t) {
  9.     t.plan(2);
  10.    
  11.     var expected = [ 'beep boop!' ];
  12.     var sm = staticModule({
  13.         fs: {
  14.             readFileSync: function (file, enc) {
  15.                 return fs.createReadStream(file).pipe(quote());
  16.             }
  17.         }
  18.     }, {
  19.         vars: { __dirname: path.join(__dirname, 'vars') },
  20.         varModules: { path: require('path') }
  21.     });
  22.    
  23.     readStream('source.js').pipe(sm).pipe(concat(function (body) {
  24.         t.equal(
  25.             body.toString('utf8'),
  26.             '\nvar path = require(\'path\');'
  27.             + '\nvar html = "beep boop";\nvar x = \'!\';'
  28.             + '\nconsole.log(html + x);\n'
  29.         );
  30.         Function(['console','require'],body)({ log: log },require);
  31.         function log (msg) { t.equal(msg, expected.shift()) }
  32.     }));
  33. });
  34.  
  35. function readStream (file) {
  36.     return fs.createReadStream(path.join(__dirname, 'varmod', file));
  37. }

Raw Paste


Login or Register to edit or fork this paste. It's free.