-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack-public-js.config.ts
76 lines (69 loc) · 1.82 KB
/
webpack-public-js.config.ts
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
import dotenv from 'dotenv';
dotenv.config();
import path from 'path';
import webpack from 'webpack';
import mongoose from 'mongoose';
import { config } from './src/util/config';
import glob from 'glob';
export default async () => {
const mode = process.env.NODE_ENV==='development' ? 'development' :
'production';
const outputPath = path.resolve(__dirname, 'dist/public/js');
const stats = 'errors-warnings';
const mongoUrl = process.env.MONGODB_URI;
mongoose.connect(mongoUrl).then(
() => {
// mongoose ready to use
}
).catch(error => {
console.error('ERROR: Failed to connect to MongoDB.');
console.error(error);
});
const configData = await config().catch(error => {
console.error('ERROR: Failed to get config.');
throw new Error('Failed to get config', { cause: error });
});
mongoose.disconnect();
const webpackConfig = [{
mode: mode,
entry: './src/public/js/main.js',
output: {
filename: 'main.js',
path: outputPath,
library: {
name: 'Common',
type: 'umd'
}
},
plugins: [
new webpack.DefinePlugin({
config: JSON.stringify({
siteUrl: configData.siteUrl,
loggedInCookie: configData.loggedInCookie
})
})
],
stats: stats
}, {
mode: mode,
entry: glob.sync('./src/public/js/pages/**/*.js').reduce(function(obj, el){
obj[el.replace(path.join('./src/public/js/pages/'),
path.join('./'))] = el;
return obj;
},{}),
output: {
filename: '[name]',
path: outputPath
},
plugins: [
new webpack.DefinePlugin({
config: JSON.stringify({
siteUrl: configData.siteUrl,
loggedInCookie: configData.loggedInCookie
})
})
],
stats: stats
}];
return webpackConfig;
};