Giter Site home page Giter Site logo

atomify-js's Introduction

Atomic web development - Combining the power of npm, Browserify, Rework and more to build small, fully encapsulated client side modules

Description

Atomify provides a centralized point of access to atomify-js and atomify-css both in code and on the command line. It also offers a server with live-reload and on-the-fly bundling support to make development a breeze.

Examples

You can learn from examples in this repository

API

atomify(opts, cb)

Just like its constituent pieces, atomify is a function that takes an opts object and a callback function.

opts

opts.js - Options to be passed to atomify-js

opts.css - Options to be passed to atomify-css

opts.assets - Used to configure opts.js.assets and opts.css.assets simultaneously (and identically). See links above.

opts.server - Options to be passed to the development server

callback

Just like the callbacks used by atomify-js and atomify-css, but with a third parameter to denote the type of bundle being provided. cb(err, src, type) where type is either 'js' or 'css'.

API Example

// build.js
var atomify = require('atomify')

var jsConfig = './entry.js' // shorthand for {entry: './entry.js'}

var cssConfig = {
  entry: './entry.css'
  , variables: {
    background: '#f00'
  }
}

function cb (err, src, type) {
  if (type === 'js') {
    // do something with JS source bundle
  } else {
    // do something with CSS source bundle
  }
}

atomify({js: jsConfig, css: cssConfig}, cb);

If you don't need to access the bundled source simply provide a file path as the output property in your options and atomify will write the file for you.

atomify.js and atomify.css

As a convenience, you can access atomify-js and atomify-css via properties on the atomify function.

var atomify = require('atomify')

atomify.js === require('atomify-js')
atomify.css === require('atomify-css')

Development server

Atomify includes a development server that provides things like on-the-fly bundling and live reload/browser sync support to make your workflow lightning fast. atomify.server(opts) provides basically the same API as atomify itself, with a few extra options (documented below) added in. The biggest difference, of course, is that instead of writing to a file or calling a callback function, atomify.server responds to http requests. The server can also be configured by including a server property in the opts object when calling atomify(opts).

Just like with atomify, the options passed to atomify.server are expected to have a js and/or css field. When the entry option of either of these is requested, the server will return the results of bundling your code. If you don't want to include the actual path to your entry file in your HTML you can also provide an alias option field. When the alias path is requested the server will bundle using your entry path.

opts.server

You can provide server-specific options in this field.

opts.server.port - Port to listen on. Default: 1337

opts.server.open - If provided, open the URL in your default browser

opts.server.path - The path to open. Appended to http://localhost:port by default. Default: /default, which is a generated HTML file that includes your CSS and JS bundles automatically.

opts.server.url - Full URL to open instead of http://localhost:port/path

opts.server.hostname - Use your machine's local hostname (via Node's os.hostname()) instead of localhost. Ideal for accessing pages from mobile devices on the same LAN. Aliased as h.

opts.server.lr - Enables live-reload support by injecting the live-reload script into any HTML pages served. Supports the following sub-properties.

  • sync: Use BrowserSync. Aliased as s. If provided as an object will be used as the ghostMode option for BrowserSync.
  • port: Port for BrowserSync server to run on. Default: 3000
  • patterns: Globbing patterns to pass to browsersync for watching. Default: ['**.html'] relative to process.cwd() as well as all files in the dependency graph of your JS and CSS bundles.
  • quiet: Suppress file change notifications on the command line. Default: false
  • verbose: Log BrowserSync's debugInfo to the console. Default: false

opts.server.sync - Shortcut for specifying opts.server.lr as {sync: true}.. Aliased as s.

opts.server.st - Options to pass to st static file server, which is what serves all non-entry/alias requests.

opts.server.html - Override the default HTML served at /default. Pass either a filepath or a function.

If you pass a function, you'll be passed one options argument with the urls to the bundled JS and CSS. You should insert those into your HTML, and return a string.

{
  server: {
    html: function html (paths, done){
      // it's important to include the body tags so that the livereload snippet from browsersync can be inserted
      var html = '<body>'
        + '<link rel="stylesheet" href="' + paths.css + '">'
        + '<h1>your current url ' + paths.request + '</h1>'
        + '<script src="' + paths.js + '"></script>'
        + '</body>'

      // you can return an error if something went wrong
      done(null, html)
    }
  }
}

If you pass a filepath, the bundled JS and CSS will automatically be inserted at the bottom of your file. However, you can place the strings __ATOMIFY_CSS__ and __ATOMIFY_JS__ when you want the relevant paths inserted to override this behavior.

opts.server.spaMode - When set to true, the default HTML will always be served. This is useful for single page apps that have a router.

package.json config

In order to support atomify turtles all the way down, you can also specify your configuration in package.json. Simply recreate an opts object structure in JSON with atomify as the key. (Omit output properties if not a top level package.)

{
  "atomify": {
  	 "server": {
  	 	"lr": true
  	 },
    "js": {
      "entry": "index.js",
      "output": "dist/bundle.js"
    },
    "css": {
      "entry": "index.css",
      "plugins": [
        ["rework-plugin-inline", "src/assets"],
        "rework-default-unit"
      ],
      "output": "example/bundle.css"
    }
  }
}

For detailed information on configuring Rework plugins in package.json see the relevant section of the atomify-css README.

CLI

Thanks to subarg, nearly everything you can do in code or JSON, you can do on the command line. JS options can be specified in a --js, -j subarg context and CSS options can be specified in a --css, -c subarg context. Server options can be specified in a --server, -s subarg context.

If you supply the --debug, -d or --output, -o args outside the --js and --css contexts they will apply to both JS and CSS bundles. When providing an --output argument that applies to both, omit the file extension and it will be applied correctly for you.

You can also configure aliases by appending them after a : in your entry field like -j [ entry.js:/bundle.js ].

Get a complete listing of options by running atomify --help

CLI Examples

atomify -j [ entry.js bundle.js ]
atomify -j [ -e entry.js -e other.js -o bundle.js -d -w ]
atomify -j [ entry.js -t funkify ] -c [ entry.css ] -o bundle
atomify -j [ src/entry.js:bundle.js ] -c [ styles/entry.css:bundle.css ] --server [ --open ]

Any top level args (js, css, server) passed on the command line will override the corresponding configuration defined in package.json. Non-conflicting top level items will be merged with package.json configuration.

Install

npm install atomify

atomify-js's People

Contributors

bclinkinbeard avatar joeybaker avatar oscargodson avatar remoe avatar serapath avatar spacepluk avatar techwraith avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

atomify-js's Issues

Upgrade browserify

We should upgrade to 9.0.3 to get this fix - browserify/watchify#143

Which is making ribcage preview quite painful to use right now.

This is also a good time to do a minifyify update for node 0.12 compat.

Remove envify dependency.

envify depends on espriam-fb, and esprima-fb can't be downloaded from npm at this moment.

It would be easier to remove envify than to fix esprima-fb.

upgrade browserify

See #55

Basically, we're stuck on browserify v7 until browserify fixes dedupe problems.

Declare 0.1.0?

Now that we support Handlebars, EJS and Jade templates I think (at least) 0.1.0 is warranted.

Next, on to tests! :)

common and watch options don't play well together

The documentation for opts.common states:

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

[ deletia ]

If piping the response you'll [...] need to listen to the 'entry' event to get the compiled entry files.

However, when combined with opts.watch, neither of the above statements are true: subsequent/watchified invocations of the callback receive only a single bundle, and the 'entry' event is not called.

Error squashing

Note how browserify reports a userful error, but atomify somehow manages to squash it.

lone/notablemind ‹master*› » atomify                                                                                                                                                                                                     8 ↵

/usr/local/lib/node_modules/atomify/node_modules/atomify-js/node_modules/write-to-path/index.js:9
    if (err) throw err
                   ^
SyntaxError: Unexpected token this
clone/notablemind ‹master*› » browserify -t reactify index.js -o a.js                                                                                                                                                                     8 ↵

/Users/jaredly/clone/notablemind/node_modules/treed/lib/local-pl.js:29
    this.find(type, id, function (err, node) this.prefix + {
                                             ^
ParseError: Unexpected token this

Allow atoms to be referenced by name

Just want to get this recorded. Right now, if you have an atom in node_modules and require('my-atom'), it doesn't work. For whatever reason, require('./node_modules/my-atom') doesn't either. An actual relative path like require('../../my-atom') does work.

When it fails it is on Handlebars files, so that makes me think hbsfy (and potentially the other transforms) aren't being used. Wondering if it's related to the fact we have to install the transforms as peers to atomify-js, which seems like it shouldn't be necessary.

make jadeify and brfs optional

Hey,

I'm using inline jade transform (browserify-plain-jade) and there is a conflict with the default jadeify transform. In addition, I have a conflict with brfs transform.

Currently, I have a postinstall hook that updates the file and removes these transforms but it will be great to have it part of the api (I'm using atomify from the command line).

Thanks

factor-bundle cannot be located by Browserify when using common option

When invoking with common: true, Browserify is unable to find factor-bundle (being a dependency of atomify-js, it's amongst its parent's node_modules and therefore cannot be resolved):

Error: Cannot find module 'factor-bundle' from '/path/to/project'
    at Function.module.exports (/path/to/project/node_modules/atomify/node_modules/atomify-js/node_modules/browserify/node_modules/resolve/lib/sync.js:33:11)
    at Browserify.plugin (/path/to/project/node_modules/atomify/node_modules/atomify-js/node_modules/browserify/index.js:291:29)
    at atomifyJs (/path/to/project/node_modules/atomify/node_modules/atomify-js/index.js:224:7)
...

$ find /path/to/project/node_modules -name factor-bundle
/path/to/project/node_modules/atomify/node_modules/atomify-js/node_modules/.bin/factor-bundle
/path/to/project/node_modules/atomify/node_modules/atomify-js/node_modules/factor-bundle

Some syntax errors silently break the build.

There is still something wrong with the way the callbacks are handled. If I
give a callback to atomify and introduce a syntax error like this:

fun ction(){}

The output bundle just contains undefined but the supplied callback is never called.

This seems to be related to reactify failing to parse it, and passing the wrapped callback here.

At that point, the value of cb is this which ends up calling this which disregards the error by calling the original callback with null.

This pull-request seems to fix it for me.

I must say that it's really weird to see reactify errors poping out from an angular app :)

Pass noParse options to Browserify?

Is there anyway using atomify-js to pass noParse options to Browserify?

As a reminder, noParse is an array of files that Browserify should not actually parse for require statements.

I'm currently working around this by using atomify-js' external options field but find it surprising there's no way to pass additional options to the internal browserify.

We should probably support opts.entries

Browserify accepts multiple entry files so we probably should too. In fact, I think I noticed some code in Browserify the other day where only entries would work, and entry would not. Regardless, we should accept opts.entries and convert opts.entry to the same.

Properly resolve entry files

This syntax doesn't work (in atomify-js or atomify-css) but should. js({ entry: './entry.js' }, cb);

Right now you have to use js({ entry: __dirname + '/entry.js' }, cb);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.