-
-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New] Use import() on esm files in supported node versions
- Loading branch information
Showing
19 changed files
with
257 additions
and
11 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
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,13 @@ | ||
'use strict'; | ||
|
||
const { extname: extnamePath } = require('path'); | ||
const getPackageType = require('get-package-type'); | ||
|
||
module.exports = function (file) { | ||
const ext = extnamePath(file); | ||
|
||
if (ext === '.mjs' || (ext === '.js' && getPackageType.sync(file) === 'module')) { | ||
return import(file); | ||
} | ||
require(file); | ||
}; |
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,93 @@ | ||
'use strict'; | ||
|
||
var tap = require('tap'); | ||
var spawn = require('child_process').spawn; | ||
var concat = require('concat-stream'); | ||
var hasDynamicImport = require('has-dynamic-import'); | ||
|
||
tap.test('importing mjs files', function (t) { | ||
hasDynamicImport().then(function (hasSupport) { | ||
if (hasSupport) { | ||
var tc = function (rows) { | ||
t.same(rows.toString('utf8'), [ | ||
'TAP version 13', | ||
'# mjs-a', | ||
'ok 1 test ran', | ||
'# mjs-b', | ||
'ok 2 test ran after mjs-a', | ||
'# mjs-c', | ||
'ok 3 test ran after mjs-b', | ||
'# mjs-d', | ||
'ok 4 test ran after mjs-c', | ||
'# mjs-e', | ||
'ok 5 test ran after mjs-d', | ||
'# mjs-f', | ||
'ok 6 test ran after mjs-e', | ||
'# mjs-g', | ||
'ok 7 test ran after mjs-f', | ||
'# mjs-h', | ||
'ok 8 test ran after mjs-g', | ||
'', | ||
'1..8', | ||
'# tests 8', | ||
'# pass 8', | ||
'', | ||
'# ok' | ||
].join('\n') + '\n\n'); | ||
}; | ||
|
||
var ps = tape('import/*.mjs'); | ||
ps.stdout.pipe(concat(tc)); | ||
ps.stderr.pipe(process.stderr); | ||
ps.on('exit', function (code) { | ||
t.equal(code, 0); | ||
t.end(); | ||
}); | ||
} else { | ||
t.pass('does not support dynamic import'); | ||
t.end(); | ||
} | ||
}); | ||
}); | ||
|
||
tap.test('importing type: "module" files', function (t) { | ||
hasDynamicImport().then(function (hasSupport) { | ||
if (hasSupport) { | ||
var tc = function (rows) { | ||
t.same(rows.toString('utf8'), [ | ||
'TAP version 13', | ||
'# package-type-a', | ||
'ok 1 test ran', | ||
'# package-type-b', | ||
'ok 2 test ran after package-type-a', | ||
'# package-type-c', | ||
'ok 3 test ran after package-type-b', | ||
'', | ||
'1..3', | ||
'# tests 3', | ||
'# pass 3', | ||
'', | ||
'# ok' | ||
].join('\n') + '\n\n'); | ||
}; | ||
|
||
var ps = tape('import/package_type/*.js'); | ||
ps.stdout.pipe(concat(tc)); | ||
ps.stderr.pipe(process.stderr); | ||
ps.on('exit', function (code) { | ||
t.equal(code, 0); | ||
t.end(); | ||
}); | ||
} else { | ||
t.pass('does not support dynamic import'); | ||
t.end(); | ||
} | ||
}); | ||
}); | ||
|
||
function tape(args) { | ||
var proc = require('child_process'); | ||
var bin = __dirname + '/../bin/tape'; | ||
|
||
return proc.spawn('node', [bin].concat(args.split(' ')), { cwd: __dirname }); | ||
} |
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,8 @@ | ||
import tape from '../../index.js'; | ||
|
||
tape.test('mjs-a', function (t) { | ||
t.pass('test ran'); | ||
t.end(); | ||
global.mjs_a = true; | ||
}); | ||
|
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,7 @@ | ||
import tape from '../../index.js'; | ||
|
||
tape.test('mjs-b', function (t) { | ||
t.ok(global.mjs_a, 'test ran after mjs-a'); | ||
t.end(); | ||
global.mjs_b = true; | ||
}); |
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,7 @@ | ||
import tape from '../../index.js'; | ||
|
||
tape.test('mjs-c', function (t) { | ||
t.ok(global.mjs_b, 'test ran after mjs-b'); | ||
t.end(); | ||
global.mjs_c = true; | ||
}); |
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,7 @@ | ||
import tape from '../../index.js'; | ||
|
||
tape.test('mjs-d', function (t) { | ||
t.ok(global.mjs_c, 'test ran after mjs-c'); | ||
t.end(); | ||
global.mjs_d = true; | ||
}); |
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,7 @@ | ||
import tape from '../../index.js'; | ||
|
||
tape.test('mjs-e', function (t) { | ||
t.ok(global.mjs_d, 'test ran after mjs-d'); | ||
t.end(); | ||
global.mjs_e = true; | ||
}); |
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,7 @@ | ||
import tape from '../../index.js'; | ||
|
||
tape.test('mjs-f', function (t) { | ||
t.ok(global.mjs_e, 'test ran after mjs-e'); | ||
t.end(); | ||
global.mjs_f = true; | ||
}); |
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,7 @@ | ||
import tape from '../../index.js'; | ||
|
||
tape.test('mjs-g', function (t) { | ||
t.ok(global.mjs_f, 'test ran after mjs-f'); | ||
t.end(); | ||
global.mjs_g = true; | ||
}); |
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,6 @@ | ||
import tape from '../../index.js'; | ||
|
||
tape.test('mjs-h', function (t) { | ||
t.ok(global.mjs_g, 'test ran after mjs-g'); | ||
t.end(); | ||
}); |
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,8 @@ | ||
import tape from '../../../index.js'; | ||
|
||
tape.test('package-type-a', function (t) { | ||
t.pass('test ran'); | ||
t.end(); | ||
global.package_type_a = true; | ||
}); | ||
|
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,7 @@ | ||
import tape from '../../../index.js'; | ||
|
||
tape.test('package-type-b', function (t) { | ||
t.ok(global.package_type_a, 'test ran after package-type-a'); | ||
t.end(); | ||
global.package_type_b = true; | ||
}); |
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,6 @@ | ||
import tape from '../../../index.js'; | ||
|
||
tape.test('package-type-c', function (t) { | ||
t.ok(global.package_type_b, 'test ran after package-type-b'); | ||
t.end(); | ||
}); |
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 @@ | ||
{ | ||
"type": "module" | ||
} |