All Projects → goto-bus-stop → split-require

goto-bus-stop / split-require

Licence: other
commonjs-first bundle splitting, for browserify

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to split-require

css-extract
Extract CSS from a browserify bundle
Stars: ✭ 46 (-42.5%)
Mutual labels:  browserify, browserify-plugin
browserify-bower
A browserify plugin, to enable you use bower components just like node modules
Stars: ✭ 20 (-75%)
Mutual labels:  browserify, browserify-plugin
mocaccino.js
Mocha test runner as a Browserify plugin
Stars: ✭ 22 (-72.5%)
Mutual labels:  browserify, browserify-plugin
memo-async-lru
Memoize Node.js style callback-last functions, using an in-memory LRU store
Stars: ✭ 17 (-78.75%)
Mutual labels:  browserify
browserify-persist-fs
Efficient & stable persistent filesystem cache for browserify
Stars: ✭ 16 (-80%)
Mutual labels:  browserify
browserify-test
An easy way to test front-end libraries using browserify and mocha
Stars: ✭ 37 (-53.75%)
Mutual labels:  browserify
typescript-css-modules-demo
A working demo of CSS Modules, using TypeScript, css-modulesify and typed-css-modules
Stars: ✭ 25 (-68.75%)
Mutual labels:  browserify
bubleify
Browserify transform that compiles es2015 to es5 using Bublé
Stars: ✭ 21 (-73.75%)
Mutual labels:  browserify
browserify-cipher
No description or website provided.
Stars: ✭ 15 (-81.25%)
Mutual labels:  browserify
titaniumifier
Get a Titanium™ SDK CommonJS module out of a Node package!
Stars: ✭ 95 (+18.75%)
Mutual labels:  browserify
typedarray-to-buffer
Convert a typed array to a Buffer without a copy.
Stars: ✭ 64 (-20%)
Mutual labels:  browserify
g5-component
Event based Browserify component scaffold. ⚾
Stars: ✭ 15 (-81.25%)
Mutual labels:  browserify
LoanJS
Calculate loan in js (browser/node.js) for equal installments, installments decreasing, the sum of interest, etc.
Stars: ✭ 20 (-75%)
Mutual labels:  browserify
idomizer
An HTML template parser compiling an incremental-dom render factory.
Stars: ✭ 15 (-81.25%)
Mutual labels:  browserify
fastify-bankai
Bankai assets compiler for Fastify
Stars: ✭ 15 (-81.25%)
Mutual labels:  browserify
cypress-browserify-preprocessor
Cypress preprocessor for bundling JavaScript via browserify
Stars: ✭ 23 (-71.25%)
Mutual labels:  browserify
sitio
imperative static site generator powered by React and browserify
Stars: ✭ 49 (-38.75%)
Mutual labels:  browserify
gupack
基于gulp的前端构建工具
Stars: ✭ 13 (-83.75%)
Mutual labels:  browserify
middleman-gulp
A Middleman 4 template using Gulp.js via the external pipeline
Stars: ✭ 42 (-47.5%)
Mutual labels:  browserify
bleh
A web framework with automatic Browserify + Less + Express + Handlebars
Stars: ✭ 47 (-41.25%)
Mutual labels:  browserify

split-require

Bundle splitting for CommonJS and ES modules (dynamic import()) in browserify

Lazy load the parts of your app that are not immediately used, to make the initial load faster.

This module works without a compile step on the server, and in the browser with the browserify plugin.

NOTE: split-require v3+ works with browserify 16 and newer. If you are using an older browserify version and can't upgrade, use split-require v2.

What? - Install - Usage - Plugin CLI - Plugin API - License: MIT

stability travis standard

What?

This plugin takes source files with splitRequire() calls like:

var html = require('choo/html')
var app = require('choo')()
var splitRequire = require('split-require')

app.route('/', mainView)
app.mount('body')

var component
function mainView () {
  return html`<body>
    ${component ? component.render() : html`
      <h1>Loading</h1>
    `}
  </body>`
}

splitRequire('./SomeComponent', function (err, SomeComponent) {
  component = new SomeComponent()
  app.emitter.emit('render')
})

And splits off splitRequire()d files into their own bundles, that will be dynamically loaded at runtime. In this case, a main bundle would be created including choo, choo/html and the above file, and a second bundle would be created for the SomeComponent.js file and its dependencies.

Install

npm install split-require

Usage

Use the split-require function for modules that should be split off into a separate bundle.

