This repository has been archived by the owner on Apr 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
132 lines (115 loc) · 3.87 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (c) 2014, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/*jslint node: true, nomen: true */
'use strict';
var libfs = require('fs'),
vm = require('vm'),
context = vm.createContext({});
// RegExp to detect ES6 modules
// cortesy of https://github.com/ModuleLoader/es6-module-loader
// comprehensively overclassifying regex detectection for es6 module syntax
var ES6ImportExportRegExp = /(?:^\s*|[}{\(\);,\n]\s*)(import\s+['"]|(import|module)\s+[^"'\(\)\n;]+\s+from\s+['"]|export\s+(\*|\{|default|function|var|const|let|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*))/,
ES6AliasRegExp = /(?:^\s*|[}{\(\);,\n]\s*)(export\s*\*\s*from\s*(?:'([^']+)'|"([^"]+)"))/;
module.exports = {
detect: detect,
extract: extract
};
/**
Analyze JavaScript source, collecting the module or modules information when possible.
@method extract
@default
@param {string} src The JavaScript source to be analyzed
@return {object|array} an object or a collection of object with the info gathered
from the analysis, it usually includes objects with `type` and `name` per module.
**/
function extract(src) {
var mods = [];
/**
YUI detection is based on a simple rule:
- if `YUI.add()` is called
**/
context.YUI = {
add: function (name, fn, version, config) {
mods.push({
type: 'yui',
name: name,
version: version,
config: config
});
}
};
/**
AMD detection is based on a simple rule:
- if `define()` is called
**/
context.define = function () {
mods.push({
type: 'amd'
});
};
/**
Steal detection is based on a simple rule:
- if `steal()` is called
**/
context.steal = function () {
mods.push({
type: 'steal'
});
};
/**
CommonJS detection is based on simple rules:
- if the script calls `require()`
- or if the script tries to export a function thru `module.exports`
- or if the script tries to export an object thru `module.exports`
- or if the script tries to export a function thru `exports`
- or if the script tries to export an object thru `exports`
- or if the script tries to add a new member to `module.exports`
**/
context.require = function () {
mods.push({
type: 'cjs'
});
throw new Error('Common JS script detected');
};
context.exports = Object.create(null);
context.module = context;
// executing the JavaScript source into a new context to avoid leaking
// globals during the detection process.
try {
vm.runInContext(src, context);
} catch (e) {
// console.log(e.stack || e);
// detection process for ES modules
if (ES6ImportExportRegExp.test(src) || ES6AliasRegExp.test(src)) {
mods.push({type: 'es'});
}
} finally {
// very dummy detection process for CommonJS modules
if (typeof context.exports === 'function' ||
typeof context.exports === 'string' ||
typeof context.exports === 'number' ||
Object.keys(context.exports).length > 0 ||
Object.getPrototypeOf(context.exports)) {
mods.push({type: 'cjs'});
}
}
// returning an array when more than one module is defined in the source
return mods.length > 1 ? mods : mods[0];
}
/**
Analyze JavaScript source, detecting if the file is a YUI, AMD or ES module.
@method detect
@default
@param {string} src The JavaScript source to be analyzed
@return {string} `yui` or `amd` or `es`
**/
function detect(src) {
var mod = extract(src);
if (Array.isArray(mod)) {
mod = mod.shift(); // picking up the first module from the list
}
return mod && mod.type;
}