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

ts-bundlify

v0.26.0

Published

TypeScript-to-Javascript build helpers for creating bundles using Gulp, Browserify, Babel, and Node-Sass

Downloads

15

Readme

ts-bundlify

Are you trying to compile JS (or TS, JSX, etc.) files into multiple bundle files with the ability to require() any file from one bundle in another bundle without asynchronous waits?

Having trouble getting browserify to output bundles exactly the way you want?

ts-bundlify addresses three issues:

  1. reduce the amount of code required to setup a browserify build process
  2. easily generate multiple, customizable, bundles from browserify
  3. reduce build dependencies by inlining build utilities/wrappers (see source-maps/ and streams/) and using ^ dependency versions in package.json so that the user of this package has greater control over dependencies.
  • aside: the node.js ecosystem is awesome but can suffer from versioning issues caused by deep dependency trees with many maintainers; it's hard to get all packages updated to new versions. 10 and 20 LOC packages should be inlined, in my opinion/experience, and that's what this package does in several places.

Several default bundle transforms are included: browserify, uglify-js and babel. You can easily plug your own transform in via BundleBuilder.buildBundle(...).transforms().

ts-bundlify can use this projects' own TsBrowserify class or a custom browserify-like implementation along with gulp.js to do the actual bundling with a little magic (see bundlers/browser/BrowserMultiPack.ts).

See each of the bundlers/ sub-directories as well as the test/ folder for examples.

Example of single and multiple out file bundles that rebuild when source file changes are detected (using watchify and browserify) and compiled using babel via BabelBundler:

Setup:

// gulpfile.js
var babelify = require("babelify");
var browserify = require("browserify");
var watchify = require("watchify");
var BundleBuilder = require("ts-bundlify/bundlers/BundleBuilder");
var BabelBundler = require("ts-bundlify/bundlers/babel/BabelBundler");

Build a single bundle with browserify and rebuild with watchify:

// gulpfile.js
// [...]

// define the bundle file paths and options
var bundlePaths: CodePaths = {
    entryFile: "./src/[...]/myApp.js",
    dstDir: "./build/",
    srcPaths: ["node_modules", "./src/[...]"],
    projectRoot: process.cwd()
};

var bundleOpts = BundleBuilder.createOptions<TsBrowserify.Options>({
    rebuild: true,
    debug: false,
    verbose: true,
    typescript: { includeHelpers: true },
    browserPack: browserPack,
    depsSort: depsSort,
    moduleDeps: moduleDeps,
}, bundlePaths, watchify);

// create the bundler implementation
var bundler = new browserify(bundleOpts);

// add a babelify transform step, you can add multiple transforms which are passed to browserify.transform()
var transforms = [
    BabelBundler.createTransformer(babelify)
];

// run the build with options about the source files to compile and the bundle destination file path
BundleBuilder.compileBundle(bundler, bundleOpts, bundlePaths.dstDir, (br) => BundleBuilder.createDefaultBundler(br, { dstFileName: "app-compiled.js" }), transforms, {
    error: (srcName, dstFileName, err) => { ... },
    finishAll: (res) => { ... },
    finishBundle: (fileName) => { ... },
    skipBundle: (fileName) => { ... },
    startBundle: (fileName) => { ... }
});

Here's an example that produces two bundles.

Bundle 1 contains code from all ./src/[...] files.

Bundle 2 contains all the required node_modules files.

// gulpfile.js
// [...]

// create the bundle file paths and options
var bundlePaths: CodePaths = {
    entryFile: "./src/[...]/myApp.js",
    dstDir: "./build/",
    srcPaths: ["node_modules", "./src/[...]"],
    projectRoot: process.cwd()
};

var bundleOpts = BundleBuilder.createOptions<TsBrowserify.Options>({
    rebuild: true,
    debug: false,
    verbose: true,
    typescript: { includeHelpers: true },
    browserPack: browserPack,
    depsSort: depsSort,
    moduleDeps: moduleDeps,
}, bundlePaths, watchify);

// create the bundler implementation
var bundler = new browserify(bundleOpts);

// the magic, insert a custom 'browser-pack' implementation into browserify's pipeline
var packer = BrowserMultiPack.overrideBrowserifyPack(bundler, () => ({
    bundles: [{
        // bundle 1 (destinationPicker() => 0)
        dstFileName: "app-compiled.js",
        prelude: bundleOpts.prelude
    }, {
        // bundle 2 (destinationPicker() => 1)
        dstFileName: "app-modules.js",
        // example of customizing the generated bundle code, in this case to insert typescript
        // helper functions like '__extends' and '__awaiter'
        prelude: bundleOpts.typescriptHelpers + "var require = " + bundleOpts.prelude,
        preludePath: "./_prelude-with-typescript-helpers.js"
    }],
    destinationPicker: (path) => {
        // this is where we pick each file's destination bundle based on file path
        return path.indexOf("node_modules") > -1 ? 1 /* app-modules.js */ : 0 /* app-compiled.js */;
    }
}));

// add a babelify transform step, you can add multiple transforms which are passed to browserify.transform()
var transforms = [
    BabelBundler.createTransformer(babelify)
];

// start the build process with options about the source files to compile
// the 'dstFileName' was configured by the previous call to BrowserMultiPack.overrideBrowserifyPack()
BundleBuilder.compileBundle(bundler, bundleOpts, bundlePaths.dstDir, packer.multiBundleSourceCreator, transforms, {
    error: (srcName, dstFileName, err) => { ... },
    finishAll: (res) => { ... },
    finishBundle: (fileName) => { ... },
    skipBundle: (fileName) => { ... },
    startBundle: (fileName) => { ... }
});