Giter VIP home page Giter VIP logo

umd's Introduction

umd

Universal Module Definition for use in automated build systems

  • simple synchronous wrapping of a string
  • return style module support
  • CommonJS support
  • prevents internal UMDs from conflicting

Build Status Dependency Status NPM version

Source Format

In order for the UMD wrapper to work the source code for your module should return the export, e.g.

function method() {
  //code
}
method.helper = function () {
  //code
}
return method;

For examples, see the examples directory. The CommonJS module format is also supported by passing true as the second argument to methods.

API

options:

  • commonJS (default: false) - If commonJS is true then it will accept CommonJS source instead of source code which returns the module.

umd(name, source, [options])

The name should the the name of the module. Use a string like name, all lower case with hyphens instead of spaces.

If source should be a string, that is wrapped in umd and returned as a string.

umd.prelude(module, [options])

return the text which will be inserted before a module.

umd.postlude(module, [options])

return the text which will be inserted after a module.

Command Line

Usage: umd <name> <source> <destination> [options]

Pipe Usage: umd <name> [options] < source > destination

Options:

 -h --help     Display usage information
 -c --commonJS Use CommonJS module format

You can easilly pipe unix commands together like:

cat my-module.js | umd my-module | uglify-js > my-module.umd.min.js

Name Casing and Characters

The name passed to umd will be converted to camel case (my-library becomes myLibrary) and may only contain:

  • alphanumeric characters
  • $
  • _

The name may not begin with a number. Invalid characters will be stripped.

License

MIT

umd's People

Contributors

bendrucker avatar domenic avatar forbeslindesay avatar greenkeeper[bot] avatar jazzzz avatar jrburke avatar ralphtheninja avatar robotlolita avatar roccivic avatar zaggino avatar zertosh 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

umd's Issues

Add support for Java 8 Nashorn Engine

The template.js fails to evaluate on Java 8 Nashorn. Nashorn doesn't support any module system like AMD out of the box and there's neither global nor window. The global context is available via this.

Please add another fallback to this in template.js.

This refers to: facebook/react#3037

umd-packaged modules don't work with packaging systems that detect the `require` calls and assume they are global

UMD-packaged modules are designed to work in any environment. Unfortunately they don't work in very many client-side packaging systems (I tried browserify and requirejs, webpack works).

Here's the scenario: ./x.js requires ./y.js and we do browserify --standalone x x.js > ./temp/a.js. If I require a from Node or drop it in a browser everything works. Great.

Now I create ./temp/b.js which requires ./a.js and I do browserify b.js from ./temp/. It throws with Error: Cannot find module './y' because the word require() is used within the source still.

This is common behavior in many of the popular browser packaging systems I mentioned. They just look for calls to a function named require().

My solution to this is to syntax transform the require literal when used as a function argument or function call to muffin. Would a pull request that does something like this (with a more mangled identifier) be accepted? It would require esprima.

streams??

