Catalog npm
Back to packageRELEASE DETAILS

0.3.2

npm

6-to-library

The 6to5 formatter for library authors.

6to5amdcommonjses6formatterglobalglobalsharmonymodulemodulesumd
Published9 Jan 2015
Direct dependencies9
Transitive dependenciesNot calculated

README

Current package documentation

Build status  Code climate  David

6-to-library

This library is deprecated. It turns out, that the core 6to5 team was working on the same feature in parallel. Starting from version 2.7.0, 6to5 exports a module directly when only the default is exported.


The 6to5 formatter for library authors.

“Author in ES6, distribute everywhere.” Seamlessly target NPM and browsers.

Installation

> npm install 6-to-library

Usage

1. Write your library

Say your library should uppercase the first letter of a string. You write in ES6 in the file es6/up-first-letter.js:

export default function (string) {
  return string.charAt(0).toLocaleUpperCase() + string.substr(1);
  }
2. Transpile through 6to5's CLI
$ 6to5  --modules 6-to-library  --out-file index.js  es6/up-first-letter.js

Make sure 6to5 is installed globally, or run the local node_modules/6to5/bin/6to5/index.js instead.

 …or programatically:

Add the option modules: "6-to-library" to your require("6to5").transform call. You can also pass this option directly to gulp-6to5 or grunt-6to5.

3. Publish your library

npm publish, git push, or whatever you normally do.

4. Profit!

After your users install your library, they can do whatever they're used to. Without any transformer.

Using CommonJS in node:

var upFirstLetter = require("up-first-letter");
upFirstLetter("ęéë");  // » "Ęéë"

Using globals in the browser:

<script src="bower_components/up-first-letter/index.js"></script>
<script>
  upFirstLetter("ęéë");  // » "Ęéë"
</script>

Using AMD with RequireJS:

require(["up-first-letter/index"], function (upFirstLetter) {
  upFirstLetter.default("ęéë");  // » "Ęéë"
  });

And using the shiny new ES6 modules:

import upFirstLetter from "./up-first-letter/es6/up-first-letter";
upFirstLetter("ęéë");  // » "Ęéë"

Example

You can have a look at the npm module as, built using the 6-to-library formatter.

How does it work

Export/import names

“ECMAScript 6 favors the single/default export style, and gives the sweetest syntax to importing the default.” We do the same. If you only export the default, your library will work as seamlessly as in the examples above.

Otherwise you'll have to reference each export by its specifier:

// original.js
export default 1;
export const two = 2;

// CommonJS
var one = require('original').default;
var two = require('original').two;

// Globals
var one = original.default;
var two = original.two;
Global names

When neither AMD nor CommonJS is supported, properties of the global object will be used as namespaces for every individual file (usually window or global).

The name of the object exported to the global scope is the basename of the module's file, camel-cased.

The name of an object imported from the global scope is the specifier of the default import (only if you're using the syntax import foo from "bar"), or the camel-cased name of the referenced file.

// my-module.js
import $ from "jquery";  // Maps to window.$
import {default as module} from "other-module";  // Maps to window.otherModule
export default "exported value";  // Maps to window.myModule
The inner workings

In your transpiled files you get a variation of the UMD module definition, tailor-cut to support all endpoints seamlessly.

For the input file my-module.js:

import {foo as bar} from "./foo-bar";
import baz from "bar";

export default baz[bar];

You get the following output:

(function (global, factory) {
  var root, exportsName, factoryArguments;

  // AMD
  if (typeof define === "function" && define.amd) {
    define(["exports", "./foo-bar", "bar"], factory);

  } else {

    // CommonJS
    if (module && typeof module.exports !== "undefined") {
      factoryArguments = [module.exports, require("./foo-bar"), require("bar")];
      root = module;
      exportsName = "exports";

    // Globals
    } else {
      factoryArguments = [global.myModule = {}, global.fooBar, global.baz];
      root = global;
      exportsName = "myModule";
    }

    factory.apply(null, factoryArguments);

    // If only the default value is exported, may the good be done.
    if (Object.keys(root[exportsName]).length == 1 && root[exportsName].propertyIsEnumerable("default")) {
      root[exportsName] = root[exportsName]["default"];
    }
  }
})(this, function (exports, _fooBar, _baz) {
  "use strict";

  var _interopRequire = function (obj) {
    return obj && (obj["default"] || obj);
  };

  // Here comes your code. Not much of it in this example.
  var bar = _fooBar.foo;
  var baz = _interopRequire(_baz);

  exports["default"] = baz[bar];
});

(Comments and whitespace added for clarity.)

License

MIT © Tomek Wiszniewski.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 9

Native requirements
9 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
101
Environment
{
  "local_name": "101",
  "alias": false,
  "peer_metadata": null
}
0.8.xruntime
6to5
Environment
{
  "local_name": "6to5",
  "alias": false,
  "peer_metadata": null
}
2.5.xruntime
as
Environment
{
  "local_name": "as",
  "alias": false,
  "peer_metadata": null
}
~0.2.2runtime
basename
Environment
{
  "local_name": "basename",
  "alias": false,
  "peer_metadata": null
}
0.1.xruntime
6-to-library
Environment
{
  "local_name": "6-to-library",
  "alias": false,
  "peer_metadata": null
}
git+https://github.com/tomekwi/6-to-library.jsdevelopment
dirs
Environment
{
  "local_name": "dirs",
  "alias": false,
  "peer_metadata": null
}
0.1.2development
is-directory
Environment
{
  "local_name": "is-directory",
  "alias": false,
  "peer_metadata": null
}
0.2.3development
tape
Environment
{
  "local_name": "tape",
  "alias": false,
  "peer_metadata": null
}
3.0.3development
trimmer
Environment
{
  "local_name": "trimmer",
  "alias": false,
  "peer_metadata": null
}
1.0.0development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "7a4d8625ae37b39ecba6b4b1de92e21b71e7fb71",
  "tarball": "https://registry.npmjs.org/6-to-library/-/6-to-library-0.3.2.tgz",
  "integrity": "sha512-SFqugXmjH9fxtiHaDiX/hYJ6LLEGn2qSVfQaAHZTOQnt+jX55in7EsYIiEN2ykSWff1K8FtfbyQJ5w1626AsbA==",
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEUCIQDBFWCzkEztZ9QeG+Fyk+Z2ucR+3MwXlKSTdtA3ONXjhQIgXaVRNuCRSh0lnyfXbCYBy3fSYuQQ8GRVc+hTz0rIs+E="
    }
  ]
}

Repository

PENDING
tomekwi/6-to-library.js

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 20:43 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:902c20ff3fa9b720176cf15448646828381f697c3fce66d400e8eb0acdfb6e54

Other collected versions

0.3.416 Jan0.3.316 Jan0.3.18 Jan0.3.07 Jan0.3.0-alpha6 Jan0.2.05 JanView all collected versions →