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

atomify-js

v4.7.3

Published

Atomic JavaScript - Reusable front-end modules using Browserify, transforms, and templates

Downloads

336

Readme

atomify-js

Build Status

Dependencies Up-to-date

Atomic JavaScript - Reusable front-end modules using Browserify, transforms, and templates

Description

atomify-js is a tool that makes it easy to create small, atomic modules of client-side code. It provides support for several templating libraries and Browserify transforms out of the box while allowing for ample customization. It also provides several convenience features to make working with Browserify even easier.

Table of Contents generated with DocToc

Install

npm install atomify-js

Default transforms and template support

API

In its default form, atomify-js takes an opts object and a callback function.

Options

opts.entry or opts.entries

Path or paths that will be provided to Browserify as entry points. For convenience, you may simply provide a string or array of strings in place of the opts object, which will be treated as the entry or entries property, respectively. Paths will be resolved relative to process.cwd().

opts.common

If you have multiple entries, you can set this to true to enable factor-bundle, which will take the common dependencies of all entries and move them to a common bundle. If you use this option, the api changes a little bit.

If using a callback, you're passed an object that with keys of each entry file and values of the compiled JS file. You'll also have a common key.

var js = require('atomify-js')
  , path = require('path')

js({
  entries [path.join(__dirname, 'entry1.js'), path.join(__dirname, 'entry2.js')]
  , common: true
  }, function(err, entries){
    console.log(entries['entry1'].toString())
    console.log(entries['entry2'].toString())
    console.log(entries['common'].toString())
  })

If piping the response, you'll be pipped the common bundle. You'll need to listen to the 'entry' event to get the compiled entry files.

var js = require('atomify-js')
  , path = require('path')

js({
  entries [path.join(__dirname, 'entry1.js'), path.join(__dirname, 'entry2.js')]
  , common: true
}).pipe(commonStream)

js.emitter.on('entry', function(content, bundleName){
  console.log(bundleName, content.toString())
})

opts.output

If you simply want your bundle written out to a file, provide the path in this property. Path will be resolved relative to process.cwd().

opts.debug

Passed to Browserify to generate source maps if true. Also provides additional CLI output, if applicable.

opts.minify

If true, minifies source code and sets debug to true. If object, passed as options to minifyify and sets debug to true. If false, no minification.

opts.watch

If true, watchify will be used to create a file watcher and speed up subsequent builds.

opts.cache

If truthy, will use browserify-incremental to cache the result of a build. This can give you dramatically faster build times if you're not using opts.watch.

If opts.cache is a string, it will be used as the file path to save the cache file to.

NOTE: opts.cache and opts.watch are incompatible. An error will be thrown if you set both.

opts.transforms

Provide your own transforms that will be added to the defaults listed above.

opts.defaultTransforms

If set to false, the default list of transforms will not be run. This is useful if you'd like to customize a default transform, or simply don't need them to run.

opts.globalTransforms

Browserify global transforms that will process all files used in your application, including those within node_modules. You should take great care when defining global transforms as noted in the Browserify documentation.

opts.require

Array of files to pass to Browserify's require method.

opts.external

Array of files to pass to Browserify's external method.

opts.assets

One of the challenges with writing truly modular code is that your templates often refer to assets that need to be accessible from your final bundle. Configuring this option solves that problem by detecting asset paths in your templates, copying them to a new location, and rewriting the references to them to use the new paths. Paths in the src attribute of img, video, and audio tags will be processed according to your configuration.

The processing is configured using two sub-properties of opts.assets: dest and prefix. The dest field determines the location files will be copied to, relative to process.cwd(), and prefix specifies what will be prepended to the new file names in the rewritten src attributes. The filenames are generated from a hash of the assets themselves, so you don't have to worry about name collisions.

To demonstrate, see the following example.

// config
{
  entry: './entry.js',
  output: 'dist/bundle.js',
  ...
  assets: {
    dest: 'dist/assets',
    prefix: 'assets/'
  }
}
<img src="some/path/to/logo.png">

becomes

<img src="assets/4314d804f81c8510.png">

and a copy of logo.png will now exist at dist/assets/4314d804f81c8510.png

You may also provide any valid browserify bundle options in the opts object as well, and they will be passed directly to Browserify.

Callback

Standard Browserify bundle callback with cb(err, src) signature. Not called if opts.output is specifed. If callback is provided as a string rather than function reference it will be used as the opts.output file path.

If opts.watch is truthy, the callback will be called on every file change

Events

The emitter property will emit events.

var js = require('atomify-js')

js('./entry.js', './bundle.js')

js.emitter.on('browserify', function setBrowserifyInstance(b){
  console.log(b)
})

browserify (<browserifyInstance> bundle)

When the browserify instance is created, emits it. This might be useful to you, but it's really just for testing.

watchify (<watchifyInstance> bundle)

When the watchify instance is created, emits it. This might be useful to you, but it's really just for testing.

changed (<String> id)

Emitted when watchify detects a file change. Passes the bundle id that changed.

bundle (<Number> time)

If opts.watch is truthy, this is emitted when the bundle changes. time is the re-compilation time.

package (<Object> package)

Proxies the package event from browserify.

entry (<Buffer> entryBuffer, <String> entryName)

If opts.common is truthy, this will be emitted when an entry file is bundled. entryBundle is the contents of the entry file in buffer form. entryName is the file name of the original entry file.

Examples

// entry.js
var thing = require('thing')
  , template = require('./template.html.hbs')

template({param: 'param'})
// build.js
var js = require('atomify-js')

var opts = {
  entry: './entry.js'
, debug: true // default: `false`
}

js(opts, function (err, src) {
  // do something with the src
})

OR

var js = require('atomify-js')

js('./entry.js', './bundle.js')

Developing

Tests can be run with npm test. You can run the tests on every file change with npm run tdd.