@a-la/export 中文文档教程
@a-la/export
@a-la/export
是 Àlamode 将 ESM 模块的 export
语句转换为 CJS 模块在 Node.js 中的 module.exports
语句。
yarn add @a-la/export
Table Of Contents
- Table Of Contents
- API
- Output Example
- Unnamed Default
- Export From
- Limitations
- Checklist
- Positions Preservation
- Copyright
API
ÀLaImport 是默认导出和包含一系列规则的数组可替换。
import ÀLaExport from '@a-la/import'
Output Example
规则集改变 export 到
module.exports
语句。 module.exports
将被分配 default
导出的值(如果存在),并且所有命名的导出都将被分配给 module.exports
那。
Input (view source) | Output |
---|---|
|
|
Unnamed Default
当有一个未命名的默认值时,例如作为 export default class {}
或 export default async function () {}
,它将被原地替换。 由于无论如何都会在最后分配所有命名的导出,所以应该没有问题。
Input (view source) | Output |
---|---|
|
|
Export From
从另一个模块导出时,创建了一些私有内部变量。 目前无法从多个模块导出一个default
,既可以是命名的,也可以是default
。
Input (view source) | Output |
---|---|
|
|
Input (view source) | Output |
---|---|
|
|
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
更轻松地生成源地图。 将来,这可能会改变。
Copyright
© Art Deco for À La Mode 2019 | Tech Nation Visa Sucks |
---|
@a-la/export
@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
- Table Of Contents
- API
- Output Example
- Unnamed Default
- Export From
- Limitations
- Checklist
- Positions Preservation
- Copyright
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 |
---|---|
|
|
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 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 |
---|---|
|
|
Input (view source) | Output |
---|---|
|
|
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 tomodule.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 createconst $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.
Copyright
© Art Deco for À La Mode 2019 | Tech Nation Visa Sucks |
---|