question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot keep source file sub-directories

See original GitHub issue

I try to use webpack with vinyl-name to compile my scripts like the demo:

var gulp = require('gulp');
var webpack = require('webpack-stream');
var named = require('vinyl-named');
gulp.task('default', function() {
  return gulp.src(['src/*.js', 'src/**/*.js'])
    .pipe(named())
    .pipe(webpack())
    .pipe(gulp.dest('dist/'));
});

But js files in src/** are compiled to the root directory in dist/ like this:

  • src/base.js --> dist/base.js // This is right
  • src/user/login.js --> dist/login.js // This should be in dist/user/login.js

How can I make it right?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
cmttcommented, Jul 6, 2015

According to reported issues, webpack does not support multiple output paths yet.

Nevertheless, there are some workarounds:

  • use webpack’s output filename templates (this does not create the described directory structure)
gulp.task('default', function() {
  return gulp.src(['src/*.js', 'src/**/*.js'])
    .pipe(named())
    .pipe(webpack({
      output : {
        filename : '[name]/[name].js'    
      }
    }))
    .pipe(gulp.dest('dist/'));
});
  • hook into the stream with Gulp plugins and change the file’s paths.

Apparently, you can use a combination of vinyl-named and gulp-rename in order to recreate the directory structure. You have to pipe your sources twice through gulp-rename, once for caching the original directory of your input, once for restoring the cached attribute.

It is an ugly and questionable solution, but it should do the job as long as the webpack module isn’t fixed.

var gulp = require('gulp');
var webpack = require('webpack-stream');
var named = require('vinyl-named');
var rename = require('gulp-rename');

gulp.task('default', function() {
  var tmp = {};
  return gulp.src(['src/*.js', 'src/**/*.js'])
    .pipe(named())
    .pipe(rename(function (path) {
      tmp[path.basename] = path;
    }))    
    .pipe(webpack({
      output : {
        filename : '[name].js'    
      }
    }))
    .pipe(rename(function (path) {
      path.dirname = tmp[path.basename].dirname;
    }))    
    .pipe(gulp.dest('dist/'));
});

Note that this example works solely when you supply globs which match across directories (e.g. a gulp.src-pattern like [‘src/1.js’, ‘src/sub/3.js’] would not work correctly).

You should write your own gulp-rename callback functions and have a look at the base option of gulp.src, too.

8reactions
asika32764commented, Dec 22, 2018

vinyl-named-with-path can solve my issue.

var gulp = require('gulp');
var webpack = require('webpack-stream');
var named = require('vinyl-named-with-path');
gulp.task('default', function() {
  return gulp.src(['src/**/*.js'])
    .pipe(named())
    .pipe(webpack())
    .pipe(gulp.dest('dist/'));
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sources from subdirectories in Makefile - Stack Overflow
I always list every single object file in my Makefiles and sometimes every single source file. Listing a few directories to loop over...
Read more >
Not able to copy entire files and folders recursively in specific ...
Hi,. I want to copy data between two adls gen2 accounts. I am unable to copy all files and subfolders in specific folder...
Read more >
Working With Files - Gradle User Manual
Almost every Gradle build interacts with files in some way: think source files, ... reports directory, including all its subdirectories, to the destination:....
Read more >
file — CMake 3.25.1 Documentation
If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake...
Read more >
Enhanced source file handling with target_sources() - Crascit -
This in turn means that subdirectories cannot directly call ... Let's now assume the source files in the foo subdirectory use features from ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found