webpack.config.js 6.15 KB
Newer Older
Johannes Edmeier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/*
 * Copyright 2014-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';
const {join, resolve} = require('path');
const webpack = require('webpack');

const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

const extractCSS = new ExtractTextPlugin({
  filename: 'assets/css/[name].css',
  allChunks: true
});
const extractSASS = new ExtractTextPlugin({
  filename: 'assets/css/[name].css',
  allChunks: true
});

const config = {
  entry: {'sba-core': ['babel-polyfill', './src/main/frontend/index.js']},
  output: {
    path: resolve(__dirname, './target/dist'),
    filename: 'assets/js/[name].js',
    chunkFilename: 'assets/js/[name].js',
    publicPath: ''
  },
  resolve: {
    extensions: ['.js', '.vue'],
    alias: {
      '@': join(__dirname, 'src/main/frontend'),
      'root': join(__dirname, 'node_modules')
      //uncomment to use vue with html compiler:
      // 'vue$': 'vue/dist/vue.esm.js'
    }
  },
  node: {
    fs: 'empty'
  },
  module: {
    rules: [
60 61
      {
        test: /\.(js|vue)$/,
62 63 64 65 66 67
        use: [{
          loader: 'eslint-loader',
          options: {
            cache: true
          }
        }],
68 69
        enforce: 'pre'
      },
Johannes Edmeier committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            js: 'babel-loader',
            scss: ['css-hot-loader'].concat(
              ExtractTextPlugin.extract({
                use: [
                  {loader: 'css-loader'},
                  {loader: 'postcss-loader'},
                  {
                    loader: 'sass-loader', options: {includePaths: [join(__dirname, 'node_modules')]}
                  }],
                fallback: 'style-loader'
              }))
          },
          transformToRequire: {
            img: 'src'
          }
        }
      },
      {
        test: /\.js$/,
94
        use: ['babel-loader'],
Johannes Edmeier committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        include: [
          resolve(__dirname, 'src/main/frontend'),
          resolve(__dirname, 'node_modules/pretty-bytes')
        ]
      },
      {
        test: /\.css$/,
        use: ['css-hot-loader'].concat(
          ExtractTextPlugin.extract({
            use: ['css-loader', 'postcss-loader'],
            fallback: 'style-loader'
          }))
      },
      {
        test: /\.scss$/,
        use: ['css-hot-loader'].concat(
          ExtractTextPlugin.extract({
            use: [
              {loader: 'css-loader'},
              {loader: 'postcss-loader'},
              {loader: 'sass-loader', options: {includePaths: [join(__dirname, 'node_modules')]}}],
            fallback: 'style-loader'
          }))
      },
      {
        test: /\.html$/,
        use: [{
          loader: 'html-loader',
          options: {
            root: resolve(__dirname, 'src/main/frontend'),
Johannes Edmeier committed
125
            attrs: []
Johannes Edmeier committed
126 127 128 129 130
          }
        }]
      },
      {
        test: /\.svg$/,
Johannes Edmeier committed
131 132 133
        oneOf: [
          {
            issuer: /\.vue$/,
134 135 136 137 138 139 140 141 142 143
            use: [{
              loader: 'vue-svg-loader',
              options: {
                svgo: {
                  plugins: [
                    {inlineStyles: false}
                  ]
                }
              },
            }]
Johannes Edmeier committed
144 145
          },
          {
146 147 148 149 150 151 152
            use: [{
              loader: 'url-loader',
              options: {
                limit: 1000,
                name: 'assets/img/[name].[ext]'
              }
            }]
Johannes Edmeier committed
153 154
          }
        ]
Johannes Edmeier committed
155 156
      },
      {
Johannes Edmeier committed
157
        test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2)(\?.+)?$/,
158 159 160 161 162 163 164
        use: [{
          loader: 'url-loader',
          options: {
            limit: 1000,
            name: 'assets/img/[name].[ext]'
          }
        }]
Johannes Edmeier committed
165 166 167 168
      }
    ]
  },
  plugins: [
Johannes Edmeier committed
169
    new CleanWebpackPlugin(['target/dist'], {
Johannes Edmeier committed
170 171 172 173
      root: __dirname,
      verbose: true,
      dry: false,
    }),
Johannes Edmeier committed
174 175 176
    new webpack.DefinePlugin({
      '__PROJECT_VERSION__': `'${process.env.PROJECT_VERSION || ''}'`
    }),
Johannes Edmeier committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    new webpack.NamedModulesPlugin(),
    new LodashModuleReplacementPlugin(),
//    new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/),
    new webpack.optimize.ModuleConcatenationPlugin(),
    new CommonsChunkPlugin({
      name: 'vendors',
      minChunks: (module) => module.resource && /node_modules/.test(module.resource)
    }),
    extractSASS,
    extractCSS,
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './src/main/frontend/index.html',
      inject: false,
      hash: process.env.NODE_ENV === 'production',
    }),
193 194 195 196 197 198
    new HtmlWebpackPlugin({
      filename: 'login.html',
      template: './src/main/frontend/login.html',
      inject: false,
      hash: process.env.NODE_ENV === 'production',
    }),
Johannes Edmeier committed
199 200 201
    new BundleAnalyzerPlugin({
      analyzerMode: 'static',
      openAnalyzer: false,
202
      reportFilename: '../report.html'
Johannes Edmeier committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    })
  ],
  devtool: '#eval-source-map'
};

if (process.env.NODE_ENV === 'production') {
  config.devtool = '#source-map';
  // http://vue-loader.vuejs.org/en/workflow/production.html
  config.plugins = config.plugins.concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      },
      sourceMap: true
    }),
    new OptimizeCSSPlugin()
  ])
}

Johannes Edmeier committed
227
module.exports = config;