-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
115 lines (108 loc) · 2.86 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
const path = require('path');
const webpack = require('webpack');
const dotenv = require('dotenv');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const TerserPlugin = require('terser-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
const PACKAGE = require('./package.json');
const MODULE_NAME = PACKAGE.moduleName || 'SampleModule';
const IS_PROD = (process.env.NODE_ENV === 'production');
const banner = (`
${PACKAGE.name} - ${PACKAGE.version}
(c) ${new Date().getFullYear()} ${PACKAGE.author}
License: ${PACKAGE.license}
${PACKAGE.homepage}
`);
// setup environment configuration
const env = dotenv.config().parsed;
env.NODE_ENV = JSON.stringify('production');
const envKeys = Object.keys(env).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(env[next]);
return prev;
}, {});
const getPlugins = () => {
let plugins = [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
inject: true,
title: 'Aeris JS - Map Module',
template: 'public/template.html',
alwaysWriteToDisk: true,
aeris: {
clientId: env.AERIS_CLIENT_ID,
clientSecret: env.AERIS_CLIENT_SECRET
}
}),
new HtmlWebpackHarddiskPlugin({
outputPath: path.resolve(__dirname, 'public')
})
];
if (IS_PROD) {
plugins.push(new webpack.DefinePlugin(envKeys));
plugins.push(new webpack.HashedModuleIdsPlugin());
plugins.push(new webpack.BannerPlugin({
banner: banner
}));
} else {
plugins.push(new BundleAnalyzerPlugin());
}
return plugins;
};
module.exports = () => {
const outputFileName = PACKAGE.name.replace(/-module$/, '');
return {
mode: IS_PROD ? 'production' : 'development',
entry: './src/index.ts',
output: {
path: path.join(__dirname, 'dist'),
publicPath: './dist/',
filename: IS_PROD ? `${outputFileName}.min.js` : `${outputFileName}.js`,
chunkFilename: `${MODULE_NAME}.js`,
library: [MODULE_NAME],
libraryExport: 'default',
libraryTarget: 'umd',
umdNamedDefine: true,
globalObject: 'this'
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx']
},
module: {
rules: [{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/
},{
test: /\.tsx?$/,
use: [{
loader: 'babel-loader'
},{
loader: 'ts-loader'
}],
exclude: /node_modules/
}]
},
optimization: {
minimizer: [
new TerserPlugin({
extractComments: false,
exclude: /\/public/,
})
],
splitChunks: {
automaticNameDelimiter: '-'
},
runtimeChunk: false
},
plugins: getPlugins(),
devtool: IS_PROD ? 'source-map' : 'inline-source-map',
devServer: {
contentBase: path.join(__dirname, 'public'),
open: true,
publicPath: '/dist/',
stats: 'errors-only'
}
};
};