-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathwebpack.config.js
114 lines (101 loc) · 2.75 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
const webpack = require('webpack');
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const WrapperPlugin = require('wrapper-webpack-plugin');
const { readFileSync } = require('fs');
const packageJson = require('./package.json');
const packageName = packageJson.name;
const libraryName = getLibraryName(packageName);
const versionFileContent = readFileSync(path.resolve(__dirname) + '/src/version.ts', 'utf8');
const version = getVersion(versionFileContent);
const licence = readFileSync(path.resolve(__dirname) + '/LICENCE');
function getLibraryName(packageName) {
return packageName
.toLowerCase()
.split('-')
.map(chunk => chunk.charAt(0).toUpperCase() + chunk.slice(1))
.join('');
}
function getVersion(versionFileContent) {
const patternStart = '= \'';
return versionFileContent.substring(
versionFileContent.indexOf(patternStart) + patternStart.length,
versionFileContent.indexOf('\';')
);
}
function getConfig(env) {
return {
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.ts','.js']
},
output: {
filename: '[name].js',
library: libraryName,
libraryTarget: 'umd',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new webpack.DefinePlugin({
DEVELOPMENT: JSON.stringify(env.DEVELOPMENT === true),
PRODUCTION: JSON.stringify(env.PRODUCTION === true)
}),
new WrapperPlugin({
header: '/*\n' + licence + '*/\n\n'
})
]
};
}
function fillDev(config) {
config.mode = 'development';
config.entry = {
[`${packageName}-v${version}`]: './src/index.ts'
};
config.devtool = 'inline-source-map';
config.devServer = {
contentBase: path.resolve(__dirname),
publicPath: '/dist/',
compress: true,
port: 8000,
hot: false,
openPage: 'example/typescript-rewrite-test-page.html',
overlay: {
warnings: true,
errors: true
}
};
}
function fillProd(config) {
config.mode = 'none'; // TODO: should be 'production'
config.entry = {
// TODO: BUG!! after introducing webpack 'modes' both files are minified - find the fix
[`${packageName}-v${version}`]: './src/index.ts',
[`${packageName}-v${version}.min`]: './src/index.ts',
};
config.devtool = 'source-map';
config.plugins.unshift(
new UglifyJsPlugin({
include: /\.min\.js$/,
sourceMap: true
})
);
}
module.exports = (env) => {
const config = getConfig(env);
if (env.DEVELOPMENT === true) {
fillDev(config);
} else if (env.PRODUCTION === true) {
fillProd(config);
} else {
throw 'Please set the environment!';
}
return config;
}