包详细信息

@a-la/export

a-la254MIT1.5.0

A set of rules for ÀLaMode to transpile ES6 export statements into module.exports assignments.

export, a-la

自述文件

@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
js /** * 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') } } ``` </td> <td>js / 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
js export function example1() {} export default class { /** * An unnamed default class. */ constructor() {} } export function example2() {} ``` </td> <td>js 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
js export const example = () => {} export { default, example2, example3 as alias3, } from 'test' ``` </td> <td>js 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
js export const example = () => {} export { default as example2, } from 'test' ``` </td> <td>js 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.

Copyright

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

更新日志

16 May 2019

1.5.0

  • [feature] Disable whitespace for builds without source maps.

24 April 2019

1.4.0

  • [types] Annotate types.

14 March 2019

1.3.1

  • [fix] Restore source code to before starting to implement advanced (advanced means replacing strings, template literals and regexes and is the default mode now until the Dynamic mode is implemented in ÀLaMode to detect imports).
  • [package] Publish the module.

1 September 2018

1.3.0

  • [feature] Access string and literal markers from alamode.

27 August 2018

1.2.2

  • [fix] Correct export of class X extends Y expression.

22 August 2018

1.2.1

  • [build] Add source maps.

21 August 2018

1.2.0

  • [feature] Preserve column numbers (e.g., replace export const a = 'test' and export default class {} with const a = 'test and module.exports=class {}) to correctly generate mappings in alamode.

20 August 2018

1.1.0

  • [feature] Preserve line numbers.
  • [test] Test with zoroaster@2.3's makeTestSuite method.

10 August 2018

1.0.0