https://github.com/goto-bus-stop/browser-pack-flat
bundle browserify modules into a single scope, a la rollup
https://github.com/goto-bus-stop/browser-pack-flat
browserify browserify-plugin
Last synced: about 2 months ago
JSON representation
bundle browserify modules into a single scope, a la rollup
- Host: GitHub
- URL: https://github.com/goto-bus-stop/browser-pack-flat
- Owner: goto-bus-stop
- License: mit
- Created: 2017-06-07T11:16:34.000Z (almost 8 years ago)
- Default Branch: default
- Last Pushed: 2024-10-10T06:39:43.000Z (8 months ago)
- Last Synced: 2025-03-28T09:08:50.004Z (about 2 months ago)
- Topics: browserify, browserify-plugin
- Language: JavaScript
- Homepage:
- Size: 351 KB
- Stars: 56
- Watchers: 2
- Forks: 13
- Open Issues: 7
-
Metadata Files:
- Readme: readme.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# browser-pack-flat
Bundle browserify modules into a single scope, a la rollup.
Caveats:
- Modules are executed fully, one after another, instead of inline.
This is a potential difference from Node.js and the default browserify behaviour.
Usually this does not matter, but rarely the order that some things are executed in may change.
- This rewrites `require()` calls to simple variable assignments.
If a module wraps `require()` somehow it probably will not work.
In practice this is quite rare.
- Using `factor-bundle` to split output code into separate files will not work with this plugin.## Install
```bash
npm install --save-dev browser-pack-flat
```## Usage
```bash
browserify /path/to/app.js | browser-unpack | browser-pack-flat
```Or as a plugin:
```bash
browserify /path/to/app.js -p browser-pack-flat
```The plugin replaces the `browser-pack` module used by default by browserify.
With the Node API:
```js
var browserify = require('browserify')
var packFlat = require('browser-pack-flat')browserify({ entries: './src/app.js' })
.plugin(packFlat, { /* options */ })
.bundle()
.pipe(fs.createWriteStream('bundle.js'))
```## What exactly?
browserify uses [browser-pack](https://github.com/browserify/browser-pack) to output a bundle.
browser-pack uses a small `require`-like runtime and wraps modules in functions to get a module loading behaviour that's almost identical to Node.js.
However this resolution can take a few milliseconds across an entire bundle.Input:
```js
var unique = require('uniq');var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];
console.log(unique(data));
```With browser-pack, this bundle would output:
```js
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o