-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ESM support #214
Add ESM support #214
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
var pathToFileURL = require('url').pathToFileURL; | ||
|
||
var importESM; | ||
try { | ||
importESM = new Function('id', 'return import(id);'); | ||
} catch (e) { | ||
importESM = null; | ||
} | ||
|
||
function requireOrImport(path, callback) { | ||
var err = null; | ||
var cjs; | ||
try { | ||
cjs = require(path); | ||
} catch (e) { | ||
if (pathToFileURL && importESM && e.code === 'ERR_REQUIRE_ESM') { | ||
var url = pathToFileURL(path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just added this as I realized that |
||
importESM(url).then(function(esm) { callback(null, esm); }, callback); | ||
return; | ||
} | ||
err = e; | ||
} | ||
process.nextTick(function() { callback(err, cjs); }); | ||
} | ||
|
||
module.exports = requireOrImport; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ var registerExports = require('../../shared/register-exports'); | |
|
||
var copyTree = require('../../shared/log/copy-tree'); | ||
var getTask = require('./log/get-task'); | ||
var requireOrImport = require('../../shared/require-or-import'); | ||
|
||
function execute(opts, env, config) { | ||
|
||
|
@@ -33,16 +34,18 @@ function execute(opts, env, config) { | |
logSyncTask(gulpInst, opts); | ||
|
||
// This is what actually loads up the gulpfile | ||
var exported = require(env.configPath); | ||
requireOrImport(env.configPath, function(err, exported) { | ||
if (err) { | ||
console.error(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, if
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this works well! Good solution :) |
||
exit(1); | ||
} | ||
|
||
registerExports(gulpInst, exported); | ||
registerExports(gulpInst, exported); | ||
|
||
// Always unmute stdout after gulpfile is required | ||
stdout.unmute(); | ||
// Always unmute stdout after gulpfile is required | ||
stdout.unmute(); | ||
|
||
process.nextTick(function() { | ||
var tree; | ||
|
||
if (opts.tasksSimple) { | ||
tree = gulpInst.tree(); | ||
return logTasksSimple(tree.nodes); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
var expect = require('expect'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var semver = require('semver'); | ||
var skipLines = require('gulp-test-tools').skipLines; | ||
var eraseTime = require('gulp-test-tools').eraseTime; | ||
var runner = require('gulp-test-tools').gulpRunner; | ||
|
||
var expectedDir = path.join(__dirname, 'expected'); | ||
|
||
if (semver.gte(process.version, '10.15.3')) { | ||
|
||
describe('ESM', function() { | ||
|
||
it('prints the task list', function(done) { | ||
var options = '--tasks --sort-tasks ' + | ||
'--gulpfile ./test/fixtures/gulpfiles/gulpfile.mjs'; | ||
var trailingLines = 1; | ||
if (!semver.satisfies(process.version, '^12.17.0 || >=13.2.0')) { | ||
options += ' --experimental-modules'; | ||
trailingLines += 2; | ||
} | ||
|
||
runner({ verbose: false }).gulp(options).run(cb); | ||
|
||
function cb(err, stdout, stderr) { | ||
expect(err).toEqual(null); | ||
expect(stderr).toMatch(/^(.*ExperimentalWarning: The ESM module loader is experimental\.\n)?$/); | ||
var filepath = path.join(expectedDir, 'esm.txt'); | ||
var expected = fs.readFileSync(filepath, 'utf-8'); | ||
stdout = eraseTime(skipLines(stdout, trailingLines)); | ||
expect(stdout).toEqual(expected); | ||
done(err); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
gulp-cli/test/fixtures/gulpfiles | ||
├── exported | ||
└── registered |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import gulp from 'gulp'; | ||
|
||
function noop(cb) { | ||
cb(); | ||
} | ||
|
||
gulp.task('registered', noop); | ||
|
||
export function exported(){}; | ||
export const string = 'no function'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Node.js <10 errors out with a SyntaxError when loading a script that uses
import()
. So I dynamically create a function for that and catch the SyntaxError. That way we can keep supporting all Node.js versions all the way back to 0.10.