Skip to content

Instantly share code, notes, and snippets.

@leon-yum
Last active August 25, 2017 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leon-yum/d4ad643f0b0a2e433e338ca8023d7b6d to your computer and use it in GitHub Desktop.
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
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
)
@leon-yum
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment