functions-to-modules

0.1.0 • Public • Published

Moves all exported functions from one module into separate modules.

  • Replaces named exports with import statements.
  • Populates the new modules with all necessary import statements.
  • Exports the imports at the end to allow for iterative migration.

Very alpha... see Known Bugs.

Usage

npx functions-to-modules module outputDir moduleDir
  • module - The input module file whose functions will be converted to modules.
  • outputDir - The directory in which the new source module will be written.
  • modulesDir - The name of the directory where the new modules will be written.

Example

// util.js used as input module
import fs from 'fs'

// hi
export const hi = () => 'hi'

// say hi
export const sayHi = () => {
  fs.writeFileSync(hi())
}
npx functions-to-modules ./util.js out util

./out/util.js:

import fs from 'fs'

import { hi } from './util/hi.js'
import { sayHi } from './util/sayHi.js'

export {
  hi,
  sayHi,
}

./out/util/sayHi.js:

import fs from 'fs'

// util
import { hi } from './hi.js'

// say hi
export const sayHi = () => {
  fs.writeFileSync(hi())
}

./out/util/hi.js:

import fs from 'fs'

// hi
export const hi = () => 'hi'

Known Bugs

  • Does not split up an import with multiple variables.
  • Does not move new import statements to the top of the new source file.
  • Does not adjust relative paths of existing imports in the new module.
  • Adds imports for modules mentioned in comments.
  • Only works on named exports.
  • Assumes all non-empty lines immediately above an export are comments.
  • No tests.

Licence

ISC

Readme

Keywords

none

Package Sidebar

Install

npm i functions-to-modules

Weekly Downloads

1

Version

0.1.0

License

ISC

Unpacked Size

5.53 kB

Total Files

3

Last publish

Collaborators

  • raine