shama / webpack-stream

:tropical_drink: Run webpack through a stream interface
MIT License
1.4k stars 123 forks source link

What webpack-stream doing with file object? #169

Open ghost opened 7 years ago

ghost commented 7 years ago

Thank you for the amazing package. Thanks to you, I don't need webpack.config.js and separate webpack launch anymore.

In the following experiment, I explored than gulp-webpack doing something with file object:

const webpackStream = require('webpack-stream'),
    webpack = webpackStream.webpack,
    named = require('vinyl-named'),
    path = require('path');

const ES6_SOURCE_FILES_GLOB_SELECTOR = [
    'development/source/public/js/**/*.js',
    'development/source/admin/js/**/*.js',
];

// without gulp-webpack:
return gulp.src(ES6_SOURCE_FILES_GLOB_SELECTOR)
    //.pipe(named())
    //.pipe(webpackStream(options))
       .pipe(gulp.dest( file => {

        console.log('dirname: '+file.dirname);
        // dirname: C:\MyIDE\projects\testProject\development\admin\js

// ... and with it:
 return gulp.src(ES6_SOURCE_FILES_GLOB_SELECTOR)
      //.pipe(named())
      .pipe(webpackStream(options))
      .pipe(gulp.dest( file => {

      console.log('dirname: '+file.dirname);
      // dirname: C:\MyIDE\projects\testProject

I need to define the conditional output in gulp.dest. To do it, I need to know full source path of every file. Here how I do it for SASS task and how I want to do same thing for webpack:

const SASS_SOURCE_FILES_SELECTOR = [
    'development/source/public/sass/**/*.sass',
    'development/source/admin/sass/**/*.sass'],

    PUBLIC_FOLDER_NAME = 'public', 
    ADMIN_FOLDER_NAME = 'admin'; 

gulp.task('sass', function(){

    return gulp.src(SASS_SOURCE_FILES_SELECTOR)
        .pipe(sass())
        .pipe(gulp.dest( file => {

            let pathSegments = file.dirname.split(path.sep);
            let pathSegmentsCount = pathSegments.length;

            for (let i = pathSegmentsCount - 1; i > 0; i--) {

                switch (pathSegments[i]) {
                    case PUBLIC_FOLDER_NAME: {
                        return `${file.cwd}\\development\\devBuild\\${PUBLIC_FOLDER_NAME}\\js`;
                    }
                    case ADMIN_FOLDER_NAME: {
                        return `${file.cwd}\\development\\devBuild\\${ADMIN_FOLDER_NAME}\\js`;
                    }
                    default:{
                        break;
                    }
                }
            }
    }));
});