From 6c03630fb58005a3e8ee56cb9314d7b8cf90a918 Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Tue, 26 Jul 2022 18:25:49 -0400 Subject: [PATCH] Initial working version --- .gitignore | 3 +++ README.md | 18 +++++++++++++++ index.coffee | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 26 ++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 index.coffee create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f9181ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +index.js +package-lock.json +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..c4bf747 --- /dev/null +++ b/README.md @@ -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"] +} +``` diff --git a/index.coffee b/index.coffee new file mode 100644 index 0000000..494f71e --- /dev/null +++ b/index.coffee @@ -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 diff --git a/package.json b/package.json new file mode 100644 index 0000000..42eddf5 --- /dev/null +++ b/package.json @@ -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": "edemaine@mit.edu", + "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" + } +}