Skip to content
This repository was archived by the owner on Apr 16, 2019. It is now read-only.

Commit bcd482c

Browse files
committed
adding support for commonJS modules detection
1 parent f864eea commit bcd482c

File tree

9 files changed

+96
-15
lines changed

9 files changed

+96
-15
lines changed

HISTORY.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
Javascript Module Formats Change History
22
========================================
33

4-
@NEXT@
4+
0.0.2 (2014-03-05)
55
------------------
6+
7+
* support for `cjs` detection.
8+
9+
0.0.1 (2014-03-05)
10+
------------------
11+
12+
* first commit/release with support for `amd`, `yui` and `es`.

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@ $ npm install js-module-formats
2727
Usage
2828
-----
2929

30-
Detect the module format of a file called `file.js`:
30+
By calling `detect()` with a filesystem path, it returns one of the following values:
31+
32+
* `yui` Modules
33+
* `amd` Modules
34+
* `cjs` CommonJS modules (including nodejs modules)
35+
* `es` Modules
36+
* `undefined` if the detection fails
37+
38+
To detect the module format of a file called `file.js`:
3139

3240
```javascript
3341
var detect = require('js-module-formats').detect,
3442

35-
console.log(detect(__dirname + '/file.js')); // output: `amd` or `yui` or `es` or `undefined`
43+
console.log(detect(__dirname + '/file.js'));
3644
```
3745

3846
**Note:** ES modules without `import` or `export` statements will not be detected.

index.js

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@
1010

1111
var libfs = require('fs'),
1212
vm = require('vm'),
13-
contextForRunInContext = vm.createContext({
14-
require: null,
15-
module: null,
16-
console: {},
17-
window: {},
18-
document: {},
19-
global: {}
20-
});
13+
context = vm.createContext({});
2114

2215
module.exports = {
2316
detect: detect,
@@ -36,7 +29,11 @@ Analyze a javascript file, collecting the module or modules information when pos
3629
function extract(file) {
3730
var mods = [];
3831

39-
contextForRunInContext.YUI = {
32+
/**
33+
YUI detection is based on a simple rule:
34+
- if `YUI.add()` is called
35+
**/
36+
context.YUI = {
4037
add: function (name, fn, version, config) {
4138
mods.push({
4239
type: 'yui',
@@ -46,21 +43,61 @@ function extract(file) {
4643
});
4744
}
4845
};
49-
contextForRunInContext.define = function (name, fn, version, config) {
46+
47+
48+
/**
49+
AMD detection is based on a simple rule:
50+
- if `define()` is called
51+
**/
52+
context.define = function (name, fn, version, config) {
5053
mods.push({
5154
type: 'amd',
5255
name: name,
5356
version: version,
5457
config: config
5558
});
5659
};
60+
61+
62+
/**
63+
CommonJS detection is based on simple rules:
64+
- if the script calls `require()`
65+
- or if the script tries to export a function thru `module.exports`
66+
- or if the script tries to export an object thru `module.exports`
67+
- or if the script tries to export a function thru `exports`
68+
- or if the script tries to export an object thru `exports`
69+
- or if the script tries to add a new member to `module.exports`
70+
**/
71+
context.require = function () {
72+
mods.push({
73+
type: 'cjs'
74+
});
75+
throw new Error('Common JS script detected');
76+
};
77+
context.module = {
78+
exports: {}
79+
};
80+
context.exports = context.module.exports;
81+
82+
83+
// executing the content of the file into a new context to avoid leaking
84+
// globals during the detection process.
5785
try {
58-
vm.runInContext(libfs.readFileSync(file, 'utf8'), contextForRunInContext, file);
86+
vm.runInContext(libfs.readFileSync(file, 'utf8'), context, file);
5987
} catch (e) {
88+
// console.log(e.stack || e);
6089
// very dummy detection process for ES modules
6190
if (e.toString() === 'SyntaxError: Unexpected reserved word') {
6291
mods.push({type: 'es'});
6392
}
93+
} finally {
94+
// very dummy detection process for CommonJS modules
95+
if (typeof context.module.exports === 'function'
96+
|| typeof context.exports === 'function'
97+
|| Object.keys(context.module.exports) > 0
98+
|| Object.keys(context.exports) > 0) {
99+
mods.push({type: 'cjs'});
100+
}
64101
}
65102
// returning an array when more than one module is defined in the file
66103
return mods.length > 1 ? mods : mods[0];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-module-formats",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "Detect different types of javascript modules formats",
55
"homepage": "https://github.com/yahoo/js-module-formats",
66
"main": "index",

test/detect.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,24 @@ describe('detect', function () {
7676

7777
});
7878

79+
describe('cjs', function () {
80+
81+
it('should detect `cjs` for fixtures/cjs.js', function () {
82+
expect(detect(fixtures + 'cjs.js')).to.be.equal('cjs');
83+
});
84+
85+
it('should detect `cjs` for fixtures/cjs-require.js', function () {
86+
expect(detect(fixtures + 'cjs-require.js')).to.be.equal('cjs');
87+
});
88+
89+
it('should detect `cjs` for fixtures/cjs-exports.js', function () {
90+
expect(detect(fixtures + 'cjs.js')).to.be.equal('cjs');
91+
});
92+
93+
it('should detect `cjs` for fixtures/cjs-module-exports.js', function () {
94+
expect(detect(fixtures + 'cjs.js')).to.be.equal('cjs');
95+
});
96+
97+
});
98+
7999
});

test/fixtures/cjs-exports.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports = function () {
2+
return true;
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function () {
2+
return true;
3+
};

test/fixtures/cjs-require.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var path = require('path');

test/fixtures/cjs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var path = require('path');
2+
module.exports = path;

0 commit comments

Comments
 (0)