Skip to content

Commit

Permalink
Initial working version
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Jul 26, 2022
0 parents commit 6c03630
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
index.js
package-lock.json
node_modules
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# babel-plugin-module-deps

This Babel plugin redefines `require` within all (CJS) modules to record any
required modules in an array of resolved filenames called `module.deps`.
(Note that the array may have duplicates.)

This lets a tool later discover what other modules this module depends on,
by inspecting `require.cache[filename].deps`.

## Usage

Babel configuration:

```json
{
"plugins": ["babel-plugin-module-deps"]
}
```
63 changes: 63 additions & 0 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module.exports = ({types}) ->
program = null
visitor:
Program: (path) ->
program = path
undefined
post: ->
program.unshiftContainer 'body',
types.expressionStatement types.assignmentExpression(
'='
types.identifier 'require'
types.callExpression(
types.arrowFunctionExpression(
[types.identifier 'r']
types.arrowFunctionExpression(
[types.identifier 'id']
types.sequenceExpression [
types.callExpression(
types.memberExpression(
types.identifier 'console'
types.identifier 'log'
)
[
types.identifier '__filename'
types.identifier 'id'
]
)
types.callExpression(
types.memberExpression(
types.memberExpression(
types.identifier 'module'
types.identifier 'deps'
)
types.identifier 'push'
)
[types.callExpression(
types.memberExpression(
types.identifier 'r'
types.identifier 'resolve'
)
[types.identifier 'id']
)]
)
types.callExpression(
types.identifier 'r'
[types.identifier 'id']
)
]
)
)
[types.identifier 'require']
)
)
program.unshiftContainer 'body',
types.expressionStatement types.assignmentExpression(
'='
types.memberExpression(
types.identifier 'module'
types.identifier 'deps'
)
types.arrayExpression()
)
undefined
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "babel-plugin-module-deps",
"version": "0.0.0",
"description": "Babel plugin to annotate CJS modules with what they require",
"main": "index.js",
"scripts": {
"prepare": "coffee --bare -c index.coffee"
},
"repository": {
"type": "git",
"url": "git+https://github.com/edemaine/babel-plugin-module-deps.git"
},
"author": {
"name": "Erik Demaine",
"email": "[email protected]",
"url": "http://erikdemaine.org/"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/edemaine/babel-plugin-module-deps/issues"
},
"homepage": "https://github.com/edemaine/babel-plugin-module-deps#readme",
"devDependencies": {
"coffeescript": "^2.7.0"
}
}

0 comments on commit 6c03630

Please sign in to comment.