I was going to use this for component but the odd stream usage makes it kinda unusable :(

Casing changes

As mentioned in #23, I think there are some changes that should be made to casing:

  • Use a module dependency for camel casing
  • Throw if the name begins with an invalid leading character that's valid later in an identifier (e.g. a number) (instead of just stripping it)
  • Throw if there are any invalid characters (instead of stripping them)

This could all get pulled out to a module as well.

Then there's the trickier issue of having automatic behavior at all. As mentioned in browserify/browserify#1210 (comment), it's potentially unexpected. But there are presumably a whole lot more people who ultimately want the behavior that's present now (camel casing) and would have to implement the casing on their own if it were removed as part of a major version.

In order to make it customizable, browser-pack would have to be updated to allow an object for opts.standalone and Browserify's docs would need updating.

UMD should has ability to pass external dependencies (at least for AMD)

Currently UMD uses just a static prelude part which looks like

var templateSTR = "(function(f){if(typeof exports===\"object\"&&typeof module!==\"undefined\"){module.exports=f()}else if(typeof define===\"function\"&&define.amd){define([],f)}else{var g;if(typeof window!==\"undefined\"){g=window}else if(typeof global!==\"undefined\"){g=global}else if(typeof self!==\"undefined\"){g=self}else{g=this}defineNamespace()}})(function(){source()});";

as you see amd part in this template looks like define([],f)

But some times modules you are producing depends on other modules,
and if you going to use your modules in AMD environment it should be able to list those dependencies in a define part of a umd template:

define(['dep1', 'dep2'], f);

There is an issue opened for browserify: browserify/browserify#1225
where my last two comments describes in depth why these dependencies should be listed in a define part of umd template.

I think there should be additional options for both cmd and runtime usage like:

umd(name, source, {deps: ['dep1', 'dep2']})

so these dependencies will become a part of a prelude header.

Request to support `"use strict";`

Hey Forbes,

Currently, umd places var define,module,exports; at the top of the generated factory function, making it impossible to 'use strict'; for wrapped code.

Would you be open to adding support for this in one way or another? One simple solution could be to add a useStrict property to the options object. I'd be happy to work on a pull request for this, if it's a feature you'd like to add.

functions limited to alphanumeric

As noted in this stackexhange post, umd doesn't allow non-alphanumeric functions, which means this give an error: browserify --standalone $ foo.js.

Using the dollar sign would be useful in places where you want to mimic a certain popular library's API. I'll admit that, running browserify -s replaceme foo.js | sed -e 's/replaceme/\$/g isn't that hard, but it seems brittle.

Dependencies

What's the recommended pattern for declaring dependencies?

I'm bundling a component using browserify with the externals option. I would like to use the standalone option as well, but the umd template doesn't support dependencies.

In the meantime I'm wrapping browserify's output using grunt-umd, but would prefer to have this be something the "standalone" option handles.

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of mocha is breaking the build 🚨

The devDependency mocha was updated from 6.0.2 to 6.1.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

mocha is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v6.1.0

6.1.0 / 2019-04-07

🔒 Security Fixes

  • #3845: Update dependency "js-yaml" to v3.13.0 per npm security advisory (@plroebuck)

🎉 Enhancements

  • #3766: Make reporter constructor support optional options parameter (@plroebuck)
  • #3760: Add support for config files with .jsonc extension (@sstephant)

📠 Deprecations

These are soft-deprecated, and will emit a warning upon use. Support will be removed in (likely) the next major version of Mocha:

🐛 Fixes

  • #3829: Use cwd-relative pathname to load config file (@plroebuck)
  • #3745: Fix async calls of this.skip() in "before each" hooks (@juergba)
  • #3669: Enable --allow-uncaught for uncaught exceptions thrown inside hooks (@givanse)

and some regressions:

📖 Documentation

🔩 Other

  • #3830: Replace dependency "findup-sync" with "find-up" for faster startup (@cspotcode)
  • #3799: Update devDependencies to fix many npm vulnerabilities (@XhmikosR)
Commits

The new version differs by 28 commits.

  • f4fc95a Release v6.1.0
  • bd29dbd update CHANGELOG for v6.1.0 [ci skip]
  • aaf2b72 Use cwd-relative pathname to load config file (#3829)
  • b079d24 upgrade deps as per npm audit fix; closes #3854
  • e87c689 Deprecate this.skip() for "after all" hooks (#3719)
  • 81cfa90 Copy Suite property "root" when cloning; closes #3847 (#3848)
  • 8aa2fc4 Fix issue 3714, hide pound icon showing on hover header on docs page (#3850)
  • 586bf78 Update JS-YAML to address security issue (#3845)
  • d1024a3 Update doc examples "tests.html" (#3811)
  • 1d570e0 Delete "/docs/example/chai.js"
  • ade8b90 runner.js: "self.test" undefined in Browser (#3835)
  • 0098147 Replace findup-sync with find-up for faster startup (#3830)
  • d5ba121 Remove "package" flag from sample config file because it can only be passes as CLI arg (#3793)
  • a3089ad update package-lock
  • 75430ec Upgrade yargs-parser dependency to avoid loading 2 copies of yargs

There are 28 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of uglify-js is breaking the build 🚨

The devDependency uglify-js was updated from 3.7.4 to 3.7.5.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

uglify-js is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v3.7.5

 

Commits

The new version differs by 6 commits.

  • 5beb7e4 v3.7.5
  • 46caaa8 enhance collapse_vars (#3680)
  • 5d25825 introduce --output-opts CLI option (#3678)
  • 14c3573 fix corner case in unsafe_math (#3677)
  • f5ceff6 fix corner case in unused (#3674)
  • 4d6771b fix corner case in collapse_vars (#3672)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

output amd with requirejs

define.amd){define([],f)}
how it support amd the right way maybe
define.amd){define('defineNamespace',[],f)}

camel case is weird

I would expect this to define window.ABCDEFG to 555, but instead it defines window.aBCDEFG which is weird and surprising in a bad way.

$ browserify --standalone ABCDEFG <(echo 'module.exports = 555')
(function(e){if("function"==typeof bootstrap)bootstrap("abcdefg",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeABCDEFG=e}else"undefined"!=typeof window?window.aBCDEFG=e():global.aBCDEFG=e()})(function(){var define,ses,bootstrap,module,exports;
return (function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0](function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({1:[function(require,module,exports){
module.exports = 555

},{}]},{},[1])(1)
});

Why uglify the template?

Is there a reason umd auto uglifies the template, instead of relying on the consumer to do so? I was thinking of submitting a patch that helps browserify produce more readable output, but wanted to know if there was a use case I was missing first.

EDIT: Sorry about the prelude mention, I completely got this confused with browser-pack. But I think my primary question is still valid as to why uglify is being applied within, instead of at a top level.

simplify CLI arguments

The CLI is great but at least two of the options provided could be derived from package.json

name can be taken from the package's name, and source would be from the package's main. This would leave only specifying the destination (or still piping to STDOUT)

I would love to be able to say just umd | tee dist/module.js | uglifyjs > dist/module.min.js for example.

Why is it necessary for umd to minify code?

Uglifyjs minifaction causes problems when generating trying to generate standalone UMD modules with browserify.

Is it minification really necessary? Shouldn't it be optional at the very least?

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.