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

bundled-source

v2.0.0

Published

Get source of module, as if bundled as its own endpoint

Downloads

4

Readme

Point it to a module, and it'll return a string of the bundled source for that module and its dependencies. Evaluating that string with new Function (NOT eval) will give you the exports of your module.

  • Works in bundles bundled with browserify.
  • Works in bundles bundled with webpack.
  • Works in node without having used a bundler IFF you have browserify installed and available in node_modules, and your module can be browserified with the node and fullPaths options.

Does all these things simultaneously if you follow the calling convention below.

Usage

const bundledSource = require('bundled-source');

const srcPromise = bundledSource(
    require.resolve && require.resolve('./some-module'), // used in node and in webpack'd bundle
    require('./some-module'),         // used in browserified bundle
);

// in non-bundled node, we call out to browserify, so need to be async.
const src = await srcPromise;

// ELSEWHERE, maybe in an isolated-vm or webworker
// you can do this to get the exports of `some-module`:

const f = new Function(src);
const exports = f();

See test/index.js to see it used for real.

Why?

Useful for taking module source and shoving it in a webworker, isolate-vm, or other separated environment, if your code is bundled with different bundlers depending on the environment it runs in. For this reason, we ensure the API is one that can be handled by all of these environments and bundlers without any changes.

You probably want to use webworkify or webworkify-webpack if you're only bundling and running in one environment.

Compatibility

Tested with:

  • webpack 4.41.2

  • browserify 16.5.0

  • node 8.15.0

  • NOT tested with webpack configurations that use more than one chunk.

  • Probably NOT compatible with babel's es modules.

Warnings

As you see above, we are forced to call require('./some-module') in the environment where we do the bundling. Would have been nice to avoid, but since we are using webworkify's code for it, unavoidable. Be aware of this and architect your some-module so that it doesn't have any side unwanted effects when you require it.

We're using code copied from webworkify and webworkify-webpack. These would take and modify self which is the top level global inside webworkers. To facilitate running the bundled code in different environments, self has been changed to be whichever is available: self, window, or global, in that order. So be aware that one of these must be available in the environment where you eval the resulting bundled source, and that it will be messed with.

Developing

Not really looking to change this much after publication. If you have a way to get rid of the code from webworkify-webpack, that'd be nice. Feels like that stuff should happen at build time, not at runtime. A test to make sure the function exported from the bundled module is somehow accessible would also be nice. Not sure if that's easy to do in a way that works with both browserify and webpack's modulization.

Testing

Do all this in sequence:

  • yarn
  • yarn build-tests
  • yarn test-node - should run the test in four environments, which must succeed

Three in-browser tests. Running each of these commands will expose a page on localhost:8080 or such. Visit and ensure the background turns green. I've found that even with Chrome dev tools set to skip cache, I had to restart Chrome to avoid this stuff being cached.

  • yarn test-browserify-browser
  • yarn test-webpack-prod-browser
  • yarn test-webpack-dev-browser

We need to test with webpack bundled with both production and development mode because require.resolve returns paths and integers in them, respectively.

ヽ(´ー`)ノ

Special thanks to everything for being a mess.