var splitRequire = require('split-require')

require('./something') // loaded immediately
splitRequire('./other', function () {}) // loaded async

This works out of the box in Node.js. Add the browserify plugin as described below in order to make it work in the browser, too.

import()

You can use split-require with ES modules import() syntax using the Babel plugin.

browserify app.js \
  -t [ babelify --plugins dynamic-import-split-require ] \
  -p split-require
import('./SomeComponent').then(function (SomeComponent) {
  // Works!
  console.log(SomeComponent)
})

! Note that this transform is not 100% spec compliant. import() is an ES modules feature, not a CommonJS one. In CommonJS with this plugin, the exports object is the value of the module.exports of the imported module, so it may well be a function or some other non-object value. In ES modules, the exports object in .then() will always be an object, with a .default property for default exports and other properties for named exports. You'd never get a function back in .then() in native ES modules.

Browserify Plugin CLI Usage

browserify ./entry -p [ split-require --out /output/directory ]
  > /output/directory/bundle.js

Options

--out

Set the output for dynamic bundles. Use a folder path to place dynamic bundles in that folder. You can also use outpipe syntax: in that case use %f in place of the bundle name. For example:

-p [ split-require --out 'uglifyjs > /output/directory/%f' ]

The default is ./, outputting in the current working directory.

--public

Public path to load dynamic bundles from. Defaults to ./, so dynamic bundle #1 is loaded as ./bundle.1.js.

--sri

Hash algorithm to use for subresource integrity. By default, subresource integrity is not used.

When enabled, the runtime loader will add crossorigin and integrity attributes to dynamically loaded bundles.

One of sha256, sha384, sha512.

Browserify Plugin API Usage

var splitRequire = require('split-require')

browserify('./entry')
  .plugin(splitRequire, {
    dir: '/output/directory'
  })
  .pipe(fs.createWriteStream('/output/directory/bundle.js'))

With factor-bundle

Through the API, split-require can also be used together with factor-bundle. Listen for the factor.pipeline event, and unshift the result of the createStream function to the 'pack' label:

b.plugin(splitRequire, { dir: '/output/directory' })
b.plugin(factorBundle, { /* opts */ })

b.on('factor.pipeline', function (file, pipeline) {
  var stream = splitRequire.createStream(b, {
    dir: '/output/directory'
  })
  pipeline.get('pack').unshift(stream)
})

Note that you must pass the options to the plugin and the stream. Other plugins that generate multiple outputs may need a similar treatment.

Options

dir

Set the folder to output dynamic bundles to. Defaults to ./. This is only necessary if the output() option is not used.

filename(entry)

Function that generates a name for a dynamic bundle. Receives the entry point row as the only parameter. The default is:

function filename (entry) {
  return 'bundle.' + entry.index + '.js'
}

output(bundleName)

Function that should return a stream. The dynamic bundle will be written to the stream. bundleName is the generated name for the dynamic bundle. At runtime, the main bundle will attempt to use this name to load the bundle, so it should be publically accessible under this name.

The bundleName can be changed by emitting a name event on the returned stream before the stream finishes. This is useful to generate a bundle name based on a hash of the file contents, for example.

var fs = require('fs')
var crypto = require('crypto')
var to = require('flush-write-stream')
b.plugin(splitRequire, {
  output: function (bundleName) {
    var stream = fs.createWriteStream('/tmp/' + bundleName)
    var hash = crypto.createHash('sha1')
    return to(onwrite, onend)

    function onwrite (chunk, enc, cb) {
      hash.update(chunk)
      stream.write(chunk, cb)
    }
    function onend (cb) {
      stream.end()
      var name = hash.digest('hex').slice(0, 10) + '.js'
      this.emit('name', name)
      fs.rename('/tmp/' + bundleName, './' + name, cb)
    }
  }
})

public

Public path to load dynamic bundles from. Defaults to ./, so dynamic bundle #1 is loaded as ./bundle.1.js.

sri

Hash algorithm to use for subresource integrity. By default, subresource integrity is not used.

When enabled, the runtime loader will add crossorigin and integrity attributes to dynamically loaded bundles.

One of sha256, sha384, sha512.

Events

b.on('split.pipeline', function (pipeline, entry, basename) {})

split-require emits an event on the browserify instance for each pipeline it creates.

pipeline is a labeled-stream-splicer with labels:

entry is the browserify row object for the entry point of the dynamic bundle. basename is the name of the dynamic bundle file.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].