@putout/plugin-esm
🐊Putout plugin improves ability to transform ESM code
Last updated 2 months ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @putout/plugin-esm 
SYNC missed versions from official npm registry.

@putout/plugin-esm NPM version

The static import statement is used to import read only live bindings which are exported by another module. The imported bindings are called live bindings because they are updated by the module that exported the binding, but cannot be re-assigned by the importing module.

(c) MDN

????Putout plugin adds ability to transform to new Node.js API and apply best practices.

Install

npm i putout @putout/plugin-esm -D

Rules

File rules

Config

{
    "rules": {
        "esm/apply-default-import": "on",
        "esm/apply-export-from": "on",
        "esm/apply-import-attirbutes": "on",
        "esm/declare-imports-first": "on",
        "esm/convert-assert-to-with": "on",
        "esm/convert-const-to-import": "on",
        "esm/group-imports-by-source": "on",
        "esm/merge-duplicate-imports": "on",
        "esm/merge-declaration-with-export": "on",
        "esm/merge-export-declaration": "on",
        "esm/remove-quotes-from-import-assertions": "on",
        "esm/remove-empty-export": "on",
        "esm/remove-empty-import": ["on", {
            "ignore": []
        }],
        "esm/sort-imports-by-specifiers": "on",
        "esm/resolve-imported-file": "off",
        "esm/shorten-imported-file": "off",
        "esm/apply-js-imported-file": "off",
        "esm/apply-name-to-imported-file": "off",
        "esm/apply-namespace-to-imported-file": "off",
        "esm/apply-privately-imported-file": "off",
        "esm/remove-useless-export-specifiers": "off"
    }
}

Rules

apply-default-import

The static import declaration is used to import read-only live bindings which are exported by another module.

(c) MDN

Check out ????Putout Editor.

❌ Example of incorrect code

import {default as a} from 'a';

βœ… Example of correct code

import a from 'a';

apply-export-from

The export declaration is used to export values from a JavaScript module.

(c) MDN

Check out ????Putout Editor:

❌ Example of incorrect code

import * as ns_1 from 'x';
import {createAsyncLoader} from './load/async-loader.js';

export {
    ns_1 as ns,
    createAsyncLoader,
};

βœ… Example of correct code

export * as ns from 'x';
export {createAsyncLoader} from './load/async-loader.js';

apply-import-attributes

The import attributes feature instructs the runtime about how a module should be loaded, including the behavior of module resolution, fetching, parsing, and evaluation.

(c) MDN

Check out ????Putout Editor.

❌ Example of incorrect code

import a from '../package.json';

export * as x from './package.json';

await import('./package.json');

βœ… Example of correct code

import a from '../package.json' with {
    type: 'json',
};

export * from './package.json' with {
    type: 'json',
};
await import('./package.json', {
    with: {
        type: 'json',
    },
});

merge-declaration-with-export

Checkout in ????Putout Editor.

❌ Example of incorrect code

const stack = [];

function sum(a, b) {
    i32.add(local.get(), local.get());
}

export {
    sum,
    stack,
};

βœ… Example of correct code

export const stack = [];

export function sum(a, b) {
    i32.add(local.get(), local.get());
}

export const {
    report,
    fix,
    scan,
} = createRemoveFiles(['*.swp', '*.swo']);

merge-export-declarations

Checkout in ????Putout Editor.

❌ Example of incorrect code

export {
    loadPlugins,
};
export {
    loadPluginsAsync,
};

❌ Example of incorrect code

export {
    loadPlugins,
    loadPluginsAsync,
};

remove-useless-export-specifiers

Check out in ????Putout Editor.

❌ Example of incorrect code

export const hello = () => 'world';
export const {
-    hello,
}

declare-imports-first

Check out in ????Putout Editor. For CommonJS use nodejs/declare-after-require.

❌ Example of incorrect code

const [arg] = process.argv;
import esbuild from 'esbuild';

βœ… Example of correct code

import esbuild from 'esbuild';

const [arg] = process.argv;

group-imports-by-source

Group order:

  • βœ… builtins;
  • βœ… external;
  • βœ… hashed;
  • βœ… internal;

Checkout in ????Putout Editor.

❌ Example of incorrect code

import fs from 'node:fs';
import {lodash} from 'lodash';
import react from 'react';
import d from '../hello.js';
import ss from '../../bb/ss.js';
import b from './ss.js';
import parse from '#parser';

const c = 5;

βœ… Example of correct code

import fs from 'node:fs';
import react from 'react';
import {lodash} from 'lodash';
import parse from '#parser';
import b from './ss.js';
import d from '../hello.js';
import ss from '../../bb/ss.js';

