|
| 1 | +var path = require( 'path' ); |
| 2 | +var webpack = require( 'webpack' ); |
| 3 | +var merge = require( 'webpack-merge' ); |
| 4 | +var HtmlWebpackPlugin = require( 'html-webpack-plugin' ); |
| 5 | +var autoprefixer = require( 'autoprefixer' ); |
| 6 | +var ExtractTextPlugin = require( 'extract-text-webpack-plugin' ); |
| 7 | +var CopyWebpackPlugin = require( 'copy-webpack-plugin' ); |
| 8 | + |
| 9 | +console.log( 'WEBPACK GO!'); |
| 10 | + |
| 11 | +// detemine build env |
| 12 | +var TARGET_ENV = process.env.npm_lifecycle_event === 'build' ? 'production' : 'development'; |
| 13 | + |
| 14 | +// common webpack config |
| 15 | +var commonConfig = { |
| 16 | + |
| 17 | + output: { |
| 18 | + path: path.resolve( __dirname, 'dist/' ), |
| 19 | + filename: '[hash].js', |
| 20 | + }, |
| 21 | + |
| 22 | + resolve: { |
| 23 | + modulesDirectories: ['node_modules'], |
| 24 | + extensions: ['', '.js', '.elm'] |
| 25 | + }, |
| 26 | + |
| 27 | + module: { |
| 28 | + noParse: /\.elm$/, |
| 29 | + loaders: [ |
| 30 | + { |
| 31 | + test: /\.(eot|ttf|woff|woff2|svg)$/, |
| 32 | + loader: 'file-loader' |
| 33 | + } |
| 34 | + ] |
| 35 | + }, |
| 36 | + |
| 37 | + plugins: [ |
| 38 | + new HtmlWebpackPlugin({ |
| 39 | + template: 'src/static/index.html', |
| 40 | + inject: 'body', |
| 41 | + filename: 'index.html' |
| 42 | + }) |
| 43 | + ], |
| 44 | + |
| 45 | + postcss: [ autoprefixer( { browsers: ['last 2 versions'] } ) ], |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +// additional webpack settings for local env (when invoked by 'npm start') |
| 50 | +if ( TARGET_ENV === 'development' ) { |
| 51 | + console.log( 'Serving locally...'); |
| 52 | + |
| 53 | + module.exports = merge( commonConfig, { |
| 54 | + |
| 55 | + entry: [ |
| 56 | + 'webpack-dev-server/client?http://localhost:8080', |
| 57 | + path.join( __dirname, 'src/static/index.js' ) |
| 58 | + ], |
| 59 | + |
| 60 | + devServer: { |
| 61 | + inline: true, |
| 62 | + progress: true |
| 63 | + }, |
| 64 | + |
| 65 | + module: { |
| 66 | + loaders: [ |
| 67 | + { |
| 68 | + test: /\.elm$/, |
| 69 | + exclude: [/elm-stuff/, /node_modules/], |
| 70 | + loader: 'elm-hot!elm-webpack?verbose=true&warn=true&debug=true' |
| 71 | + }, |
| 72 | + { |
| 73 | + test: /\.(css|scss)$/, |
| 74 | + loaders: [ |
| 75 | + 'style-loader', |
| 76 | + 'css-loader', |
| 77 | + 'postcss-loader', |
| 78 | + 'sass-loader' |
| 79 | + ] |
| 80 | + } |
| 81 | + ] |
| 82 | + } |
| 83 | + |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +// additional webpack settings for prod env (when invoked via 'npm run build') |
| 88 | +if ( TARGET_ENV === 'production' ) { |
| 89 | + console.log( 'Building for prod...'); |
| 90 | + |
| 91 | + module.exports = merge( commonConfig, { |
| 92 | + |
| 93 | + entry: path.join( __dirname, 'src/static/index.js' ), |
| 94 | + |
| 95 | + module: { |
| 96 | + loaders: [ |
| 97 | + { |
| 98 | + test: /\.elm$/, |
| 99 | + exclude: [/elm-stuff/, /node_modules/], |
| 100 | + loader: 'elm-webpack' |
| 101 | + }, |
| 102 | + { |
| 103 | + test: /\.(css|scss)$/, |
| 104 | + loader: ExtractTextPlugin.extract( 'style-loader', [ |
| 105 | + 'css-loader', |
| 106 | + 'postcss-loader', |
| 107 | + 'sass-loader' |
| 108 | + ]) |
| 109 | + } |
| 110 | + ] |
| 111 | + }, |
| 112 | + |
| 113 | + plugins: [ |
| 114 | + new CopyWebpackPlugin([ |
| 115 | + { |
| 116 | + from: 'src/static/img/', |
| 117 | + to: 'static/img/' |
| 118 | + }, |
| 119 | + { |
| 120 | + from: 'src/favicon.ico' |
| 121 | + }, |
| 122 | + ]), |
| 123 | + |
| 124 | + new webpack.optimize.OccurenceOrderPlugin(), |
| 125 | + |
| 126 | + // extract CSS into a separate file |
| 127 | + new ExtractTextPlugin( './[hash].css', { allChunks: true } ), |
| 128 | + |
| 129 | + // minify & mangle JS/CSS |
| 130 | + new webpack.optimize.UglifyJsPlugin({ |
| 131 | + minimize: true, |
| 132 | + compressor: { warnings: false } |
| 133 | + // mangle: true |
| 134 | + }) |
| 135 | + ] |
| 136 | + |
| 137 | + }); |
| 138 | +} |
0 commit comments