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

unsweeten

v0.1.1

Published

Transform require.js sugar syntax into the standard syntax.

Downloads

64

Readme

unsweeten Build Status NPM version Dependency Status

Transform require.js sugar syntax into the standard syntax.

Install via npm:

npm install unsweeten --save-dev

What it does

This module takes the require.js sugar syntax:

define(['require', 'dependency1', 'dependency2'], function(require) {
    var dependency1 = require('dependency1'),
        dependency2 = require('dependency2');

    return function() {};
});

And converts it back into the standard syntax:

define(['dependency1', 'dependency2'], function(dependency1, dependency2) {
    return function() {};
});

It will also work for dependencies that you are just loading but are not assigned to any variables, such as loading jQuery and a plugin:

define(['require', 'jquery', 'somejqueryplugin'], function(require) {
    var $ = require('jquery');
    require('somejqueryplugin');

    return function() {};
});

Becomes:

define(['jquery', 'somejqueryplugin'], function($) {
    return function() {};
});

For large projects using AMD, using unsweeten will help you cut down a significant amount of loader code that just isn't necessary.

Example

The best way to use this module is as a part of your r.js build step. Using the onBuildWrite function, you can apply extra transformations to your source code before it is minified. Below is an example configuration using gulp as a task runner:

var path      = require('path'),
    gulp      = require('gulp'),
    gutil     = require('gulp-util'),
    requirejs = require('requirejs'),
    unsweeten = require('unsweeten');

gulp.task('build', function(cb) {
    requirejs.optimize({
        name: 'main',
        baseUrl: 'lib',
        mainConfigFile: 'lib/config.js',
        out: 'web/js/master.min.js',
        optimizeAllPluginResources: true,
        stubModules: ['text'],
        onBuildWrite: function(name, filePath, contents) {
            if (path.extname(filePath).indexOf('js') > -1) {
                return unsweeten(contents);
            }
            return contents;
        }
    }, function() {
        gutil.log('[' + gutil.colors.green('require.js') + '] Build complete.');
        cb();
    }, cb);
});

Limitations

unsweeten destructively transforms your JavaScript source code, changing its footprint to make it smaller. For this reason, it's recommended to use it on a copy of your file, in a build step, so that your original code is left untouched. Remember that the sugar syntax is recommended for development use to help you visualise the file's dependencies.

Some files may fail this transform. In the test directory are examples of code that will be transformed; notably absent are those files that have more than one define call in them. When using this library on your own code you will most likely find that all of your development files have a single call to define, as in my case. However, some vendor code may package up several of these calls into a single file. For this reason it is recommended to only use unsweeten on your own code. Bear in mind that any file not run through this transform will continue to work as normal.

If you find an example of breakage using unsweeten then please open an issue with the source code, output, and the expected output. If you can, a pull request is even better!

Contributing

Pull requests are welcome. If you add functionality, then please add unit tests to cover it.

License

MIT © Ben Briggs