Giter Site home page Giter Site logo

module-extractor's Introduction

module-extractor

npm Code style: Prettier Donate

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')

module-extractor's People

Contributors

aleclarson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

module-extractor's Issues

Support for multiple package.json inputs

Sometimes dev dependencies can exist in a monorepo's root package.json, or maybe I have multiple entries from different packages that I want merged. In the latter case, it would probably be best to add support for an entry-specific config, like so:

// This maps "outSrcRoot" => "entry"
entries: {
  'src/foo': 'packages/foo/src/index.ts',
  'src/bar': 'packages/bar/src/index.ts',
}

Then let's add an array version of pkgRoot option. The highest common ancestor is used to compute module IDs.

pkgRoots: ['/path/to/packages/foo', '/path/to/packages/bar']

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.