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

virtual_modules

v0.0.29

Published

Enabling the virtual module space

Downloads

126

Readme

Node Virtual Modules

Virtual Modules lets you create and modify files in a way that node treats them as if they were physically presented in the file system.

Install

# with NPM
$ npm i --save virtual_modules

# with Yarn
$ yarn add virtual_modules

Basic Usage

First patch Node's file system and require to consider our virtual one like the following.

import vm from 'virtual_modules'
vm.patchFS()

This will have no conflict with @babel/register

Next, you can write to the virtual space like the following.

vm.mkdirSync('/path/to/assets', { recursive: true })
vm.writeFileSync('/path/to/assets/foo.js', 'module.exports = { foo: "foo" }')

//... somewhere else
const foo = require('/path/to/assets/foo.js')
console.log(foo.foo) //--> "foo"

While it looks a file was simply written in the file system, it won't actually exist upon manual inspection. What was created is a virtual file. You can also read a virtual file like the following.

fs.readFileSync('/path/to/assets/foo.js', 'utf8') //--> module.exports = { foo: "foo" }

Virtual Node Modules

This also works for defining virtual files in node_modules.

vm.writeFileSync('/my/project/node_modules/foo.js', 'module.exports = { foo: "foo" }')

//... somewhere in /my/project
const foo = require('foo')
console.log(foo.foo) //--> "foo"

Virtual File Routing

Just call route() to make virtual file routes. Once they are registered, they can be immediately used.

vm.route('/my/post/:id/info.json', (filename, res, vm) => {
  //extract the params from the filename
  const params = vm.routeParams(filename, '/my/post/:id/info.json')
  //set the response body as a string or object
  res.body = { id: params.params.id }
})

fs.readFileSync('/my/post/1/info.json').toString() //--> { id: 1 }
require('/my/post/1/info.json').id //--> 1

When a route is requested by readFileSync(), it gets computed and written to the vm in a lazy way.

Transforming files

Virtual modules has a basic transformer that sits on top that you can use optionally.

vm.addRule(/\.(js)$/, (file, code) => {
  return code + ';console.log("transformed");'
})

If you are using babel to transform files, you can still use babel without interupting your work flow.

You can optionally replace @babel/register with virtual modules like the following snippet.

const babel = require('@babel/core')
vm.addRule(/\.(js)$/, (file, code) => {
  return babel.transform(code, {
    presets: [
      '@babel/preset-env',
      '@babel/preset-react'
    ]
  }).code
})

Stop/Start

vm works directly with Node's Module and require.cache. To cover cases to revert to the original Node methods you can start and stop virtual modules like this.

vm.revertPatch()

vm.patchFS()

Wepack Plugin

Using routes in virtual modules could be problematic with webpack because it uses enhanced-resolve, and that uses fs.stat() on folders to determine if a file exists.

Since routes like /my/post/:id/info.json have an unlimited permutation, it would not be possible for vm to populate stats in a directory. It could still work however, if all the possible permutations were pre-written to virtual modules.

Therefore we created a webpack plugin you can insert as a plugin resolver in your webpack config.

import VMWebpackPlugin from 'virtual_modules-webpack'

module.exports = {
  resolve: {
    ...
    plugins: [ new VMWebpackPlugin ]
  },
  ...
}