-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.config.js
More file actions
134 lines (121 loc) · 3.28 KB
/
webpack.config.js
File metadata and controls
134 lines (121 loc) · 3.28 KB
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
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 {
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/
}
]
},
resolve: {
alias: {
...relativePaths()
},
extensions: ['.ts', '.js']
},
target: 'web',
output: {
filename: '[name].js',
library: libraryName,
libraryTarget: 'umd',
path: path.resolve(__dirname, 'dist'),
globalObject: 'this'
},
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)
})
]
};
}
function fillDev(config) {
config.mode = 'development';
config.entry = {
[`${packageJson.name}-v${packageJson.version}`]: './src/lib/index.ts'
};
config.devtool = 'inline-source-map';
config.devServer = {
contentBase: path.resolve(__dirname), // TODO probably not needed
publicPath: '/dist/',
compress: true,
port: 8000,
hot: false,
openPage: 'dist/index.html',
overlay: {
warnings: true,
errors: true
}
};
}
function fillProd(config, env) {
config.mode = 'production';
config.entry = {
[`${packageJson.name}-v${packageJson.version}`]: './src/lib/index.ts'
};
env.ANALYZER && config.plugins.push(new BundleAnalyzerPlugin());
// TODO think if source maps will be ever needed on production, if no delete this line:
// config.devtool = 'source-map';
}
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;
};