Gulp: Add support for mjs gulpfile

Created on 24 Sep 2017  路  9Comments  路  Source: gulpjs/gulp

Hello,

As of Node 8.5.0, we can now have native support for ESM (https://nodejs.org/en/blog/release/v8.5.0/#header-notable-changes). I know this feature is still experimental, but I think it would be nice if gulp would support it, in the same way it already supports gulpfile.babel.js.

What were you expecting to happen?

$ gulp
[16:10:25] Using gulpfile.mjs - ExperimentalWarning: The ESM module loader is experimental.
[16:10:25] Starting 'default'...
Gulp with ES module! Yeah!
[16:10:25] Finished 'default' after 1.62 ms

What actually happened?

$ gulp
[16:10:19] No gulpfile found
$ gulp --gulpfile gulpfile.mjs
.../gulpfile.mjs:1
(function (exports, require, module, __filename, __dirname) { import gulp from "gulp";
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:588:28)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at execute (/usr/local/lib/node_modules/gulp-cli/lib/versioned/^4.0.0-alpha.2/index.js:35:18)

The gulpfile I used:

import gulp from "gulp";

const testTask = () =>
  new Promise(done => {
    console.log("Gulp with ES module! Yeah!");
    done();
  });

export default testTask;

What version of gulp are you using?

$ gulp -v
[16:42:58] CLI version 1.4.0
[16:42:58] Local version 4.0.0-alpha.2

What versions of npm and node are you using?

$ node -v
v8.5.0
$ npm -v
5.3.0
$ yarn -v
1.0.2

Thank you again for your amazing work at making Gulp!

All 9 comments

I'm not against this, but the way v8flags, interpret and liftoff work together don't allow for flags + extensions.

Hi @phated, will gulpfile.mjs be supported by gulp when node will release a new version with ES2015 modules without an experimental flag?

@t-ricci-molecle
Support for gulpfile.mjs should already be technically possible using @std/esm but it's safer to wait for native support of dynamic imports before implementing it in Gulp.

I haven't tested it, but following the example in @std/esm, you could try to have a gulpfile.js file bootstraping a gulpfile.mjs if you want to start using ESM right now.

Edit: It should be already possible to expose the programmatic API with an .mjs interface.

I want to support imports but I don't trust that the experimental version will ship as-is so I'm going to wait.

If you want imports right now, it's easiest to rename your gulpfile with .ts and install typescript + ts-node (no config files necessary).

Understandable, I was just asking for future support (which seems to be ok, and that reassures me)
I'm probably going to use TypeScript anyway

@t-ricci-molecle here's an issue to keep an eye on it: https://github.com/js-cli/js-interpret/issues/50

@phated can you reopen this issue? Now with Node 13.2 .mjs works without --experimental-modules.

If you'd like to help enable .mjs support, please see https://github.com/gulpjs/interpret/issues/65

Was this page helpful?
0 / 5 - 0 ratings