-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
115 lines (105 loc) · 2.98 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 BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
const packageJson = require('./package.json');
const libraryName = packageJson.name
.toLowerCase()
.split('-')
.map((chunk) => chunk.charAt(0).toUpperCase() + chunk.slice(1))
.join('');
const relativePaths = () => {
const paths = require('./tsconfig').compilerOptions.paths;
const getFullPath = (lastPart) => path.join(__dirname, 'src/lib', lastPart);
const result = {};
Object.keys(paths).forEach((key) =>
key === '@'
? (result['@'] = getFullPath('index'))
: (result[key.replace('/*', '')] = getFullPath(paths[key][0].replace('/*', '')))
);
return result;
};
function getConfig(env) {
return {
entry: { [`${packageJson.name}-v${packageJson.version}`]: './src/lib/index.ts' },
module: {
rules: [
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: {
minimize: env.PRODUCTION
}
}
]
},
{
test: /\.scss$/,
use: [
{
loader: path.resolve('src/setup/webpack-style-loader.js'), // creates 'style' html tag from JS strings
options: { rootElementId: `${packageJson.name}-root` }
},
'css-loader', // translates CSS into CommonJS
'sass-loader' // compiles Sass to CSS
]
},
{ test: /\.ts$/, use: 'ts-loader', exclude: /node_modules/ }
]
},
output: {
filename: '[name].js',
globalObject: 'this',
library: libraryName,
libraryTarget: 'umd',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
hash: true,
minify: false,
template: './src/index.html'
}),
new webpack.DefinePlugin({
DEVELOPMENT: JSON.stringify(env.DEVELOPMENT === true),
PRODUCTION: JSON.stringify(env.PRODUCTION === true)
})
],
resolve: {
alias: { ...relativePaths() },
extensions: ['.ts', '.js']
},
target: 'web'
};
}
function fillDev(config) {
config.devServer = {
compress: true,
contentBase: path.resolve(__dirname), // TODO probably not needed
hot: false,
openPage: 'dist/index.html',
overlay: { warnings: true, errors: true },
port: 8000,
publicPath: '/dist/'
};
config.devtool = 'inline-source-map';
config.mode = 'development';
}
function fillProd(config, env) {
config.mode = 'production';
env.ANALYZER && config.plugins.push(new BundleAnalyzerPlugin());
}
module.exports = (env) => {
const config = getConfig(env);
if (env.DEVELOPMENT === true) {
fillDev(config);
} else if (env.PRODUCTION === true) {
fillProd(config, env);
} else {
throw 'Please set the environment!';
}
return config;
};