Skip to main content
Version: 1.3.1

Refactor Command

The refactor command processes the mapped JSON file generated by the map command and outputs the refactored JavaScript files to a specified directory. This is particularly useful for code transformation and restructuring tasks.

Usage​

js-recon refactor -t <technology> [options]

Options​

OptionAliasDescriptionDefaultRequired
--mapped-json <file>-mPath to the mapped JSON file generated by the map command.mapped.jsonNo
--output <directory>-oDirectory where refactored files will be saved.output_refactoredNo
--tech <tech>-tTechnology used in the JS files (run with -l/--list to see available options).Yes
--list-lList available technologies.falseNo

Examples​

Basic Usage​

To refactor code using the default mapped.json file and output to the default output_refactored directory:

js-recon refactor -t next

Custom Input and Output​

Specify a custom input JSON file and output directory:

js-recon refactor -m custom-mapped.json -o refactored_output -t next

List Available Technologies​

To see all supported technologies:

js-recon refactor --list

Output Structure​

The command creates a directory of individual .js files, one per webpack module ID, plus an index.js for the application entrypoint (React only):

output_refactored/
├── index.js ← entrypoint / bootstrap (React only)
├── 1.js
├── 2.js
└── ...

Each numbered .js file corresponds to a webpack module from the input bundle. All output files use ES module syntax (import/export) and are formatted with Prettier.

How it works (Next.js)​

webpack bundles each module as a function with the signature (module, exports, __webpack_require__). The third argument — the __webpack_require__ function — is what one module uses to import another. In the minified bundle this parameter is renamed to a single letter (for example, e, r, t), making the inter-module relationships opaque.

The refactor transform does two things to each chunk:

  1. Replaces webpack module calls with require(): The tool finds the name of the third function parameter and replaces every call <param>(<numeric_id>) with require("./<numeric_id>.js"). This restores standard Node.js import semantics, so the chunk can be statically analysed or run with node.

  2. Adds export default: The top-level function or arrow function declaration in each chunk is identified and export default <name>; is appended. This makes the module consumable as an ES module.

The result is a directory of standalone .js files that can be opened in an IDE, passed to other static-analysis tools, or executed individually.

Technology Reference​

For detailed, technology-specific documentation see:

  • React — webpack 5 React bundles: module splitting, entrypoint extraction, require-helper removal

Notes​

  • The command requires a mapped JSON file generated by the map command.
  • The technology (-t/--tech) flag is required to ensure proper refactoring rules are applied.
  • If the output directory doesn't exist, it will be created automatically.
  • Existing files in the output directory with the same names will be overwritten.