module-extractor
Extract a module and its dependencies into a new package
Usage
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
// 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
// Γ Not preserved in module copy! console.log('test')