-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwebpack.config.js
167 lines (148 loc) · 4.15 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Oligrapher's webpack build can be configured via the command line via changes these variables:
env.output_path | asset output directory. defaults to ./dist
env.public_path | code chunks will be fetched from this path. defaults to "http://localhost:8090"
env.filename | defaults to "oligrapher.js" or "oligrapher-dev.js"
env.production | enables production mode
env.development | enables development mode
env.dev_server | enables dev server mode
env.api_url | littlesis datasource url. defaults to "https://littlesis.org"
Also see the yarn scripts in package.json
*/
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
function getOutputPath(env) {
if (env.output_path) {
let output_path = path.resolve(env.output_path)
if (fs.existsSync(output_path) && fs.lstatSync(output_path).isDirectory()) {
return output_path
} else {
throw new Error(`Invalid output path: ${output_path}`)
}
} else {
return path.resolve(__dirname, 'dist')
}
}
function getFilename(env) {
if (env.filename) {
if (env.filename.slice(-3) === '.js') {
return env.filename
} else {
throw new Error(`Invalid filename: ${env.filename}`)
}
}
if (env.production) {
return "oligrapher.js"
} else {
return "oligrapher-dev.js"
}
}
function getChunkFilename(env) {
return getFilename(env).slice(0, -3) + '-[name].js'
}
function getDevServerConfig(env) {
if (env.dev_server) {
return {
contentBase: path.resolve(__dirname, 'html'),
publicPath: 'http://localhost:8090/',
port: 8090,
serveIndex: true,
historyApiFallback: true,
hot: true,
headers: { 'Access-Control-Allow-Origin': '*' },
watchOptions: {
ignored: /node_modules/
}
}
} else {
return undefined
}
}
function getPublicPath(env) {
return env.public_path ? env.public_path : 'http://localhost:8090/'
}
module.exports = function(env) {
if (!env) {
throw new Error("Webpack env configuration is missing")
}
const production = Boolean(env.production)
const development = env.development || env.dev_server
const sourcemap = Boolean(env.sourcemap)
return {
mode: production ? 'production' : 'development',
entry: path.resolve(__dirname, 'app/Oligrapher.jsx'),
output: {
path: getOutputPath(env),
publicPath: getPublicPath(env),
library: 'Oligrapher',
libraryTarget: 'umd',
libraryExport: "default",
filename: getFilename(env),
chunkFilename: getChunkFilename(env)
},
optimization: {
minimize: production
},
devServer: getDevServerConfig(env),
devtool: development ? 'eval-source-map' : (sourcemap ? 'inline-source-map' : false),
module: {
rules: [
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader"
}
]
},
{
test: /\.jsx?$/,
exclude: /node_modules\/(?!(@public-accountability.*?\\.js$))/,
use: [
{ loader: 'babel-loader' }
]
},
{
test: /\.(woff2?|ttf|eot|otf|svg)$/,
use: [
{
loader: 'url-loader'
}
]
},
{
test: /\.s?css$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader'
},
{
loader: 'sass-loader',
options: {
implementation: require('sass')
}
}
]
}
]
},
plugins: [
new webpack.DefinePlugin({
'API_URL': JSON.stringify(env.api_url ? env.api_url : 'https://littlesis.org'),
'PRODUCTION': JSON.stringify(env.production)
}),
env.dev_server ? new webpack.HotModuleReplacementPlugin() : false
].filter(Boolean),
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
alias: {
'react-dom': env.dev_server ? '@hot-loader/react-dom' : 'react-dom'
}
}
}
}