-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
101 lines (93 loc) · 2.47 KB
/
webpack.config.ts
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
const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const dotenv = require('dotenv')
const webpackMerge = require('webpack-merge')
const CopyPlugin = require('copy-webpack-plugin')
const WorkerPlugin = require('worker-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const getEnv = env => {
const currentPath = path.join(__dirname)
const basePath = currentPath + '/.env'
const envPath = basePath + '.' + env.ENVIRONMENT
const finalPath = fs.existsSync(envPath) ? envPath : basePath
const fileEnv = dotenv.config({ path: finalPath }).parsed
const envKeys = Object.keys(fileEnv).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(fileEnv[next])
return prev
}, {})
return {
'process.env': envKeys,
...envKeys,
}
}
const sharedConfig = env => {
const config = {
entry: ['@babel/polyfill', './src/index.tsx'],
mode: env.ENVIRONMENT ? 'production' : 'development',
devtool: 'source-map',
output: {
filename: `reagram.js`,
path: path.resolve(__dirname, 'build'),
},
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
extensions: ['.tsx', '.ts', '.js'],
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
externals: {
tdweb: 'tdweb',
},
plugins: [
new CleanWebpackPlugin(),
new webpack.LoaderOptionsPlugin({
options: {
tslfint: {
emitErrors: true,
failOnHint: true,
},
},
}),
new webpack.DefinePlugin(getEnv(env)),
new WorkerPlugin(),
new CopyPlugin([{ from: path.resolve(__dirname, 'views'), to: '.' }]),
new CopyPlugin([
{
from: path.resolve(__dirname, 'node_modules/tdweb/dist/**/*'),
to: '.',
flatten: true,
copyUnmodified: true,
ignore: ['tdweb.js'],
},
]),
],
}
if (!env.prod) {
config.devServer = {
contentBase: path.join(__dirname, 'public/'),
compress: true,
port: 8000,
hot: true,
writeToDisk: true,
}
}
return config
}
const sharedClientConfig = {
module: {
rules: [
{
test: /\.(js|tsx?)$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
envName: 'client',
},
},
],
},
}
const reagram = (env = {}) => webpackMerge(sharedConfig(env), sharedClientConfig)
module.exports = [reagram]