Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0666eebed9a1c866b1d223c554e12e3bf0d6f548 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (c) 2014-2019 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     BSI Business Systems Integration AG - initial API and implementation
 */
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const AfterEmitWebpackPlugin = require('./AfterEmitWebpackPlugin');

const path = require('path');
const webpack = require('webpack');
const scoutBuildConstants = require('./constants');

module.exports = (env, args) => {
  const {devMode, outSubDir, cssFilename, jsFilename} = scoutBuildConstants.getConstantsForMode(args.mode);
  const outDir = path.resolve(scoutBuildConstants.outDir, outSubDir);
  const resDirArray = args.resDirArray || ['res'];
  console.log(`Webpack mode: ${args.mode}`);

  // # Copy static web-resources delivered by the modules
  const copyPluginConfig = [];
  for (const resDir of resDirArray) {
    copyPluginConfig.push(
      {
        from: resDir,
        to: '../res'
      });
  }

  const config = {
    target: 'web',
    mode: args.mode,
    devtool: devMode ? 'inline-module-source-map' : undefined,
    output: {
      filename: jsFilename,
      path: outDir,
      libraryTarget: 'umd',
      globalObject: 'this',
      umdNamedDefine: true
    },
    performance: {
      hints: false
    },
    stats: 'normal',
    module: {
      // LESS
      rules: [{
        test: /\.less$/,
        use: [{
          // Extracts CSS into separate files. It creates a CSS file per JS file which contains CSS.
          // It supports On-Demand-Loading of CSS and SourceMaps.
          // see: https://webpack.js.org/plugins/mini-css-extract-plugin/
          //
          // Note: this creates some useless *.js files, like dark-theme.js
          // This seems to be an issue in webpack, workaround is to remove the files later
          // see: https://github.com/webpack-contrib/mini-css-extract-plugin/issues/151
          // seems to be fixed in webpack 5, workaround to manually delete js files can be removed as soon as webpack 5 is released
          loader: MiniCssExtractPlugin.loader
        }, {
          // Interprets @import and url() like import/require() and will resolve them.
          // see: https://webpack.js.org/loaders/css-loader/
          loader: require.resolve('css-loader'),
          options: {
            sourceMap: devMode,
            modules: false, // We don't want to work with CSS modules
            url: false // Don't resolve URLs in LESS, because relative path does not match /res/fonts
          }
        }, {
          // Compiles Less to CSS.
          // see: https://webpack.js.org/loaders/less-loader/
          loader: require.resolve('less-loader'),
          options: {
            sourceMap: devMode,
            relativeUrls: false, // deprecated in future rewriteUrls is used
            rewriteUrls: 'off'
          }
        }]
      }, {
        // # Babel
        test: /\.m?js$/,
        exclude: [],
        use: {
          loader: require.resolve('babel-loader'),
          options: {
            compact: false,
            sourceMaps: devMode ? 'inline' : undefined,
            plugins: [
              require.resolve('@babel/plugin-transform-object-assign'),
              require.resolve('@babel/plugin-proposal-class-properties'),
              require.resolve('@babel/plugin-proposal-object-rest-spread')],
            presets: [
              [require.resolve('@babel/preset-env'), {
                debug: false,
                targets: {
                  firefox: '35',
                  chrome: '40',
                  ie: '11',
                  edge: '12',
                  safari: '8'
                }
              }]
            ]
          }
        }
      }]
    },
    plugins: [
      // see: extracts css into separate files
      new MiniCssExtractPlugin({
        filename: cssFilename
      }),
      // run post-build script hook
      new AfterEmitWebpackPlugin({
        createFileList: !devMode,
        outDir: outDir
      }),
      // # Copy resources
      new CopyPlugin(copyPluginConfig),
      // Shows progress information in the console
      new webpack.ProgressPlugin()
    ],
    optimization: {
      minimizer: [
        // minify css
        new OptimizeCssAssetsPlugin({
          assetNameRegExp: /\.min\.css$/g,
          cssProcessorPluginOptions: {
            preset: ['default', {
              discardComments: {removeAll: true}
            }]
          }
        }),
        // minify js
        new TerserPlugin({
          test: /\.js(\?.*)?$/i,
          sourceMap: devMode
        })
      ],
      splitChunks: {
        chunks: 'all',
        cacheGroups: {
          scout: {
            // Scout may be loaded as node module or may be part of the workspace
            // Also make sure the regex only matches *.js files to prevent the output from mixing with css
            test: /([\\/]node_modules[\\/]@eclipse-scout[\\/].*\.js|.*[\\/]eclipse-scout.*[\\/].*\.js)/,
            name: 'eclipse-scout',
            priority: -5,
            reuseExistingChunk: true,
            enforce: true
          },
          jquery: {
            test: /[\\/]node_modules[\\/]jquery[\\/]/,
            name: 'jquery',
            priority: -1,
            reuseExistingChunk: true,
            enforce: true
          }
        }
      }
    }
  };

  const isWatchMode = args && !!args.watch;
  if (!isWatchMode) {
    // see: https://webpack.js.org/guides/output-management/#cleaning-up-the-dist-folder
    config.plugins.push(new CleanWebpackPlugin());
  }

  return config;
};

Back to the top