-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
130 lines (117 loc) · 3.24 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const webpack = require('webpack');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackExcludeAssetsPlugin = require('html-webpack-exclude-assets-plugin');
const HtmlWebpackPlugin = require('html-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/lib/version.ts', 'utf8');
const version = getVersion(versionFileContent);
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']
},
target: 'web',
output: {
filename: '[name].js',
library: libraryName,
libraryTarget: 'umd',
path: path.resolve(__dirname, 'dist'),
globalObject: 'this'
},
plugins: [
new HtmlWebpackPlugin({
filename: 'ascii-runner-browser.html',
hash: true,
minify: false,
template: './src/templates/ascii-runner-browser.html',
excludeAssets: [/^dev.*.js/]
}),
new HtmlWebpackExcludeAssetsPlugin(), // https://stackoverflow.com/a/50830422
new webpack.DefinePlugin({
DEVELOPMENT: JSON.stringify(env.DEVELOPMENT === true),
PRODUCTION: JSON.stringify(env.PRODUCTION === true)
})
]
};
}
function fillDev(config) {
config.mode = 'development';
config.entry = {
[`${packageName}-v${version}`]: './src/main.ts',
[`dev`]: './src/dev.ts'
};
config.devtool = 'inline-source-map';
config.devServer = {
contentBase: path.resolve(__dirname),
publicPath: '/dist/',
compress: true,
port: 8000,
hot: false,
openPage: 'dist/ascii-runner-browser.html',
overlay: {
warnings: true,
errors: true
}
};
}
function fillProd(config) {
config.mode = 'production';
config.entry = {
[`${packageName}-v${version}`]: './src/main.ts'
};
config.devtool = 'source-map';
config.plugins.push(
new CopyWebpackPlugin(
[
{
from: path.resolve(__dirname) + '/src/templates/ascii-runner-node.js',
to: path.resolve(__dirname) + '/dist/ascii-runner-node.js',
toType: 'file'
},
{
from: path.resolve(__dirname) + '/src/templates/api-client-browser.html',
to: path.resolve(__dirname) + '/dist/api-client-browser.html',
toType: 'file'
}
]
)
);
}
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;
};