Giter Club home page Giter Club logo

deamdify's Introduction

deAMDify

Build Status David DM

This module is a browserify plugin that will transform AMD modules into Node.js-style modules so that they can be included in browser-ified bundles.

With this transform in place, Node and AMD modules can be freely intermixed, and the resulting bundle can be used without the need for a separate loader such as RequireJS.

For when you're stuck and need help:

Join the chat at https://gitter.im/jaredhanson/deamdify

Install

$ npm install deamdify

Usage

Command Line

Bundle up all required modules, including AMD modules, into a single file using browserify with the deamdify transform.

browserify -t deamdify main.js -o bundle.js

API

var browserify = require('browserify');
var fs = require('fs');

var b = browserify('main.js');
b.transform('deamdify');

b.bundle().pipe(fs.createWriteStream('bundle.js'));

package.json

For packages that are written as AMD modules, add a browserify transform field to package.json and browserify will apply the transform to all modules in the package as it builds a bundle.

{
  "name": "anchor",
  "main": "main",
  "browserify": {
    "transform": "deamdify"
  }
}

Tests

$ npm install
# fast run
$ npm run test-main
# test all browserify major versions
$ npm test

Credits

License

The MIT License

Copyright (c) 2015 Jared Hanson <http://jaredhanson.net/>

deamdify's People

Contributors

andreas avatar bendrucker avatar fgnass avatar gitter-badger avatar jaredhanson avatar kirbysayshi avatar lrowe avatar patrick-steele-idem avatar phillipgreenii avatar tbranyen avatar tootallnate avatar xjamundx 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

deamdify's Issues

Errors not emitted properly

Currently, when deamdify encounters a syntax error, it throws a regular JavaScript error. That makes it very hard to catch it in the Browserify stream.

Instead, it should emit an "error" event on the stream, with the original error as an argument โ€“ that is how, for example, es6ify does it.

Is there any specific reason why it's done the way it is currently, or would you be willing to accept a Pull Request?

Should Not Crash

Bad client-side JS probably shouldn't crash your web-server.

In my case this particular line caused the following errors:

