To get all the strings from a Javascript file, you’ll need to parse the file to get a syntax tree (AST), then walk it.
You can do this trivially with the ESPrima library (for parsing) and Estraverse (for walking the AST).
To identify strings, we need to check for “Literal” type tokens, and check whether the value is actually a string or not (I’m doing this with lodash, out of convenience):
const fs = require("fs");
const esprima = require("esprima");
const estraverse = require("estraverse");
const _ = require("lodash");
const filename = "node_modules/react/dist/react.js";
const ast = esprima.parse(
fs.readFileSync(filename)
);
estraverse.traverse(ast, {
enter: (node, parent) => {
if (node.type === "Literal") {
if (_.isString(node.value)) {
console.log(node.value);
}
}
}
});
- https://esprima.org/
- https://www.garysieling.com/blog/extracting-text-strings-javascript-file/