-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Support ESM w/ mjs extension where available (#214)
- Loading branch information
Showing
10 changed files
with
145 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
var pathToFileURL = require('url').pathToFileURL; | ||
|
||
var importESM; | ||
try { | ||
// Node.js <10 errors out with a SyntaxError when loading a script that uses import(). | ||
// So a function is dynamically created to catch the SyntaxError at runtime instead of parsetime. | ||
// That way we can keep supporting all Node.js versions all the way back to 0.10. | ||
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') { | ||
// This is needed on Windows, because import() fails if providing a Windows file path. | ||
var url = pathToFileURL(path); | ||
importESM(url).then(function(esm) { callback(null, esm); }, callback); | ||
return; | ||
} | ||
err = e; | ||
} | ||
process.nextTick(function() { callback(err, cjs); }); | ||
} | ||
|
||
module.exports = requireOrImport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
describe('ESM', function() { | ||
|
||
it('prints the task list', function(done) { | ||
if (semver.lt(process.version, '10.15.3')) { | ||
this.skip(); | ||
} | ||
|
||
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); | ||
} | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
gulp-cli/test/fixtures/gulpfiles | ||
├── exported | ||
└── registered |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |