Code Monkey home page Code Monkey logo

simplifyify's Introduction

Simplifyify

A simplified Browserify and Watchify CLI

Cross-Platform Compatibility Build Status

Coverage Status Dependencies

npm License Buy us a tree

I constantly find myself using the same Browserify plug-ins and transforms on every project, and I always end up writing pretty much the same Gulp script over and over again. Simplifyify is the solution to that problem.

Features

  • Supports globs, even on Windows
  • Supports Browserify transforms and plugins, such as Babel, CoffeeScript, TypeScript, etc.
  • Built-in support for TypeScript. Enabled automatically if the entry file has a .ts or .tsx extension
  • Has a programmatic API, for use with build tools like Grunt, Gulp, Broccoli, etc.
  • Bundle everything into one big file, or create different bundles for each part of your app (see examples below)
  • Add a banner with version, date, license, etc. via browserify-banner
  • One command creates all the files you need:
    • --bundle bundles your code and nothing else. Useful during development
    • --debug creates external source-maps (.map) using exorcist
    • --minify shrinks your code using uglifyify and Uglify-ES
    • --coverage adds code-coverage instrumentation using istanbul
    • --watch uses watchify for fast differential re-builds as files change

Related Projects

  • globify
    Run browserify and watchify with globs - even on Windows

  • sourcemapify
    Sourcemap plugin for Browserify

  • browserify-banner
    Add a comment (and/or code) to the top of your Browserify bundle

Installation

Install using npm:

npm install @jsdevtools/simplifyify

Usage

Usage: simplifyify [options] <source-files...>

Options:

  -b, --bundle              Create a non-minified bundle (*.js) for each source file.
                            This is the default if no other output option is set.

  -m, --minify              Create a minified bundle (*.min.js) for each source file.

  -c, --coverage            Create a bundle with code-coverage instrumentation
                            (*.coverage.js) for each source file.

  -d, --debug               Create a source map (*.js.map) for each bundle

  -w, --watch               Watch source file(s) and rebuild the bundle(s) automatically

  -o, --outfile <filespec>  The output file or directory.
                            May include a filename pattern (e.g. "*.bundle.js")

  -x, --exclude <filespec>  File path or glob pattern to exclude.
                            Don't forget to put quotes around glob patterns

  -s, --standalone <name>   Export as a named UMD bundle (e.g. "my.cool.module")
                            May include a wildcard (e.g. "MyLib.*")

Arguments:

  <source-files...>         One or more file paths and/or glob patterns.
                            Don't forget to put quotes around glob patterns.
                            A separate Browserify bundle will be created
                            for each source file.

Examples

One entry file --> one output file

In the simplest usage, you can use Simplifyify to bundle all of your code into a single file:

simplifyify src/index.js

src/index.js --> src/index.bundle.js                # <-- unminified code

By default, the output file is at the same path as the entry file, but with a .bundle.js extension. You can customize this using the --outfile argument:

simplifyify src/index.js --outfile dist/my-package.js

src/index.js --> dist/my-package.js                 # <-- unminified code

If you want the bundled code to be minified, then add the --minify flag:

simplifyify src/index.js --outfile dist/my-package.js --minify

src/index.js --> dist/my-package.js                 # <-- minified code

What if you also want a source map (.map) file? Just add the --debug flag.

simplifyify src/index.js --outfile dist/my-package.js --minify --debug

src/index.js --> dist/my-package.js                 # <-- minified code
src/index.js --> dist/my-package.js.map             # <-- source map

One entry file --> multiple output files

Simplifyify can output multiple bundles of your code in a single command. Let's say you want to create an unminified bundle for development (with a source map), a minified bundle for production (with a source map), and a test bundle (with code-coverage instrumentation) for testing:

simplifyify src/index.js --outfile dist/my-package.js --bundle --debug --minify --coverage

src/index.js --> dist/my-package.js                 # <-- unminified code
src/index.js --> dist/my-package.js.map             # <-- source map
src/index.js --> dist/my-package.min.js             # <-- minified code
src/index.js --> dist/my-package.min.js.map         # <-- source map
src/index.js --> dist/my-package.coverage.js        # <-- code-coverage

Multiple entry files --> multiple output files for each

In many applications, it doesn't make sense for all of your code to be bundled into one huge file. Maybe you want to create separate bundles for each folder, or for each component or section of your app. Simplifyify makes this easy. It will create separate bundles for each entry file that you specify. For example:

simplifyify src/store.js src/cart.js src/checkout.js --outfile dist --bundle --minify --debug

src/store.js --> dist/store.js                      # <-- unminified code
src/store.js --> dist/store.js.map                  # <-- source map
src/store.js --> dist/store.min.js                  # <-- minified code
src/store.js --> dist/store.min.js.map              # <-- source map
src/cart.js --> dist/cart.js                        # <-- unminified code
src/cart.js --> dist/cart.js.map                    # <-- source map
src/cart.js --> dist/cart.min.js                    # <-- minified code
src/cart.js --> dist/cart.min.js.map                # <-- source map
src/checkout.js --> dist/checkout.js                # <-- unminified code
src/checkout.js --> dist/checkout.js.map            # <-- source map
src/checkout.js --> dist/checkout.min.js            # <-- minified code
src/checkout.js --> dist/checkout.min.js.map        # <-- source map

Specifying each entry file can quickly become cumbersome though. That's where globs come in. You can specify one or more globs, and Simplifyify will create a separate bundle for each file that matches the glob pattern. For example:

simplifyify "src/*/index.js" --outfile "dist/*.bundle.js" --bundle --minify --debug

src/store/index.js --> dist/store/index.bundle.js               # <-- unminified code
src/store/index.js --> dist/store/index.bundle.js.map           # <-- source map
src/store/index.js --> dist/store/index.bundle.min.js           # <-- minified code
src/store/index.js --> dist/store/index.bundle.min.js.map       # <-- source map
src/cart/index.js --> dist/cart/index.bundle.js                 # <-- unminified code
src/cart/index.js --> dist/cart/index.bundle.js.map             # <-- source map
src/cart/index.js --> dist/cart/index.bundle.min.js             # <-- minified code
src/cart/index.js --> dist/cart/index.bundle.min.js.map         # <-- source map
src/checkout/index.js --> dist/checkout/index.bundle.js         # <-- unminified code
src/checkout/index.js --> dist/checkout/index.bundle.js.map     # <-- source map
src/checkout/index.js --> dist/checkout/index.bundle.min.js     # <-- minified code
src/checkout/index.js --> dist/checkout/index.bundle.min.js.map # <-- source map

TIP: Don't forget to put quotes around your glob patterns! Otherwise, some shells (e.g. Bash) will try to expand them themselves, which may or may not work

Browserify Transforms

Simplifyify honors the browserify.transform field in your package.json file. For example, the following configuration uses Babelify to transform your ES6 code to ES5:

{
  "name": "my-package",
  "version": "1.2.3",
  "browserify": {
    "transform": ["babelify"]
  },
  "devDependencies": {
    "babelify": "^10.0.0"
  }
}

You can also specify options for your transforms. The exact options depend on the transform you're using. Here's an example that configures Babelify and also modifies Simplifyify's default config for uglifyify:

{
  "name": "my-package",
  "version": "1.2.3",
  "browserify": {
    "transform": [
      ["babelify", {
        "presets": ["@babel/preset-env"]
      }],
      ["uglifyify", {
        "mangle": true,
        "compress": {
          "sequences": true,
          "dead_code": true,
          "booleans": true,
          "conditionals": true,
          "if_return": false,
          "drop_console": false,
          "keep_fnames": true
        },
        "output": {
          "comments": false
        }
      }]
    ]
  },
  "devDependencies": {
    "@babel/preset-env": "^7.0.0",
    "babelify": "^10.0.0"
  }
}

Browserify Plugins

The same technique described above for Browserify transforms also works for Browserify plugins. Just add a browserify.plugins field to your package.json file. For example, the following configuration configures TSify to transpile your TypeScript code, and browserify-banner to add a banner comment to the top of your output file(s).

{
  "name": "my-package",
  "version": "1.2.3",
  "browserify": {
    "plugins": [
      ["browserify-banner", {
        "template": "<%= pkg.name %> v<%= pkg.version %>"
      }],
      ["tsify", {
        "target": "esnext",
        "module": "commonjs",
        "moduleResolution": "node",
        "jsx": "react"
      }]
    ]
  },
  "devDependencies": {
    "typescript": "^3.0.3"
  }
}

API

Simplifyify also has a programmatic API, so you can use it directly in your build scripts (Gulp, Grunt, Broccoli, etc.)

Here's the API definition, and here's a full example. Just pass an array of strings (file paths and/or glob patterns) and an options param. You get back an EventEmitter, which fires all the Browserify & Watchify events.

