@a-la/export 中文文档教程

发布于 6年前 浏览 26 项目主页 更新于 3年前

@a-la/export

npm version

@a-la/exportÀlamodeESM 模块的 export 语句转换为 CJS 模块在 Node.js 中的 module.exports 语句。

yarn add @a-la/export

Table Of Contents

API

ÀLaImport 是默认导出和包含一系列规则的数组可替换

import ÀLaExport from '@a-la/import'

Output Example

规则集改变 export 到 module.exports 语句。 module.exports 将被分配 default 导出的值(如果存在),并且所有命名的导出都将被分配给 module.exports那。

Input (view source)Output
/**
 * Example 1.
 */
export const example1 = async () => {
  console.log('named export 1')
}

/**
 * Example 2.
 */
export function example2() {
  console.log('named export 2')
}

/**
 * Example 3.
 */
function example3() {
  console.log('named export 3')
}

/**
 * Example 4.
 */
const example4 = async () => {
  console.log('named export 4')
}

export { example3, example4 as alias4 }

/**
 * Default Class Example.
 */
export default class Example {
  /**
   * A constructor for the example.
   * @constructor
   */
  constructor() {
    console.log('default export')
  }
}

/**
 * Example 1.
 */
       const example1 = async () => {
  console.log('named export 1')
}

/**
 * Example 2.
 */
       function example2() {
  console.log('named export 2')
}

/**
 * Example 3.
 */
function example3() {
  console.log('named export 3')
}

/**
 * Example 4.
 */
const example4 = async () => {
  console.log('named export 4')
}



/**
 * Default Class Example.
 */
               class Example {
  /**
   * A constructor for the example.
   * @constructor
   */
  constructor() {
    console.log('default export')
  }
}


module.exports = Example
module.exports.example1 = example1
module.exports.example2 = example2
module.exports.example3 = example3
module.exports.alias4 = example4

Unnamed Default

当有一个未命名的默认值时,例如作为 export default class {}export default async function () {},它将被原地替换。 由于无论如何都会在最后分配所有命名的导出,所以应该没有问题。

Input (view source)Output
export function example1() {}

export default class {
  /**
   * An unnamed default class.
   */
  constructor() {}
}

export function example2() {}

function example1() {}

module.exports=class {
  /**
   * An unnamed default class.
   */
  constructor() {}
}

       function example2() {}

module.exports.example1 = example1
module.exports.example2 = example2

Export From

从另一个模块导出时,创建了一些私有内部变量。 目前无法从多个模块导出一个default,既可以是命名的,也可以是default

Input (view source)Output
export const example = () => {}

export {
  default,
  example2,
  example3 as alias3,
} from 'test'

const example = () => {}





const $test = require('test');


module.exports = $test
module.exports.example = example
module.exports.example2 = $test.example2
module.exports.alias3 = $test.example3
Input (view source)Output
export const example = () => {}

export {
  default as example2,
} from 'test'

const example = () => {}



const $test = require('test');


module.exports.example = example
module.exports.example2 = $test

Limitations

  • 如果默认导出的是原始类型,比如 boolean或数字,也不可能使用命名导出,因为 module.exports 将绑定到原语,并且进一步分配给 module.exports.namedExport 将不会有什么影响。

    module.exports = 'STRING' // primitive (string)
    module.exports.example = function () {}
    console.log(module.exports.example) // undefined
    
  • 声明的串行导出是不可能的,因为很难使用正则表达式解析它们。

    // not possible
    export const
      a = 'test',
      b = () => {}
    
  • 当使用 export from 语句时,将创建目标模块的私有变量,例如,export { default } from test 将创建 const $test = require ('test') 变量,因此如果在代码中声明具有此类名称的变量,则可能会发生冲突。

Checklist

  • [x] export { name1, name2 , …, nameN };

  • [x] export { variable1 as name1, variable2 as name2, …, nameN };

  • [x] export let name1, name2, …, nameN ; // 也是 var, const

  • [ ] export let name1 = …, name2 = …, …, nameN; // 也是 var, const

  • [x] export function FunctionName(){...}

  • [x] export class ClassName {...}

  • [x ] export default expression;

  • [x] export default function (...) { ... } // also class, function*

  • [x] export default function name1(... ) { … } // 还有类、函数*

  • [x] export { name1 as default, … };

  • [ ] export * from …;

  • [x ] export { name1, name2, …, nameN } from …;

  • [x] export { import1 as name1, import2 as name2, …, nameN } from …;

  • [x ] export { default } from …;

  • [ ] 确保像 export function(/* string */ adc) {} 这样的注释是有效的。

Positions Preservation

