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 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-static-fs

v3.0.0

Published

statically transforms Node fs for the browser

Downloads

12,969

Readme

babel-plugin-static-fs

experimental

A babel plugin to statically inline Node fs calls. This is useful for building "universal" JavaScript (code that targets the browser and server) and bundler-agnostic modules.

It also can be used as a replacement for brfs in some cases – adding source maps, cleaner output code, ES2015 import support, and more robust handling of unusual syntax.

For example, say you have the following ES2015 source:

import { readFileSync } from 'fs';
import { join } from 'path';
const src = readFileSync(join(__dirname, 'hello.txt'), 'utf8');

And hello.txt is a text file containing the string "Hello, World!".

After transformation, it will look like this:

import { join } from 'path';
const src = 'Hello, World!';

Your ES5 (npm) distribution code is now usable in Node, Browserify, Webpack, JSPM, and everything in between.

Features

Currently supports CommonJS require() statements and common flavours of ES2015 import (no wild cards).

The following fs functions are supported:

  • fs.readFileSync(filepath, [enc])
  • fs.readdirSync(filepath)

The following path functions will be evaluated statically when they are found inside the arguments of the above calls:

  • path.join()
  • path.resolve()

You can also use require.resolve() like so:

const fs = require('fs');
const str = fs.readFileSync(require.resolve('./hello.txt'), 'utf8');
console.log(str);

By default, this plugin will gracefully skip expressions that can't be statically evaluated at build time.

Install

npm install babel-plugin-static-fs --save-dev

After installing, you will need to add it to your .babelrc as a new plugin, or set it up as an option to babelify or Webpack's babel-loader.

For example, in browserify:

browserify src/index.js -t [ babelify --plugins static-fs ]

Usage

See babel docs for more info on using plugins.

You can specify an onFile function to the plugin to receive new dependencies.

var staticFs = require('babel-plugin-static-fs');
var babel = require('babel-core');

var result = babel.transform(input, {
  plugins: [
    [ staticFs, {
      target: 'browser', // defaults to node
      dynamic: false, // defaults to true
      onFile: onFile // callback that receives new file dependencies
    } ]
  ],
  filename: filename
});

function onFile (file) {
  console.log('Discovered new dependency:', file);
}

You can specify the { target } field as either 'node' or 'browser', this will only affect transformed require.resolve() calls to modules, determining whether to use a package's "browser" field or not. By default, the target is 'node'.

The { dynamic } option (default true) when enabled will gracefully skip expressions that cannot be statically evaluated. If you set this to false, then the build will fail and throw an error when it reaches an expression that cannot be evaluated at build time.

File Watching & Re-Compilation

Since this introduces a new dependency in your graph at build-time, it might not get picked up by file watchers like watchify, budo, webpack, et al.

This seems to be a limitation in the babel + bundler bridge; see here.

To get around this in Browserify, you can use brfs-babel which replaces brfs.

watchify index.js -t brfs-babel

Contributing

This module only supported a limited subset of fs. In future, it would be nice to support more features, such as readFile and readdir. Please open an issue if you would like to help contribute.

License

MIT, see LICENSE.md for details.