https://github.com/aleclarson/module-extractor
Extract a JS/TS module and its dependencies into a new package
https://github.com/aleclarson/module-extractor
Last synced: 12 months ago
JSON representation
Extract a JS/TS module and its dependencies into a new package
- Host: GitHub
- URL: https://github.com/aleclarson/module-extractor
- Owner: aleclarson
- Created: 2022-04-30T06:46:43.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-05-19T15:12:28.000Z (about 4 years ago)
- Last Synced: 2025-01-02T06:08:32.008Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 62.5 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# module-extractor
[](https://www.npmjs.com/package/module-extractor)
[](https://github.com/prettier/prettier)
[](https://paypal.me/alecdotbiz)
> Extract a module and its dependencies into a new package
### Usage
```ts
import { extractModules } from 'module-extractor'
const extraction = extractModules({
// At least one entry module must be given
entries: ['index.ts'],
// The directory that contains package.json and source files
pkgRoot: '/path/to/package',
// Where to write the extracted modules and package.json
outPkgRoot: './extracted',
})
// Called as the module graph is crawled
extraction.on('moduleAdded', module => {...})
// Called when an import statement points to an unknown file
extraction.on('moduleNotFound', (id, importer) => {...})
// Called after each tree-shaked module is written
extraction.on('moduleWritten', (filename, code, module) => {...})
// Called after the new package.json is written
extraction.on('packageCreated', (pkgJson) => {...})
// Called when the extraction is complete
extraction.then(() => {...})
```
### Quirks
- Namespace imports prevent tree-shaking
```ts
// All exports of "./foo" will be kept, even if only some are needed.
import * as foo from './foo'
```
- Top-level side effects with unused return values are not preserved
```ts
// × Not preserved in module copy!
console.log('test')
```