{"id":13448523,"url":"https://github.com/wessberg/cjstoesm","last_synced_at":"2025-05-16T13:05:09.019Z","repository":{"id":43101216,"uuid":"184328158","full_name":"wessberg/cjstoesm","owner":"wessberg","description":"A tool that can transform CommonJS to ESM","archived":false,"fork":false,"pushed_at":"2023-12-09T17:45:04.000Z","size":5801,"stargazers_count":376,"open_issues_count":19,"forks_count":16,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-05-01T22:50:46.131Z","etag":null,"topics":["api","cjs","cli","commonjs","compiler","es6","esm","modules","rollup","transformer","treeshake","typescript","webpack"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/wessberg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"wessberg","patreon":"wessberg"}},"created_at":"2019-04-30T20:36:38.000Z","updated_at":"2024-05-10T09:52:33.487Z","dependencies_parsed_at":"2024-05-10T09:52:04.713Z","dependency_job_id":"eee41eb6-c2bf-470e-8e43-bdb700e5db20","html_url":"https://github.com/wessberg/cjstoesm","commit_stats":{"total_commits":138,"total_committers":2,"mean_commits":69.0,"dds":0.007246376811594235,"last_synced_commit":"86b287d2fdae98e2ff2bfdccd0c96e690e28a146"},"previous_names":["wessberg/cjs-to-esm-transformer"],"tags_count":38,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wessberg%2Fcjstoesm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wessberg%2Fcjstoesm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wessberg%2Fcjstoesm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wessberg%2Fcjstoesm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wessberg","download_url":"https://codeload.github.com/wessberg/cjstoesm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247999859,"owners_count":21031046,"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":["api","cjs","cli","commonjs","compiler","es6","esm","modules","rollup","transformer","treeshake","typescript","webpack"],"created_at":"2024-07-31T05:01:47.966Z","updated_at":"2025-04-09T08:05:57.023Z","avatar_url":"https://github.com/wessberg.png","language":"TypeScript","funding_links":["https://github.com/sponsors/wessberg","https://patreon.com/wessberg","https://www.patreon.com/bePatron?u=11315442"],"categories":["TypeScript","Tools"],"sub_categories":[],"readme":"\u003c!-- SHADOW_SECTION_LOGO_START --\u003e\n\n\u003cdiv\u003e\u003cimg alt=\"Logo\" src=\"https://raw.githubusercontent.com/wessberg/cjstoesm/master/documentation/asset/logo.png\" height=\"150\"   /\u003e\u003c/div\u003e\n\n\u003c!-- SHADOW_SECTION_LOGO_END --\u003e\n\n\u003c!-- SHADOW_SECTION_DESCRIPTION_SHORT_START --\u003e\n\n\u003e A tool that can transform CommonJS to ESM\n\n\u003c!-- SHADOW_SECTION_DESCRIPTION_SHORT_END --\u003e\n\n\u003c!-- SHADOW_SECTION_BADGES_START --\u003e\n\n\u003ca href=\"https://npmcharts.com/compare/cjstoesm?minimal=true\"\u003e\u003cimg alt=\"Downloads per month\" src=\"https://img.shields.io/npm/dm/cjstoesm.svg\"    /\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/package/cjstoesm\"\u003e\u003cimg alt=\"NPM version\" src=\"https://badge.fury.io/js/cjstoesm.svg\"    /\u003e\u003c/a\u003e\n\u003cimg alt=\"Dependencies\" src=\"https://img.shields.io/librariesio/github/wessberg%2Fcjstoesm.svg\"    /\u003e\n\u003ca href=\"https://github.com/wessberg/cjstoesm/graphs/contributors\"\u003e\u003cimg alt=\"Contributors\" src=\"https://img.shields.io/github/contributors/wessberg%2Fcjstoesm.svg\"    /\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/prettier/prettier\"\u003e\u003cimg alt=\"code style: prettier\" src=\"https://img.shields.io/badge/code_style-prettier-ff69b4.svg\"    /\u003e\u003c/a\u003e\n\u003ca href=\"https://opensource.org/licenses/MIT\"\u003e\u003cimg alt=\"License: MIT\" src=\"https://img.shields.io/badge/License-MIT-yellow.svg\"    /\u003e\u003c/a\u003e\n\u003ca href=\"https://www.patreon.com/bePatron?u=11315442\"\u003e\u003cimg alt=\"Support on Patreon\" src=\"https://img.shields.io/badge/patreon-donate-green.svg\"    /\u003e\u003c/a\u003e\n\n\u003c!-- SHADOW_SECTION_BADGES_END --\u003e\n\n\u003c!-- SHADOW_SECTION_DESCRIPTION_LONG_START --\u003e\n\n## Description\n\n\u003c!-- SHADOW_SECTION_DESCRIPTION_LONG_END --\u003e\n\nThis is a tool that converts [CommonJS modules](https://requirejs.org/docs/commonjs.html) into tree-shakeable [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import).\nThis allows you to not only bundle CommonJS modules for the browser, but also makes it possible for you to bundle them in modern tools such as [Rollup](https://rollupjs.org/).\n\nIt can also be used as a tool for migrating a CommonJS-based codebase to one based on ES-modules via a simple [CLI](#cli-usage).\n\n`cjstoesm` can be used from the [Command Line](#cli-usage), as [a JavaScript library](#api-usage), and as a [TypeScript Custom Transformer](#usage-with-typescripts-compiler-apis).\n\nPrior art such as [babel-plugin-transform-commonjs](https://github.com/tbranyen/babel-plugin-transform-commonjs) and [rollup-plugin-commonjs](https://github.com/rollup/rollup-plugin-commonjs) exists, but this Custom Transformer aims at producing code that is just as tree-shakeable as equivalent code written\nnatively with ES Modules. Additionally, it aims to be as clean as possible, with no _\"wrappers\"_ around modules as can be seen in other similar solutions.\n\nFor example, here's how `cjstoesm` may rewrite a CommonJS module:\n\n**Input**\n\n```typescript\nexports.foo = function foo() {};\n```\n\n**Output**\n\n```typescript\nexport function foo() {}\n```\n\nHere's another example:\n\n**Input**\n\n```typescript\nmodule.exports = {\n  foo() {\n    return 2 + 2;\n  },\n  bar: 3,\n  baz: new RegExp(\"\")\n};\n```\n\n**Output**\n\n```typescript\nexport function foo() {\n  return 2 + 2;\n}\nexport const bar = 3;\nexport const baz = new RegExp(\"\");\nexport default {foo, bar, baz};\n```\n\nThe same goes for `require(...)` calls:\n\n**Input:**\n\n```typescript\nconst {foo: bar} = require(\"./my-module\");\n```\n\n**Output:**\n\n```typescript\nimport {foo as bar} from \"./my-module.js\";\n```\n\nAnd for complex require calls such as:\n\n**Input:**\n\n```typescript\nconst {\n  foo: {bar: baz}\n} = require(\"./my-module\").something(\"bar\");\n```\n\n**Output:**\n\n```typescript\nimport {something} from \"./my-module.js\";\nconst {\n  foo: {bar: baz}\n} = {something}.something(\"bar\");\n```\n\nAs you can see, this transformer will attempt to produce code that generates as granular imports and exports as possible.\n\nIt includes Import Attributes too! And out of the box, these are added where relevant and necessary:\n\n**Input**\n\n```typescript\nconst pkg = require(\"./package.json\");\n```\n\n**Output**\n\n```typescript\nimport pkg from \"./package.json\" with {type: \"json\"};\n```\n\n\u003c!-- SHADOW_SECTION_FEATURES_START --\u003e\n\n### Features\n\n\u003c!-- SHADOW_SECTION_FEATURES_END --\u003e\n\n- Transformation of CommonJS to ESM\n- Tree-shaking friendly\n- Clean, idiomatic output\n- Automatic and configurable addition of file extensions to module specifiers\n- Automatic and configurable addition of import attributes to relevant import declarations\n- No wrappers\n- Low-level implementation that can be used as the foundation for other tools such as Loaders, Plugins, CLIs, and Linters.\n- CLI integration, enabling you to convert a project from CJS to ESM from the command line.\n- API support, enabling you to convert a project from CJS to ESM programmatically.\n\n\u003c!-- SHADOW_SECTION_FEATURE_IMAGE_START --\u003e\n\n\u003cdiv\u003e\u003cimg alt=\"Feature image\" src=\"https://raw.githubusercontent.com/wessberg/cjstoesm/master/documentation/asset/feature.gif\" height=\"500\"   /\u003e\u003c/div\u003e\u003cbr\u003e\n\n\u003c!-- SHADOW_SECTION_FEATURE_IMAGE_END --\u003e\n\n\u003c!-- SHADOW_SECTION_BACKERS_START --\u003e\n\n## Backers\n\n### Patreon\n\n\u003ca href=\"https://www.patreon.com/bePatron?u=11315442\"\u003e\u003cimg alt=\"Patrons on Patreon\" src=\"https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%3Fusername%3Dwessberg%26type%3Dpatrons\"  width=\"200\"  /\u003e\u003c/a\u003e\n\n\u003c!-- SHADOW_SECTION_BACKERS_END --\u003e\n\n\u003c!-- SHADOW_SECTION_TOC_START --\u003e\n\n## Table of Contents\n\n- [Description](#description)\n  - [Features](#features)\n- [Backers](#backers)\n  - [Patreon](#patreon)\n- [Table of Contents](#table-of-contents)\n- [Install](#install)\n  - [npm](#npm)\n  - [Yarn](#yarn)\n  - [pnpm](#pnpm)\n  - [Run once with npx](#run-once-with-npx)\n  - [Peer Dependencies](#peer-dependencies)\n- [Engine](#engine)\n- [File extension handling](#file-extension-handling)\n- [Import Attributes handling](#import-attributes-handling)\n- [Usage](#usage)\n  - [CLI usage](#cli-usage)\n  - [API Usage](#api-usage)\n    - [API options](#api-options)\n  - [Usage with TypeScript's Compiler APIs](#usage-with-typescripts-compiler-apis)\n  - [Usage with Rollup](#usage-with-rollup)\n    - [Usage with @rollup/plugin-typescript](#usage-with-rollupplugin-typescript)\n  - [Usage with Webpack](#usage-with-webpack)\n    - [Usage with awesome-typescript-loader](#usage-with-awesome-typescript-loader)\n    - [Usage with ts-loader](#usage-with-ts-loader)\n  - [Custom Transformer Options](#custom-transformer-options)\n- [Contributing](#contributing)\n- [FAQ](#faq)\n  - [Is conditional require(...) syntax converted into dynamic imports?](#is-conditional-require-syntax-converted-into-dynamic-imports)\n- [License](#license)\n\n\u003c!-- SHADOW_SECTION_TOC_END --\u003e\n\n\u003c!-- SHADOW_SECTION_INSTALL_START --\u003e\n\n## Install\n\n### npm\n\n```\n$ npm install cjstoesm\n```\n\n### Yarn\n\n```\n$ yarn add cjstoesm\n```\n\n### pnpm\n\n```\n$ pnpm add cjstoesm\n```\n\n### Run once with npx\n\nFirst, add the peer dependency `typescript` as a dependency to the package from which you're going to run `cjstoesm`. Alternatively, if you want to run it from _anywhere_, you can also install it globally: `npm i -g typescript`. Now, you can simply run:\n\n```\n$ npx cjstoesm\n```\n\nYou can also run `cjstoesm` along with its peer dependencies in one combined command:\n\n```\n$ npx -p typescript -p cjstoesm cjstoesm\n```\n\n### Peer Dependencies\n\n`cjstoesm` depends on `typescript`, so you need to manually install this as well.\n\n\u003c!-- SHADOW_SECTION_INSTALL_END --\u003e\n\n## Engine\n\n`cjstoesm` requires Node.js v14.19.0 or newer to function correctly.\n\n## File extension handling\n\nThe default behavior is to add file extensions to module specifiers to align with the implementation in [node.js](https://nodejs.org/dist/latest/docs/api/esm.html#esm_mandatory_file_extensions) and across browsers.\n\nYou can customize this with the `--preserve-module-specifiers` command line option, or with the `preserveModuleSpecifiers` API option. See the [API Options](#api-options) for documentation for the possible values you can pass to it.\n\n## Import Attributes handling\n\nThe default behavior is to add Import Attributes to Import Declarations when necessary and relevant, such as for when referencing JSON files. This aligns with the implementation in [node.js](https://nodejs.org/dist/latest/docs/api/esm.html#import-attributes) and across browsers.\n\nYou can customize this with the `--import-attributes` command line option, or with the `importAttributes` API option. See the [API Options](#api-options) for documentation for the possible values you can pass to it.\n\n\u003c!-- SHADOW_SECTION_USAGE_START --\u003e\n\n## Usage\n\n\u003c!-- SHADOW_SECTION_USAGE_END --\u003e\n\n`cjstoesm` can be used in a variety of ways. The most straightforward usage is directly from the CLI:\n\n### CLI usage\n\nYou can use this library as a CLI to convert your project files from using CommonJS to using ESM.\n\nThe following command transforms all files matched by the glob `**/*.*` and overwrites them in-place:\n\n```\ncjstoesm \"**/*.*\"\n```\n\nYou can also just pass in a folder name, in which case all direct descendents of that folder will be transformed and overwritten:\n\n```\ncjstoesm \"src\"\n```\n\nYou can also pass in a second argument, `outDir`, to avoid overwriting the source files. The following command transforms all files matched by the glob `**/*.*` and emits them to the folder `dist` from the current working directory:\n\n```\ncjstoesm \"**/*.*\" dist\n```\n\nHere's an overview of the options that can be passed via the CLI:\n\n```\n$ cjstoesm --help\nUsage: cjstoesm [options] \u003cinput\u003e \u003coutDir\u003e\n\nTransforms CJS to ESM modules based on the input glob\n\nOptions:\n  -d, --debug [arg]                       Whether to print debug information\n  -v, --verbose [arg]                     Whether to print verbose information\n  -s, --silent [arg]                      Whether to not print anything\n  -c, --cwd [arg]                         Optionally which directory to use as the current working directory\n  -p, --preserve-module-specifiers [arg]  Determines whether or not module specifiers are preserved. Possible values are: \"external\", \"internal\", \"always\", and \"never\" (default: \"external\")\n  -a, --import-attributes [arg]           Determines whether or not Import Attributes are included where they are relevant. Possible values are: true and false (default: true)\n  -m, --dry [arg]                         If true, no files will be written to disk\n  -h, --help                              display help for command\n```\n\n### API Usage\n\nYou can also use this library programmatically:\n\n```ts\nimport {transform} from \"cjstoesm\";\n\nawait transform({\n  input: \"src/**/*.*\",\n  outDir: \"dist\"\n});\n```\n\nAlternatively, if you don't want the transform function to automatically write files to disk, you can pass `write: false` as an option and handle\nit yourself:\n\n```ts\nimport {transform} from \"cjstoesm\";\nimport {writeFileSync} from \"fs\";\n\nconst result = await transform({\n  input: \"src/**/*.*\",\n  write: false\n});\n\n// Write to disk\nfor (const {fileName, text} of result.files) {\n  writeFileSync(fileName, text);\n}\n```\n\n#### API options\n\n```typescript\ninterface TransformOptions {\n  /**\n   * The input glob(s) to match against the file system\n   */\n  input: string[] | string;\n  /**\n   * Optionally, the output directory to use. Defaults to inheriting that of the matched input files`\n   */\n  outDir?: string;\n  /**\n   * If write is false, no files will be written to disk\n   */\n  write: boolean;\n  /**\n   * The FileSystem to use. Useful if you want to work with a virtual file system. Defaults to using the \"fs\" module\n   */\n  fileSystem: FileSystem;\n  /**\n   * A logger that can print messages of varying severity depending on the log level\n   */\n  logger: Loggable;\n  /**\n   * The base directory (defaults to process.cwd())\n   */\n  cwd: string;\n  /**\n   * Determines how module specifiers are treated.\n   * - external (default): CommonJS module specifiers identifying libraries or built-in modules are preserved (default)\n   * - internal: CommonJS module specifiers identifying anything else than libraries or built-in modules are preserved\n   * - always: CommonJS module specifiers are never transformed.\n   * - never: CommonJS module specifiers are always transformed\n   * It can also take a function that is invoked with a module specifier and returns a boolean determining whether or not it should be preserved\n   */\n  preserveModuleSpecifiers:\n    | \"always\"\n    | \"never\"\n    | \"external\"\n    | \"internal\"\n    | ((specifier: string) =\u003e boolean);\n\n  /**\n   * Determines whether or not to include import attributes when converting require() calls referencing JSON files to ESM.\n   * - true (default): Import attributes will always be added when relevant.\n   * - false: Import attributes will never be added.\n   * It can also take a function that is invoked with a module specifier and returns a boolean determining whether or not import attributes should be added\n   */\n  importAttributes: boolean | ((specifier: string) =\u003e boolean);\n\n  /**\n   * If given, a specific TypeScript version to use\n   */\n  typescript: typeof TS;\n  /**\n   * If true, debug information will be printed. If a function is provided, it will be invoked for each file name. Returning true from the function\n   * determines that debug information will be printed related to that file\n   */\n  debug: boolean | string | ((file: string) =\u003e boolean);\n}\n```\n\n### Usage with TypeScript's Compiler APIs\n\n`cjstoesm` also provides its functionality as a [Custom Transformer](https://github.com/Microsoft/TypeScript/pull/13940) for Typescript.\nThis makes it possible for you to use it directly with TypeScript's Compiler APIs. **It works completely fine on JavaScript files, so long as you enable `allowJs` in your [CompilerOptions](https://www.typescriptlang.org/docs/handbook/compiler-options.html)**.\n\nThe simplest way of transpiling with Typescript would be with `transpileModule`:\n\n```typescript\nimport {ModuleKind, transpileModule} from \"typescript\";\nimport {cjsToEsm} from \"cjstoesm\";\n\nconst result = transpileModule(`const {join} = require(\"path\");`, {\n  transformers: cjsToEsm(),\n  compilerOptions: {\n    module: ModuleKind.ESNext\n  }\n});\n\n// 'import { join } from \"path\"' is printed to the console\nconsole.log(result.outputText);\n```\n\nYou may use this is conjunction with other Custom Transformers by importing `commonJsToEsmTransformerFactory` instead:\n\n```typescript\nimport {ModuleKind, transpileModule} from \"typescript\";\nimport {cjsToEsmTransformerFactory} from \"cjstoesm\";\n\ntranspileModule(`const {join} = require(\"path\");`, {\n  transformers: {\n    before: [cjsToEsmTransformerFactory(), someOtherTransformerFactory()],\n    after: [\n      // ...\n    ],\n    afterDeclarations: [\n      // ...\n    ]\n  },\n  compilerOptions: {\n    module: ModuleKind.ESNext\n  }\n});\n```\n\nYou can also use Custom Transformers with entire Typescript _Programs_:\n\n```typescript\nimport {getDefaultCompilerOptions, createProgram, createCompilerHost} from \"typescript\";\nimport {cjsToEsm} from \"cjstoesm\";\n\nconst options = getDefaultCompilerOptions();\nconst program = createProgram({\n  options,\n  rootNames: [\"my-file.js\", \"my-other-file.ts\"],\n  host: createCompilerHost(options)\n});\nprogram.emit(undefined, undefined, undefined, undefined, cjsToEsm());\n```\n\n### Usage with Rollup\n\nThere are a few TypeScript plugins for Rollup that support Custom Transformers, and this tool can be easily integrated with them.\n\n#### Usage with @rollup/plugin-typescript\n\nHere's how you may integrate this tool with [@rollup/plugin-typescript](https://www.npmjs.com/package/@rollup/plugin-typescript):\n\n```typescript\nimport ts from \"@rollup/plugin-typescript\";\nimport {cjsToEsm} from \"cjstoesm\";\n\nexport default {\n  input: \"...\",\n  output: [\n    /* ... */\n  ],\n  plugins: [\n    ts({\n      transformers: program =\u003e cjsToEsm({program})\n    })\n  ]\n};\n```\n\n### Usage with Webpack\n\nThere are two popular TypeScript loaders for Webpack that support Custom Transformers:\n\n- [awesome-typescript-loader](https://github.com/s-panferov/awesome-typescript-loader)\n- [ts-loader](https://github.com/TypeStrong/ts-loader)\n\n#### Usage with awesome-typescript-loader\n\n```typescript\nimport {cjsToEsm} from \"cjstoesm\";\nconst config = {\n  // ...\n  module: {\n    rules: [\n      {\n        // Match .mjs, .js, .jsx, and .tsx files\n        test: /(\\.mjs)|(\\.[jt]sx?)$/,\n        loader: \"awesome-typescript-loader\",\n        options: {\n          // ...\n          getCustomTransformers: () =\u003e cjsToEsm()\n        }\n      }\n    ]\n  }\n  // ...\n};\n```\n\n#### Usage with ts-loader\n\n```typescript\nimport {cjsToEsm} from \"cjstoesm\";\nconst config = {\n  // ...\n  module: {\n    rules: [\n      {\n        // Match .mjs, .js, .jsx, and .tsx files\n        test: /(\\.mjs)|(\\.[jt]sx?)$/,\n        loader: \"ts-loader\",\n        options: {\n          // ...\n          getCustomTransformers: () =\u003e cjsToEsm\n        }\n      }\n    ]\n  }\n  // ...\n};\n```\n\n### Custom Transformer Options\n\nYou can provide options to the `cjsToEsm` Custom Transformer to configure its behavior:\n\n| Option                                  | Description                                                                                                                                                                     |\n| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `debug` _(optional)_                    | If `true`, errors will be thrown if unexpected or unhandled cases are encountered. Additionally, debugging information will be printed during transpilation.                    |\n| `fileSystem` _(optional)_               | If given, the file system to use. Useful if you are using `cjstoesm` inside a virtual file system.                                                                              |\n| `preserveModuleSpecifiers` _(optional)_ | Determines whether or not module specifiers are preserved. Possible values are: \"external\", \"internal\", \"always\", and \"never\". See [API options](#api-options) for more details |\n| `importAttributes` _(optional)_         | Determines whether or not Import Attributes are included where relevant. Possible values are: true and false. See [API options](#api-options) for more details                  |\n| `typescript` _(optional)_               | If given, the TypeScript version to use internally for all operations.                                                                                                          |\n| `cwd` _(optional)_                      | The directory to use as the current working directory.                                                                                                                          |\n\n\u003c!-- SHADOW_SECTION_CONTRIBUTING_START --\u003e\n\n## Contributing\n\nDo you want to contribute? Awesome! Please follow [these recommendations](./CONTRIBUTING.md).\n\n\u003c!-- SHADOW_SECTION_CONTRIBUTING_END --\u003e\n\n\u003c!-- SHADOW_SECTION_MAINTAINERS_START --\u003e\n\n\u003c!-- SHADOW_SECTION_MAINTAINERS_END --\u003e\n\n\u003c!-- SHADOW_SECTION_FAQ_START --\u003e\n\n## FAQ\n\n\u003c!-- SHADOW_SECTION_FAQ_END --\u003e\n\n### Is conditional require(...) syntax converted into dynamic imports?\n\nNo. For the input:\n\n```typescript\nconst result = true ? require(\"./foo\") : require(\"./bar\");\n```\n\nThe following may be the output, depending on the internal structure of the modules referenced by the `require` calls:\n\n```typescript\nimport foo from \"./foo.js\";\nimport bar from \"./bar.js\";\n\nconst result = true ? foo : bar;\n```\n\nCommonJS `require()` syntax are _Expressions_, whereas ESM `import/export` syntax are _Declarations_, and to achieve the same expressiveness with ESM, dynamic imports are required.\nHowever, these return `Promises` and as such cannot be transformed equivalently.\n\n\u003c!-- SHADOW_SECTION_LICENSE_START --\u003e\n\n## License\n\nMIT ©\n\n\u003c!-- SHADOW_SECTION_LICENSE_END --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwessberg%2Fcjstoesm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwessberg%2Fcjstoesm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwessberg%2Fcjstoesm/lists"}