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

rollup-plugin-body-iife

v1.0.0

Published

Transform a script file with imports and early returns into valid ES

Downloads

3

Readme

rollup-plugin-body-iife

This plugin is designed to convert scripts that use static imports and early returns into valid JS or TS. This is needed because this combination of language elements won't parse normally; both returns outside function bodies and non-top-level static imports fail parsing.

Input

Here is an example of a file that this plugin will transform:

import { checkGeoLocation } from '../util';

if (!checkGeoLocation(Flow.client.ipaddr)) return;

Application('suspiciousGeo').commit();

In this case, the desired output is:

import { checkGeoLocation } from '../util';

(function () {
    if (!checkGeoLocation(Flow.client.ipaddr)) return;
    Application('suspiciousGeo').commit();
})();

This transformation allows build-time dependency resolution using existing tools by making the file valid JS. Because the "main" function is immediately invoked, this is conceptually equivalent to leaving the main body outside a function declaration.

Build-Time Invocation

Here is an example of how to invoke this plugin when using the rollup module bundler. In rollup.config.js in the project root, include the following:

import * as glob from 'glob';
import bodyIife from 'rollup-plugin-body-iife';

/**
 * Glob pattern to match each trigger's script file.
 */
const TRIGGER_SCRIPT_GLOB = 'triggers/**/script.js';

/**
 * Inline dependencies and perform tree-shaking to convert an in-repo trigger
 * to one that is understood by the in-product trigger editor and runtime.
 *
 * @param {string} inputPath File path to the trigger script
 */
export const buildOneTrigger = inputPath => ({
    input: inputPath,
    output: {
        file: 'output/' + inputPath,
        format: 'cjs',
    },
    plugins: [
        bodyIife({
            // Only transform the triggers; transforming other JS files will
            // cause build errors.
            include: TRIGGER_SCRIPT_GLOB
        }),
    ],
});

/**
 * Rollup a JS "bundle" for each trigger.
 */
export default glob.sync(TRIGGER_SCRIPT_GLOB).map(buildOneTrigger);

It is recommended that this plugin run before others to ensure that later transforms get valid input. The input and output paths can be changed freely.