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

vite-plugin-iife

v1.1.6

Published

Vite plugin for simple IIFE-compiled script imports.

Downloads

332

Readme

vite-plugin-iife

NPM Package vite-plugin-iife License: MIT

Vite plugin for simple IIFE-compiled script imports.

Overview

Sometimes you need a small snippet of code to run in a specific place at at a specific time during a page's loading process to achieve a specific effect or prevent the dreaded FOUC.

Running scripts as modules is great, but by design they are unavoidably executed with an implicit defer, and build systems can make it tricky to get a single chunk of code to behave differently.

This plugin lets you write scripts to be inlined using either JavaScript or TypeScript, and get a minified JavaScript IIFE snippet of code via an import statement, which makes it easy to integrate with static site generators:

import inlineIifeSnippet from './some-script.ts?iife'

// Logs the raw IIFE-compiled code from some-script.ts:
console.log(inlineIifeSnippet)

Installation

1. Install the plugin package

Assuming you're starting with a Vite project of some flavor:

npm install --save-dev vite-plugin-iife

2. Add the plugin to your vite.config file

// vite.config.ts
import iife from 'vite-plugin-iife'
import { defineConfig } from 'vite'

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

3. Configure TypeScript

Skip this step if you're using plain JavaScript.

Add the extension declarations to your types in tsconfig.json:

{
  "compilerOptions": {
    "types": ["vite-plugin-iife/ext"]
  }
}

Alternately, you can add a triple-slash package dependency directive to your global types file (e.g. env.d.ts or similar):

/// <reference types="vite-plugin-iife/ext" />

This step should take care of errors like:

Cannot find module './test-script.ts?iife' or its corresponding type declarations.ts(2307)

Usage

IIFE imports

Append ?iife to any script import string to receive the IIFE-compiled code as a default export string:

// Imported as an IIFE-compiled string
// Embed it in a <script> tag somewhere, etc.
import iifeSnippet from './some-script.ts?iife'

// Imported normally
import snippet from './some-script.ts'

Since there's no code splitting or externalization of dependencies, vite-plugin-iife is only recommended for small and relatively self-contained scripts. The verbose option can help you keep tabs on the output size during development.

Plugin options

The plugin accepts a few options in its initialization function in your vite.config file. The options object type is exported as IifePluginOptions.

| Key | Type | Description | Default | | --------- | ------------------------- | ------------------------------------------------------------------- | ------- | | minify | true \| false \| 'auto' | Minify output. The 'auto' value only minifies on production builds. | 'auto' | | verbose | boolean | Log information to the console. | false |

Maintainers

@kitschpatrol

Contributing

Issues and pull requests are welcome.

License

MIT © Eric Mika