var simplifyify = require("@jsdevtools/simplifyify");

gulp.task("browserify", function(done) {
  simplifyify("lib/*.module.js",
    {
        outfile: "dist/*.bundle.js",
        debug: true,
        minify: true
    })
    .on("end", function() {
        // Finished successfully!
        done();
    })
    .on("error", function(err) {
        // Something went wrong
        done(err);
    });
});

Contributing

Contributions, enhancements, and bug-fixes are welcome! Open an issue on GitHub and submit a pull request.

Building

To build the project locally on your computer:

  1. Clone this repo
    git clone https://github.com/JS-DevTools/simplifyify.git

  2. Install dependencies
    npm install

  3. Run the tests
    npm test

License

Simplifyify is 100% free and open-source, under the MIT license. Use it however you want.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Big Thanks To

Thanks to these awesome companies for their support of Open Source developers ❤

GitHub NPM Coveralls Travis CI SauceLabs

simplifyify's People

Contributors

jamesmessinger avatar paulcpederson avatar taye 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

Watchers

 avatar  avatar  avatar

simplifyify's Issues

Compatibility with Browsersync

First off, thanks for the great tool!

I'm having an issue when using Browsersync watch/reload with simplifyify. Here is the npm script I'm using.

simplifyify js/main.js -m -d -w -o js/main.min.js

Browsersync is watching for changes to js/main.min.js and reloading at the correct time. The problem I'm seeing is that after automatic reload there doesn't seem to be any js/main.min.js file on the page. I took a look in Chrome dev tools sources and there's no such file there at all. However after reloading the page manually via the browser, the JS file is loaded correctly.

Does simplifyify modify the output file multiple times? It seems like Browsersync is picking up on the change too early if that's the case. I even tried adding a reloadDebounce to Browsersync, but the results were inconsistent - the automatic reload only worked correctly sometimes. My guess is that with a high enough value it might fix the problem, but ideally I'd like to keep the delay at 0.

Would appreciate any insight you have here. Thanks!

Add support for factor-bundle plugin

It would be awesome if simplifyify could support the factor bundle plugin. This saves a lot of bytes when We have to include multiple bundles in a same page.

Let's say I have two entry-points: x.js and y.js with both sharing modules in common.

We could run the following command to create the bundles:

