{"id":22423376,"url":"https://github.com/prantlf/requirejs-esm","last_synced_at":"2026-03-11T15:03:32.869Z","repository":{"id":57307187,"uuid":"458816116","full_name":"prantlf/requirejs-esm","owner":"prantlf","description":"A RequireJS plugin converting JavaScript modules from ESM to AMD.","archived":false,"fork":false,"pushed_at":"2025-11-09T14:14:12.000Z","size":328,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-09T16:10:21.425Z","etag":null,"topics":["amd","es6","esm","plugin","requirejs","requirejs-plugin"],"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/prantlf.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-02-13T13:23:27.000Z","updated_at":"2025-11-09T14:14:15.000Z","dependencies_parsed_at":"2024-12-05T18:11:02.747Z","dependency_job_id":"9ed2620c-2d71-48a8-81c9-4101c3ad0fd2","html_url":"https://github.com/prantlf/requirejs-esm","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"purl":"pkg:github/prantlf/requirejs-esm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Frequirejs-esm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Frequirejs-esm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Frequirejs-esm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Frequirejs-esm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prantlf","download_url":"https://codeload.github.com/prantlf/requirejs-esm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Frequirejs-esm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30385036,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-11T14:10:17.325Z","status":"ssl_error","status_checked_at":"2026-03-11T14:09:37.934Z","response_time":84,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["amd","es6","esm","plugin","requirejs","requirejs-plugin"],"created_at":"2024-12-05T18:10:50.203Z","updated_at":"2026-03-11T15:03:32.863Z","avatar_url":"https://github.com/prantlf.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ESM to AMD Plugin for RequireJS\n\n[![Latest version](https://img.shields.io/npm/v/requirejs-esm)\n ![Dependency status](https://img.shields.io/librariesio/release/npm/requirejs-esm)\n](https://www.npmjs.com/package/requirejs-esm)\n\nA [RequireJS] plugin converting JavaScript modules from ESM to AMD. It takes care only of the module format; it does not transpile the language and that is why it is [a lot faster] than plugins using [Babel]. If you need to transpile the code to an earlier ECMAScript version, have a look at [requirejs-babel7].\n\nThe official [RequireJS optimizer] (`r.js`) does not wire up source maps from the original (not transpiled) sources to the source map of the output bundle. It makes this or similar plugins unfeasible for serious work. If you want the proper support for source maps, replace the official optimizer package ([`requirejs`]) with the forked [`@prantlf/requirejs`], which is fixed.\n\nAn alternative to this plugin is a preprocessor, which converts the module format before RequireJS consumes it. It is a lot less intrusive solution, but it requires a pluggable development web server, so that a plugin (compatible with [connect middleware]) can be registered in it. See [requirejs-esm-preprocessor] for more information.\n\n## Installation\n\nThis module can be installed in your project using [NPM], [PNPM] or [Yarn]. Make sure, that you use [Node.js] version 14 or newer.\n\n```sh\nnpm i -D requirejs-esm\npnpm i -D requirejs-esm\nyarn add requirejs-esm\n```\n\n## Usage\n\nAdd the following paths to the RequireJS configuration:\n\n```javascript\npaths: {\n  esm: 'node_modules/requirejs-esm/dist/plugin'\n}\n```\n\nReference ESM source files files via the `esm!` plugin prefix:\n\n```javascript\ndefine(['esm!your-esm-module'], function (module) {\n  // ...\n})\n```\n\nYou can use the ESM module format in modules loaded by the `esm!` plugin including the keyword `import` for loading nested dependencies. The plugin `esm!` has to be used only in the topmost `require` or `define` statement.\n\nThis plugin transpiles only ESM source files. If it detects a statement calling functions `define`, `require` or `require.config` on the root level of the source file, it will return the text of the source file as-is. Source files, which are already AMD modules, are assumed to contain ES5 only.\n\nIf you use the RequireJS optimizer `r.js`, you have to bundle the `esm` plugin without the compiling functionality by adding the following to the RequireJS build configuration:\n\n```js\npragmasOnSave: {\n  excludeEsm: true // removes the transpiling code from esm.js\n}\n```\n\nSee also a [demo-local] project, which includes sources only from the local `src` directory:\n\n```sh\nnpm start\nopen http://localhost:8967/demo-local/normal.html\nopen http://localhost:8967/demo-local/optimized.html\n```\n\nSee also a [demo-extern] project, which includes sources from the local `src` directory and from `node_modules` outside of it:\n\n```sh\nnpm run start\nopen http://localhost:8967/demo-extern/normal.html\nopen http://localhost:8967/demo-extern/optimized.html\n```\n\n## Advanced\n\nYou can customize the [default module name resolution] with the `resolvePath` key (see [resolvePath] for more information) to transpile only modules with a special file extension:\n\n```js\n// import * from 'es5module.js'  -\u003e define(['es5module])\n// import * from 'es6module.mjs' -\u003e define(['esm!es6module])\nfileExtension: '.mjs',\nresolvePath: function (sourcePath, currentFile, options, originalResolvePath) {\n  // Ignore paths with other plugins applied and the three built-in\n  // pseudo-modules of RequireJS.\n  if (sourcePath.includes('!') || sourcePath === 'require' ||\n      sourcePath === 'module' || sourcePath === 'exports') return\n\n  let lengthWithoutExtension = sourcePath.length - 3\n  if (sourcePath.lastIndexOf('.js') === lengthWithoutExtension) {\n    return sourcePath.substr(0, lengthWithoutExtension)\n  }\n  --lengthWithoutExtension\n  if (sourcePath.lastIndexOf('.mjs') === lengthWithoutExtension) {\n    return 'esm!' + sourcePath.substr(0, lengthWithoutExtension)\n  }\n}\n```\n\nThe default implementation of `resolvePath` ensures that every JavaScript dependency will be converted:\n\n```js\nfunction (sourcePath) {\n  if (sourcePath.includes('!') || sourcePath === 'require' ||\n      sourcePath === 'module' || sourcePath === 'exports') return\n\n  return 'esm!' + sourcePath\n}\n```\n\n## Options\n\nThe `esm` plugin supports configuration with the following defaults:\n\n```js\n{\n  esm: {\n    // Update paths of module dependencies.\n    resolvePath: func, // see above\n    // Allow using a different plugin alias than `esm` in the source code.\n    pluginName: 'esm',\n    // The file extension of source files to be transformed.\n    fileExtension: '.js',\n    // Skip modules already in the AMD format without trying to parse them.\n    // Module prefixes like \"lib/vendor/\" are accepted too. Skipped modules\n    // must have all their deep dependencies already transformed.\n    skipModules: [],\n    // Enforce transpiling even if a optimized module has been loaded.\n    mixedAmdAndEsm: false,\n    // Suppress transpiling even if an optimized module has not been loaded yet.\n    onlyAmd: false,\n    // Do not insert `\"use strict\"` expression to the AMD modules. You'd set it\n    // to `false` if your bundler inserts `\"use strict\"` to the outer scope.\n    useStrict: true,\n    // Enable source maps, can be an object with booleans { inline, content }.\n    // If set to true, the object will be set to { inline: true, content: true }.\n    sourceMap: false,\n    // Enable console logging.\n    verbose: false,\n    // Save a copy of the transformed modules to a directory for debugging purposes.\n    debugDir: ''\n  }\n}\n```\n\n## API\n\nThe transformation applied by the plugin can be performed programmatically too.\n\n```js\nconst { transform } = require('requirejs-esm/dist/api')\nconst { code }  = transform('import a from \"a\"', 'test', { sourceMap: true })\n```\n\nThe `transform` method supports a subset of plugin options:\n\n```js\n{\n  // Update paths of module dependencies.\n  resolvePath: func,\n  // Allow using a different plugin alias than `esm` in the source code.\n  pluginName: 'esm',\n  // Do not insert `\"use strict\"` expression to the AMD code output. You'd set\n  // it to `false` if your bundler inserts `\"use strict\"` to the outer scope.\n  useStrict: true,\n  // Enable source maps, can be an object with booleans { inline, content }.\n  // If set to true, the object will be set to { inline: true, content: true }.\n  sourceMap: false,\n  // ESM transformation callbacks.\n  onBeforeTransform,\n  onAfterTransform,\n  // AMD update callbacks.\n  onBeforeUpdate,\n  onAfterUpdate\n}\n```\n\nThe returned object:\n\n```js\n{\n  // The transpiled module code.\n  code: '...',\n  // The source map if sourceMap.inline from the options above is false.\n  map: undefined | { ... },\n  // If the transpilation modified the original source text.\n  updated: false | true\n}\n```\n\n## Contributing\n\nIn lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.\n\n## License\n\nCopyright (c) 2022-2025 Ferdinand Prantl\n\nLicensed under the MIT license.\n\n[Babel]: https://babeljs.io/\n[RequireJS]: http://requirejs.org\n[RequireJS optimizer]: https://requirejs.org/docs/optimization.html\n[requirejs-babel7]: https://www.npmjs.com/package/requirejs-babel7\n[requirejs-esm-preprocessor]: https://www.npmjs.com/package/requirejs-esm-preprocessor\n[connect middleware]: https://github.com/senchalabs/connect/wiki\n[`requirejs`]: https://www.npmjs.com/package/requirejs\n[`@prantlf/requirejs`]: https://www.npmjs.com/package/@prantlf/requirejs\n[Node.js]: http://nodejs.org/\n[NPM]: https://www.npmjs.com/\n[PNPM]: https://pnpm.io/\n[Yarn]: https://yarnpkg.com/\n[demo-local]: https://github.com/prantlf/requirejs-esm/tree/master/demo-local\n[demo-extern]: https://github.com/prantlf/requirejs-esm/tree/master/demo-extern\n[default module name resolution]: https://github.com/prantlf/requirejs-esm/blob/master/src/resolve-path.js#L48\n[resolvePath]: https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#resolvepath\n[a lot faster]: ./perf/README.md#readme\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprantlf%2Frequirejs-esm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprantlf%2Frequirejs-esm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprantlf%2Frequirejs-esm/lists"}