Tuesday, 11 April 2017

Babel-transpile express/consign app files into dist, preserving paths' structure

I'm trying to (in test mean app; uses: angular2, webpack-stream+gulp, express/consign, babel-node/gulp) transpile es6 moduled files of server app into wwwroot (dist) forlder sustaining app's files'/paths' structure (thus, dummy ...pipe(dist) doesn't work, as flattens the output files to dist in line); Here's code:

... merged = require('merge-stream')(),.....

gulp.task('compile', ['clean:wwwroot'], () => {
  gulpEnv.set({
    NODE_ENV: 'production',
    ENV: 'prod'
  });
  let appFilesPaths = ['./app.js', './utils', './routes', './db.js', './boot.js', './models'];
  let streams = [];

  let compileAppFile(file, rel = './') => {
    log('[compile]:Rel ::: ', rel)
    let fullPath = path.join(__dirname, file);
    let fullDestPath = path.join(paths.appDist, rel);
    log('[compile:::full_Path]', fullPath);
    log('[compile:::full_Dest]', fullDestPath)

    let stream = gulp.src(fullPath)
      .pipe(babel())
      .pipe(debug({
        title: 'Gulp-Debug'
      }))
      .pipe(gulp.dest(fullDestPath))
      .on('finish', () => { log('DONE') })
      .on('error', (err) => { log('[compile error]', err) });

    merged.add(stream)
      .pipe(debug({
        title: `[Debug::MergedStreams]::: ${stream}`
      }));
  }

  let traverseAppPaths(fsEntity, rel = './') => {
    log('[traverse]:Rel ::: ', rel)
    let fullPath = path.resolve(rel, fsEntity);
    fs.stat(fullPath, (err, stats) => {
      if (err) throw new Error(`Error reading app file : \n ${err}`);
      if (stats.isFile()) {
        log('[traverse:::fsEntity]', fsEntity)
        if (fsEntity.indexOf('js') !== -1)
          compileAppFile(fsEntity, rel);
      } else {
        fs.readdir(fsEntity, (err, files) => {
          if (err) throw new Error(`Error reading app directory's files :  ${err}`);
          files.filter(item => item.indexOf('js') !== -1)
            .forEach(item => traverseAppPaths(item, fsEntity));
        })
      }
    })
  }

  appFilesPaths.forEach(fsItem => traverseAppPaths(fsItem));
  return merged
    .pipe(debug({
      title: '[merged_streams]:::'
    }));

}); 
gulp.task('test', ['compile']);

BUT... it otputs only 3 files to wwwroot: transpiled app.js, boot.js, db.js (i.e, no paths like utils.. compiled/moved indo wwwroot)


P.S also problem with using webpack/babel-loader gist



via Vovan_Super

No comments:

Post a Comment