简体   繁体   中英

Where does the value of “file” argument come from

Here is a code snippet from a gulp file. I'd like to understand passing of argument "file" to internal functions. More importantly, I'd like to understand this idiom because I see it often in Javascript. My guess is that "tsResult.js" iterates through various javascript files in this Typescript project and the "file" argument is each such file. How can I decipher such usage below and in future

gulp.task('scripts', ['clean'], () => {
const tsResult = tsProject.src().pipe(sourcemaps.init()).pipe(tsProject());
  return tsResult.js.pipe(sourcemaps.write({
    includeContent: false,
    sourceRoot: function (file) {
      return path.relative(path.dirname(file.path), file.base);
    }
  }))
    .pipe(gulp.dest(OUTPUT_FOLDER));
});

Many JS libraries make heavy use of a design pattern called Dependency Injection .

Basically, gulp-sourcemaps is giving you the option of overriding how the source root (the root URL where the file is located) is determined.

From the gulp-sourcemaps documentation :

Set the location where the source files are hosted (use this when includeContent is set to false). This is usually a URL (or an absolute URL path), not a local file system path. By default the source root is '' or in case destPath is set, the relative path from the source map to the source base directory (this should work for many dev environments). If a relative path is used (empty string or one starting with a .), it is interpreted as a path relative to the destination. The plugin rewrites it to a path relative to each source map.

The sourceRoot function is called, passing in the file that is currently being processed, and is expected to return the root path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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