# babel-plugin-static-fs

> statically transforms Node fs for the browser

Latest version **3.0.0** (published 2019-07-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-static-fs
pnpm add babel-plugin-static-fs
yarn add babel-plugin-static-fs
bun add babel-plugin-static-fs
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2019-07-21 |
| First published | 2016-01-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 20.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 35 |
| Author | Matt DesLauriers |
| Maintainers | mattdesl |
| Keywords | fs, readFileSync, inline, transform, string, stringify, brfs, browser, bundler, bundle, transforms, static, statically, utf8, string, text |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-static-fs
- Repository: https://github.com/Jam3/babel-plugin-static-fs
- Issues: https://github.com/Jam3/babel-plugin-static-fs/issues
- npm.io page: https://npm.io/package/babel-plugin-static-fs

## Dependencies (5)

- [events](https://npm.io/package/events.md) ^1.1.0
- [resolve](https://npm.io/package/resolve.md) ^1.11.1
- [@babel/types](https://npm.io/package/@babel/types.md) ^7.5.5
- [@babel/template](https://npm.io/package/@babel/template.md) ^7.4.4
- [browser-resolve](https://npm.io/package/browser-resolve.md) ^1.11.3

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 3.0.0 (latest) — 2019-07-21
- 2.0.1 — 2019-07-20
- 2.0.0 — 2019-07-20
- 1.2.1 — 2019-07-20
- 1.2.0 — 2019-01-18
- 1.1.0 — 2016-01-12
- 1.0.0 — 2016-01-11

## README

# babel-plugin-static-fs

[![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)

A babel plugin to statically inline [Node `fs` calls](https://nodejs.org/api/fs.html). This is useful for building "universal" JavaScript (code that targets the browser and server) and [bundler-agnostic modules](https://gist.github.com/mattdesl/aaf759da84cc44c22305).

It also can be used as a replacement for [brfs](https://github.com/substack/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:

```js
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:

```js
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:

```js
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

```sh
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:

```sh
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.

```js
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](https://github.com/babel/babelify/issues/173).

To get around this in Browserify, you can use [brfs-babel](https://www.npmjs.com/package/brfs-babel) which replaces `brfs`.

```sh
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](http://github.com/Jam3/babel-plugin-static-fs/blob/master/LICENSE.md) for details.

---
_Source: https://npm.io/package/babel-plugin-static-fs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
