js-transform-plugin
v1.1.10
Published
An ast plugin for transforming js/ts/jsx/tsx files from commonJS to ESmodule.
Downloads
2
Maintainers
Readme
English | 简体中文
An ast plugin for transforming js/ts/jsx/tsx files from commonJS to ESmodule. Here for the examples.
Usage
npm install -D js-transform-plugin
Add this script to package.json
{ "scripts": { "transform": "js-transform" } }
npm run transform [absolute path]
- The default value of [absolute path] is process.cwd().
- The configuration of ignored files has not been supported. To avoid transforming node_modules dir, you can provide param, like, the absolute path of src dir. Or easier, just delete node_modules dir.
- Then js/ts/jsx/tsx files in [absolute path] will be transformed from commonJS to ESmodule recursively.
Examples
// before
const a = require("my-package");
// after
import a from "my-package";
// before
const { a, b, c } = require("my-package");
// after
import { a, b, c } from "my-package";
// before
require("my-package");
// after
import "my-package";
// before
module.exports = { ... }
// after
export { ... }
// or
export default { ... }
// before
module.exports = abc;
module.exports = new Abc();
module.exports = fun(a, b);
module.exports = function(){};
module.exports = () => {};
// after
export default abc;
export default new Abc();
export default fun(a, b);
export default function(){};
export default () => {};
// before
const { b, c } = require("my-package");
// after
import { b, c } from "my-package";
// before
const { d: aliasD } = require("my-package");
// after
import { d as aliasD } from "my-package";