2

我正在尝试使用 browserify 编译所有 js 文件。

我目前遇到的问题是:https ://github.com/amsul/pickadate.js

在:https ://github.com/amsul/pickadate.js/blob/master/lib/picker.date.js

它需要一个相对路径:

require('./picker.js')

我想我会在其他文件中再次遇到这个问题。

一旦所有 js 文件都被编译并移动到我的“dest”目录,这个相对路径就不再正确了。

有没有办法覆盖它以便获得正确的文件?

这是我目前正在使用的完整 gulp 任务:

gulp.task('js', function () {
  var browserify = require('browserify');
  var source = require('vinyl-source-stream');
  var buffer = require('vinyl-buffer');
  var globby = require('globby');
  var through = require('through2');
  var uglify = require('gulp-uglify');
  var sourcemaps = require('gulp-sourcemaps');
  var reactify = require('reactify');

  // gulp expects tasks to return a stream, so we create one here.
  var bundledStream = through();

  bundledStream
    // turns the output bundle stream into a stream containing
    // the normal attributes gulp plugins expect.
    .pipe(source('app.js'))
    // the rest of the gulp task, as you would normally write it.
    // here we're copying from the Browserify + Uglify2 recipe.
    .pipe(buffer())
    .pipe(sourcemaps.init({loadMaps: true}))
      // Add gulp plugins to the pipeline here.
    .pipe(uglify())
    .on('error', gutil.log)
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./dest/js/'))
    .pipe(connect.reload());

  // "globby" replaces the normal "gulp.src" as Browserify
  // creates it's own readable stream.
  globby([
    './app/js/*.js',
    './app/js/materialize/**/*.js',
    './app/components/**/*.js'
  ], function(err, entries) {
    // ensure any errors from globby are handled
    if (err) {
      bundledStream.emit('error', err);
      return;
    }

    // create the Browserify instance.
    var b = browserify({
      entries: entries,
      debug: true,
      transform: [reactify]
    });

    var browser = browser({

    });

    // pipe the Browserify stream into the stream we created earlier
    // this starts our gulp pipeline.
    b.bundle().pipe(bundledStream);
  });

  // finally, we return the stream, so gulp knows when this task is done.
  return bundledStream;
});

picker.js 位于:./app/js/materialize/date_picker/picker.js

在我浏览并移动它之前,这是正确的位置。

4

0 回答 0