da66461b4a128bb75a9c74a20d6cb7e9961fc27b
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / webpack.config.js
1 const path = require('path');
2 const merge = require('webpack-merge');
3 const webpack = require('webpack');
4 const CleanWebpackPlugin = require('clean-webpack-plugin');
5 const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
6 const CopyWebpackPlugin = require('copy-webpack-plugin');
7
8 const buildPath = 'build';
9
10 const CSS_FILES = [
11   'node_modules/angular-hotkeys/build/hotkeys.min.css',
12   'node_modules/bootstrap/dist/css/bootstrap.min.css',
13   'node_modules/ng-toast/dist/ngToast.min.css',
14   'node_modules/ng-toast/dist/ngToast-animations.min.css',
15   'node_modules/angular-tree-control/css/tree-control.css',
16   'node_modules/angular-tree-control/css/tree-control-attribute.css',
17   'node_modules/angular-tablesort/tablesort.css'
18 ];
19
20 const FONT_FILES = [
21   'node_modules/bootstrap/dist/fonts/glyphicons-halflings-regular.eot',
22   'node_modules/bootstrap/dist/fonts/glyphicons-halflings-regular.svg',
23   'node_modules/bootstrap/dist/fonts/glyphicons-halflings-regular.ttf',
24   'node_modules/bootstrap/dist/fonts/glyphicons-halflings-regular.woff',
25   'node_modules/bootstrap/dist/fonts/glyphicons-halflings-regular.woff2'
26 ];
27
28 const IMAGE_FILES = [
29   'node_modules/angular-tree-control/images/sample.png',
30   'node_modules/angular-tree-control/images/node-opened-2.png',
31   'node_modules/angular-tree-control/images/folder.png',
32   'node_modules/angular-tree-control/images/node-closed.png',
33   'node_modules/angular-tree-control/images/node-closed-light.png',
34   'node_modules/angular-tree-control/images/node-opened.png',
35   'node_modules/angular-tree-control/images/node-opened-light.png',
36   'node_modules/angular-tree-control/images/folder-closed.png',
37   'node_modules/angular-tree-control/images/node-closed-2.png',
38   'node_modules/angular-tree-control/images/file.png'
39 ]
40
41 // Some common JS files are left un-bundled.
42 // https://github.com/webpack/webpack/issues/3128
43 const JS_FILES = [
44   './node_modules/moment/min/moment-with-locales.min.js',
45   './node_modules/moment-timezone/builds/moment-timezone-with-data.min.js',
46   './node_modules/iframe-resizer/js/iframeResizer.contentWindow.min.js',
47   './node_modules/iframe-resizer/js/iframeResizer.min.js',
48   // lovefield is loaded from multiple locations.  Make it stand-alone
49   // so we only need a single copy.
50   './node_modules/lovefield/dist/lovefield.min.js'
51 ]
52
53
54 // Copy files as-is from => to.
55 const directCopyFiles = [
56
57   // jquery is copied to the common build location, up one directory.
58   {from: './node_modules/jquery/dist/jquery.min.js', 
59      to: __dirname + '/../common/build/js'}
60 ];
61
62 CSS_FILES.forEach(file => directCopyFiles.push({from: file, to: './css'}));
63 FONT_FILES.forEach(file => directCopyFiles.push({from: file, to: './fonts'}));
64 IMAGE_FILES.forEach(file => directCopyFiles.push({from: file, to: './images'}));
65 JS_FILES.forEach(file => directCopyFiles.push({from: file, to: './js'}));
66
67 // EG JS files loaded on every page
68 const coreJsFiles = [
69   './services/core.js',
70   './services/strings.js',
71   './services/idl.js',
72   './services/event.js',
73   './services/net.js',
74   './services/auth.js',
75   './services/pcrud.js',
76   './services/env.js',
77   './services/org.js',
78   './services/startup.js',
79   './services/hatch.js',
80   './services/print.js',
81   './services/audio.js',
82   './services/coresvc.js',
83   './services/user.js',
84   './services/navbar.js',
85   './services/ui.js',
86   './services/i18n.js',
87   './services/date.js',
88   './services/op_change.js',
89   './services/lovefield.js'
90 ];
91
92 // 3rd-party (AKA vendor) JS files loaded on every page.
93 // Webpack knows to look in ./node_modules/
94 const vendorJsFiles = [
95   'angular',
96   'angular-route',
97   'angular-ui-bootstrap',
98   'angular-hotkeys',
99   'angular-file-saver',
100   'angular-location-update',
101   'angular-animate',
102   'angular-sanitize',
103   'angular-cookies',
104   'ng-toast',
105   'angular-tree-control',
106   'angular-tree-control/context-menu.js',
107   'angular-order-object-by',
108   'angular-tablesort'
109 ];
110
111
112 let commmonOptions = {
113   // As of today, we are only bundling common files.  Individual app.js
114   // and optional service files are still imported via script tags.
115   entry: {
116     core: coreJsFiles,
117     vendor: vendorJsFiles
118   },
119   plugins: [
120     new CleanWebpackPlugin([buildPath]),
121     new CopyWebpackPlugin(directCopyFiles, {copyUnmodified: true}),
122     new webpack.optimize.CommonsChunkPlugin({
123       names: ['core', 'vendor'], // ORDER MATTERS
124       minChunks: 2 // TODO: huh?
125     })
126   ],
127   output: {
128     filename: 'js/[name].bundle.js',
129     path: path.resolve(__dirname, buildPath)
130   }
131 };
132
133 // improve debugging during development with inline source maps
134 // for bundled files.
135 let devOptions = {
136   devtool: 'inline-source-map',
137   plugins: [
138     // Avoid minifiying the core bundle in development mode.
139     // TODO: Add other bundles as necessary, but leave the 'vendor'
140     // bundle out, since we always want to minify that (it's big).
141     new UglifyJSPlugin({
142       exclude: [/core/]
143     })
144   ],
145   watchOptions: {
146     aggregateTimeout: 300,
147     poll: 1000,
148     ignored : [
149         /node_modules/
150     ]
151   }
152 };
153
154 // minify for production
155 let prodOptions = {
156   plugins: [
157     new UglifyJSPlugin()
158   ],
159 };
160
161 module.exports = env => env.prod ? 
162     merge(commmonOptions, prodOptions) : merge(commmonOptions, devOptions); 
163