{"id":19401780,"url":"https://github.com/webpack-contrib/exports-loader","last_synced_at":"2025-05-14T07:08:30.891Z","repository":{"id":5444417,"uuid":"6637462","full_name":"webpack-contrib/exports-loader","owner":"webpack-contrib","description":"Exports Loader","archived":false,"fork":false,"pushed_at":"2025-01-21T17:28:45.000Z","size":2044,"stargazers_count":217,"open_issues_count":1,"forks_count":37,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-05-08T00:06:28.171Z","etag":null,"topics":["webpack-loader"],"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/webpack-contrib.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"webpack"}},"created_at":"2012-11-11T10:38:10.000Z","updated_at":"2025-01-21T17:28:50.000Z","dependencies_parsed_at":"2024-02-03T10:22:40.886Z","dependency_job_id":"d78b62cb-275e-4af6-9495-0261910ce912","html_url":"https://github.com/webpack-contrib/exports-loader","commit_stats":{"total_commits":118,"total_committers":26,"mean_commits":4.538461538461538,"dds":0.6440677966101696,"last_synced_commit":"81c076b2a5b5fffcf87143f862a7013577292e9a"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fexports-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fexports-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fexports-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fexports-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webpack-contrib","download_url":"https://codeload.github.com/webpack-contrib/exports-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254092776,"owners_count":22013290,"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":["webpack-loader"],"created_at":"2024-11-10T11:19:45.573Z","updated_at":"2025-05-14T07:08:30.864Z","avatar_url":"https://github.com/webpack-contrib.png","language":"JavaScript","funding_links":["https://opencollective.com/webpack"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/webpack/webpack\"\u003e\n    \u003cimg width=\"200\" height=\"200\" src=\"https://webpack.js.org/assets/icon-square-big.svg\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n[![npm][npm]][npm-url]\n[![node][node]][node-url]\n[![tests][tests]][tests-url]\n[![coverage][cover]][cover-url]\n[![discussion][discussion]][discussion-url]\n[![size][size]][size-url]\n\n# exports-loader\n\nAllow to setup exports `module.exports`/`export` for source files.\n\nUseful when a source file does not contain exports or something does not export.\n\nFor further hints on compatibility issues, check out [Shimming](https://webpack.js.org/guides/shimming/) of the official docs.\n\n\u003e [!WARNING]\n\u003e\n\u003e By default loader generate ES module named syntax.\n\n\u003e [!WARNING]\n\u003e\n\u003e Be careful, existing exports (`export`/`module.exports`/`exports`) in the original code and exporting new values can cause a failure.\n\n## Getting Started\n\nTo begin, you'll need to install `exports-loader`:\n\n```console\nnpm install exports-loader --save-dev\n```\n\nor\n\n```console\nyarn add -D exports-loader\n```\n\nor\n\n```console\npnpm add -D exports-loader\n```\n\n### Inline\n\nThe `|` or `%20` (space) allow to separate the `syntax`, `name` and `alias` of export.\nThe documentation and syntax examples can be read [here](#syntax).\n\n\u003e [!WARNING]\n\u003e\n\u003e `%20` is space in a query string, because you can't use spaces in URLs\n\nThen add the loader to the desired `import` statement or `require` calls. For example:\n\n```js\nimport { myFunction } from \"exports-loader?exports=myFunction!./file.js\";\n// Adds the following code to the file's source:\n//\n// ...\n// Code\n// ...\n//\n// export { myFunction }\n\nmyFunction(\"Hello world\");\n```\n\n```js\nimport {\n  myVariable,\n  myFunction,\n} from \"exports-loader?exports=myVariable,myFunction!./file.js\";\n// Adds the following code to the file's source:\n//\n// ...\n// Code\n// ...\n//\n// export { myVariable, myFunction };\n\nconst newVariable = myVariable + \"!!!\";\n\nconsole.log(newVariable);\n\nmyFunction(\"Hello world\");\n```\n\n```js\nconst {\n  myFunction,\n} = require(\"exports-loader?type=commonjs\u0026exports=myFunction!./file.js\");\n// Adds the following code to the file's source:\n//\n// ...\n// Code\n// ...\n//\n// module.exports = { myFunction }\n\nmyFunction(\"Hello world\");\n```\n\n```js\n// Alternative syntax:\n// import myFunction from 'exports-loader?exports=default%20myFunction!./file.js';\nimport myFunction from \"exports-loader?exports=default|myFunction!./file.js\";\n// `%20` is space in a query string, equivalently `default myFunction`\n// Adds the following code to the file's source:\n//\n// ...\n// Code\n// ...\n//\n// exports default myFunction;\n\nmyFunction(\"Hello world\");\n```\n\n```js\nconst myFunction = require(\"exports-loader?type=commonjs\u0026exports=single|myFunction!./file.js\");\n// `|` is separator in a query string, equivalently `single|myFunction`\n// Adds the following code to the file's source:\n//\n// ...\n// Code\n// ...\n//\n// module.exports = myFunction;\n\nmyFunction(\"Hello world\");\n```\n\n```js\nimport { myFunctionAlias } from \"exports-loader?exports=named|myFunction|myFunctionAlias!./file.js\";\n// `|` is separator in a query string, equivalently `named|myFunction|myFunctionAlias`\n// Adds the following code to the file's source:\n//\n// ...\n// Code\n// ...\n//\n// exports { myFunction as myFunctionAlias };\n\nmyFunctionAlias(\"Hello world\");\n```\n\nDescription of string values can be found in the documentation below.\n\n### Using Configuration\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        // You can use `regexp`\n        // test: /vendor\\.js/$\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          exports: \"myFunction\",\n        },\n      },\n    ],\n  },\n};\n```\n\nAnd run `webpack` via your preferred method.\n\n## Options\n\n|           Name            |                   Type                    |   Default   | Description                 |\n| :-----------------------: | :---------------------------------------: | :---------: | :-------------------------- |\n|    **[`type`](#type)**    |                `{String}`                 |  `module`   | Format of generated exports |\n| **[`exports`](#exports)** | `{String\\|Object\\|Array\u003cString\\|Object\u003e}` | `undefined` | List of exports             |\n\n### `type`\n\nType: `String`\nDefault: `module`\n\nFormat of generated exports.\n\nPossible values - `commonjs` (CommonJS module syntax) and `module` (ES module syntax).\n\n#### `commonjs`\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          type: \"commonjs\",\n          exports: \"Foo\",\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nmodule.exports = { Foo };\n```\n\n#### `module`\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          type: \"module\",\n          exports: \"Foo\",\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nexport { Foo };\n```\n\n### `exports`\n\nType: `String|Array`\nDefault: `undefined`\n\nList of exports.\n\n#### `String`\n\nAllows to use a string to describe an export.\n\n##### `Syntax`\n\nThe `|` or `%20` (space) allow to separate the `syntax`, `name` and `alias` of export.\n\nString syntax - `[[syntax] [name] [alias]]` or `[[syntax]|[name]|[alias]]`, where:\n\n- `[syntax]` (**may be omitted**) -\n\n  - if `type` is `module`- can be `default` and `named`,\n  - if `type` is `commonjs`- can be `single` and `multiple`\n\n- `[name]` - name of an exported value (**required**)\n- `[alias]` - alias of an exported value (**may be omitted**)\n\nExamples:\n\n- `[Foo]` - generates `export { Foo };`.\n- `[default Foo]` - generates `export default Foo;`.\n- `[named Foo]` - generates `export { Foo };`.\n- `[named Foo FooA]` - generates `export { Foo as FooA };`.\n- `[single Foo]` - generates `module.exports = Foo;`.\n- `[multiple Foo]` - generates `module.exports = { Foo };`.\n- `[multiple Foo FooA]` - generates `module.exports = { 'FooA': Foo };`.\n- `[named [name] [name]Alias]` - generates ES module named exports and exports a value equal to the filename under other name., for `single.js` it will be `single` and `singleAlias`, generates `export { single as singleAlias };`.\n\n\u003e [!WARNING]\n\u003e\n\u003e You need to set `type: \"commonjs\"` to use `single` or `multiple` syntaxes.\n\n\u003e [!WARNING]\n\u003e\n\u003e Aliases can't be used together with `default` or `single` syntaxes.\n\n##### Examples\n\n###### ES Module Default Export\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          exports: \"default Foo\",\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nexport default Foo;\n```\n\n###### ES Module Named Exports\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          exports: \"named Foo FooA\",\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nexport { Foo as FooA };\n```\n\n###### CommonJS Single Export\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          type: \"commonjs\",\n          exports: \"single Foo\",\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nmodule.exports = Foo;\n```\n\n###### CommonJS Multiple Exports\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          type: \"commonjs\",\n          exports: \"multiple Foo FooA\",\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nmodule.exports = { FooA: Foo };\n```\n\n#### `Object`\n\nAllows to use an object to describe an export.\n\nProperties:\n\n- `syntax` - can be `default` or `named` for the `module` type (`ES modules` module format), and `single` or `multiple` for the `commonjs` type (`CommonJS` module format) (**may be omitted**)\n- `name` - name of an exported value (**required**)\n- `alias` - alias of an exported value (**may be omitted**)\n\n##### Examples\n\n###### ES Module Default Export\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          exports: {\n            syntax: \"default\",\n            name: \"Foo\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nexport default Foo;\n```\n\n###### ES Module Named Exports\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          exports: {\n            syntax: \"named\",\n            name: \"Foo\",\n            alias: \"FooA\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nexport { Foo as FooA };\n```\n\n###### CommonJS Single Export\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          type: \"commonjs\",\n          exports: {\n            syntax: \"single\",\n            name: \"Foo\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nmodule.exports = Foo;\n```\n\n###### CommonJS Multiple Exports\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          type: \"commonjs\",\n          exports: {\n            syntax: \"multiple\",\n            name: \"Foo\",\n            alias: \"FooA\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nmodule.exports = { FooA: Foo };\n```\n\n#### `Array`\n\nAllow to specify multiple exports. Each item can be a [`string`](https://github.com/webpack-contrib/exports-loader#string) or an [`object`](https://github.com/webpack-contrib/exports-loader#object).\n\n\u003e [!WARNING]\n\u003e\n\u003e Not possible to use `single` and `multiple` syntaxes together due to CommonJS format limitations.\n\n\u003e [!WARNING]\n\u003e\n\u003e Not possible to use multiple `default` values due to ES module format limitations.\n\n\u003e [!WARNING]\n\u003e\n\u003e Not possible to use multiple `single` values due to CommonJS format limitations.\n\n##### Examples\n\n###### CommonJS Multiple Exports\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          type: \"commonjs\",\n          exports: [\"Foo\", \"multiple Bar\", \"multiple Baz BazA\"],\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nmodule.exports = { Foo, Bar, BazA: Bar };\n```\n\n###### ES Module Default Export And Named Exports Together\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          exports: [\"default Foo\", \"named Bar BarA\"],\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nexport default Foo;\nexport { Bar as BarA };\n```\n\n###### Named Exports\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: require.resolve(\"./path/to/vendor.js\"),\n        loader: \"exports-loader\",\n        options: {\n          exports: [\n            { syntax: \"named\", name: \"Foo\", alias: \"FooA\" },\n            { syntax: \"named\", name: \"Bar\" },\n            \"Baz\",\n          ],\n        },\n      },\n    ],\n  },\n};\n```\n\nGenerate output:\n\n```js\n// ...\n// Code\n// ...\n\nexport { Foo as FooA, Bar, Baz };\n```\n\n## Contributing\n\nPlease take a moment to read our contributing guidelines if you haven't yet done so.\n\n[CONTRIBUTING](./.github/CONTRIBUTING.md)\n\n## License\n\n[MIT](./LICENSE)\n\n[npm]: https://img.shields.io/npm/v/exports-loader.svg\n[npm-url]: https://npmjs.com/package/exports-loader\n[node]: https://img.shields.io/node/v/exports-loader.svg\n[node-url]: https://nodejs.org\n[tests]: https://github.com/webpack-contrib/exports-loader/workflows/exports-loader/badge.svg\n[tests-url]: https://github.com/webpack-contrib/exports-loader/actions\n[cover]: https://codecov.io/gh/webpack-contrib/exports-loader/branch/master/graph/badge.svg\n[cover-url]: https://codecov.io/gh/webpack-contrib/exports-loader\n[discussion]: https://img.shields.io/github/discussions/webpack/webpack\n[discussion-url]: https://github.com/webpack/webpack/discussions\n[size]: https://packagephobia.now.sh/badge?p=exports-loader\n[size-url]: https://packagephobia.now.sh/result?p=exports-loader\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-contrib%2Fexports-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebpack-contrib%2Fexports-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-contrib%2Fexports-loader/lists"}