简体   繁体   English

如何使用Gulp中的Webpack将文件输出到其源目录的父目录?

[英]How to output files to the parent of their source directory using Webpack in Gulp?

So far I have this code, which I got from here : 到目前为止,我有这个代码,我从这里得到:

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

gulp.task('default', function() {
  return gulp.src('*/lib/app.js', { base: '.' })
    .pipe(named())
    .pipe(webpack())
    .pipe(gulp.dest('.'));
});

My folder structure is like: 我的文件夹结构如下:

site1/lib/app.js
site2/lib/app.js

I want to create the output files like the following, with each file containing only their respective lib/app.js file's code (and any require()s made in them): 我想创建如下所示的输出文件,每个文件只包含各自的lib / app.js文件的代码(以及其中的任何require()):

site1/app.js
site2/app.js

However, the code I have now just outputs to the project's root directory. 但是,我现在的代码只输出到项目的根目录。 I've tried several combinations, such as removing the { base: '.' } 我尝试了几种组合,例如删除{ base: '.' } { base: '.' } , but nothing works. { base: '.' } ,但没有任何工程。 If I remove the named() and webpack() pipes, though, then the current code actually outputs to the correct directory. 但是,如果我删除了named()webpack()管道,那么当前代码实际输出到正确的目录。 So, in the process, it seems like perhaps Webpack loses the originating directory information? 那么,在这个过程中,似乎Webpack可能丢失了原始目录信息?

Also, it possible to get a solution that also works with Webpack's "watch: true" option, so that compiling modified files is quick, rather than using Gulp to always iterate through every single file on every file change? 此外,有可能获得一个也适用于Webpack的“watch:true”选项的解决方案,因此编译修改后的文件很快,而不是使用Gulp总是迭代每个文件更改的每个文件?

I assume you want to create a app.js for each site that packs only the code for that site (and not the others). 我假设您要为每个包含该站点代码(而不是其他站点)的站点创建app.js

In that case you can use gulp-foreach to effectively iterate over all your app.js files and send each one down its own stream. 在这种情况下,您可以使用gulp-foreach有效地迭代所有app.js文件,并将每个文件发送到自己的流中。 Then you can use the node.js built-in path module to figure out where the parent directory for each app.js file is and write it there with gulp.dest() . 然后,您可以使用node.js内置path模块来确定每个app.js文件的父目录的位置,并使用gulp.dest()将其写入。

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

gulp.task('default', function() {
  return gulp.src('*/lib/app.js')
    .pipe(foreach(function(stream, file) {
      var parentDir = path.dirname(path.dirname(file.path));
      return stream
        .pipe(named())
        .pipe(webpack())
        .pipe(gulp.dest(parentDir));
    }));
});

If you want to use webpack({watch:true}) you'll have to use a different approach. 如果你想使用webpack({watch:true})你将不得不使用不同的方法。 The following uses glob to iterate over all the app.js files. 以下使用glob迭代所有app.js文件。 Each app.js file is again send down its own stream, however this time all the streams are merged before being returned. 每个app.js文件再次向下发送它自己的流,但是这次所有流在返回之前被合并

var gulp = require('gulp');
var webpack = require('webpack-stream');
var named = require('vinyl-named');
var path = require('path');
var merge = require('merge-stream');
var glob = require('glob');

gulp.task('default', function() {
  return merge.apply(null, glob.sync('*/lib/app.js').map(function(file) {
    var parentDir = path.dirname(path.dirname(file));
    return gulp.src(file)
      .pipe(named())
      .pipe(webpack({watch:true}))
      .pipe(gulp.dest(parentDir));
  }));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM