Created
March 4, 2019 15:50
-
-
Save mohamedelshorbagy/83303b03f2d1a94351009d9dd4c06528 to your computer and use it in GitHub Desktop.
Get all imports in a file including [import, require] , get the basic export
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { data } from 'data'; | |
const fs = require('fs'); | |
const d = require('babylon') | |
export { config, data }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const babylon = require('babylon'); | |
const traverse = require('babel-traverse').default; | |
const fs = require('fs'); | |
function parseImportsAndRequire(entryFile, { dynamicImports = true, parse = { sourceType: 'module', plugins: '*' } } = {}) { | |
let content = fs.readFileSync(entryFile, 'utf-8'); | |
let regModules = /\b(require|export|import)\b/gi; | |
let modules = { expressions: [], require: [], import: [], export: [] } | |
if (!regModules.test(content)) { | |
return modules; | |
} | |
let ast = babylon.parse(content, { sourceType: 'module', plugins: '*' }); | |
traverse(ast, { | |
enter(path) { | |
if (path.node.type === 'CallExpression') { | |
const callee = path.get('callee'); | |
const isDynamicImport = dynamicImports && callee.isImport(); | |
if (callee.isIdentifier({ name: 'require' }) || isDynamicImport) { | |
const arg = path.node.arguments[0]; | |
if (arg.type === 'StringLiteral') { | |
modules.require.push(arg.value) | |
} else { | |
modules.expressions.push(content.slice(arg.start, arg.end)) | |
} | |
} | |
} | |
else { | |
switch (path.node.type) { | |
case 'ImportDeclaration': | |
const { source } = path.node; | |
if (source && source.value) { | |
modules.import.push(source.value); | |
} | |
break; | |
case 'ExportNamedDeclaration': | |
const { specifiers = [] } = path.node; | |
for (let spec of specifiers) { | |
let value = getValue('exported.name', spec) | |
if (value) { | |
modules.export.push(value); | |
} | |
} | |
break; | |
case 'ExportDefaultDeclaration': | |
let value = getExportValueByType(path.node); | |
if (value) { | |
({}).toString.call([]).replace(/[\[\]]/g, '').split(' ')[1].toLowerCase() === 'array' | |
? modules.export.push(...value) | |
: modules.export.push(value); | |
} | |
break; | |
} | |
} | |
}, | |
}) | |
return modules; | |
} | |
console.log(JSON.stringify(parseImportsAndRequire('data.js'), null, 2)); | |
function getValue(p, o) { | |
let paths = typeof p === "string" ? p.split('.') : p; | |
return paths.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, o) | |
} | |
function getExportValueByType(node) { | |
let { declaration } = node; | |
switch (declaration.type) { | |
case 'Identifier': | |
return getValue('name', declaration) | |
case 'ObjectExpression': | |
let { properties = [] } = declaration; | |
return properties.map(p => getValue('key.name', p)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment