virtual-file

[![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url] [![Dependency Status][david-image]][david-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][dow

Usage no npm install needed!

<script type="module">
  import virtualFile from 'https://cdn.skypack.dev/virtual-file';
</script>

README

virtual-file

NPM version Build status Test coverage Dependency Status License Downloads Gittip

A virtual file abstraction for. This is meant to be used as the file abstraction for normalize.

Goals:

  • Caching ideal for long running processes
  • Easily file transformations, including intermediary files
  • Seamless source map support
  • Suitable for production usage

Current limitations:

  • Files are considered to be on-disk. However, inline files (i.e. inlined <script>s) are currently not supported. Need to figure out a mechanism for those types of files.

API

var file = new File(uri)

Creates a new file abstraction based on a URI. A URI could have #s or ?s. However, the file must be a local file. If it's a remote file, download it first.

Properties

  • .filename - the filename of the file. Unlike the URI, this property is dynamic and removes any #s and ?s in the name.
  • .dirname - the folder this file is located.
  • .basename - the basename of this file. This property is dynamic, meaning you can set it, and the filename will be updated automatically.
  • .type - the current mime type of the file. Set the current mime type of the file to keep track of the current transformations.
  • .exists - whether the file exists, defaulting to true.
  • .sourcemap - whether source maps are enabled, defaulting to true.
  • .static - whether this file changes over time (i.e. for a watching instance), defaulting to false. Set this to true in production when you know files aren't going to change.

file.stat().then( => )

Check fs.stat() the file and get the sha256 sum of the file. Adds the following properties:

  • .length - the byte length of the file
  • .mtime - the modified time of the file as a Date.
  • .hash - the sha256 sum of the file as a Buffer.

You should run this before running anything else.

file.getString().then( string => )

Get the current contents of the file as a string. Also sets it as .string. If the string contains an inline source map, it will be removed and moved to .map.

file.string=

Set a string as the current contents of the file after a transformation. If the string contains an inline source map, it will be removed and moved to .map.

file.map

Get the current source map of the file, if any.

file.map=

Set the source map of the file. This should be set before file.string=. If a previous source map exists, it will be applied to the newest source map. Otherwise, the current string and .basename will be set as the source.

file.isStale().then( stale => )

Check whether the file is stale. If it is stale, then a new file object will be returned. Being using the new file object instead of this one. The file is stale if any of the following is true:

  • The file previously did not exist, but now does.
  • The file's length has changed.
  • The file's mtime has changed.
  • The sha256 sum of the file has changed.