-
Notifications
You must be signed in to change notification settings - Fork 44
/
check-esnext.js
36 lines (30 loc) · 972 Bytes
/
check-esnext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
var fs = require('fs');
var path = require('path');
var getPkgDir = require('pkg-dir').sync;
var appDir = getPkgDir();
function checkESnextPath(filepath) {
return (
filepath.indexOf(appDir) === 0 && filepath.indexOf('node_modules') === -1
);
}
function checkEsnextConfig(filepath) {
var pkgDir = getPkgDir(path.dirname(filepath));
var packageJsonPath = path.join(pkgDir, 'package.json');
if (fs.existsSync(packageJsonPath)) {
var json = fs.readFileSync(packageJsonPath, 'utf8');
return /"((e|j)snext(:(browser|server|main))?)":/m.test(json);
}
}
var cache = {};
module.exports = function checkEsnextCached(module) {
if (!(module in cache)) {
if (path.isAbsolute(module)) {
cache[module] = checkESnextPath(module) || checkEsnextConfig(module);
} else {
var absModule = require.resolve(module);
cache[module] = absModule !== module && checkEsnextCached(absModule);
}
}
return cache[module];
};