{"id":13789454,"url":"https://github.com/sidvishnoi/esm-to-cjs","last_synced_at":"2025-03-25T17:31:59.681Z","repository":{"id":45031427,"uuid":"155614336","full_name":"sidvishnoi/esm-to-cjs","owner":"sidvishnoi","description":"Transform ESM to Common JS for present NodeJS, without any junk wrappers or useless renaming","archived":false,"fork":false,"pushed_at":"2022-02-19T22:21:32.000Z","size":46,"stargazers_count":47,"open_issues_count":4,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-23T12:14:33.122Z","etag":null,"topics":["commonjs","esm","esmodules","gulp-plugin","nodejs","string-manipulation","transformer"],"latest_commit_sha":null,"homepage":"https://sidvishnoi.github.io/esm-to-cjs/","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/sidvishnoi.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-10-31T19:45:01.000Z","updated_at":"2024-08-30T10:58:02.000Z","dependencies_parsed_at":"2022-07-25T23:30:47.212Z","dependency_job_id":null,"html_url":"https://github.com/sidvishnoi/esm-to-cjs","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidvishnoi%2Fesm-to-cjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidvishnoi%2Fesm-to-cjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidvishnoi%2Fesm-to-cjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidvishnoi%2Fesm-to-cjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sidvishnoi","download_url":"https://codeload.github.com/sidvishnoi/esm-to-cjs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222089562,"owners_count":16929180,"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":["commonjs","esm","esmodules","gulp-plugin","nodejs","string-manipulation","transformer"],"created_at":"2024-08-03T22:00:22.911Z","updated_at":"2024-10-29T17:36:02.077Z","avatar_url":"https://github.com/sidvishnoi.png","language":"JavaScript","funding_links":[],"categories":["打包工具"],"sub_categories":[],"readme":"## esm-to-cjs\n\n\u003e Transform ESM to Common JS for present NodeJS, without any junk wrappers or useless renaming.\n\n## Motivation\n\nI was working on a TypeScript project for NodeJS and using ES modules. The transformations to CommonJS by TypeScript (or by Babel + plugins) causes variable renaming (prefixing) and adds some wrapper functions to the transformed code.\n\nI was not happy with this transformation. So I created this tool to convert the ESM import/exports to the kinds that a NodeJS developer would write today.\n\n``` js\n// input\nimport { resolve as resolvePath } from \"path\";\nresolvePath(\"./hello\")\n\n// what i wanted\nconst {  resolve: resolvePath } = require(\"path\");\nresolvePath(\"./hello\")\n\n// typescript gave me:\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst path_1 = require(\"path\");\npath_1.resolve(\"./hello\");\n```\n\n``` js\n// input\nasync () =\u003e {\n  const path = await import(\"path\");\n}\n\n// what i wanted\nasync () =\u003e {\n  const path = require(\"path\");\n}\n\n// typescript gave me:\nvar __importStar = (this \u0026\u0026 this.__importStar) || function (mod) {\n    if (mod \u0026\u0026 mod.__esModule) return mod;\n    var result = {};\n    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];\n    result[\"default\"] = mod;\n    return result;\n};\nasync () =\u003e {\n    const path = await Promise.resolve().then(() =\u003e __importStar(require(\"path\")));\n};\n```\n\n``` js\n// input\nexport const foo = 5;\nconsole.log(foo);\n\n// what i wanted\nconst foo = 5;\nmodule.exports = {\n  foo\n}\n\n// typescript gave me:\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.foo = 5;\nconsole.log(exports.foo);\n```\n\n\n``` js\n// input\nexport { foo as wow, bar } from \"baz\";\nexport { baz } from \"lorem\";\n\n// what i wanted\nconst { foo: __foo__, bar: __bar__ } = require(\"baz\");\nmodule.exports = {\n  wow: __foo__,\n  bar: __bar__,\n  baz: require(\"lorem\").baz\n}\n\n// typescript gave me\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar baz_1 = require(\"baz\");\nvar lorem_1 = require(\"lorem\");\nexports.wow = baz_1.foo;\nexports.bar = baz_1.bar;\nexports.baz = lorem1.baz;\n```\n\n\nSo, I created this tool using some simple string manipulations. A lot of sample input/output are [available here](https://github.com/sidvishnoi/esm-to-cjs/blob/main/test/fixtures/supported.md).\n\n\n\n## Limitations\n\n- `import * as foo from \"bar\";` is converted to `const foo = require(\"bar\");`. Not sure if this is what everyone wants. I did it as per my project requirements.\n- Also, `import foo from \"bar\";` is converted to `const foo = require(\"bar\").default;\"`.\n- No support for `export *` syntax.\n- No mixing of default import, named imports and `import *` in same statement.\n- See Bug: [\"The simpler transform is semantically wrong\"](https://github.com/sidvishnoi/esm-to-cjs/issues/4)\n\n## Packages\n\nThis tool is available in form of two packages:\n\n- `esm-to-cjs`: the core module.\n- `gulp-esm-to-cjs`: as a gulp plugin.\n\n### `esm-to-cjs`\n\nThis is the core module. It includes a tokenizer and a transformer. I didn't use some specific JS parser due to overheads and created my own using string manipulation.\n\n**Install:**\n\n```\nnpm i --save-dev esm-to-cjs\n```\n\n**Usage:**\n\n``` js\nconst { runTransform } = require(\"esm-to-cjs\");\n\nconst input = `import { resolve as resolvePath } from \"path\";`\nconst options = { quote: \"double\" }; // see details below\nconst output = runTransform(input, options);\nconsole.log(output);\n// const { resolve: resolvePath } = require(\"path\");\n```\n\n**Options:**\n\n``` yaml\nquote:\n  type: string\n  default: \"double\"\n  available: \"double\" | \"single\"\n  description: Tells parser what kind of quotes you use in module names, i.e. like `from \"moduleName\"`\n\nlenDestructure:\n  type: number (of characters)\n  default: 60\n  description: Used by parser to improve performance. Set to a higher value if your object destruturing statements are longer.\n\nlenModuleName:\n  type: number (of characters)\n  default: 20\n  description: Used by parser to improve performance. Set to a higher value if your module names are longer.\n\nlenIdentifier:\n  type: number (of characters)\n  default: 60\n  description: Used by parser to improve performance. Set to a higher value if your identifier names are longer.\n\nindent:\n  type: number\n  default: 2\n  description: Indentation (spaces) in output code.\n```\n\n\n### `gulp-esm-to-cjs`\n\nGulp plugin for esm-to-cjs.\n\n**Install**:\n\n```\nnpm i --save-dev gulp-esm-to-cjs\n```\n\n**Usage**:\n\n``` js\n// gulpfile.js\nconst esmToCjs = require(\"gulp-esm-to-cjs\");\n\nfunction convert() {\n  return gulp\n    .src(src)\n    .pipe(esmToCjs(options))\n    .pipe(gulp.dest(dest));\n}\nmodule.exports.convert = convert;\n\n// use as:\n// $ gulp convert\n```\n\n\n## Contributing\n\n- If you've issues regarding the project - documentation, supported features and transformations etc., please file them on [GitHub](https://github.com/sidvishnoi/esm-to-cjs/issues) where we can discuss.\n- Pull requests are welcome!\n- Please bear in mind that I created this project in a hurry, so the code isn't very impressive. Also, I didn't add all the transformations. See limitations above. Would be nice if we can overcome them!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsidvishnoi%2Fesm-to-cjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsidvishnoi%2Fesm-to-cjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsidvishnoi%2Fesm-to-cjs/lists"}