const c = 5;

merge-duplicate-imports/join

❌ Example of incorrect code

import test from 'supertape';
import {stub} from 'supertape';

βœ… Example of correct code

import test, {stub} from 'supertape';

merge-duplicate-imports/rename

Checkout in ????Putout Editor. To disable use:

{
    "rules": {
        "esm/merge-duplicate-imports-rename": "off"
    }
}

❌ Example of incorrect code

import putout from './putout.js';
import all from './putout.js';
import x from './putout.js';

console.log(all);
console.log(x);

βœ… Example of correct code

import putout from './putout.js';

console.log(putout);
console.log(putout);

remove-empty-export

-export {};

remove-empty-import

-import 'abc';

remove-quotes-from-import-assertions

Checkout in ????Putout Editor.

❌ Example of incorrect code

import json from './mod.json' with {
    type: 'json',
};

βœ… Example of correct code

import json from './mod.json' with {
    type: 'json',
};

sort-imports-by-specifiers

Checkout in ????Putout Editor.

❌ Example of incorrect code

import {
    a,
    b,
    c,
    d,
} from 'd';
import a1 from 'a1';

βœ… Example of correct code

import a1 from 'a1';
import {
    a,
    b,
    c,
    d,
} from 'd';

convert-assert-to-with

This feature would ideally use the with keyword to denote attributes, but there are existing implementations based on a previous version of the proposal using the assert keyword. Due to potential web compatibility risks, the proposal still includes assert marked as deprecated. Usage of the old syntax is discouraged, and its removal is being investigated.

(c) tc39

Check out in ????Putout Editor.

❌ Example of incorrect code

import json from './foo.json' assert {
    type: 'json',
};

import('foo.json', {
    assert: {
        type: 'json',
    },
});

βœ… Example of correct code

import json from './foo.json' with {
    type: 'json',
};

import('foo.json', {
    with: {
        type: 'json',
    },
});

convert-const-to-import

Check out in ????Putout Editor.

❌ Example of incorrect code

const {Server} from 'socket.io';

βœ… Example of correct code

import {Server} from 'socket.io';

File Rules

apply-namespace-to-imported-file

The rule fixes:

SyntaxError: The requested module './a.js' does not provide an export named 'default'

Check out in ????Putout Editor:

Let's consider file structure:

/
└── lib/
    β”œβ”€β”€ index.js "import a from './a.js';"
    └── b.js "export const b = 2;" 

In this case index.js can be fixed:

❌ Example of incorrect code

import a from './a.js';

βœ… Example of correct code

import * as a from './a.js';

apply-name-to-imported-file

Checkout in ????Putout Editor:

Let's consider file structure:

/
└── lib/
    β”œβ”€β”€ index.js "import a from './a.js';\n export * as b from './b.js'"
    β”œβ”€β”€ a.js "export const a = 2;"
    └── b.js "export const b = 2;" 

In this case index.js can be fixed:

❌ Example of incorrect code
import a from './a.js';

export * as b from './b.js';
βœ… Example of correct code
import {a} from './a.js';

export {b} from './b.js';

apply-privately-imported-file

Entries in the imports field must be strings starting with #. Package imports permit mapping to external packages. This field defines subpath imports for the current package.

(c) nodejs.org

Check out in ????Putout Editor:

Let's consider file structure:

