Last active
August 25, 2017 16:22
-
-
Save leon-yum/d4ad643f0b0a2e433e338ca8023d7b6d to your computer and use it in GitHub Desktop.
Babel Webpack config with dev and prod env settings using npm_lifecycle_event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import webpack from 'webpack' | |
import path from 'path' | |
import HtmlWebpackPlugin from 'html-webpack-plugin' | |
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({ | |
template: __dirname + '/app/index.html', | |
filename: 'index.html', | |
inject: 'body' | |
}) | |
const PATHS = { | |
app: path.join(__dirname, 'app'), | |
build: path.join(__dirname, 'dist'), | |
} | |
const LAUNCH_COMMAND = process.env.npm_lifecycle_event | |
const isProduction = LAUNCH_COMMAND === 'production' | |
const productionPlugin = new webpack.DefinePlugin({ | |
'process.env': { | |
NODE_ENV: JSON.stringify('production') | |
} | |
}) | |
const base = { | |
entry: [ | |
PATHS.app | |
], | |
output: { | |
path: PATHS.build, | |
filename: 'index_bundle.js' | |
}, | |
module: { | |
loaders: [ | |
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' }, | |
{ test: /\.css$/, loader: 'style-loader!css-loader' } | |
] | |
} | |
} | |
const developmentConfig = { | |
devtool: 'cheap-module-inline-source-map', | |
plugins: [HtmlWebpackPluginConfig] | |
} | |
const productionConfig = { | |
devtool: 'cheap-module-source-map', | |
plugins: [HtmlWebpackPluginConfig, productionPlugin] | |
} | |
console.log('LAUNCH_COMMAND npm run', LAUNCH_COMMAND) | |
export default Object.assign({}, base, | |
isProduction === true ? productionConfig : developmentConfig | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More details about
npm_lifecycle_event
https://medium.com/@brianhan/use-this-npm-variable-as-a-flag-for-your-build-scripts-31069f5e2e57