'路径必须是字符串。收到undefined'在'Gulp.dest`

时间:2017-06-28 16:52:05

标签: javascript node.js webpack gulp webpack-3

我得到什么

gulpfile.js输出以下错误:

path.js:7
    throw new TypeError('Path must be a string. Received ' + inspect(path));
    ^

TypeError: Path must be a string. Received undefined
    at assertPath (path.js:7:11)
    at Object.resolve (path.js:1146:7)
    at DestroyableTransform.saveFile [as _transform] (/home/.../node_modules/vinyl-fs/lib/dest/index.js:41:26)
    at DestroyableTransform.Transform._read (/home/.../node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_transform.js:184:10)
    at DestroyableTransform.Transform._write (/home/.../node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_transform.js:172:12)
    at doWrite (/home/.../node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_writable.js:237:10)
    at clearBuffer (/home/.../node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_writable.js:316:5)
    at onwrite (/home/.../node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_writable.js:274:7)
    at WritableState.onwrite (/home/.../node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_writable.js:106:5)
    at afterTransform (/home/.../node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_transform.js:104:5)

错误从this line of vinyl-fs发出(file.relativeundefined)。

我该怎么办?

我的构建

我有以下目录结构:

src/
├── index.js
├── injects
│   ├── iframe
│   │   ├── index.html
│   │   ├── index.js
│   │   ├── src
│   │   └── webpack.config.js
│   ├── index.css
│   ├── index.js
│   ├── src
│   └── webpack.config.js
├── manifest.json
├── src
└── webpack.config.js

我想将 webpack gulp 转换为:

dist/
├── bundle.js
├── injects
│   ├── iframe
│   │   ├── index.html
│   │   └── bundle.js
│   ├── index.css
│   └── bundle.js
└── manifest.json

所以我想以递归方式将每个(./webpack.config.js./index.js./src)转换为bundle.js

我写了以下gulpfile.js

'use strict';

const Path = require('path');
const Gulp = require('gulp');
const Through = require('through2');
const WebpackStream = require('webpack-stream');

const WpSubs = Through.obj(function(file, encoding, cb) {

  const wpFile = 'webpack.config.js';
  if (!file.path.endsWith(Path.sep + wpFile)) {
    return cb(null, file);
  }

  const wpConf = require(file.path);

  const path = Path.resolve( file.path.replace(wpFile, 'index.js') );
  const stream = Gulp
    .src(path)
    .pipe( WebpackStream(wpConf) );
  return cb(null, stream);

});

const excFolder = (name) => [`!./src/**/${name}`, `!./src/**/${name}/**/*`];
const excluded = [ ...excFolder('test') , ...excFolder('node_modules'), ...excFolder('src'), '!./**/index.js' ];

const src = ['./src/**/*', ...excluded];
const dst = './dist/';

Gulp.task('build', function(cb) {

  Gulp.src(src)
    .pipe(WpSubs)
    .pipe(Gulp.dest(dst))
    .on('end', cb);

});

webpack.config.js的典型内容:

'use strict';

const Path = require('path');

module.exports = {
  entry: Path.join(__dirname, './index.js'),
  output: {
    filename: 'bundle.js',
  }
};

0 个答案:

没有答案