转换将尝试将行号和列号保留为它们用于通过 alamode 更轻松地生成源地图。 将来,这可能会改变。

Art Deco© Art Deco for À La Mode 2019Tech Nation VisaTech Nation Visa Sucks

@a-la/export

npm version

@a-la/export is a a set of rules for Àlamode to transpile ESM modules' export statements into CJS modules' module.exports statements in Node.js.

yarn add @a-la/export

Table Of Contents

API

The ÀLaImport is the default export and an array containing a sequence of rules for Replaceable.

import ÀLaExport from '@a-la/import'

Output Example

The set of rules changes export to module.exports statements. module.exports will be assigned the value of the default export if it is present, and all named exports will be assigned to module.exports after that.

Input (view source)Output
/**
 * Example 1.
 */
export const example1 = async () => {
  console.log('named export 1')
}

/**
 * Example 2.
 */
export function example2() {
  console.log('named export 2')
}

/**
 * Example 3.
 */
function example3() {
  console.log('named export 3')
}

/**
 * Example 4.
 */
const example4 = async () => {
  console.log('named export 4')
}

export { example3, example4 as alias4 }

/**
 * Default Class Example.
 */
export default class Example {
  /**
   * A constructor for the example.
   * @constructor
   */
  constructor() {
    console.log('default export')
  }
}

/**
 * Example 1.
 */
       const example1 = async () => {
  console.log('named export 1')
}

/**
 * Example 2.
 */
       function example2() {
  console.log('named export 2')
}

/**
 * Example 3.
 */
function example3() {
  console.log('named export 3')
}

/**
 * Example 4.
 */
const example4 = async () => {
  console.log('named export 4')
}



/**
 * Default Class Example.
 */
               class Example {
  /**
   * A constructor for the example.
   * @constructor
   */
  constructor() {
    console.log('default export')
  }
}


module.exports = Example
module.exports.example1 = example1
module.exports.example2 = example2
module.exports.example3 = example3
module.exports.alias4 = example4

Unnamed Default

When there's an unnamed default such as export default class {} or export default async function () {}, it will be replaced in place. Since all named exports will be assigned at the end anyway, there shouldn't be a problem.

Input (view source)Output
export function example1() {}

export default class {
  /**
   * An unnamed default class.
   */
  constructor() {}
}

export function example2() {}

function example1() {}

module.exports=class {
  /**
   * An unnamed default class.
   */
  constructor() {}
}

       function example2() {}

module.exports.example1 = example1
module.exports.example2 = example2

Export From

When exporting from another module, some private internal variables are created. It is currently not possible to export a default either as named, or as default from more than one module.

Input (view source)Output
export const example = () => {}

export {
  default,
  example2,
  example3 as alias3,
} from 'test'

const example = () => {}





const $test = require('test');


module.exports = $test
module.exports.example = example
module.exports.example2 = $test.example2
module.exports.alias3 = $test.example3
Input (view source)Output
export const example = () => {}

export {
  default as example2,
} from 'test'

const example = () => {}



const $test = require('test');


module.exports.example = example
module.exports.example2 = $test

Limitations

  • If the default export is a primitive type such as boolean or number, it is not possible to use named exports as well, because module.exports will be binded to the primitive, and further assignments to module.exports.namedExport will not have any effect.

    module.exports = 'STRING' // primitive (string)
    module.exports.example = function () {}
    console.log(module.exports.example) // undefined
    
  • Serial exports of declarations are not possible as it's difficult to parse them using a regular expression.

    // not possible
    export const
      a = 'test',
      b = () => {}
    
  • When using the export from statement, a private variable for the targeted module will be created, e.g., export { default } from test will create const $test = require('test') variable, therefore a collision could happen if a variable with such name was declared in the code.

Checklist

  • [x] export { name1, name2, …, nameN };

  • [x] export { variable1 as name1, variable2 as name2, …, nameN };

  • [x] export let name1, name2, …, nameN; // also var, const

  • [ ] export let name1 = …, name2 = …, …, nameN; // also var, const

  • [x] export function FunctionName(){...}

  • [x] export class ClassName {...}

  • [x] export default expression;

  • [x] export default function (…) { … } // also class, function*

  • [x] export default function name1(…) { … } // also class, function*

  • [x] export { name1 as default, … };

  • [ ] export * from …;

  • [x] export { name1, name2, …, nameN } from …;

  • [x] export { import1 as name1, import2 as name2, …, nameN } from …;

  • [x] export { default } from …;

  • [ ] Make sure that comments like export function(/* string */ adc) {} are functional.

Positions Preservation

The transform will attempt to preserve line and column numbers as they are for easier generation of source maps by alamode. In future, this might change.

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