6-to-library 中文文档教程

发布于 10年前 浏览 24 项目主页 更新于 3年前

构建状态代码气候David

6-to-library

此库已弃用。事实证明,核心 6to5 团队正在并行处理相同的功能. 从2.7.0版本开始,6to5在只导出default时直接导出一个模块。


库作者的 6to5 格式化程序。

Installation

> npm install 6-to-library

Usage

1. Write your library

假设您的库应该将字符串的第一个字母大写。 您在文件 es6/up-first-letter.js 中用 ES6 编写:

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

确保全局安装了 6to5,或者运行本地 node_modules/6to5/bin /6to5/index.js 代替。

 …or programatically:

将选项 modules: "6-to-library" 添加到您的 require("6to5").transform 调用中。 您也可以将此选项直接传递给 gulp-6to5grunt-6to5

3. Publish your library

npm publishgit push 或您通常执行的任何操作。

4. Profit!

在您的用户安装您的库后,他们可以做任何他们习惯的事情。 无需任何变压器。

在节点中使用 CommonJS:

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

在浏览器中使用全局变量:

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

将 AMD 与 RequireJS 结合使用:

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

并使用闪亮的新 ES6 模块:

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

Example

您可以查看 npm 模块 as,使用 6-to-library 格式化程序构建。

How does it work

Export/import names

“ECMAScript 6 支持单一/默认导出样式,并为导入默认样式提供最甜美的语法。” 我们这样做相同。 如果您只导出默认值,您的库将像上面的示例一样无缝运行。

否则,您必须通过说明符引用每个导出:

// 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

当既不支持 AMD 也不支持 CommonJS 时,全局对象的属性将用作每个单独文件的命名空间(通常是 windowglobal )。

对象的名称​​导出到 全局范围是模块文件的基本名称,驼峰式。

从全局范围导入 的对象的名称是默认导入的说明符(仅当您使用语法 import foo from "bar" 时),或者引用文件的驼峰命名。

// 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

在您的转译文件中,您会得到 UMD 模块定义的变体,量身定制以无缝支持所有端点。

对于输入文件 my-module.js

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

export default baz[bar];

您会得到以下输出:

(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];
});

(为清楚起见添加了注释和空格。)

License

麻省理工学院 © Tomek Wiszniewski

Build statusCode climateDavid

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.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文