Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/node-env-webpack-plugin
Simplified `NODE_ENV` handling with webpack
https://github.com/sindresorhus/node-env-webpack-plugin
javascript node-env nodejs npm-package webpack webpack-plugin
Last synced: 3 months ago
JSON representation
Simplified `NODE_ENV` handling with webpack
- Host: GitHub
- URL: https://github.com/sindresorhus/node-env-webpack-plugin
- Owner: sindresorhus
- License: mit
- Created: 2018-01-02T13:28:30.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2024-04-30T18:23:49.000Z (8 months ago)
- Last Synced: 2024-05-02T11:50:25.595Z (8 months ago)
- Topics: javascript, node-env, nodejs, npm-package, webpack, webpack-plugin
- Language: JavaScript
- Size: 7.81 KB
- Stars: 53
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# node-env-webpack-plugin
> Simplified `NODE_ENV` handling with [webpack](https://webpack.js.org)
## Install
```sh
npm install node-env-webpack-plugin
```## Usage
```diff
import path from 'node:path';
+ import NodeEnvPlugin from 'node-env-webpack-plugin';- const NODE_ENV = process.env.NODE_ENV || 'development';
- const isProduction = NODE_ENV === 'production';module.exports = {
entry: './source',
output: {
path: path.join(__dirname, 'distribution'),
filename: 'bundle.js'
},
- devtool: isProduction ? 'source-map' : 'cheap-module-source-map',
+ devtool: NodeEnvPlugin.devtool,
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
},
plugins: [
- new webpack.EnvironmentPlugin({
- NODE_ENV: 'development'
- }),
+ new NodeEnvPlugin()
]
};
```## API
Sets `process.env.NODE_ENV` in the Node.js process to `development` at import-time if it's not defined, or `production` if webpack is run with `webpack -p`.
### NodeEnvPlugin()
Sets `process.env.NODE_ENV` in the bundle to the same as in the Node.js process.
### NodeEnvPlugin.isProduction
For convenience and to prevent typos.
`process.env.NODE_ENV === 'production'`
### NodeEnvPlugin.isDevelopment
`process.env.NODE_ENV === 'development'`
### NodeEnvPlugin.isTest
`process.env.NODE_ENV === 'test'`
### NodeEnvPlugin.devtool
Pass this to the webpack `devtool` option. It will give you good but slow source maps in production (`source-map`) and fast source maps in development (`cheap-module-source-map`).
## Related
- [add-asset-webpack-plugin](https://github.com/sindresorhus/add-asset-webpack-plugin) - Dynamically add an asset to the webpack graph