{"id":16332787,"url":"https://github.com/eight04/cjs-es","last_synced_at":"2025-03-20T23:30:30.697Z","repository":{"id":54404350,"uuid":"130906593","full_name":"eight04/cjs-es","owner":"eight04","description":"Transform CommonJS modules into ES modules.","archived":false,"fork":false,"pushed_at":"2022-08-08T14:36:57.000Z","size":367,"stargazers_count":27,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-11T23:33:08.586Z","etag":null,"topics":["ast","commonjs","es6"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eight04.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-24T19:55:06.000Z","updated_at":"2024-08-24T21:42:32.000Z","dependencies_parsed_at":"2022-08-13T14:40:20.689Z","dependency_job_id":null,"html_url":"https://github.com/eight04/cjs-es","commit_stats":null,"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eight04%2Fcjs-es","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eight04%2Fcjs-es/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eight04%2Fcjs-es/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eight04%2Fcjs-es/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eight04","download_url":"https://codeload.github.com/eight04/cjs-es/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221809808,"owners_count":16883976,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","commonjs","es6"],"created_at":"2024-10-10T23:32:57.399Z","updated_at":"2024-10-28T08:50:12.148Z","avatar_url":"https://github.com/eight04.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"cjs-es\n======\n\n[![.github/workflows/build.yml](https://github.com/eight04/cjs-es/actions/workflows/build.yml/badge.svg)](https://github.com/eight04/cjs-es/actions/workflows/build.yml)\n[![codecov](https://codecov.io/gh/eight04/cjs-es/branch/master/graph/badge.svg)](https://codecov.io/gh/eight04/cjs-es)\n[![install size](https://packagephobia.now.sh/badge?p=cjs-es)](https://packagephobia.now.sh/result?p=cjs-es)\n\nTransform CommonJS module into ES module.\n\nFeatures\n--------\n\n* Transform the syntax that is interchangeable between mjs and js e.g. `const foo = require(\"foo\")` -\u003e `import * as foo from \"foo\";`.\n* Hoist the `require`/`exports` statement that is not top-level.\n* Transform dynamic imports i.e. `Promise.resolve(require(\"foo\"))` -\u003e `import(\"foo\")`.\n* Prefer named import/export when possible.\n\nThere are more examples under `test/cases` folder.\n\nUsage\n-----\n\n```js\nconst {parse} = require(\"acorn\");\nconst {transform} = require(\"cjs-es\");\nconst code = `\nfunction foo() {}\nfunction bar() {}\nmodule.exports = {foo, bar};\n`;\ntransform({code, ast: parse(code, {ecmaVersion: \"latest\"})})\n  .then(result =\u003e {\n    console.log(result.code);\n    /* -\u003e\n    function foo() {}\n    function bar() {}\n    export {foo};\n    export {bar};\n    */\n  });\n```\n\nImport style\n------------\n\nWhen binding the module into one identifier:\n\n```js\nconst foo = require(\"foo\");\n```\n\nThe transformer imports all members from the module by default:\n\n```js\nimport * as foo from \"foo\";\n```\n   \nTo import the default member, mark `require()` as `// default`:\n\n```js\nconst foo = require(\"foo\"); // default\n```\n\nResult:\n\n```js\nimport foo from \"foo\";\n```\n\nNote that if the identifier is used as the callee of a function/new expression, it would be considered as the default member since the namespace is not callable.\n\nExport style\n------------\n\nIf the `module.exports` is assigned with an object pattern:\n\n```js\nconst foo = \"foo\";\nconst bar = \"bar\";\nmodule.exports = {\n  foo,\n  bar\n};\n```\n\nThe transformer converts it into named exports:\n\n```js\nconst foo = \"foo\";\nconst bar = \"bar\";\nexport {foo};\nexport {bar};\n```\n    \nTo export the entire object as the default member, mark `module.exports` as `// default`:\n\n```js\nconst foo = \"foo\";\nconst bar = \"bar\";\nmodule.exports = { // default\n  foo,\n  bar\n};\n```\n\nResult:\n\n```js\nconst foo = \"foo\";\nconst bar = \"bar\";\nexport default {\n  foo,\n  bar\n};\n```\n\nAlso note that if you set `exportStyle` to `default`, all named exports would be merged into a namespace object:\n\n```js\nconst foo = \"foo\";\nconst bar = \"bar\";\nexports.foo = foo;\nexports.bar = bar;\n```\n\nResult:\n\n```js\nconst foo = \"foo\";\nconst bar = \"bar\";\nconst _module_exports_ = {};\nexport {_module_exports_ as default};\n_module_exports_.foo = foo;\n_module_exports_.bar = bar;\n```\n\nHoist\n-----\n\nIf the `require`/`module`/`exports` statement are nested, they would be hoisted.\n\n#### Require statement\n\n```js\nif (foo) {\n  require(\"foo\").foo();\n}\n```\n\nResult:\n\n```js\nimport * as _require_foo_ from \"foo\";\nif (foo) {\n  _require_foo_.foo();\n}\n```\n\n#### Export statement\n\n```js\nif (foo) {\n  module.exports = () =\u003e \"foo\";\n} else {\n  module.exports = () =\u003e \"bar\";\n}\n```\n\nResult:\n\n```js\nlet _module_exports_;\nexport {_module_exports_ as default};\nif (foo) {\n  _module_exports_ = () =\u003e \"foo\";\n} else {\n  _module_exports_ = () =\u003e \"bar\";\n}\n```\n\n#### Named export\n\n```js\nif (foo) {\n  exports.foo = () =\u003e \"foo\";\n}\nfunction test() {\n  exports.foo = () =\u003e \"bar\";\n}\n```\n\nResult:\n\n```js\nlet _export_foo_;\nexport {_export_foo_ as foo};\nif (foo) {\n  _export_foo_ = () =\u003e \"foo\";\n}\nfunction test() {\n  _export_foo_ = () =\u003e \"bar\";\n}\n```\n\nDynamic import\n--------------\n\nES6 lazy load `import(\"...\")` is async and return a promise. It is interchangeable with `Promise.resolve(require(\"...\"))` in CommonJS:\n\n```js\nmodule.exports = () =\u003e {\n  return Promise.resolve(require(\"foo\"));\n};\n```\n\nResult:\n\n```js\nexport default () =\u003e {\n  return import(\"foo\");\n};\n```\n\nUse `module.exports`/`exports` at the same time\n-----------------------------------------------\n\nIt is not a good idea to put `exports` everywhere, but it is a common pattern:\n\n```js\nif (foo) {\n  exports = module.exports = () =\u003e \"foo\";\n} else {\n  module.exports = exports = () =\u003e \"bar\";\n}\nexports.OK = \"OK\";\nconsole.log(module.exports);\n```\n\nAll `module.export` and `exports` would be converted into a single reference:\n\n```js\nlet _module_exports_;\nexport {_module_exports_ as default};\nif (foo) {\n  _module_exports_ = () =\u003e \"foo\";\n} else {\n  _module_exports_ = () =\u003e \"bar\";\n}\n_module_exports_.OK = \"OK\";\nconsole.log(_module_exports_);\n```\n\nPassing `module` around\n-----------------------\n\nIt will generate a module wrapper in this case:\n\n```js\nvar define = require('amdefine')(module);\ndefine(() =\u003e {});\n```\n\nResult:\n\n```js\nconst _module_ = {exports: {}};\nimport _require_amdefine_ from \"amdefine\";\nvar define = _require_amdefine_(_module_);\ndefine(() =\u003e {});\nexport default _module_.exports;\n```\n\nAPI reference\n-------------\n\nThis module exports following members.\n\n* `transform`: A function which can convert CJS module synax into ES module syntax.\n\n### transform\n\n```js\nasync transform({\n  parse?: (code: String) =\u003e ESTree,\n  code: String,\n  ast?: ESTree,\n  sourceMap?: Boolean = false,\n  importStyle?: String | async (moduleId) =\u003e String,\n  exportStyle?: String | async () =\u003e String,\n  nested?: Boolean = false,\n  warn?: (message: String, pos: Number) =\u003e void\n})\n  =\u003e TransformResult\n```\n\n* `parse` is a parser function which can parse JavaScript code into AST. The module will use this function to parse `code`. You don't have to provide the `parse` function if `ast` is set.\n\n* `code` is the JavaScript source code.\n\n* `ast` - if you already have the AST of the code, you can set it as `ast` so the module don't have to parse the code again.\n\n* `sourceMap` - if `true` then generate the source map.\n\n* `importStyle` and `exportStyle` are used to decide how to transform import/export statements. The value or the value returned by the function must be `\"named\"` or `\"default\"`. By default, the transformer always prefer to use named exports for import/export statements.\n\n  If `importStyle` is a function, it will only be called once for each `moduleId` if needed.\n\n  If `exportStyle` is a function, it will only be called once if needed.\n\n* `nested` - By default, only top-level nodes are analyzed and transformed. To analyze the entire tree, set this to true.\n\n* `warn` - the transformer uses `warn` function to emit a warning. If `warn` is not set then the transformer will print the message to the console using `console.error`.\n\nIf an error is thrown during walking the AST, the error has a property `pos` which points to the index of the current node.\n\n### TransformResult\n\n```js\n{\n  code: String,\n  isTouched: Boolean,\n  map: Object | null\n}\n```\n\n* `code` - the result ES source code.\n\n* `isTouched` - if `true` then the code is changed.\n\n* `map` is the source map object generated by [`magicString.generateMap`](https://github.com/Rich-Harris/magic-string#sgeneratemap-options-). Only available if `isTouched` and the `sourceMap` option are both `true`.\n\nChangelog\n---------\n\n* 0.9.2 (Aug 8, 2022)\n\n  - Fix: always put named export wrapper at the top.\n\n* 0.9.1 (Aug 8, 2022)\n\n  - Fix: the module wrapper is removed when imports are trasnformed.\n\n* 0.9.0 (Aug 8, 2022)\n\n  - Bump dependencies.\n  - Fix: always put module wrapper at the top.\n\n* 0.8.2 (Jul 2, 2019)\n\n  - Fix: nested export assignment doesn't check if exports is shadowed.\n\n* 0.8.1 (Jun 18, 2019)\n\n  - Fix: don't hoist duplicated imports.\n\n* 0.8.0 (Jun 13, 2019)\n\n  - Refactor scope analyzer and import writer.\n  - Add: `context.finalImportType`.\n\n* 0.7.0 (Jun 13, 2019)\n\n  - Add: collect import/exrpot information.\n  - Change: export names when module exports object literal and uses nested exports.\n\n* 0.6.4 (Jun 6, 2019)\n\n  - Fix: export default if the object literal has function properties and the function contains `this`.\n\n* 0.6.3 (Jun 6, 2019)\n\n  - Fix: assign a default object if `typeof exports` exists.\n\n* 0.6.2 (Sep 19, 2018)\n\n  - Enhance: try to export live-binding when exporting defaults.\n  - Fix: the logic of module wrapper.\n  - Fix: mixed exports.\n  - Fix: nested module assigned with named exports.\n\n* 0.6.1 (Sep 19, 2018)\n\n  - Bump dependencies.\n\n* 0.6.0 (Sep 19, 2018)\n\n  - Fix: computed properties are detected as named exports.\n  - Fix: TypeError when analyzing empty array elements: `[, foo]`.\n  - **Breaking: convert `exports` and `module.exports` to a single reference.**\n\n* 0.5.0 (Jul 19, 2018)\n\n  - Add: don't hoist export statements in some cases.\n\n* 0.4.9 (Jun 29, 2018)\n\n  - Fix: failed to transform code without semicolon.\n\n* 0.4.8 (Jun 22, 2018)\n\n  - Add: transform multi-line variable declaration.\n  - Fix: super class cannot be a namespace.\n\n* 0.4.7 (May 15, 2018)\n\n  - Fix: default function/class should be converted into an expression.\n  - Fix: exporting default IIFE causes syntax error.\n\n* 0.4.6 (May 13, 2018)\n\n  - Fix: use hires map.\n\n* 0.4.5 (May 1, 2018)\n\n  - Fix: arguments of callable require node is ignored.\n\n* 0.4.4 (May 1, 2018)\n\n  - Fix: write export statement after last statement instead of the end of the file.\n\n* 0.4.3 (May 1, 2018)\n\n  - Fix: reassigned import is not a namespace.\n  - Add: `options.warn`.\n  - Add: warn users for unconverted `require`.\n  - Add: support rename for declared named import.\n  - Add: support declared export `const foo = module.exports = ...`.\n\n* 0.4.2 (Apr 30, 2018)\n\n  - Fix: template tag is callable.\n\n* 0.4.1 (Apr 30, 2018)\n\n  - Fix: syntax error if exported value is enclosed by parentheses.\n\n* 0.4.0 (Apr 30, 2018)\n\n  - Rewrite for async. `options.importStyle` and `options.exportStyle` are async now.\n  - **Change: `transform` function is async now.**\n  - **Drop: `options.hoist`, `options.dynamicImport`.**\n  - Add: `options.nested`.\n  - Fix: namespace is not callable.\n\n* 0.3.3 (Apr 29, 2018)\n\n  - Add: `options.ast`.\n\n* 0.3.2 (Apr 28, 2018)\n\n  - Add: expose `.node` property for tree-walk error.\n  - Fix: hoist named export if prefer default + hoist.\n  - Fix: hoist named require if prefer default + hoist.\n  - Fix: declare.init could be null.\n\n* 0.3.1 (Apr 28, 2018)\n\n  - Fix: error while binding default export to object pattern.\n\n* 0.3.0 (Apr 27, 2018)\n\n  - Merge cjs-hoist.\n  - Add: `hoist` option.\n  - Add: `dynamicImport` option.\n\n* 0.2.2 (Apr 26, 2018)\n\n  - Add: `isTouched` property.\n\n* 0.2.1 (Apr 26, 2018)\n\n  - Add: transform top-level require call.\n\n* 0.2.0 (Apr 26, 2018)\n\n  - Change: don't suppress parse error.\n  - Change: remove `// all` comment.\n  - Add: importStyle, exportStyle option.\n  - Add: use `// default` to change import/export style.\n\n* 0.1.0 (Apr 25, 2018)\n\n  - Initial release.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feight04%2Fcjs-es","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feight04%2Fcjs-es","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feight04%2Fcjs-es/lists"}