Fieldmap: reimplement d3 rebind
[sgn.git] / js / build.webpack.config.js
blob6de08bbe81210e50099c8ae06cd1bfb1755d4b08
1 const path = require('path');
2 const glob = require("glob");
3 const filemap = require(path.resolve(__dirname,"./webpack_util/webpack-filemap-plugin.js"));
4 const UglifyWebpackPlugin = require("uglifyjs-webpack-plugin");
6 const sourcePath = path.resolve(__dirname, "source");
7 const entryPath = path.resolve(sourcePath, "entries");
8 const legacyPath = path.resolve(sourcePath, "legacy");
10 module.exports = {
11     mode: "production",
12     target: 'web',
13     entry: (() => {
14         var entries = {};
15         glob.sync(path.resolve(entryPath, "**/*.js")).forEach(val => {
16             var prekey = val.replace(entryPath+"/","");
17             var key = prekey.match(/(.*)\.js$/)[1];
18             entries[key] = val;
19         });
20         return entries;
21     })(),
22     // Give entries a common library name
23     output: {
24         path: path.resolve(__dirname, "build/"),
25         publicPath: '/js',
26         filename: '[name].min.js',
27         chunkFilename: `chunk.[chunkhash].js`,
28         library: ["jsMod","[name]"],
29         libraryTarget: "umd"
30     },
31     // Set up babel and JSAN processing
32     module: {
33         rules: [
34             {
35                 test: /\.js$/,
36                 exclude: /(node_modules|bower_components)/,
37                 use: [{
38                     loader: 'babel-loader',
39                     options: {
40                         sourceType: "unambiguous",
41                         presets: [['@babel/preset-env',{
42                           useBuiltIns: 'usage'
43                         }]]
44                     }
45                 },{
46                     loader: path.resolve(__dirname,"./webpack_util/jsan-preprocess-loader.js"),
47                     options:{'legacyPath':legacyPath}
48                 }]
49             },
50             {
51                 test: legacyPath,
52                 use: [{
53                     loader: path.resolve(__dirname,"./webpack_util/jsan-error-loader.js")
54                 }]
55             }
56         ]
57     },
58     // Chunks and Minimization settings
59     optimization: {
60         minimize: true,
61         namedChunks: true,
62         minimizer: [new UglifyWebpackPlugin({
63             uglifyOptions: {
64                 output: {
65                     ascii_only: true
66                 },
67             },
68             'sourceMap': true,
69             'parallel': 4,
70         })],
71         runtimeChunk: {
72             name: 'runtime'
73         },
74         splitChunks: {
75             cacheGroups: {
76                 default: false,
77                 shared: {
78                     minChunks: 2,
79                     test: sourcePath,
80                     chunks: "initial",
81                     minSize: 1000
82                 },
83                 jsan: {
84                     minChunks: 2,
85                     test: path.resolve(__dirname, "webpack_util/adaptor.js"),
86                     chunks: "all",
87                     minSize: 1000
88                 },
89                 async: {
90                     minChunks: 2,
91                     test: sourcePath,
92                     chunks: "async",
93                     minSize: 1000
94                 }
95             }
96         }
97     },
98     devtool: "source-map",
99     plugins: [new filemap({'legacy_regex':"./webpack_util/dependency.regex"})]