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

@inox-tools/inline-mod

v2.0.1

Published

Define a virtual module inline with any reference to buildtime values

Downloads

156

Readme

Inline Virtual Modules

Ever wanted to pass configuration values from build-time to runtime? To provide a clean and friendly API to customize an integration, theme or plugin with custom functions for maximum versatility, but was prevented from doing that by the different execution times of the configuration and the code of your library.

That is what this library is for!

It allows serializing non-trivial values into an ECMAScript module that can be imported at runtime, server or client. Currently, this library only provides bindings for Vite, but the core can be used to generate modules as files or to create binding for any other bundler that supports virtual modules.

Developers are then able to pass and receive non-trivial values on configuration files that affect the behavior of their applications. For example, imagine you have a plugin to fetch remote fucntions locally

Imagine if you could have a Vite plugin with this API:

// vite.config.ts
export default defineConfig({
  plugins: [
    yourPlugin({
      fetchFunction: (url, requestInit) => {
        console.log('Lib is calling fetch');
        return fetch(url, requestInit);
      },
    }),
  ],
});

And access your configuration at runtime inside your runtime code like a normal module:

// plugin/runtime.ts
import { fetchFunction } from 'yourLib:config';

const response = await fetchFunction(/* ... */);

No more weirdly JSON-based JS generators replicated in every project to serialize a configuration.
No more oceans of flags and options to encode every possible use case as a plain object that can be turned into a JSON to decide what to do at runtime. Use whatever is most semantic for your API. Supports:

  • Classes
  • Sub-classes
  • Instances
  • Functions
  • Circular values
  • Any type of number (including Infinity, -Infinity, NaN and -0)
  • BigInts
  • Symbols
  • Implementations of JS syntax features like custom iterators and async iterators
  • And more!

Getting Started

Add the Vite plugin that will resolve inline modules to their source code during the bundling:

// vite.config.mjs
import { defineConfig } from 'vite';
import inlineModulePlugin from '@inox-tools/inline-mod/vite';

export default defineConfig({
  plugins: [inlineModulePlugin()],
});

Then define a module inline:

// vite.config.mjs
import { defineConfig } from 'vite';
import inlineModulePlugin, { defineModule } from '@inox-tools/inline-mod/vite';

defineModule('virtual:config', {
  constExport: {
    configFunction: () => 'value from config',
  },
});

// Or get the module name auto-generated
const moduleName = inlineModule({
  constExport: {
    configFunction: () => 'value from config',
  },
});

export default defineConfig({
  plugins: [inlineModulePlugin()],
});

Finally, import the defined module from anywhere in your code:

import { configFunction } from 'virtual:config';

const configValue = configFunction();

License

Because this is a port and derivation of part of an idea that is within some existing code, the appropriate licensing for this is somewhat confusing.

The original code by Pulumi Corporation is licensed under the Apache 2.0 license.
All the code made by me is licensed under the MIT license.