simplifyify source/*.js -o dist/*.bundle.js

This will create two bundles: x.bundle.js and y.bundle.js. But both will have the same modules in common in each bundles. This is not good if We need to include both bundles in a same page.

So, we could add the factor-bundle option to resolve this problem:

simplifyify source/*.js -o dist/*.bundle.js --factor-bundle dist/common.bundle.js

This option would extract the common modules between the bundles into the common.bundle.js file.

simplifyify fails to pick browserify-shim out of package.json

This block:

  "main": "js/src/app/bootstrap.js",
  "browserify": {
    "transform": [
      "browserify-shim"
    ]
  },
  "browserify-shim": {
    "./js/vendor/history.js": "history",
    "./js/vendor/bluebird/js/browser/bluebird.js": "bluebird"
  }

Generates a proper JS when using browserify 13.0.0 , but fails when using simplifyify, with message:

Error reading Browserify transforms for js/src/App/bootstrap.js
Cannot find module 'browserify-shim'
```. What am I doing wrong exactly? 

Task completion callback called too many times

I'm using Simplifyify with Gulp like that.

gulp.task("simplifyify", function(done) {
  simplifyify("app/dev/js/*.js",
    {
      outfile: "app/prod/js/*.js",
      debug: true,
      minify: false
    })
    .on("end", function() {
      done();
    })
    .on("error", function(err) {
      done(err);
    });
});

If there is just one file in the source folder, Simplifyify works perfectly. But if there are two or more files in the source folder Simplifyify gives those errors.

'simplifyify' errored after 845 ms
[16:59:35] Error: task completion callback called too many times
    at finish (/path/electron/node_modules/orchestrator/lib/runTask.js:15:10)
    at cb (/path/electron/node_modules/orchestrator/lib/runTask.js:29:3)
    at EventEmitter.<anonymous> (/path/electron/gulpfile.js:62:4)
    at emitOne (events.js:96:13)
    at EventEmitter.emit (events.js:191:7)
    at Browserify.browserify.on (/path/electron/node_modules/simplifyify/lib/write-bundles.js:175:12)
    at emitNone (events.js:91:20)
    at Browserify.emit (events.js:188:7)
    at browserify.postProcessing.then (/path/electron/node_modules/simplifyify/lib/write-bundles.js:228:22)

I couldn't find a helpful answer. Any solution?

source map outputs '\\' but not '/' in the sources path list on Windows

Here is my simple gulpfile, just copying from the given sample of simplifyify README.md:

var gulp        = require("gulp"),
    simplifyify = require("simplifyify");   // https://www.npmjs.com/package/simplifyify

gulp.task("browserify", function(done) {
  simplifyify("app.js",
    {
        outfile: "bundle.js",
        debug: true,
        minify: true
    })
    // .on("end", function() {
    //     // Finished successfully!
    //     done();
    // })
    .on("error", function(err) {
        // Something went wrong
        done(err);
    });
});

And the output of the bundle.js.map as below (my OS is Windows 10 64bit)

  "version": 3,
  "sources": [
    "node_modules\\browserify\\node_modules\\browser-pack\\_prelude.js",
    "app.js",
    "buttons\\button.js",
    "node_modules\\jquery\\dist\\jquery.js"
  ],
  ...

however, if I use browserify app.js --debug | exorcist bundle.js.map > bundle.js, I will get as follow:

  "version": 3,
  "sources": [
    "node_modules/browserify/node_modules/browser-pack/_prelude.js",
    "app.js",
    "buttons/button.js",
    "node_modules/jquery/dist/jquery.js"
  ],
  ...

I think the output path of the source with '' will be not correct when I deploy them to the server. Is it a bug? or where can I have an option to make the path separator to '/', but not system default sep ''?

Thank you.

Dots in directories causes error

If there are any dots (.) inside an outfile directory name the task will fail, for example reverse url notation (au.com.company.dev). Is there an option that can be passed to ignore dots in outfile names or a workaround?

stringify

When using the following

"browserify": {
"transform": [
"babelify",
[
"stringify",
{
"extensions": [
".html"
]
}
]
]
}

my bundle has \n instead of \n, in all my .html files, and any escaped chars have . So there seems to be one too many back slashes throughout my templates.

Any idea or suggestion how I could fix this?

(works as expected with watchify)

Does Simplifyify have a programmatic API (for use in Gulp, Grunt, etc.)?

This question was asked by @nmccready on another issue. I'm answering it here so it's easy for other people to find as well.

The answer is, "Yes!". Simplifyify does have a programmatic API. You aren't limited to using its command-line interface. In fact, the CLI is simply a very thin wrapper around the programmatic API. Here's the API, and here's sample code. Basically, you just pass an array of strings (file paths and/or glob patterns) and an options param. You get back an EventEmitter, which fires all the Browserify & Watchify events.

var simplifyify = require("simplifyify");

gulp.task("browserify", function(done) {
  simplifyify("lib/*.module.js", 
    { 
        outfile: "dist/*.bundle.js", 
        debug: true,
        minify: true 
    })
    .on("end", function() {
        // Finished successfully!
        done();
    })
    .on("error", function(err) {
        // Something went wrong
        done(err);
    });
});

fullPaths: true

Hi,

How can I pass the flags to browserify?
Im trying to user discify and I got this error:

Error: Please recompile this browserify bundle using the --full-paths flag (when using the command-line interface) or with the fullPaths option set to true (when using the JavaScript API).

Errors swallowed in watch mode

Say you have a JavaScript file named gloob.js with an invalid syntax like:

var gloob = hello())

If you run simplifyify gloob.js it correctly reports the error and exits.

If, however, you run simplifyify gloob.js -w. When you save the file and introduce this error, it simply reports that the file is changed. The error is not reported. The output bundle file will be blank, but nothing indicates that there was an error.

I did a bit of poking around and it seems that the error event is not firing in bin/simplifyify.js or in lib/index.js. Perhaps it is being swallowed in write-bundles.js?

I could try writing a failing test for this and submitting a pr if that would help. I'm not super experienced with chai, but I could probably figure it out.

Oh, and also simplifyify is pretty awesome. 👍

Reuse included code's source maps when they exist

When using simplifyify on a project that was already pre compiled with typescript, the source maps that were generated then are not reused.

Instead, a new sourcemap referencing the generated js is created.

Could simplifyify detect the source maps when they exist instead of attempting to recreate a new one ?

Command line transforms

Is it possible to use transforms in the command line with -t coffeeify without having to specify them in package.json?

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.