npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2021 – Pkg Stats / Ryan Hefner

babel-parse-wild-code

v1.1.1

Published

parse a file in a foreign directory with babel, using ambient babel config

Downloads

72

Readme

babel-parse-wild-code

Provides an easy-to-use API for parsing users' js/ts/tsx files using their project's installed Babel version and config. This way it won't fail if they're using funky features like the smart pipeline operator. This is a big problem for codemod tools; for example, jscodeshift would choke on the smart pipeline operator unless you pass a custom parser. I want my codemod tools to just work.

Note: only Babel 7 is currently supported.

babel-parse-wild-code also improves performance. Until recently, doing require('@babel/core').loadOptionsAsync and passing that config to a bunch of require('@babel/core').parseAsync calls didn't improve performance as expected; parseAsync was accidentally re-doing a lot of the work that had already been done by loadOptionsAsync. That bug has been fixed since I reported it, but babel-parse-wild-code works around this for older versions of Babel 7 by extracting the options for @babel/parser from the user's Babel config.

If babel-parse-wild-code fails to load @babel/core, @babel/parser, or the Babel config from the user's project, it falls back to parsing with reasonable default options.

API

parseSync(file: string, options?: { encoding?: BufferEncoding } & Omit<ParserOptions, 'plugins'>): File

import { parseSync } from 'babel-parse-wild-code'

Parses the given file synchronously, returning the File node.

encoding defaults to utf8. The remaining options are passed to @babel/parser's parse function.

parseAsync(file: string, options?: { encoding?: BufferEncoding } & Omit<ParserOptions, 'plugins'>): Promise<File>

import { parseAsync } from 'babel-parse-wild-code'

Parses the given file asynchronously, returning a Promise that will resolve to the File node.

encoding defaults to utf8. The remaining options are passed to @babel/parser's parse function.

clearCache(): void

Instances of @babel/core, @babel/parser and parser options are cached on a per-directory basis. Calling clearCache() clears this cache, and deletes instances of @babel/core and @babel/parser from require.cache.

You should probably do this before any bulk parsing operation. It would be nice to bust the cache automatically when the user's Babel version or config changes, but setting up the watchers would be complicated. Clearing the cache before you parse a bunch of files is simpler and won't have a huge impact on performance.

getParserSync(file: string, options?: Omit<ParserOptions, 'plugins'>): Parser

import { getParserSync } from 'babel-parse-wild-code'

Gets a fully-configured parser for the given file synchronously.

options is additional options for @babel/parser's parse function. For example when working with jscodeshift or recast, you should pass { tokens: true }.

getParserAsync(file: string, options?: Omit<ParserOptions, 'plugins'>): Promise<Parser>

import { getParserSync } from 'babel-parse-wild-code'

Gets a fully-configured parser for the given file asynchronously.

options is additional options for @babel/parser's parse function. For example when working with jscodeshift or recast, you should pass { tokens: true }.

class Parser

import { Parser } from 'babel-parse-wild-code'

Type defs:

import * as defaultBabelParser from '@babel/parser'

type BabelParser = Pick<typeof defaultBabelParser, 'parse' | 'parseExpression'>

export class Parser {
  readonly babelParser: BabelParser
  readonly parserOpts: ParserOptions

  constructor(babelParser: BabelParser, parserOpts: ParserOptions)
  parse(code: string, parserOpts?: ParserOptions): t.File
  parseExpression(code: string, parserOpts?: ParserOptions): t.Expression
  bindParserOpts(parserOpts: ParserOptions): Parser
}

tsParser

import { tsParser } from 'babel-parse-wild-code'

The parser used for .ts files.

tsxParser

import { tsxParser } from 'babel-parse-wild-code'

The parser used for .tsx files.

jsParser

import { jsParser } from 'babel-parse-wild-code'

The fallback parser used for parsing .js files when babel-parse-wild-code failed to load Babel modules or config for the file's directory.