6to5-loader 中文文档教程

发布于 7年前 浏览 25 项目主页 更新于 3年前

6to5-loader

使用 6to5 无需运行时即可将 ES6 代码转换为原始 ES5;

注意:输出问题应在 6to5 问题跟踪器 上报告;

Install

$ npm install --save-dev 6to5-loader

Usage

import Animal from '6to5!./Animal.js';

class Person extends Animal {
  constructor(arg='default') {
    this.eat = 'Happy Meal';
  }
}

export default Person;
var Person = require('6to5!./Person.js').default;
new Person();

或者在 webpack 配置中:

module: {
    loaders: [
        { test: /\.js$/, exclude: /node_modules/, loader: '6to5-loader'}
    ]
}

然后正常导入:

import Person from './Person.js';

Troubleshooting

6to5-loader is slow!

确保你转换的文件尽可能少。 因为你很可能是 匹配 /\.js$/,您可能正在转换 node_modules 文件夹或其他不需要的 来源。 请参阅上面记录的 loaders 配置中的 exclude 选项。

6to5 is injecting a runtime into each file and bloating my code!

6to5 为诸如 _extend 之类的常用函数使用了非常小的运行时。 默认情况下 这将被添加到每个需要它的文件中。

您可以将 6to5 运行时作为一个单独的模块来避免重复。

以下配置禁用 6to5 中的自动每个文件运行时注入,而是 捆绑需要 6to5-runtime 并让所有助手使用它。

注意:您必须运行 npm install 6to5-runtime --save 以将其包含在您的项目中。

loaders: [
  // runtime=true tells 6to5 to expect a runtime, but we still need to bundle it.
  {test: /\.jsx?$/, exclude: /node_modules/, loader: '6to5-loader?experimental&optional=selfContained'}
]

如果您在许多模块中使用 6to5,这可以节省大量开销。

Options

查看 6to5 选项

License

MIT © Luis Couto

6to5-loader

Turn ES6 code into vanilla ES5 with no runtime required using 6to5;

Notes: Issues with the output should be reported on the 6to5 issue tracker;

Install

$ npm install --save-dev 6to5-loader

Usage

import Animal from '6to5!./Animal.js';

class Person extends Animal {
  constructor(arg='default') {
    this.eat = 'Happy Meal';
  }
}

export default Person;
var Person = require('6to5!./Person.js').default;
new Person();

Or within the webpack config:

module: {
    loaders: [
        { test: /\.js$/, exclude: /node_modules/, loader: '6to5-loader'}
    ]
}

and then import normally:

import Person from './Person.js';

Troubleshooting

6to5-loader is slow!

Make sure you are transforming as few files as possible. Because you are probably matching /\.js$/, you might be transforming the node_modules folder or other unwanted source. See the exclude option in the loaders config as documented above.

6to5 is injecting a runtime into each file and bloating my code!

6to5 uses a very small runtime for common functions such as _extend. By default this will be added to every file that requires it.

You can instead require the 6to5 runtime as a separate module to avoid the duplication.

The following configuration disables automatic per-file runtime injection in 6to5, instead bundling requiring 6to5-runtime and making all helpers use it.

NOTE: You must run npm install 6to5-runtime --save to include this in your project.

loaders: [
  // runtime=true tells 6to5 to expect a runtime, but we still need to bundle it.
  {test: /\.jsx?$/, exclude: /node_modules/, loader: '6to5-loader?experimental&optional=selfContained'}
]

This can save significant overhead if you use 6to5 in many modules.

Options

See the 6to5 options

License

MIT © Luis Couto

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