console.log("testing out a dynamic require for', '../view/' + path + '.js');
Tue, 19 Aug 2014 13:59:39 GMT uncaughtException Line 99: Unexpected token ILLEGAL
Error: Line 99: Unexpected token ILLEGAL
    at throwError (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:1836:21)
    at scanStringLiteral (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:1009:13)
    at advance (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:1297:20)
    at lex (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:1360:78)
    at expect (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:1896:21)
    at parseArguments (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:2209:9)
    at parseLeftHandSideExpressionAllowCall (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:2283:24)
    at parsePostfixExpression (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:2325:16)
    at parseUnaryExpression (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:2352:20)
    at parseBinaryExpression (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:2473:16)
    at parseConditionalExpression (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:2533:16)
    at parseAssignmentExpression (/Users/jamuferguson/dev/paypal/8ballUI/node_modules/deamdify/node_modules/esprima/esprima.js:2559:23)

Why removing 0s after decimal points?

WebGL programs with GLSL (e.g., gl_PointSize = 1.0) converted from javascript function strings are broken because of deamdify is remove then 0s after decimal points. Is any way to config this? Thanks.

Support modules that are defined put not passed into the function

Sometimes we do things like this in our node code:

require('some-thing');

Similarly in AMD-land we might want to do something like this on occasion:

define(["thing", "some-thing"] function(thing) {
    // we need to know that some-thing exists, but it doesn't need a variable
});

Currently deamdify crashes in this scenario throwing a very obscure error:

Error: Parsing file ... Line 1: Unexpected token =

support the concept of baseUrl

Currently a lot of AMD implementations depend on the require.js baseUrl config property to resolve modules properly. This implementation does not seem to support that concept, so a lot of work must be done to modify existing modules to follow a node-like directory structure.

I'd love it if this module automatically took care of that so we don't need to resort to any sort of mass file modification as we start to bring common.js modules into our codebase.

New npm package

Hey, can you please provide newer npm package than 0.1.1 which is already 2 years old?

Thanks a lot
Michal

deamdified module get changed

I'm using deamdify transform inside my browserify task.
I need to deamdify https://github.com/guardian/scribe since it's based on amd.

But the resulting Scribe module I get is this function

Scribe function isNative(value) {
    return typeof value == 'function' && reNative.test(value);
}

support dependencies declared with expressions

In some usages of dojo, the modules are cleverly defined by specifying a string and splitting on it:

define('my module', 'dep1 dep2 dep3'.split(' '), function(dep1, dep2, dep3) {
  //i'm clever
});

This scenario isn't caught by deamdify, but it could be

Parsing exceptions with JSON format

Hello!

I were using this module in one of my projects.

I'm storing global configuration in config.json file and I require it from my scripts like this: var config = require('config.json'). When I do so, deamdify throws exceptions at me.

/node_modules/deamdify/node_modules/esprima/esprima.js:3734
            throw e;
                  ^
Error: Line 2: Unexpected token :
    at throwError (/node_modules/deamdify/node_modules/esprima/esprima.js:1846:21)
    at throwUnexpected (/node_modules/deamdify/node_modules/esprima/esprima.js:1904:9)
    at consumeSemicolon (/node_modules/deamdify/node_modules/esprima/esprima.js:1983:13)
    at parseStatement (/node_modules/deamdify/node_modules/esprima/esprima.js:3281:9)
...

consider getting a new maintainer

Are you willing to let someone else assist in maintaining deAMDify? There are a lot of unmerged PRs and comments without feedback. Let us know how we can help!

Variable `module` inside module breaks deamdify

Let's say I have two files

a.js

define(function () {
    var module = {
        t: function () {
            console.log('a');
        }
    };
    return module;
});

b.js

define(['./a'], function (a) {
    a.t();
});

Then browserify -t deamdify b.js -o bundle.js yields

...

var module = {
        t: function () {
            console.log('a');
        }
    };
module.exports = module;

...

Importing that in node throws an error:

$ node
> require('./bundle')
TypeError: Object #<Object> has no method 't'
    at Object../a (/private/tmp/amd/bundle.js:10:3)

Support for UMD

I tried building jQuery UI, which uses UMD without CJS, with browserify and this module. While its likely that I've missed something, it doesn't seem to work, and the "// TODO: Implement support for amdWeb UMD modules." comment implies that it can't work.

My test is here: https://github.com/jzaefferer/browserify-jquery-ui

Running npm run build will build a bundle, but it doesn't make a difference if deamdify is used or not, it won't do anything.

What it should be doing is to find the if ( typeof define === "function" && define.amd ) { check and resolve the define call inside of that.

Support define(moduleName, callback)

There is one more parameter set for the define function that seems to be unsupported at the moment.

An AMD module can be defined by passing its name (string) and the definition callback to the define function. This simply defines a named object without any dependencies.

It'd be great if you could support that as well or mention that it isn't supported.

Let me know if you don't have the time to do it but would like to have that feature. Maybe I could implement it myself and make a pull request for it.

And a sample code:

define('jidejs/base/Util', function() {
    return { foo: 'bar' };
});

When deamdifyied this will result in a module where the define function is still invoked.

including global plugin imgAreaSelect breaks deamdify-ed modules which requires it

If a module contains the line:

require('...path.../imgareaselect/jquery.imgareaselect.js');

which is this file:
https://github.com/odyniec/imgareaselect/blob/new/jquery.imgareaselect.js

Then the transformed bundle will have the module's code messed up. Comments and new lines are removed, and even some brackets are removed.

For example:

config.screenshotareaselect.setOptions({
            x1: x - (width / 2),
            x2: x + (width / 2),
            y1: y - (height / 2),
            y2: y + (height / 2),

becames

config.screenshotareaselect.setOptions({
            x1: x - width / 2,
            x2: x + width / 2,
            y1: y - height / 2,
            y2: y + height / 2,

resolving dependencies

Here is what I'm trying to do:

var jquery = require('jquery');
var jqm = require('jquery-mobile');
browserify -t debowerify -t deamdify main.js -o bundle.js

Here is output I get

Error: module "jquery" not found from "~/jqm/bower_components/jquery-mobile/js/jquery.mobile.animationComplete.js"

Basically, jqm depends on jquery which is defined in requirejs
How can use it from CJS module?
Or what is the way to fix it?

Ordering with template transforms like hbsfy are important

Using both https://github.com/epeli/node-hbsfy and deamdify, hbsfy has to be included first as a transformation else deamdify will error on the loaded hbs file with the error "Fatal error: Line 1: Unexpected token <", which is pretty unhelpful when debugging.

Not sure if this is a case that you actually want to try and handle, or just treat as a known issue. This could also be a problem with other template transforms for browserify.

support AMD Plugins

AMD supports plugins (requires containing a bang (!)) that changes the semantics of require. This is used by different libraries, but the one troubling me is dojo.

Skip over dynamic require calls

If using a dynamic require such as:

var template = require(loc);

Deamdify converts to:

var template = module.exports = loc;

Which is definitely not correct.

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.