Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/add-module-exports-webpack-plugin
Add `module.exports` for Babel and TypeScript compiled code
https://github.com/sindresorhus/add-module-exports-webpack-plugin
javascript nodejs npm-package webpack webpack-plugin
Last synced: about 2 months ago
JSON representation
Add `module.exports` for Babel and TypeScript compiled code
- Host: GitHub
- URL: https://github.com/sindresorhus/add-module-exports-webpack-plugin
- Owner: sindresorhus
- License: mit
- Archived: true
- Created: 2017-12-07T12:18:20.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2024-04-30T12:04:34.000Z (7 months ago)
- Last Synced: 2024-05-22T18:06:07.800Z (6 months ago)
- Topics: javascript, nodejs, npm-package, webpack, webpack-plugin
- Language: JavaScript
- Size: 10.7 KB
- Stars: 35
- Watchers: 3
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
> [!WARNING]
> Deprecated. Prefer using JavaScript Modules instead of CommonJS.# add-module-exports-webpack-plugin
> Add `module.exports` for Babel and TypeScript compiled code
When you use ES2015 modules with Babel or have a default export in TypeScript, they generate code which requires you to import it with `require('x').default` in CommonJS instead of `require('x')`. This plugin enables the latter.
## Install
```
$ npm install add-module-exports-webpack-plugin
```## Usage
```js
const AddModuleExportsPlugin = require('add-module-exports-webpack-plugin');module.exports = {
// …
output: {
filename: 'dist/index.js',
libraryTarget: 'commonjs2'
},
plugins: [
new AddModuleExportsPlugin()
]
};
```**`output.libraryTarget` must be `commonjs2`**
### Primitive default exports
When exporting a primitive value as default export, other exported values will not be exported anymore. The reason is that this module redeclares the exports as follows.
```js
module.exports.default = (arg1, arg2) => arg1 + arg2;
module.exports.subtract = (arg1, arg2) => arg1 - arg2;
```This module re-exports them as follows.
```js
module.exports = (arg1, arg2) => arg1 + arg2;
module.exports.default = (arg1, arg2) => arg1 + arg2;
module.exports.subtract = (arg1, arg2) => arg1 - arg2;
```Because you can't attach properties to a primitive value, it's not possible to re-export the other properties and so you would end up with only a `module.exports`.
## Related
- [node-env-webpack-plugin](https://github.com/sindresorhus/node-env-webpack-plugin) - Simplified `NODE_ENV` handling
- [add-asset-webpack-plugin](https://github.com/sindresorhus/add-asset-webpack-plugin) - Dynamically add an asset to the webpack graph