forked from openmrs/openmrs-owa-obsadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
208 lines (170 loc) · 5.02 KB
/
webpack.config.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* * This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
'use strict';
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const env = require('yargs').argv.mode;
const target = require('yargs').argv.target;
const targetPort = require('yargs').argv.targetPort;
const UglifyPlugin = webpack.optimize.UglifyJsPlugin;
const CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
const DedupePlugin = webpack.optimize.DedupePlugin;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const WebpackOnBuildPlugin = require('on-build-webpack');
const nodeModulesDir = path.resolve(__dirname, '../node_modules');
const THIS_APP_ID = 'obsadmin';
var plugins = [];
const nodeModules = {};
let outputFile = `.bundle`;
let vendorOutputFile;
let outputPath;
var configJson;
let appEntryPoint;
let localOwaFolder;
let devtool;
var getConfig = function () {
var config;
try {
// look for config file
config = require('./config.json');
} catch (err) {
// create file with defaults if not found
config = {
'LOCAL_OWA_FOLDER': '/Users/andeladeveloper/Downloads/referenceapplication-standalone-2.6.0/appdata\owa/',
'APP_ENTRY_POINT': 'http://localhost:8081/openmrs-standalone/owa/obsadmin/index.html'
};
fs.writeFile('config.json', JSON.stringify(config));
} finally {
return config;
};
}
var config = getConfig();
var resolveBrowserSyncTarget = function () {
if (targetPort != null && targetPort != 'null') {
return config.APP_ENTRY_POINT.substr(0, 'http://localhost:'.length)
+ targetPort
+ config.APP_ENTRY_POINT.substr('http://localhost:'.length + targetPort.toString().length, config.APP_ENTRY_POINT.length);
}
else {
return config.APP_ENTRY_POINT
}
};
var browserSyncTarget = resolveBrowserSyncTarget();
/** Minify for production */
if (env === 'production') {
plugins.push(new UglifyPlugin({
output: {
comments: false,
},
minimize: true,
sourceMap: false,
compress: {
warnings: false
}
}));
plugins.push(new DedupePlugin());
outputFile = `${outputFile}.min.[chunkhash].js`;
vendorOutputFile = "vendor.bundle.[chunkhash].js";
outputPath = `${__dirname}/dist/`;
plugins.push(new WebpackOnBuildPlugin(function(stats){
//create zip file
var archiver = require('archiver');
var output = fs.createWriteStream(THIS_APP_ID+'.zip');
var archive = archiver('zip');
output.on('close', function () {
console.log('distributable has been zipped! size: '+archive.pointer());
});
archive.on('error', function(err){
throw err;
});
archive.pipe(output);
archive.directory(`${outputPath}`, '');
archive.finalize();
}))
} else if (env === 'deploy') {
outputFile = `${outputFile}.js`;
vendorOutputFile = "vendor.bundle.js";
outputPath = `${config.LOCAL_OWA_FOLDER}${THIS_APP_ID}`;
devtool = 'source-map';
} else if (env === 'dev') {
outputFile = `${outputFile}.js`;
vendorOutputFile = "vendor.bundle.js";
outputPath = `${__dirname}/dist/`;
devtool = 'source-map';
}
plugins.push(new BrowserSyncPlugin({
proxy: {
target : browserSyncTarget
}
}));
plugins.push(new CommonsChunkPlugin({
name: 'vendor',
filename: vendorOutputFile
}));
plugins.push(new HtmlWebpackPlugin({
template: './app/index.html',
inject: 'body'
}));
plugins.push(new CopyWebpackPlugin([{
from: './app/manifest.webapp'
}]));
plugins.push(new CopyWebpackPlugin([{
from: 'libs',
to: 'libs'
}]));
plugins.push(new CopyWebpackPlugin([{
from: './app/img',
to: 'img'
}]));
var webpackConfig = {
quiet: false,
entry: {
app : `${__dirname}/app/js/obsadmin`,
css: `${__dirname}/app/css/obsadmin.css`,
vendor : [
'react', 'react-router'
]
},
devtool: devtool,
target,
output: {
path: outputPath,
filename: '[name]'+outputFile,
},
module: {
loaders: [{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: [ 'es2015', 'react' ],
cacheDirectory : true
}
},{
test: /\.css$/,
loader: 'style-loader!css-loader'
}, {
test: /\.(png|jpg|jpeg|gif|svg)$/,
loader: 'url'
}, {
test: /\.html$/,
loader: 'html'
}],
},
resolve: {
root: path.resolve('./src'),
extensions: ['', '.js', '.jsx'],
},
plugins,
externals: nodeModules,
};
module.exports = webpackConfig;