/
β”œβ”€β”€ package.json {"imports": {"#is: {"default": "./lib/tokenize/is.js"}}}
└──  lib/
    └── tokenize/
        β”œβ”€β”€ is.js "export const isPrev = () => {}"
        └── expressions/
            └── spread-element.js "import {isPrev} from '../is.js"

In this case spread-element.js can be fixed:

❌ Example of incorrect code

import {isPrev} from '../is.js';

βœ… Example of correct code

import {isPrev} from '#is';

resolve-imported-file

import a directory URL is unsupported.

(c) nodejs.org

Check out in ????Putout Editor:

Let's consider file structure:

/
β”œβ”€β”€ package.json
└── lib/
    β”œβ”€β”€ index.js
    └── a.js

In this case index.js can be fixed:

❌ Example of incorrect code

import a from './a';
import info from '../package';

βœ… Example of correct code

import a from './a.js';
import info from '../package.json' with {
    type: 'json',
};

shorten-imported-file

Check out in ????Putout Editor:

Let's consider file structure:

/
└── processors/
    β”œβ”€β”€ index.js
    └── parse-processor-names.js

In this case index.js can be fixed:

❌ Example of incorrect code

import {parseProcessorNames} from '../processors/parse-processor-names.js';

βœ… Example of correct code

import {parseProcessorNames} from './parse-processor-names.js';

apply-js-imported-file

Checkout in ????Putout Editor:

Let's consider file structure:

/
└── lib/
    β”œβ”€β”€ index.js
    └── a.js

In this case index.js can be fixed:

❌ Example of incorrect code

import a from './a.mjs';

βœ… Example of correct code

import a from './a.js';

License

MIT

Current Tags

  • 10.4.0                                ...           latest (a month ago)

91 Versions

  • 10.4.0                                ...           a month ago
  • 10.3.0                                ...           2 months ago
  • 10.1.0                                ...           2 months ago
  • 10.0.2                                ...           2 months ago
  • 10.0.1                                ...           2 months ago
  • 10.0.0                                ...           2 months ago
  • 9.11.1                                ...           2 months ago
  • 9.11.0                                ...           2 months ago
  • 9.10.0                                ...           2 months ago
  • 9.9.0                                ...           2 months ago
  • 9.8.0                                ...           2 months ago
  • 9.7.0                                ...           2 months ago
  • 9.6.0                                ...           2 months ago
  • 9.5.0                                ...           2 months ago
  • 9.4.0                                ...           2 months ago
  • 9.3.0                                ...           2 months ago
  • 9.2.0                                ...           2 months ago
  • 9.1.0                                ...           2 months ago
  • 9.0.0                                ...           2 months ago
  • 8.8.0                                ...           2 months ago
  • 8.7.0                                ...           2 months ago
  • 8.6.0                                ...           2 months ago
  • 8.5.1                                ...           2 months ago
  • 8.5.0                                ...           2 months ago
  • 8.4.0                                ...           2 months ago
  • 8.3.0                                ...           2 months ago
  • 8.2.0                                ...           2 months ago
  • 8.1.0                                ...           2 months ago
  • 8.0.2                                ...           2 months ago
  • 8.0.1                                ...           2 months ago
  • 8.0.0                                ...           3 months ago
  • 7.7.1                                ...           3 months ago
  • 7.7.0                                ...           3 months ago
  • 7.6.0                                ...           3 months ago
  • 7.5.1                                ...           3 months ago
  • 7.5.0                                ...           3 months ago
  • 7.4.3                                ...           3 months ago
  • 7.4.2                                ...           3 months ago
  • 7.4.1                                ...           3 months ago
  • 7.4.0                                ...           3 months ago
  • 7.3.0                                ...           3 months ago
  • 7.2.1                                ...           3 months ago
  • 7.2.0                                ...           3 months ago
  • 7.1.2                                ...           3 months ago
  • 7.1.1                                ...           3 months ago
  • 7.1.0                                ...           3 months ago
  • 7.0.0                                ...           3 months ago
  • 6.6.0                                ...           3 months ago
  • 6.5.1                                ...           3 months ago
  • 6.5.0                                ...           3 months ago
  • 6.4.0                                ...           3 months ago
  • 6.3.2                                ...           3 months ago
  • 6.3.1                                ...           3 months ago
  • 6.3.0                                ...           3 months ago
  • 6.2.0                                ...           3 months ago
  • 6.1.1                                ...           3 months ago
  • 6.1.0                                ...           3 months ago
  • 6.0.0                                ...           4 months ago
  • 5.3.1                                ...           5 months ago
  • 5.3.0                                ...           5 months ago
  • 5.2.0                                ...           5 months ago
  • 5.1.1                                ...           5 months ago
  • 5.1.0                                ...           5 months ago
  • 5.0.0                                ...           8 months ago
  • 4.10.0                                ...           8 months ago
  • 4.9.0                                ...           10 months ago
  • 4.8.0                                ...           10 months ago
  • 4.7.0                                ...           10 months ago
  • 4.6.0                                ...           10 months ago
  • 4.5.0                                ...           a year ago
  • 4.4.0                                ...           a year ago
  • 4.3.0                                ...           a year ago
  • 4.2.2                                ...           a year ago
  • 4.2.1                                ...           a year ago
  • 4.2.0                                ...           a year ago
  • 4.1.2                                ...           a year ago
  • 4.1.1                                ...           a year ago
  • 4.1.0                                ...           a year ago
  • 4.0.0                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.3.0                                ...           a year ago
  • 2.2.0                                ...           a year ago
  • 2.1.2                                ...           a year ago
  • 2.1.1                                ...           a year ago
  • 2.1.0                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.3.0                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 4
Last Day 0
Last Week 4
Last Month 0
Dependencies (2)
Dependents (2)

Copyright 2013 - present © cnpmjs.org | Home |