Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

[Question] Why does import require functions on the prototype to be bound to the instance? #797

@phated

Description

@phated

I'm working on some sample code for gulp and found that I can import series or parallel successfully but not src or dest.

Examples:

// gulpfile.esm.js
import { series, parallel } from 'gulp';
// gulpfile.esm.js
import { src, dest } from 'gulp';
// errors with SyntaxError: The requested module 'file:///User/home/v4/node_modules/gulp/index.js' does not provide an export named 'src'
// errors with SyntaxError: The requested module 'file:///User/home/v4/node_modules/gulp/index.js' does not provide an export named 'dest'

I've found that is because gulp does the following:

function Gulp() {
  Undertaker.call(this);

  // Bind the functions for destructuring
  this.watch = this.watch.bind(this);
  this.task = this.task.bind(this);
  this.series = this.series.bind(this);
  this.parallel = this.parallel.bind(this);
  this.registry = this.registry.bind(this);
  this.tree = this.tree.bind(this);
  this.lastRun = this.lastRun.bind(this);
}
util.inherits(Gulp, Undertaker);

Gulp.prototype.src = vfs.src;
Gulp.prototype.dest = vfs.dest;
Gulp.prototype.symlink = vfs.symlink;

module.exports = new Gulp();

If the following lines are added to the constructor, the imports work successfully:

this.src = this.src.bind(this);
this.dest = this.dest.bind(this);
this.symlink = this.symlink.bind(this);

I'll likely be patching gulp so everything works correctly with esm but I'm just wondering why this issue has occurred.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions